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

DCGAN with images larger than 32x32 #150

Closed
dnvnxg opened this issue Feb 7, 2023 · 4 comments
Closed

DCGAN with images larger than 32x32 #150

dnvnxg opened this issue Feb 7, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@dnvnxg
Copy link

dnvnxg commented Feb 7, 2023

Describe the bug
Getting the following error when trying to train on images of size larger than 32x32:
"RuntimeError: shape '[64]' is invalid for input of size 1600"

To Reproduce
My hyperparameters for the networks as well as the data I am loading are below

tfs = transforms.Compose([
    transforms.Resize(64),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = torchvision.datasets.ImageFolder(root=data_dir+"train", transform=tfs)
val_dataset = torchvision.datasets.ImageFolder(root=data_dir+"/val", transform=tfs)
test_dataset = torchvision.datasets.ImageFolder(root=data_dir+"/test", transform=tfs)

dataset = train_dataset

dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True, num_workers=4)


dcgan_network = {
    "generator": {
        "name": DCGANGenerator,
        "args": {
            "encoding_dims": 100,
            "out_channels": 3,
            "step_channels": 64,
            "nonlinearity": nn.LeakyReLU(0.2),
            "last_nonlinearity": nn.Tanh(),
        },
        "optimizer": {"name": Adam, "args": {"lr": 0.0001, "betas": (0.5, 0.999)}},
    },
    "discriminator": {
        "name": DCGANDiscriminator,
        "args": {
            "in_channels": 3,
            "step_channels": 64,
            "nonlinearity": nn.LeakyReLU(0.2),
            "last_nonlinearity": nn.LeakyReLU(0.2),
        },
        "optimizer": {"name": Adam, "args": {"lr": 0.0003, "betas": (0.5, 0.999)}},
    },
}```

**Expected behavior**
Normal training as usual

**Installation**
- Conda
@dnvnxg dnvnxg added the bug Something isn't working label Feb 7, 2023
@egebeysel
Copy link

egebeysel commented Feb 20, 2023

Had the same problem, the shape [64] you have there comes from the batch_size though.
The problem here is the dcgan_network configuration. DCGANGenerator has an argument named out_size and the DCGANDiscriminator has another argument named in_size. If you set these to match your image size, it should work just fine.

@egebeysel
Copy link

egebeysel commented Feb 20, 2023

In this sense, I would also recommend putting these parameters to Tutorial 1 since these might be easy to oversee and can be mixed up with step_channels, which I guess you did in this case

@egebeysel
Copy link

And also a quick note on my side: ImageFolder is perfectly capable of reading your dataset in a way that it distinguishes the splits of your dataset according to the folder that they are in, you dont have to create 3 seperate datasets for it. You just give data_loader the split that you want and voila.

@dnvnxg
Copy link
Author

dnvnxg commented Feb 21, 2023

Worked like a charm. Thanks @egebeysel !

@dnvnxg dnvnxg closed this as completed Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants