bentrevett / pytorch-image-classification

Tutorials on how to implement a few key architectures for image classification using PyTorch and TorchVision.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

value error

Rukhmini opened this issue · comments

hello,
Whenever I am trying to run with iterator it is showing error

ValueError Traceback (most recent call last)
in ()
7 start_time = time.time()
8
----> 9 train_loss, train_acc = train(model, train_iterator, optimizer, criterion, device)
10 valid_loss, valid_acc = evaluate(model, valid_iterator, criterion, device)
11

in train(model, iterator, optimizer, criterion, device)
13 optimizer.zero_grad()
14
---> 15 y_pred, _ = model(x)
16
17 loss = criterion(y_pred, y)

ValueError: too many values to unpack (expected 2)

For which notebook is this? Only the first 3 notebooks should be considered complete, the rest are in the process of being re-written.

The model should return a tuple of tensors, in the first three we are returning both the final output and an intermediate layer so we can plot it with PCA/t-SNE. The error you are getting is due to the model only returning a single tensor, i.e. your model's forward method should end with return x, h, but yours is only doing return x (or something similar).

I have executed the resnet.ipynb file with my own dataset. The model has run successfully but I want to visualize the predicted labels as well as confusion matrix as it is shown in the "Lenet.ipynb" tutorial. So, whenever I am trying to execute "images, labels, probs = get_predictions(model, test_iterator)" this line of code it is throwing error like this,
RuntimeError: size mismatch, m1: [40 x 2048], m2: [512 x 2] at C:/w/1/s/tmp_conda_3.6_095855/conda/conda-bld/pytorch_1579082406639/work/aten/src\THC/generic/THCTensorMathBlas.cu:290.
and when I replace the test iterator with valid iterator "images, labels, probs = get_predictions(model, valid_iterator)" it is throwing the previous error
ValueError Traceback (most recent call last)
in ()
----> 1 images, labels, probs = get_predictions(model, valid_iterator)

in get_predictions(model, iterator)
13 x = x.to(device)
14
---> 15 y_pred, _ = model(x)
16
17 y_prob = F.softmax(y_pred, dim = -1)

ValueError: too many values to unpack (expected 2)

In the ResNet model, change the last few lines of the forward method to:

h = out.view(out.shape[0], -1)
out = self.fc(h)
return out, h

changing these line of code is throwing another error
AttributeError: 'tuple' object has no attribute 'log_softmax'

Can you upload the notebook to a GitHub gist? I'll have a quick look at it.

In the train and evaluate functions you need to change fx = model(x) to fx, _ = model(x).

In both the ResNet18 and ResNet34 models, within the forward method you need to change:

out = out.view(out.shape[0], -1)
out = self.fc(out)
return out

to

h = out.view(out.shape[0], -1)
out = self.fc(h)
return out, h

https://gist.github.com/bentrevett/3f899ce17781d91e8c9dbdad2c1a1cc9

Here is the code for the AlexNet notebook but with the model changed to ResNet18 - might be useful for you.