Question about GoogLeNet

The googlenet model on the torchvision exists on master branch.
I am using torchvision==0.2.3a0+ae81313 (latest master)
If I call googlenet model from examples/imagenet/main.py is failed on following error.
Is there any way to solve this?

Traceback (most recent call last):
  File "examples/imagenet/main.py", line 417, in <module>
    main()
  File "examples/imagenet/main.py", line 113, in main
    main_worker(args.gpu, ngpus_per_node, args)
  File "examples/imagenet/main.py", line 239, in main_worker
    train(train_loader, model, criterion, optimizer, epoch, args)
  File "examples/imagenet/main.py", line 282, in train
    loss = criterion(output, target)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py", line 942, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 2056, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 1350, in log_softmax
    ret = input.log_softmax(dim)
AttributeError: 'GoogLeNetOuputs' object has no attribute 'log_softmax'

Based on the error message it looks like you are using nn.CrossEntropyLoss as your criterion.
By default, you will get three outputs from googlenet: logits, aux_logits1, and aux_logits2.
If you simply pass all three outputs to your criterion, you’ll get this error.

If you don’t need the aux outputs, just pass the logits to your criterion as:

model = models.googlenet()

x = torch.randn(2, 3, 224, 224)
output = model(x)

criterion = nn.CrossEntropyLoss()
criterion(output.logits, torch.randint(0, 1000, (2,)))
2 Likes

Thank you for comments.
I am just executing imagenet example on google colab with following instruction.
I try to change example code with your suggestion but another error appeared.
But just for my simple test, I will check it later.

Thanks again

Get examples and data (imagenett), and update model (torchvision)

!wget https://s3.amazonaws.com/fast-ai-imageclas/imagenette-320.tgz
!tar xzf imagenette-320.tgz`
!git clone http://github.com/pytorch/examples
!pip uninstall -y torchvision
!git clone http://github.com/pytorch/vision
!cd vision;python setup.py install

Executing googlenet

!python examples/imagenet/main.py -a googlenet -j 2 -b 64 --epochs 10 imagenette-320/

Which other error did occur?
Let me know, if you get stuck, and we can have another look.

With above execution, following error is appeared.

=> creating model 'googlenet'
Traceback (most recent call last):
  File "examples/imagenet/main.py", line 417, in <module>
    main()
  File "examples/imagenet/main.py", line 113, in main
    main_worker(args.gpu, ngpus_per_node, args)
  File "examples/imagenet/main.py", line 239, in main_worker
    train(train_loader, model, criterion, optimizer, epoch, args)
  File "examples/imagenet/main.py", line 285, in train
    acc1, acc5 = accuracy(output, target, topk=(1, 5))
  File "examples/imagenet/main.py", line 405, in accuracy
    _, pred = output.topk(maxk, 1, True, True)
AttributeError: 'GoogLeNetOuputs' object has no attribute 'topk'

P.S. Workaround fix by your suggestion is in following URL

Make sure your output tensor are the logits, not GoogLeNetOutputs.
If you don’t need the aux logits, just add this line to your code:

output = model(x)
output = output.logits
...
1 Like

Thank you, it works!

I create a ticket for this issue for further notice.