aitorzip / PyTorch-CycleGAN

A clean and readable Pytorch implementation of CycleGAN
https://arxiv.org/abs/1703.10593
GNU General Public License v3.0
1.22k stars 283 forks source link

The expanded size of the tensor must match the existing size at non-singleton dimension 0 #18

Open latlio opened 5 years ago

latlio commented 5 years ago

This error message appears when the total number of training images modulo batch size does not equal zero. For instance, if I had 50 images with a batch-size of 8, it would make 6 batches, but there would be a leftover batch size of 2, which throws off the tensor shape (expected: [8, nc, height, width], observed: [2, nc, height, width]. What's the best way to overcome this issue?

MounirD commented 5 years ago

I have the same issue on both macOS and Ubuntu.

RuntimeError: The expanded size of the tensor (3) must match the existing size (4) at non-singleton dimension 1. Target sizes: [1, 3, 256, 256]. Tensor sizes: [1, 4, 256, 256]

I have 1239 images in ./train/A and 4952 in ./train/B

mhaghighat commented 4 years ago

I made a temporary fix skipping that batch. Add the following before setting the model input in train.py line #97:

# Skip the final batch when the total number of training images modulo batch-size does not equal zero
if len(batch['A']) != opt.batchSize or len(batch['B']) != opt.batchSize:
    continue   #TODO
IceClear commented 3 years ago

Actually, I think just set drop_last=True for the dataloader in https://github.com/aitorzip/PyTorch-CycleGAN/blob/master/train#L87 solves the bug.

spreusler commented 3 years ago

Actually, I think just set drop_last=True for the dataloader in https://github.com/aitorzip/PyTorch-CycleGAN/blob/master/train#L87 solves the bug.

Worked for me!