Normalising a single image with ImageNet statistics

Is there a way to normalize a single image loaded as a fastai Image with mean=(0,485, 0,456, 0,406) and std=(0,229, 0,224, 0,225), as calculated from the ImageNet statistics?

“.normalize(imagenet_stats)” only seems to work for a DataBunch.

1 Like

It is possible to create a DataBunch with one image :slight_smile: but it probably isn’t the best way to normalise a single image.

What if:

means = FloatTensor([0, 485, 0, 456, 0, 406])
stds = FloatTensor([0, 229, 0, 224, 0, 225])
pxs = myImage.px
myImage.px = normalize(pxs, means, stds)

I haven’t tested it…

You can simply do the tensor arithmetic manually.
In one line you can do:

img.data = (img.data -
torch.Tensor(imagenet_stats[0]).reshape(-1,1,1))/torch.Tensor(imagenet_stats[1]).reshape(-1,1,1)

3 Likes