I am running resnet50 to realize multi-label annotation. But it comes out the RuntimeError

username: Yaqi_CAI
cooked: <p>I am running finetuning resnt50 to realize multi-label annotation. But it comes out the problem: RuntimeError: size mismatch, m1: [2 x 73728], m2: [2048 x 1000] at c:\programdata\miniconda3\conda-bld\pytorch_1533090265711\work\aten\src\thc\generic/THCTensorMathBlas.cu:249</p>
<p>Here are the codes:<br>
from collections import defaultdict<br>
train_results = defaultdict(list)<br>
train_iter, test_iter, best_acc = 0,0,0<br>
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize = (10, 10))<br>
ax1.set_title(‘Train Loss’)<br>
ax2.set_title(‘Train Accuracy’)<br>
ax3.set_title(‘Test Loss’)<br>
ax4.set_title(‘Test Accuracy’)</p>
<p>f1_scores = defaultdict(list)</p>
<p>for i in tnrange(epochs, desc=‘Epochs’):<br>
print("Epoch ",i)<br>
## Train Phase<br>
<span class="hashtag-raw">#Model</span> switches to train phase<br>
model.train()</p>
<pre><code>all_outputs = []
all_targets = []
# Running through all mini batches in the dataset
count, loss_val, correct, total = train_iter, 0, 0, 0
for img_data, target in tqdm_notebook(train_loader, desc='Training'):    
    img_data, target = img_data.to(device), target.to(device)
    
    output = model(img_data) #FWD prop

    loss = criterion(output, target) #Cross entropy loss
    c_loss = loss.data.item()
    ax1.plot(count, c_loss, 'r.')
    loss_val += c_loss

    optimizer.zero_grad() #Zero out any cached gradients
    loss.backward() #Backward pass
    optimizer.step() #Update the weights

    total_batch = (target.size(0) * target.size(1))
    total += total_batch
    output_data = torch.sigmoid(output)&gt;=0.5
    target_data = (target==1.0)
    for arr1,arr2 in zip(output_data, target_data):
        all_outputs.append(list(arr1.cpu().numpy()))
        all_targets.append(list(arr2.cpu().numpy()))
    c_acc = torch.sum((output_data == target_data.to(device)).to(torch.float)).item()
    ax2.plot(count, c_acc/total_batch, 'r.')
    correct += c_acc
    count +=1
    
all_outputs = np.array(all_outputs)
all_targets = np.array(all_targets)
f1score_samples = f1_score(y_true=all_targets, y_pred=all_outputs, average='samples')
f1score_macro = f1_score(y_true=all_targets, y_pred=all_outputs, average='macro')
f1score_weighted = f1_score(y_true=all_targets, y_pred=all_outputs, average='weighted')
recall = recall_score(y_true=all_targets, y_pred=all_outputs, average='samples')
prec = precision_score(y_true=all_targets, y_pred=all_outputs, average='samples')
hamming = hamming_score(y_true=all_targets, y_pred=all_outputs)

f1_scores["samples_train"].append(f1score_samples)
f1_scores["macro_train"].append(f1score_macro)
f1_scores["weighted_train"].append(f1score_weighted)
f1_scores["hamming_train"].append(hamming)

train_loss_val, train_iter, train_acc = loss_val/len(train_loader.dataset), count, correct/float(total)

print("Training loss: ", train_loss_val, " train acc: ",train_acc)    
## Test Phase

#Model switches to test phase
model.eval()

all_outputs = []
all_targets = []
#Running through all mini batches in the dataset
count, correct, total, lost_val = test_iter, 0, 0, 0
for img_data, target in tqdm_notebook(val_loader, desc='Testing'):
    img_data, target = img_data.to(device), target.to(device)
    output = model(img_data)
    loss = criterion(output, target) #Cross entropy loss
    c_loss = loss.data.item()
    ax3.plot(count, c_loss, 'b.')
    loss_val += c_loss
    #Compute accuracy
    #predicted = output.data.max(1)[1] #get index of max
    total_batch = (target.size(0) * target.size(1))
    total += total_batch
    output_data = torch.sigmoid(output)&gt;=0.5
    target_data = (target==1.0)
    #print("Predictions: ", output_data)
    #print("Actual: ", target_data)
    for arr1,arr2 in zip(output_data, target_data):
        all_outputs.append(list(arr1.cpu().numpy()))
        all_targets.append(list(arr2.cpu().numpy()))
    c_acc = torch.sum((output_data == target_data.to(device)).to(torch.float)).item()
    ax4.plot(count, c_acc/total_batch, 'b.')
    correct += c_acc
    count += 1

#print("Outputs: ", len(all_outputs), " x ", len(all_outputs[0]))
#print("Targets: ", len(all_targets), " x ", len(all_targets[0]))

#F1 Score
all_outputs = np.array(all_outputs)
all_targets = np.array(all_targets)
f1score_samples = f1_score(y_true=all_targets, y_pred=all_outputs, average='samples')
f1score_macro = f1_score(y_true=all_targets, y_pred=all_outputs, average='macro')
f1score_weighted = f1_score(y_true=all_targets, y_pred=all_outputs, average='weighted')
recall = recall_score(y_true=all_targets, y_pred=all_outputs, average='samples')
prec = precision_score(y_true=all_targets, y_pred=all_outputs, average='samples')
hamming = hamming_score(y_true=all_targets, y_pred=all_outputs)

f1_scores["samples_test"].append(f1score_samples)
f1_scores["macro_test"].append(f1score_macro)
f1_scores["weighted_test"].append(f1score_weighted)
f1_scores["hamming_test"].append(hamming)

#Accuracy over entire dataset
test_acc, test_iter, test_loss_val = correct/float(total), count, loss_val/len(test_loader.dataset)
print("Test set accuracy: ",test_acc)

train_results['epoch'].append(i)
train_results['train_loss'].append(train_loss_val)
train_results['train_acc'].append(train_acc)
train_results['train_iter'].append(train_iter)

train_results['test_loss'].append(test_loss_val)
train_results['test_acc'].append(test_acc)
train_results['test_iter'].append(test_iter)

#Save model with best accuracy
if test_acc &gt; best_acc:
    best_acc = test_acc
    torch.save(model.state_dict(), 'best_model.pth') 
</code></pre>
<p>fig.savefig(‘train_curves.png’)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/e/de6af7ca7ea758d0408ae91abdca0f248c625321.png" data-download-href="https://discuss.pytorch.org/uploads/default/de6af7ca7ea758d0408ae91abdca0f248c625321" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/e/de6af7ca7ea758d0408ae91abdca0f248c625321.png" alt="image" data-base62-sha1="vJBaVeJ2aRu5yMwYPVCoFU9iryV" width="540" height="500" data-dominant-color="F4F6F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">821×759 12.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>Could you post the shape of <code>img_data</code>?<br>
Also, are you using a standard renset50 or did you manipulate it somehow?</p>

username: Yaqi_CAI
cooked: <p>I am running this github project: <a href="https://github.com/kshen3778/Ingredient-Detection" class="inline-onebox" rel="noopener nofollow ugc">GitHub - kshen3778/Ingredient-Detection: A Pytorch implementation of the paper Food Ingredients Recognition through Multi-label Learning</a><br>
The dataset is recipes50 ,food41 and ingredients. And when i was running the detection.ipynb, it turns out the runtime error.</p>
<p>I guess the img_shape is as the figure below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/2/f2c792eca92986dd95b381f19eb72a1017345db9.png" data-download-href="https://discuss.pytorch.org/uploads/default/f2c792eca92986dd95b381f19eb72a1017345db9" title="Z3NAKL@JQYI)%QW2OG`SY"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/2/f2c792eca92986dd95b381f19eb72a1017345db9.png" alt="Z3NAKL@JQYI)%QW2OG`SY" data-base62-sha1="yDJ8fhyrqUkFGFCCFgGVJjcCNoR" width="607" height="500" data-dominant-color="F0F0EF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Z3NAKL@JQYI)%QW2OG`SY</span><span class="informations">870×716 14.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Yaqi_CAI
cooked: <p>Besides, I have not change the project resnet50 model. I just changed the path of data and ran the original codes.</p>

username: ptrblck
cooked: <p>I’m not sure, what’s causing this error, as the notebook seems to show a standard resnet with your specified input shape, which works:</p>
<pre><code class="lang-python">model = models.resnet50(pretrained=False)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10)

x = torch.randn(1, 3, 384, 384)
out = model(x)
</code></pre>
<p>Could you rerun the notebook from the beginning and make sure to call all cells in order?<br>
Notebooks are prone to errors caused by a different execution order.</p>

username: Yaqi_CAI
cooked: <p>I rerun the codes several times and the problem exists.</p>
<p><code>&gt; size mismatch, m1: [64 x 73728], m2: [2048 x 227] at c:\programdata\miniconda3\conda-bld\pytorch_1533090265711\work\aten\src\thc\generic/THCTensorMathBlas.cu:249</code></p>
<p>And when I change the codes:</p>
<pre><code class="lang-auto">model = models.resnet50(pretrained=True)
# #freeze layers
# for param in model.parameters():
#      param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, len(targets))

ct = 0
for name, child in model.named_children():
    ct += 1
    if ct &lt; 8:
        for name2, params in child.named_parameters():
            params.requires_grad = False
</code></pre>
<p>into:</p>
<pre><code class="lang-auto">model = models.resnet50(pretrained=False)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10)

x = torch.randn(1, 3, 384, 384)
out = model(x)
ct = 0
for name, child in model.named_children():
    ct += 1
    if ct &lt; 8:
        for name2, params in child.named_parameters():
            params.requires_grad = False
</code></pre>
<p>it occurs the problem:<br>
<code>RuntimeError: size mismatch, m1: [1 x 73728], m2: [2048 x 10] at c:\programdata\miniconda3\conda-bld\pytorch_1533090265711\work\aten\src\th\generic/THTensorMath.cpp:2070</code></p>
<p>So I do not know what I should do to solve it?</p>

username: ptrblck
cooked: <p>Are you getting the same error using my code snippet or just by running the notebook?</p>

username: Yaqi_CAI
cooked: <p>Yeah, I am getting the same error using you codes snippet.</p>

username: Yaqi_CAI
cooked: <p>After I changed the codes, the problem occured as the figure:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/f/bfb5f5ee7bc5d2e501c7176def2ab898343033a8.png" data-download-href="https://discuss.pytorch.org/uploads/default/bfb5f5ee7bc5d2e501c7176def2ab898343033a8" title="error2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/f/bfb5f5ee7bc5d2e501c7176def2ab898343033a8.png" alt="error2" data-base62-sha1="rlX2Q1VPG3kChE7j0U0xZVO9xb2" width="606" height="500" data-dominant-color="F1F5F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error2</span><span class="informations">841×693 11.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Yaqi_CAI
cooked: <p>I reran the codes and the problem occured as the figure:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/8/f84509348002a6c832820b20a426ef7ece3a535f.png" data-download-href="https://discuss.pytorch.org/uploads/default/f84509348002a6c832820b20a426ef7ece3a535f" title="error1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/8/f84509348002a6c832820b20a426ef7ece3a535f.png" alt="error1" data-base62-sha1="zqijVX4YqgA4JqjrayTF0wIqvuL" width="545" height="500" data-dominant-color="F3F6F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error1</span><span class="informations">833×764 15.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>Which <code>torchvision</code> version are you using? Could you check it via <code>print(torchvision.__version__)</code>?</p>

username: Yaqi_CAI
cooked: <p>My torchvision version is 0.2.1.</p>

username: ptrblck
cooked: <p>Could you update it to the latest stable version and rerun the code?<br>
Your version might be missing the adaptive pooling layers, as it’s quite old.</p>

username: Yaqi_CAI
cooked: <p>Hey, thanks for your suggestions! I have updated CUDA version to 9.2 and the problem has been solved.</p>