Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
latlio opened this issue Jul 26, 2019 · 4 comments

Comments

@latlio
Copy link

latlio commented Jul 26, 2019

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
Copy link

MounirD commented Aug 2, 2019

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
Copy link

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
Copy link

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants