'NoneType' object is not callable when i try to test

Archy_dragon: <p>Hi i try to test my model and this error pop up i use the similar method when i train the data and there is no error can anyone check what went wrong?<br>
this is when i train</p>
<pre><code class="lang-auto">def train(dataloader,net): 
      net = load_net(net, 'gpu')
      net = net.cuda()   
      epoch = 2
      criterion = nn.CrossEntropyLoss()
      optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
      train_loss = list()
      #set_trace()
      for i, data in enumerate(dataloader):
        
        inps, labs = data
        inps, labs = inps.cuda(args['device']), labs.cuda(args['device'])

        inps = Variable(inps).cuda(args['device'])
        labs = Variable(labs).cuda(args['device'])
        optimizer.zero_grad()
        outs = net(inps.permute(0, 3, 1, 2).float())
        soft_outs = F.softmax(outs, dim=1)
        prds = soft_outs.data.max(1)[1]
        loss = criterion(outs, labs)
        loss.backward()
        optimizer.step()
        prds = prds.cpu().numpy()
        inps_np = inps.detach().cpu().numpy()
        labs_np = labs.detach().cpu().numpy()
        train_loss.append(loss.data.item
                        ())
        print('[epoch %d], [iter %d / %d], [train loss %.5f]' % (epoch, i + 1, len(dataloader), np.asarray(train_loss).mean()))
</code></pre>
<pre><code class="lang-auto">x=train(dataloadertrain,net='mobiface')
</code></pre>
<p>This is for test</p>
<pre><code class="lang-auto">def test(test_loader, net):
  correct = 0
  total = 0
  since_test = time.time()
  # Iterating over batches.
  for i, data in enumerate(test_loader):
    # Obtaining images, labels and paths for batch.

  
    inps, labs = data
    inps, labs = inps.cuda(args['device']), labs.cuda(args['device'])
    #inps.squeeze_(0) # create a mini-batch as expected by the model
    #labs.squeeze_(0)

    inps = Variable(inps).cuda(args['device'])
    labs = Variable(labs).cuda(args['device'])
    # # Forwarding.
    outs = net(inps.permute(0, 3, 1, 2).float())
    
    for j in range(len(outs['out'])):
      diff = outs['out'][j].argmax(0) - labs[j]
      diff = diff.int().cpu().numpy()
      unique, counts = np.unique(diff,return_counts=True)
      print(unique, counts)
      total += np.sum(counts)
      correct += np.sum(counts[unique == 0])
      print(total,correct)
  
  time_elapsed = time.time() - since_test ##Edit
  print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60)) # Edit
  print(f"accuracy is {(correct/total)*100}%")

</code></pre>
<pre><code class="lang-auto">test(dataloadertest, x)
</code></pre>
<p>I found this method online and it look fine to another person but when i try to impliment it this error pop up</p>
<pre><code class="lang-auto">&lt;ipython-input-142-ce0ef120179a&gt; in &lt;module&gt;()
----&gt; 1 test(dataloadertest, x)

&lt;ipython-input-141-4000f8300d82&gt; in test(test_loader, net)
     16     labs = Variable(labs).cuda(args['device'])
     17     # # Forwarding.
---&gt; 18     outs = net(inps.permute(0, 3, 1, 2).float())
     19 
     20     for j in range(len(outs['out'])):

TypeError: 'NoneType' object is not callable
</code></pre>

mariosasko: <p>You are passing <code>x</code> that is <code>None</code> to <code>test</code> as the second argument. The <code>train</code> function doesn’t have an explicit return statement so it returns <code>None</code>. Just return the model in <code>train</code> to remove the error. Additionally, you can decorate the test function with <code>torch.no_grad()</code> to speed up the inference and call <code>net.eval()</code> to set the model in evaluation mode.</p>

Archy_dragon: <p>so am i correct return net for training and<br>
can you explain what is <code>torch.no_grad()</code> and net.eval()</p>

mariosasko: <p>Without <code>torch.no_grad()</code> any operation that involves the tensors with <code>requires_grad=True</code> is recorded by <code>autograd</code> and this process consumes memory. Plus, it can make your code less error-prone (it can save you from occasions when you call <code>backward</code> in the eval phase by accident).</p>
<p>Calling <code>net.eval()</code> is mandatory on the model if the model contains layers/modules with different behaviour between the train and eval phase (dropout, batchnorm, etc.).</p>

Archy_dragon: <p>just i never heard of this 2 technique before where do it put in in a code?</p>

mariosasko: <p>It should look like this:</p>
<pre><code class="lang-python">@torch.no_grad()
def test(data_loader, net):
    net.eval()
    # the rest of the code
</code></pre>
<p>Please check the docs for any further questions.</p>

Archy_dragon: <p>thank you for the help if i got more problem can i ask?</p>

Archy_dragon: <p>Hi just one last question do you have good reinforcement i can fine to develop my own convolution layer just i need to develop my own convolution layer but im not sure where to start nd what is the basic principle to develop my own one</p>

mariosasko: <p>Please open a new  thread for that.</p>