About the first problem the “blue thing”. When you use transforms.ToTensor()
PyTorch standardize your input from [0, 255] to [0, 1]. So if you want to visualize these images using matplotlib, you need to first convert back to [0, 255] and you can use torch.ToPILImage()
to extract a batch of your images then convert them to numpy and plot using matplotlib. torch.vutils
is a good approach to visualize your images during training or testing process.
# obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy() # convert images to numpy for display
# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(20):
ax = fig.add_subplot(2, 20/2, idx+1, xticks=[], yticks=[])
plt.imshow(np.transpose(images[idx], (1, 2, 0)))
#ax.set_title(classes[labels[idx]])
About the size problem. I saw your code and I cannot really figure out where you have such a problem, I just noticed you used transforms.Normalize
for single channel image.
transforms.Normalize([0.5,], std=[0.5,])
First thing came to my mind is that all of your images are grayscale. Right? So when you use PIL
library, it figures out the type of your image between possible type such as RGB, CMYK, aRGB, I (binary), etc
. So I really when saw images at the start of notebook, I thought why opencv is saying it has 3 channels. Actually, I think [1, 224, 224]
is the right shape for your images but if you insist to use RGB
, as you have vgg
as a feature extractor, you can just set mode
argument in Image.open(path, mode='string')
method to force PIL
to use RGB
encodings.
image = Image.open(img_name+'.jpg')
Final note that you already took care of it is that PyTorch uses [batch_size, channel_size, height, width]
convention, in contrary, numpy or others (I don’t know about the others!), use batch_size, height, width, channel_size]
approach. So be aware of it when converting to each other.
PS: It is really hard to comment on jupyter notebook in github, if it was .py file, I would comment these things on the aforementioned lines.