runtimeerror size mismatch at tensor
error message: RuntimeError: size mismatch, m1: [64 x 3200], m2: [512 x 1] at C:/w/1/s/windows/pytorch/aten/src\THC/generic/THCTensorMathBlas.cu:290

The code is the following.:

class Generator(nn.Module):
def __init__(self):
    super(Generator, self).__init__()

    self.label_emb = nn.Embedding(opt.n_classes, opt.latent_dim)

    self.init_size = opt.img_size // 4  # Initial size before upsampling
    self.l1 = nn.Sequential(nn.Linear(opt.latent_dim, 128 * self.init_size ** 2))

    self.conv_blocks = nn.Sequential(
        nn.BatchNorm2d(128),
        nn.Upsample(scale_factor=2),
        nn.Conv2d(128, 128, 3, stride=1, padding=1),
        nn.BatchNorm2d(128, 0.8),
        nn.LeakyReLU(0.2, inplace=True),
        nn.Upsample(scale_factor=2),
        nn.Conv2d(128, 64, 3, stride=1, padding=1),
        nn.BatchNorm2d(64, 0.8),
        nn.LeakyReLU(0.2, inplace=True),
        nn.Conv2d(64, opt.channels, 3, stride=1, padding=1),
        nn.Tanh(),
    )

def forward(self, noise, labels):
    gen_input = torch.mul(self.label_emb(labels), noise)
    out = self.l1(gen_input)
    out = out.view(out.shape[0], 128, self.init_size, self.init_size)
    img = self.conv_blocks(out)
    return img


class Discriminator(nn.Module):
def __init__(self):
    super(Discriminator, self).__init__()

    def discriminator_block(in_filters, out_filters, bn=True):
        """Returns layers of each discriminator block"""
        block = [nn.Conv2d(in_filters, out_filters, 3, 2, 1), nn.LeakyReLU(0.2, inplace=True), nn.Dropout2d(0.25)]
        if bn:
            block.append(nn.BatchNorm2d(out_filters, 0.8))
        return block

    self.conv_blocks = nn.Sequential(
        *discriminator_block(opt.channels, 16, bn=False),
        *discriminator_block(16, 32),
        *discriminator_block(32, 64),
        *discriminator_block(64, 128),
    )

    # The height and width of downsampled image
    ds_size = opt.img_size // 2 ** 4

    # Output layers
    self.adv_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, 1), nn.Sigmoid())
    self.aux_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, opt.n_classes), nn.Softmax())

def forward(self, img):
    out = self.conv_blocks(img)
    out = out.view(out.shape[0], -1)
    validity = self.adv_layer(out)
    label = self.aux_layer(out)

    return validity, label


# Loss functions
adversarial_loss = torch.nn.BCELoss()
auxiliary_loss = torch.nn.CrossEntropyLoss()

# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()

os.makedirs("../../data/mnist", exist_ok=True)
labels_path = 'C:/project/PyTorch-GAN/ulna/train-labels-idx1-ubyte.gz'
images_path = 'C:/project/PyTorch-GAN/ulna/train-images-idx3-ubyte.gz'
label_name = []

with gzip.open(labels_path, 'rb') as lbpath:
labels = np.frombuffer(lbpath.read(), dtype="int32", offset=8)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype="int32", offset=16).reshape(len(labels),70,70,1)

hand_transform2 = transforms.Compose([
            transforms.Resize((70, 70)),
            transforms.Grayscale(1),
            transforms.ToTensor(),
            transforms.Normalize([0.5], [0.5])
            ])

#images=cv2.resize(images, (70, 70),1)
dataset1 = datasets.ImageFolder('C:/project/PyTorch-GAN/ulna/ulna', transform=hand_transform2)


dataloader = torch.utils.data.DataLoader(
dataset1,
batch_size=opt.batch_size,
shuffle=True,
)


The Traceback is the following.:

Traceback (most recent call last):
File "acgan.py", line 225, in &lt;module&gt;
real_pred, real_aux = discriminator(real_imgs)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "acgan.py", line 110, in forward
validity = self.adv_layer(out)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\modules\container.py", line 92, in forward
input = module(input)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\modules\linear.py", line 87, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\S\AppData\Local\conda\conda\envs\venv\lib\site-packages\torch\nn\functional.py", line 1370, in linear
ret = torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [64 x 3200], m2: [512 x 1] at C:/w/1/s/windows/pytorch/aten/src\THC/generic/THCTensorMathBlas.cu:290


What I want to practice is the GAN code. The entire GAN code before modification can be found at the following link: https://github.com/eriklindernoren/PyTorch-GAN/blob/master/implementations/acgan/acgan.py
Input image is an x-ray image adjusted to 70x70, and output image is a fake x-ray image that is newly created by learning input x-ray image. The code worked well when practised with the minist database. I'm afraid I don't have a clue about the problem of code.
please help me! Thank you.

It seems that opt.img_size might still be set to 32 as if you were using CIFAR. As you resize it to 70, it should be set to 70.

Anyway, another problem will arise because ds_size = opt.img_size // 2 ** 4 is not valid for opt.img_size=70. If you want a hard-coded solution, set ds_size=5. This fixes the Discriminator, but the same thing will happen to the Generator.

If you don't understand how to fix this properly, I would recommend you to take some time to read about how these models work. If you want to use the code as is, I would recommend you to use an img_size that is multiple of 16, e.g., opt.img_size=80 and you would have no problem. To avoid other problems, you may want to use transforms.Resize((opt.img_size, opt.img_size)) instead of hard-coding the img_size there.
