PyTorch-CycleGAN icon indicating copy to clipboard operation
PyTorch-CycleGAN copied to clipboard

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

Open latlio opened this issue 4 years ago • 4 comments

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?

latlio avatar Jul 26 '19 18:07 latlio

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

MounirD avatar Aug 02 '19 10:08 MounirD

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

mhaghighat avatar Jun 24 '20 16:06 mhaghighat

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.

IceClear avatar Jun 16 '21 15:06 IceClear

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!

spreusler avatar Jun 30 '21 14:06 spreusler