Hello all, I am new to Pytorch and machine learning and recently I tried to run https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py with my own dataset.
My dataset is as following:
def __init__(self, datapath, train=False, transform=None):
self.data = []
self.map_dic = {"-" : 0, "X" : 1, "S" : 2, "?" : 3, "Q" : 4, "E" : 5, "<" : 6, ">" : 7, "[" : 8, "]" : 9, "o" : 10, "B" : 11, "b" : 12}
for file_name in os.listdir(datapath):
with open(base_path + "/" + file_name, 'r') as f:
res = np.array( list( map( lambda l: [self.map_dic.get(c, c) for c in l.strip()], f.readlines() ) ) )
self.data.append(res)
self.transform = transform
def __getitem__(self, index):
img = self.data[index]
img = torch.from_numpy(img)
img = img.unsqueeze(0)
# image transform:
# img = Image.open('dataset_img/{}.png'.format(index)).convert('L')
# if self.transform is not None:
# img = self.transform(img)
return img,0
def __len__(self):
return len(self.data)
The code works fine originally when I load the image from file (using the comment part code) and transform with the following
transforms.Resize(size=(16,224)),
transforms.ToTensor()
])
The image file here is created bymatplotlib.pyplot.imsave('name1.png', data, cmap='gray')
function from the numpy array(like this
[[0 0 0 … 0 0 0]
[0 0 0 … 0 0 0]
[0 0 0 … 0 0 0]
…
[1 1 1 … 1 1 1]
[1 1 1 … 1 1 1]
[1 1 1 … 1 1 1]]
)
Then I think that it is not necessary to convert numpy array to image and then to tensor. Instead I could convert directly from numpy array to tensor. After I use the above dataset, it constantly gives the error:
File "maintxt1.py", line 241, in <module>
#in original code it is line 122
output = model(input)
File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(*input, **kwargs)
File "/home/tangyeping/pixel-cnn-pp/model.py", line 119, in forward
x = x if sample else torch.cat((x, self.init_padding), 1)
RuntimeError: Expected object of scalar type long int but got scalar type float for sequence element 1.
I have tried something like img = torch.from_numpy(img).long()
or model.double()
but this error keeps appering. Appreciate for any suggestion.