JobHunter69
JobHunter69

Reputation: 2290

Pytorch pretrained model not recognizing my image

I have an image that is a torch.Tensor format. I want to feed it directly into a pre-trained classifier, like Inception v3.

However, it's being predicted incorrectly (no error message, just output is wrong). I guess this is because I didn't normalize it (in accordance with: https://pytorch.org/docs/stable/torchvision/models.html), so that's what I'm trying to do.

The problem is, the normalization requires numpy input. However, to get numpy, I do this and it gives me an error:

----> 9 image = data.numpy()[0].transpose((1, 2, 0))  # [image_size, image_size, RGB]

RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

I cannot call detach because I require gradients to flow through the image (which was generated by other functions). Is there a way to bypass converting this to a numpy. If not, how do I keep the gradient flow?

Upvotes: 0

Views: 300

Answers (1)

Jacob Deasy
Jacob Deasy

Reputation: 336

One option is to scale images into the interval [0, 1], before using the mean and standard deviation vectors as tensors.

import torch

data = (data - data.min()) / (data.max() - data.min())  # rescale to [0, 1]
mean = torch.tensor([[[0.485, 0.456, 0.406]]])
std = torch.tensor([[[0.229, 0.224, 0.225]]])
data = (data - mean) / std  # normalise with tensors

Upvotes: 3

Related Questions