"RuntimeError: mat1 dim 1 must match mat2 dim 0" only at eval

username: 111526
cooked: <p>File “classifier.py”, line 144, in <br>
test_loss, test_accuracy = evaluate(model, dataloaders[“val”])<br>
File “classifier.py”, line 115, in evaluate<br>
output = model(image)<br>
File “/home/bak/anaconda3/envs/GNN/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/bak/anaconda3/envs/GNN/lib/python3.7/site-packages/torchvision/models/resnet.py”, line 151, in forward<br>
x = self.fc(x)<br>
File “/home/bak/anaconda3/envs/GNN/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/bak/anaconda3/envs/GNN/lib/python3.7/site-packages/torch/nn/modules/linear.py”, line 94, in forward<br>
return F.linear(input, self.weight, self.bias)<br>
File “/home/bak/anaconda3/envs/GNN/lib/python3.7/site-packages/torch/nn/functional.py”, line 1753, in linear<br>
return torch._C._nn.linear(input, weight, bias)<br>
RuntimeError: mat1 dim 1 must match mat2 dim 0</p>
<hr>
<p>import numpy as np<br>
import matplotlib.pyplot as plt</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torchvision import transforms, datasets, models, utils</p>
<p>if torch.cuda.is_available():<br>
DEVICE = torch.device(‘cuda’)<br>
else:<br>
DEVICE = torch.device(‘cpu’)</p>
<p>BATCH_SIZE = 8<br>
EPOCHS = 50</p>
<p>data_transforms = {<br>
‘train’: transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
# transforms.Resize(256),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.CenterCrop(224),<br>
transforms.Resize(256),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])<br>
]),<br>
}</p>
<p>image_datasets = {x: datasets.ImageFolder("/home/bak/Dataset/hymenoptera_data/hymenoptera_data", data_transforms[x]) for x in [‘train’, ‘val’]}<br>
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size = BATCH_SIZE, num_workers = 0, shuffle = True) for x in [‘train’, ‘val’]}<br>
dataset_sizes = {x: len(image_datasets[x]) for x in [‘train’, ‘val’]}<br>
class_names = image_datasets[‘train’].classes</p>
<p>def train(model, train_loader, optimizer, log_interval):<br>
model.train()<br>
for batch_idx, (image, label) in enumerate(train_loader):<br>
image = image.to(DEVICE)<br>
label = label.to(DEVICE)<br>
optimizer.zero_grad()<br>
output = model(image)<br>
loss = criterion(output, label)<br>
loss.backward()<br>
optimizer.step()</p>
<pre><code>    if batch_idx % log_interval == 0:
        print("Train Epoch: {} [{}/{} ({:.0f}%)]\tTrain Loss: {:.6f}".format(
            epoch, batch_idx * len(image),
            len(train_loader.dataset), 100. * batch_idx / len(train_loader),
            loss.item()))
</code></pre>
<p>def evaluate(model, test_loader):<br>
model.eval()<br>
test_loss = 0<br>
correct = 0</p>
<pre><code>with torch.no_grad():
    for image, label in test_loader:
        image = image.to(DEVICE)
        label = label.to(DEVICE)
        output = model(image)
        test_loss += criterion(output, label).item()
        prediction = output.max(1, keepdim = True)[1]
        correct += prediction.eq(label.view_as(prediction)).sum().item()

test_loss /= (len(test_loader.dataset) / BATCH_SIZE)
test_accuracy = 100. * correct / len(test_loader.dataset)
return test_loss, test_accuracy
</code></pre>
<p>model = models.resnet18(pretrained = False)<br>
num_ftrs = model.fc.in_features<br>
model.fc = nn.Linear(num_ftrs, 2)<br>
model = model.to(DEVICE)</p>
<p>optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)<br>
criterion = nn.CrossEntropyLoss()</p>
<p>for epoch in range(1, EPOCHS + 1):<br>
train(model, dataloaders[“train”], optimizer, log_interval = 5)<br>
test_loss, test_accuracy = evaluate(model, dataloaders[“val”])<br>
print("\n[EPOCH: {}], \tTest Loss: {:.4f}, \tTest Accuracy: {:.2f} % \n".format(<br>
epoch, test_loss, test_accuracy))</p>
<hr>
<p>Train works well until the end of the first epoch. However, Runtimeerror appears at evaluation. I saw the linear dimension was different from the train.</p>
<p>I still don’t understand why these changes appear…</p>

username: eqy
cooked: <p>Have you checked whether the shapes are what you expect at each stage in the model?</p>
<p>Even if the image resolution is different at validation time it shouldn’t affect the input dimension to the linear layer as there is an average pooling step after the last conv layer in ResNet-18.</p>

username: 111526
cooked: <p>Finally, I found the reason! Due to the limitation of the input size of ResNet, there was a miss-match between CNN layer and FC layer. When I change the resize value of transforms, it works well.</p>
<p>Thank you.</p>