pytorch-image-classification icon indicating copy to clipboard operation
pytorch-image-classification copied to clipboard

value error

Open Rukhmini opened this issue 4 years ago • 10 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)

Rukhmini avatar Apr 05 '20 17:04 Rukhmini

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).

bentrevett avatar Apr 06 '20 12:04 bentrevett

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)

Rukhmini avatar Apr 06 '20 13:04 Rukhmini

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

bentrevett avatar Apr 06 '20 13:04 bentrevett

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

Rukhmini avatar Apr 06 '20 13:04 Rukhmini

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

bentrevett avatar Apr 06 '20 14:04 bentrevett

I have uploaded the notebook as a Github gist.

On Mon, Apr 6, 2020 at 7:38 PM Ben Trevett [email protected] wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bentrevett/pytorch-image-classification/issues/2#issuecomment-609817725, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL4XZOCYAEZLKAVTRREVXBLRLHO5TANCNFSM4MBF7VZQ .

-- With Regards, Rukhmini Roy

Rukhmini avatar Apr 06 '20 14:04 Rukhmini

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

bentrevett avatar Apr 06 '20 16:04 bentrevett

Thanks for all the help Ben, but still I am getting the same runtime error while using test iterator.

On Mon, Apr 6, 2020 at 9:41 PM Ben Trevett [email protected] wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bentrevett/pytorch-image-classification/issues/2#issuecomment-609889365, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL4XZODYZY4TEFUQ7C2ESRDRLH5J3ANCNFSM4MBF7VZQ .

-- With Regards, Rukhmini Roy

Rukhmini avatar Apr 06 '20 16:04 Rukhmini

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.

bentrevett avatar Apr 06 '20 18:04 bentrevett

This also throwing the same runtime error. I am using gray scale images to train and test the network. Will that be a problem?

On Mon, Apr 6, 2020 at 11:40 PM Ben Trevett [email protected] wrote:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bentrevett/pytorch-image-classification/issues/2#issuecomment-609952914, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL4XZOE3UH2FAG5K5ZHJ373RLILKDANCNFSM4MBF7VZQ .

-- With Regards, Rukhmini Roy

Rukhmini avatar Apr 07 '20 08:04 Rukhmini