pclucas14 / pixel-cnn-pp

Pytorch Implementation of OpenAI's PixelCNN++
Other
344 stars 76 forks source link

Problem of image channel when using my own dataset #21

Closed NeroArcher closed 3 years ago

NeroArcher commented 3 years ago

I am wondering about what should be the proper format for input image from dataset.

I have been trying to use a set of VGLC txtfiles as the input dataset. I managed to convert the text into numpy arrays, my dataset is as following

` class MyDataset(Dataset): def init(self, datapath, train=False, transform=None): self.data = [] for file_name in os.listdir(datapath): with open(base_path + "/" + file_name, 'r') as f: res = np.array( list( map( lambda l: [ord(c) for c in l.strip()], f.readlines() ) ) ) self.data.append(res) self.transform = transform

def __getitem__(self, index):
    img = self.data[index]
    img = Image.fromarray(img, mode="RGB")
    if self.transform is not None:
        img = self.transform(img)
    return img, 0

def __len__(self):
    return len(self.data)

Then I apply the dataloader as following train_loader = torch.utils.data.DataLoader(MyDataset(base_path, transform=m_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)

test_loader = torch.utils.data.DataLoader(MyDataset(base_path,
                transform=m_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)

loss_op   = lambda real, fake : discretized_mix_logistic_loss_1d(real, fake)
sample_op = lambda x : sample_from_discretized_mix_logistic_1d(x, args.nr_logistic_mix)`

However, the expected input channels gives a error

Traceback (most recent call last): File "maintxt1.py", line 200, in 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/anaconda3/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py", line 155, in forward outputs = self.parallel_apply(replicas, inputs, kwargs) File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py", line 165, in parallel_apply return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)]) File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/parallel/parallel_apply.py", line 85, in parallel_apply output.reraise() File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/_utils.py", line 395, in reraise raise self.exc_type(msg) RuntimeError: Caught RuntimeError in replica 0 on device 0. Original Traceback (most recent call last): File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/parallel/parallel_apply.py", line 60, in _worker output = module(*input, *kwargs) 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 120, in forward u_list = [self.u_init(x)] 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/layers.py", line 53, in forward x = self.conv(x) 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/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 353, in forward return self._conv_forward(input, self.weight) File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 350, in _conv_forward self.padding, self.dilation, self.groups) RuntimeError: Given groups=1, weight of size [80, 4, 2, 3], expected input[4, 2, 15, 32] to have 4 channels, but got 2 channels instead

I thought this maybe related to the wrong mode for my Image.fromarray(img, mode="L"), but even if I choose RGB, it still gives RuntimeError: The size of tensor a (16) must match the size of tensor b (15) at non-singleton dimension 3 Therefore, I am wondering how I can make the input image fit the expected input for this project.

NeroArcher commented 3 years ago

I kind of solved the problem after manipulate the dataset shape

pclucas14 commented 3 years ago

Sorry I just saw this, glad to see you could figure it out!