python v 3.7.3
, pytorch v 0.4.1
, using jupyter
I am working on creating an image classifier using transfer learning with ResNet50 as the base model. I've run across an error that I don't understand how to debug, even after searching for solutions online. I'm not sure why my dimensions are so off. Any help on what to do would be appreciated. Also, any book recs on Deep Learning using Pytorch would be great :)
Code for final layer of ResNet:
fc_inputs = model.fc.in_features
model.fc = nn.Sequential(OrderedDict([
('fc1', nn.Linear(fc_inputs, 256)),
('relu', nn.ReLU()),
('dropout', nn.Dropout(0.2)),
('fc2', nn.Linear(256, 25)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
print(model)
print(model.classifier[0])
print(model.classifier[3])
optimizer = torch.optim.Adam(model.classifier.parameters(), lr = 0.001)
criterion = nn.CrossEntropyLoss()
scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)
code for training:
epochs = 1
steps = 0
running_loss = 0
print_every = 10
for epoch in range(epochs):
for images, labels in dataloaders['train']:
images = images.view(images.shape[0], -1) #this flattens it?
steps += 1
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
logps = model(images)
loss = criterion(logps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if step % print_every == 0:
model.eval()
test_loss = 0
accuracy = 0
for images, labels in dataloader:
images, labels = images.to(device), labels.to(device)
logps = model(images)
loss = criterion(logps, labels)
test_loss += loss.item()
ps = torch.exp(logps)
top_ps, top_class = ps.topk(1, dim=1)
equality = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equality.type(torch.FloatTensor)).item()
train_losses.append(running_loss / len(dataloader['train']))
test_losses.append(test_loss / len(dataloader['test']))
print(f"Epoch {epoch + 1}/{epochs}.."
f"Train loss: {running_loss / print_every:.3f}.."
f"Test loss: {test_loss/len(dataloader['test']):.3f}.."
f"Test accuracy: {accuracy/len(dataloader['test']):.3f}")
running_loss = 0
model.train()
error:
RuntimeError Traceback (most recent call last)
<ipython-input-47-8f88694d7909> in <module>
16 optimizer.zero_grad()
17
---> 18 logps = model(images)
19
20 loss = criterion(logps, labels)
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
475 result = self._slow_forward(*input, **kwargs)
476 else:
--> 477 result = self.forward(*input, **kwargs)
478 for hook in self._forward_hooks.values():
479 hook_result = hook(self, input, result)
~\Anaconda3\lib\site-packages\torchvision\models\resnet.py in forward(self, x)
137
138 def forward(self, x):
--> 139 x = self.conv1(x)
140 x = self.bn1(x)
141 x = self.relu(x)
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
475 result = self._slow_forward(*input, **kwargs)
476 else:
--> 477 result = self.forward(*input, **kwargs)
478 for hook in self._forward_hooks.values():
479 hook_result = hook(self, input, result)
~\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
299 def forward(self, input):
300 return F.conv2d(input, self.weight, self.bias, self.stride,
--> 301 self.padding, self.dilation, self.groups)
302
303
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 7, 7], but got input of size [8, 196608] instead
EDIT: I removed images = images.view.shape[0], -1
, which was converting my tensor to [8, 196608]
. But now I am receiving a new error:
RuntimeError: size mismatch, m1: [8 x 8192], m2: [2048 x 256] at c:\programdata\miniconda3\conda-bld\pytorch_1532509700152\work\aten\src\th\generic/THTensorMath.cpp:2070