ResNet: TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>

Viewed 16

I have an numpy array of images. The shape is (5594, 246, 246, 3).

When I used it on Pytorch's squeezenet, it worked:

m = torch.hub.load('pytorch/vision:v0.10.0', 'squeezenet1_0', pretrained=True)
m.classifier[1] = nn.Conv2d(512, 10, kernel_size=(2,2), stride=(2,2))
preprocess = transforms.Compose([transforms.ToTensor()])
... training code, apply "preprocess" on my data ...

Now, I'd like to use ResNet:

m = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
m.fc = nn.Linear(512, 10)
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
... training code, apply "preprocess" on my data ...

However, I'm getting an error:

TypeError: img should be PIL Image. Got <class 'numpy.ndarray'> 

Q: How should I convert my data to PIL images?

0 Answers
Related