TypeError: 'module' object is not callable

Hi everyone. I’m trying to load a pre-trained model and see its accuracy for a small apple diseases dataset:

import torch
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets, models

transform = transforms.Compose(
     transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

testset = torchvision.datasets.ImageFolder('data/', transform=transforms)     
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=True, num_workers=2)

"""testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)"""

classes = ('Folha Desfolha Precoce', 'Folha Glifosato', 'Folha Magnésio', 'Folha Potássio',
           'Folha Sadia Gala', 'Fruto Alternaria', 'Fruto Bitter Pit', 'Fruto Bitter Pit', 'Fruto Colletotrichum', 
           'Fruto Penicillium')

device = torch.device('cpu')
net = torch.load('applesnet/main.pt', map_location=device)

The problem is when I try to run the code below to measure the accuracy, I get the error
message “TypeError: ‘module’ object is not callable”:

correct = 0
total = 0
with torch.no_grad():
    for data in testloader: 
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the test images: %d %%' % (
    100 * correct / total))
TypeError                                 Traceback (most recent call last)
<ipython-input-10-bc22d0ffcea3> in <module>
      3 with torch.no_grad():
----> 5     for data in testloader:
      6         images, labels = data
      7         outputs = net(images)

~/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    635                 self.reorder_dict[idx] = batch
    636                 continue
--> 637             return self._process_next_batch(batch)
    639     next = __next__  # Python 2 compatibility

~/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
    656         self._put_indices()
    657         if isinstance(batch, ExceptionWrapper):
--> 658             raise batch.exc_type(batch.exc_msg)
    659         return batch

TypeError: Traceback (most recent call last):
  File "/home/gustavo/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/gustavo/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/gustavo/anaconda3/lib/python3.7/site-packages/torchvision/datasets/folder.py", line 103, in __getitem__
    sample = self.transform(sample)
TypeError: 'module' object is not callable

The most strange thing is, when I use the CIFAR-10 dataset instead of this particular dataset, I don’t get this error message.

You have typo here, it must be transform not transforms. I mean s is the problem.

Based on your definition in below line

transform is composed object and transforms is the PyTorch package itself.

In the CIFAR10 example, you are using transform!

Good luck


Yes, that was my problem. It worked now. I can’t believe that was just it!
Thank you very much for the help @Nikronic.

1 Like

No problem mate, I can say approximately %90 of my problems related to python type system. As a computer engineering student, I really do not know why this language is even popular! If you omit great frameworks, python is a shame for computer engineering!
I highly benefited from using PyCharm (a smarter IDE). Recently, I found a framework on top of python that enables you do type checking even hierarchically but I cannot remember its name unfortunately.