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

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?
this is when i train

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()))
x=train(dataloadertrain,net='mobiface')

This is for test

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}%")

test(dataloadertest, x)

I found this method online and it look fine to another person but when i try to impliment it this error pop up

<ipython-input-142-ce0ef120179a> in <module>()
----> 1 test(dataloadertest, x)

<ipython-input-141-4000f8300d82> in test(test_loader, net)
     16     labs = Variable(labs).cuda(args['device'])
     17     # # Forwarding.
---> 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

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

so am i correct return net for training and
can you explain what is torch.no_grad() and net.eval()

Without torch.no_grad() any operation that involves the tensors with requires_grad=True is recorded by autograd and this process consumes memory. Plus, it can make your code less error-prone (it can save you from occasions when you call backward in the eval phase by accident).

Calling net.eval() is mandatory on the model if the model contains layers/modules with different behaviour between the train and eval phase (dropout, batchnorm, etc.).

just i never heard of this 2 technique before where do it put in in a code?

It should look like this:

@torch.no_grad()
def test(data_loader, net):
    net.eval()
    # the rest of the code

Please check the docs for any further questions.

1 Like

thank you for the help if i got more problem can i ask?

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

Please open a new thread for that.