TypeError: 'module' object is not callable

username: Gustavo_Adolfo_Schwa
cooked: <p>Hi everyone. I’m trying to load a pre-trained model and see its accuracy for a small apple diseases dataset:</p>
<pre><code class="lang-auto">import torch
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets, models

transform = transforms.Compose(
    [transforms.ToTensor(),
     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)
net.eval()
</code></pre>
<p>The problem is when I try to run the code below to measure the accuracy, I get the error<br>
message “TypeError: ‘module’ object is not callable”:</p>
<pre><code class="lang-auto">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))
</code></pre>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-10-bc22d0ffcea3&gt; in &lt;module&gt;
      3 with torch.no_grad():
      4 
----&gt; 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
--&gt; 637             return self._process_next_batch(batch)
    638 
    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):
--&gt; 658             raise batch.exc_type(batch.exc_msg)
    659         return batch
    660 

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 &lt;listcomp&gt;
    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
</code></pre>
<p>The most strange thing is, when I use the CIFAR-10 dataset instead of this particular dataset, I don’t get this error message.</p>

username: Nikronic
cooked: <p>Hi,<br>
You have typo here, it must be <code>transform</code> not <code>transforms</code>. I mean <code>s</code> is the problem.</p>
<aside class="quote no-group quote-modified" data-username="Gustavo_Adolfo_Schwa" data-post="1" data-topic="55425">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gustavo_adolfo_schwa/48/15744_2.png" class="avatar"> Gustavo_Adolfo_Schwa:</div>
<blockquote>
<p>testset = torchvision.datasets.ImageFolder(‘data/’, transform=transforms)</p>
</blockquote>
</aside>
<p>Based on your definition in below line</p>
<aside class="quote no-group" data-username="Gustavo_Adolfo_Schwa" data-post="1" data-topic="55425">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gustavo_adolfo_schwa/48/15744_2.png" class="avatar"> Gustavo_Adolfo_Schwa:</div>
<blockquote>
<p>transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])</p>
</blockquote>
</aside>
<p><code>transform</code> is composed object and <code>transforms</code> is the PyTorch package itself.</p>
<p>In the CIFAR10 example, you are using <code>transform</code>!</p>
<aside class="quote no-group quote-modified" data-username="Gustavo_Adolfo_Schwa" data-post="1" data-topic="55425">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gustavo_adolfo_schwa/48/15744_2.png" class="avatar"> Gustavo_Adolfo_Schwa:</div>
<blockquote>
<p>“”"testset = torchvision.datasets.CIFAR10(root=‘./data’, train=False, download=True, transform=transform)</p>
</blockquote>
</aside>
<p>Good luck</p>

username: Gustavo_Adolfo_Schwa
cooked: <p>Yes, that was my problem. It worked now. I can’t believe that was just it!<br>
Thank you very much for the help <a class="mention" href="/u/nikronic">@Nikronic</a>.</p>

username: Nikronic
cooked: <p>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!<br>
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.</p>
<p>Bests</p>