Hey, I have implemented ResNet and Densenet in PyTorch. I am now using Inception V3. But when I was first using it throws me an error, that I solved by changing the -
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(),
transforms.RandomResizedCrop(244), to
transforms.RandomResizedCrop(299)
# transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
But after that, it is giving me this error -
AttributeError Traceback (most recent call last)
in ()
47
48 output = model.forward(images)
â> 49 loss = criterion(output, labels)
50 loss.backward()
51 optimizer.step()/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
â 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
208 @weak_script_method
209 def forward(self, input, target):
â 210 return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
211
212/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1780 if size_average is not None or reduce is not None:
1781 reduction = _Reduction.legacy_get_string(size_average, reduce)
â 1782 dim = input.dim()
1783 if dim < 2:
1784 raise ValueError(âExpected 2 or more dimensions (got {})â.format(dim))AttributeError: âtupleâ object has no attribute âdimâ
Here is my code -
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = models.inception_v3(pretrained=True)
for param in model.parameters():
param.requires_grad = True
param.aux_logits=False
model.fc = nn.Sequential(nn.Linear(2048, 1024),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(1024,512),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(512,4),
nn.LogSoftmax(dim=1))
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.fc.parameters(), lr = 0.0001)
model.to(device);
epochs = 60
#steps = 0
#print_every = 5
for epoch in range(epochs):
running_loss = 0
model.train()
for images, labels in dataloader_train:
#steps += 1
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
output = model.forward(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
#if steps % print_every == 0:
valid_loss = 0
accuracy = 0
model.eval()
for images, labels in dataloader_test:
optimizer.zero_grad()
with torch.no_grad():
images, labels = images.to(device), labels.to(device)
output = model.forward(images)
loss = criterion(output, labels)
valid_loss += loss.item()
ps = torch.exp(output)
top_p, top_class = ps.topk(1, dim = 1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
I have read many posts, but in no post can particularly answer my problem. Can any of you guys please resolve the issue? Thank you guys in advance.