title: PyTorch mat1 and mat2 shapes cannot be multiplied
body: <p>I am attempting to train an image classifier using PyTorch. I followed the tutorial at <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.htm" rel="nofollow noreferrer">https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.htm</a>, and It worked perfectly fine.</p>
  <p>I am now trying to use a custom dataset instead of the one provided by the tutorial, and I am encountering some issues.</p>
  <p>Here is my code for preparing the images:</p>
  <pre><code>transform = transforms.Compose(
  [transforms.ToTensor(),
   transforms.Resize((224,224)),
   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
  </code></pre>
  <p>Here is my code for creating the data loaders:</p>
  <pre><code>trainset = datasets.ImageFolder('./Dataset/train', transform)
  trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=0)
  
  testset = datasets.ImageFolder('./Dataset/test', transform)
  testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=True, num_workers=0)
  </code></pre>
  <p>The issue appears in this class:</p>
  <pre><code>class Net(nn.Module):
      def __init__(self):
          super().__init__()
          self.conv1 = nn.Conv2d(3, 6, 5)
          self.pool = nn.MaxPool2d(2, 2)
          self.conv2 = nn.Conv2d(6, 16, 5)
          self.fc1 = nn.Linear(16 * 5 * 5, 120)
          self.fc2 = nn.Linear(120, 84)
          self.fc3 = nn.Linear(84, 10)
  
      def forward(self, x):
          x = self.pool(F.relu(self.conv1(x)))
          x = self.pool(F.relu(self.conv2(x)))
          x = torch.flatten(x, 1) 
          x = F.relu(self.fc1(x))
          x = F.relu(self.fc2(x))
          x = self.fc3(x)
          return x
  </code></pre>
  <p>When I run my code, I get the error:</p>
  <pre><code>RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x44944 and 400x120)
  </code></pre>
  <p>This error did not appear when I used the dataset provided by the tutorial, even though the <code>Net()</code> class is exactly the same. The only differences between the code that did work and the code that does not work are the data loaders.</p>
  <p>Here is the code for the original data loaders in the tutorial:</p>
  <pre><code>trainset = torchvision.datasets.CIFAR10(root='./data/train', train=True,
                                      download=True, transform=transform)
  trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                        shuffle=True, num_workers=0)
  
  testset = torchvision.datasets.CIFAR10(root='./data/test', train=False,
                                     download=True, transform=transform)
  testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
                                       shuffle=False, num_workers=0)
  </code></pre>
  <p>I have already tried many suggestions from answers to similar questions, but none of them worked. Any suggestions about what I should do?</p>