Question about GoogLeNet

sakaia: <p>The googlenet model on the torchvision exists on master branch.<br>
I am using torchvision==0.2.3a0+ae81313 (latest master)<br>
If I call googlenet model from  examples/imagenet/main.py is failed on following error.<br>
Is there any way to solve this?</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "examples/imagenet/main.py", line 417, in &lt;module&gt;
    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'
</code></pre>

ptrblck: <p>Based on the error message it looks like you are using <code>nn.CrossEntropyLoss</code> as your criterion.<br>
By default, you will get three outputs from <code>googlenet</code>: <code>logits</code>, <code>aux_logits1</code>, and <code>aux_logits2</code>.<br>
If you simply pass all three outputs to your criterion, you’ll get this error.</p>
<p>If you don’t need the aux outputs, just pass the logits to your criterion as:</p>
<pre><code class="lang-python">model = models.googlenet()

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

criterion = nn.CrossEntropyLoss()
criterion(output.logits, torch.randint(0, 1000, (2,)))
</code></pre>

sakaia: <p>Thank you for comments.<br>
I am just executing imagenet example on google colab with following instruction.<br>
I try to change example code with your suggestion but another error appeared.<br>
But just for my simple test, I will check it later.</p>
<p>Thanks again</p>
<p>Get examples and data (imagenett),  and update model (torchvision)</p>
<pre><code class="lang-auto">!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
</code></pre>
<p>Executing googlenet</p>
<pre><code class="lang-auto">!python examples/imagenet/main.py -a googlenet -j 2 -b 64 --epochs 10 imagenette-320/
</code></pre>

ptrblck: <p>Which other error did occur?<br>
Let me know, if you get stuck, and we can have another look.</p>

sakaia: <p>With above execution, following error is appeared.</p>
<pre><code class="lang-auto">=&gt; creating model 'googlenet'
Traceback (most recent call last):
  File "examples/imagenet/main.py", line 417, in &lt;module&gt;
    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'
</code></pre>
<p>P.S.  Workaround fix by your suggestion is in following URL<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/sakaia/examples/tree/googlenet" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/144597?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/sakaia/examples/tree/googlenet" target="_blank" rel="nofollow noopener">sakaia/examples</a></h3>

<p>A set of examples around pytorch in Vision, Text, Reinforcement Learning, etc. - sakaia/examples</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

ptrblck: <p>Make sure your <code>output</code> tensor are the logits, not <code>GoogLeNetOutputs</code>.<br>
If you don’t need the aux logits, just add this line to your code:</p>
<pre><code class="lang-python">output = model(x)
output = output.logits
...
</code></pre>

sakaia: <p>Thank you, it works!</p>

sakaia: <p>I create a ticket for this issue for further notice.<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/examples/issues/559" target="_blank" rel="nofollow noopener">github.com/pytorch/examples</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/sakaia" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/144597?v=2&amp;s=96" class="thumbnail onebox-avatar" width="60" height="60">
</a>

<h4><a href="https://github.com/pytorch/examples/issues/559" target="_blank" rel="nofollow noopener">Issue: GoogleNet does not work on imagenet</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/sakaia" target="_blank" rel="nofollow noopener">sakaia</a>
	on <a href="https://github.com/pytorch/examples/issues/559" target="_blank" rel="nofollow noopener">2019-05-13</a>
	</div>
	<div class="user">
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">GoogLeNet does not run on imagenet training example.
Since googlenet model have multiple (logits etc) outputs.
To reproduce, do following instruction on Google...</pre>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>