Dataset is not callable

username: Giuseppe
cooked: <p>Hy guys, I have a strange error and I don’t know why.</p>
<p>This is my code:</p>
<pre><code class="lang-auto">from torch.utils.data.dataset import Dataset
import pandas as pd
from PIL import Image
from os import path
import numpy as np
from torchvision import transforms


class Dataset(Dataset):

    def __init__(self, base_path, Intensitypath, csv_list):
        super(Dataset,self).__init__()


        self.base_path = base_path

        self.IntensityPath = Intensitypath

        self.csv_list=csv_list

        self.images = pd.read_csv(self.csv_list, names=['ID','IDIntensity', '#PUNTI2', 'DX', 'DZ','DA','u','v'])




    def __getitem__(self, index):

        ID, IDIntensity, PUNTI2, DX, DZ, DA, u, v = self.images.iloc[index]

        Rest_path = str(ID) + ".png"
        img = Image.open(path.join(self.base_path, Rest_path))

        transformations= transforms.Compose([transforms.Resize(256),
                                             transforms.CenterCrop(224),
                                             transforms.ToTensor(),  # conversione in tensore
                                             transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

        img = transformations(img)

        Rest_path2 = str(IDIntensity) + ".png"

        imgInt = Image.open(path.join(self.IntensityPath, Rest_path2))


        # restituiamo un dizionario contenente immagine, movimento ed orientamento
        return {'image': img,"image_intensity": imgInt,'Movement': np.array([DX, DZ, u, v], dtype='float')
    def __len__(self):
        return len(self.images)

</code></pre>
<p>In another module:</p>
<pre><code class="lang-auto">


import torch
import torch.nn as nn
from torch.optim import Adam
import numpy as np
from dataset import Dataset
from torch.utils.data import DataLoader
from torchnet.meter import AverageValueMeter
from torchnet.logger import VisdomPlotLogger, VisdomSaver
import nets

def start_all():

  
    torch.set_num_threads(4)

    model = nets.final_model()



    #Define train dataset and loader
    train = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Train.csv')
    valid = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Validation.csv')

    train_loader = DataLoader(train, batch_size=64,num_workers=2)
    valid_loader = DataLoader(valid, batch_size=64,num_workers=2)


    model_trained= train(model, train_loader, valid_loader, exp_name="MultiHead", epochs=1000)

</code></pre>
<p>in main module I call the method start_all().</p>
<p>the error that my code give me is:<br>
runfile(’/Users/giuseppepuglisi/Desktop/MultiHead/main.py’, wdir=’/Users/giuseppepuglisi/Desktop/MultiHead’)<br>
Traceback (most recent call last):<br>
File “/Users/giuseppepuglisi/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py”, line 3331, in run_code<br>
exec(code_obj, self.user_global_ns, self.user_ns)<br>
File “”, line 1, in <br>
runfile(’/Users/giuseppepuglisi/Desktop/MultiHead/main.py’, wdir=’/Users/giuseppepuglisi/Desktop/MultiHead’)<br>
File “/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py”, line 197, in runfile<br>
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script<br>
File “/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py”, line 18, in execfile<br>
exec(compile(contents+"\n", file, ‘exec’), glob, loc)<br>
File “/Users/giuseppepuglisi/Desktop/MultiHead/main.py”, line 11, in <br>
train.start_all()<br>
File “/Users/giuseppepuglisi/Desktop/MultiHead/train.py”, line 125, in start_all<br>
model_trained= train(model, train_loader, valid_loader, exp_name=“MultiHead”, epochs=1000)<br>
TypeError: ‘Dataset’ object is not callable</p>
<p>And I don’t know why</p>

username: SaharCarmel
cooked: <p>Try changing the import to</p>
<pre><code class="lang-auto">from torch.utils.data import Dataset
</code></pre>
<p>Also what does the function train do?</p>

username: Giuseppe
cooked: <p>Nothing changes <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"><br>
The strange thing is that i always used this Dataset class and never gave me problems…</p>

username: SaharCarmel
cooked: <p>Sounds like you are not on the same venv you are normally using that might have a different torch version.</p>

username: Giuseppe
cooked: <p>I always used the same conda envirnoiment, try to check</p>

username: Giuseppe
cooked: <p>The same conda environment…  <img src="https://discuss.pytorch.org/images/emoji/apple/frowning_face.png?v=9" title=":frowning_face:" class="emoji" alt=":frowning_face:"></p>

username: SaharCarmel
cooked: <p>Can you post the train function?</p>

username: Giuseppe
cooked: <pre><code class="lang-auto">def train(model, train_loader, valid_loader, exp_name = "prova",  lr=0.000001, epochs=1000, wd = 0.00001):

    #La loss di training
    criterion = nn.MSELoss()
    optimizer = Adam(model.parameters(), lr=lr, wd=wd)

    # meters
    lossAE_meter = AverageValueMeter()
    lossXZ_meter = AverageValueMeter()
    lossUV_meter = AverageValueMeter()
    total_loss_meter = AverageValueMeter()


    # device
    device = "cuda" if torch.cuda.is_available() else "cpu"

    model.to(device)

    loader = {"train": train_loader, "test": valid_loader}

    loss_AE_logger = VisdomPlotLogger('line', env=exp_name, opts={'title': 'LossAE', 'legend': ['train', 'test']})
    loss_XZ_logger = VisdomPlotLogger('line', env=exp_name, opts={'title': 'LossXZ', 'legend': ['train', 'test']})
    loss_UV_logger = VisdomPlotLogger('line', env=exp_name, opts={'title': 'LossUV', 'legend': ['train', 'test']})
    total_loss_logger = VisdomPlotLogger('line', env=exp_name, opts={'title': 'Total Loss', 'legend': ['train', 'test']})

    visdom_saver = VisdomSaver(envs=[exp_name])

    last_best_loss = np.inf
    for e in range(epochs):
        for mode in ["train", "test"]:

            lossAE_meter.reset()
            lossXZ_meter.reset()
            lossUV_meter.reset()
            total_loss_meter.reset()

            model.train() if mode == "train" else model.eval()

            with torch.set_grad_enabled(mode == "train"):  # abilitiamo i gradienti in training

                for i, batch in enumerate(loader[mode]):


                    x = batch["image"].to(device)
                    Movement = batch['Movement'][:, :2].float().to(device)
                    Orientation = batch['Movement'][:, 2:4].float().to(device)

                    target = batch["image_intensity"].to(device) #Immagine da ricostruire
                    code, reconstructed, MovementOrientation = model(x)

                    out1, out2 = MovementOrientation[:, :2], MovementOrientation[:, 2:4]

                    lossAE = criterion(reconstructed,target)
                    lossXZ = criterion(out1,Movement)
                    lossUV = criterion(out2, Orientation)

                    l = lossAE + lossXZ + lossUV
                    if mode == "train":
                        optimizer.zero_grad()
                        l.backward()
                        optimizer.step()
                    else:
                        if l &lt; last_best_loss:
                            torch.save(model.state_dict(), 'Best_ %s.pth' % exp_name)
                            last_best_loss = l



                    n = x.shape[0]  # numero di elementi nel batch

                    lossAE_meter.add(lossAE.item() * n, n)
                    lossXZ_meter.add(lossXZ.item()*n,n)
                    lossUV_meter.add(lossUV.item()* n, n)



                    if mode == "train":
                        loss_AE_logger.log(e + (i + 1) / len(loader[mode]), lossAE_meter.value()[0], name=mode)
                        loss_XZ_logger.log(e + (i + 1) / len(loader[mode]), lossXZ_meter.value()[0], name=mode)
                        loss_UV_logger.log(e + (i + 1) / len(loader[mode]), lossUV_meter.value()[0], name=mode)
                        total_loss_logger.log(e + (i + 1) / len(loader[mode]), total_loss_meter.value()[0], name=mode)

                    loss_AE_logger.log(e + (i + 1) / len(loader[mode]), lossAE_meter.value()[0], name=mode)
                    loss_XZ_logger.log(e + (i + 1) / len(loader[mode]), lossXZ_meter.value()[0], name=mode)
                    loss_UV_logger.log(e + (i + 1) / len(loader[mode]), lossUV_meter.value()[0], name=mode)
                    total_loss_logger.log(e + (i + 1) / len(loader[mode]), total_loss_meter.value()[0], name=mode)

        visdom_saver.save()

        # conserviamo solo l'ultimo modello sovrascrivendo i vecchi, salviamo anche il best di volta in volta
        torch.save(model.state_dict(), '%s.pth' % exp_name)

    return model
</code></pre>
<p>But the problem is not here</p>

username: user_123454321
cooked: <p>Is train a function or dataset ? This implies function…</p>
<pre><code class="lang-auto">train(model, train_loader, valid_loader, exp_name="MultiHead", epochs=1000)
</code></pre>
<p>But this implies dataset…</p>
<pre><code class="lang-auto">train = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Train.csv')
</code></pre>
<p>I think you are calling the dataset instead of the train function…</p>

username: hhaoao
cooked: <p>I think your situation is similar to this, you should redesign your program according to the provided tutorial.</p><aside class="quote quote-modified" data-post="1" data-topic="74979">
  <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/sreekar/48/22221_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979">TypeError: 'DataLoader' object is not callable</a> 
  </div>
  <blockquote>
    train_loader = DataLoader(dataset=dataset, batch_size=40, shuffle=False) 
" This is my train loader variable." 
for epoch in range(num_epochs): 
for i in enumerate(train_loader): 
t = train_loader(-1,1).to(device) 
outputs = model(t) 
loss = criterion(outputs) 
optimizer.zero_grad() 
loss.backward() 
optimizer.step() 
    if (i + 1) % 100 == 0:
        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

 <a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8.png" data-download-href="https://discuss.pytorch.org/uploads/default/759e9d80551a842d2cd10d427140c292e50afdc8" title="Screenshot 2020-04-01 at 12.57.59 PM">[Screenshot 2020-04-01 at 12.5…</a>
  </blockquote>
</aside>


username: Giuseppe
cooked: <p>First I create the dataset for train and validation, then create the data loader and after i pass the data loaders to the train function.<br>
When I call the train I pass the two data loaders</p>

username: user_123454321
cooked: <p>Can you replace</p>
<pre><code class="lang-auto">    train = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Train.csv')
    valid = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Validation.csv')

    train_loader = DataLoader(train, batch_size=64,num_workers=2)
    valid_loader = DataLoader(valid, batch_size=64,num_workers=2)

</code></pre>
<p>with</p>
<pre><code class="lang-auto">    train_dataset = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Train.csv')
    valid_dataset = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Validation.csv')

    train_loader = DataLoader(train_dataset, batch_size=64,num_workers=2)
    valid_loader = DataLoader(train_dataset, batch_size=64,num_workers=2)

</code></pre>

username: Giuseppe
cooked: <p>The error still exists</p>

username: user_123454321
cooked: <p>Your train dataset name is same as that of train function…, so in effect you are calling the dataset (which is rightly not callable) instead of the train function.</p>

username: Giuseppe
cooked: <p>Oh stupid error. You are right man<br>
Thank to all of you for your time and please forgive me!</p>