inputing numpy array images into pytorch neural net
I have a numpy array representation of an image and I want to turn it into a tensor so I can feed it through my pytorch neural network. 

I understand that the neural networks take in transformed tensors which are not arranged in [100,100,3] but [3,100,100] and the pixels are rescaled and the images must be in batches. 

So I did the following:

import cv2
my_img = cv2.imread('testset/img0.png')
my_img.shape #reuturns [100,100,3] a 3 channel image with 100x100 resolution
my_img = np.transpose(my_img,(2,0,1))
my_img.shape #returns [3,100,100] 
#convert the numpy array to tensor
my_img_tensor = torch.from_numpy(my_img)
#rescale to be [0,1] like the data it was trained on by default 
my_img_tensor *= (1/255)
#turn the tensor into a batch of size 1
my_img_tensor = my_img_tensor.unsqueeze(0)
#send image to gpu 
my_img_tensor.to(device)
#put forward through my neural network.
net(my_img_tensor)


However this returns the error:

RuntimeError: _thnn_conv2d_forward is not implemented for type torch.ByteTensor


The problem is that the input you give to your network is of type ByteTensor while only float operations are implemented for conv like operations. Try the following

my_img_tensor = my_img_tensor.type('torch.DoubleTensor')
# for converting to double tensor


Source PyTorch Discussion Forum

Thanks to AlbanD
