Dataset is not callable

Hy guys, I have a strange error and I don’t know why.

This is my code:

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)

In another module:




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)

in main module I call the method start_all().

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

And I don’t know why

1 Like

Try changing the import to

from torch.utils.data import Dataset

Also what does the function train do?

Nothing changes :frowning:
The strange thing is that i always used this Dataset class and never gave me problems…

Sounds like you are not on the same venv you are normally using that might have a different torch version.

I always used the same conda envirnoiment, try to check

The same conda environment… :frowning_face:

Can you post the train function?

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 < 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

But the problem is not here

Is train a function or dataset ? This implies function…

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

But this implies dataset…

train = Dataset('./Dataset/Input','./Dataset/ToRec','./Dataset/Train.csv')

I think you are calling the dataset instead of the train function…

I think your situation is similar to this, you should redesign your program according to the provided tutorial.

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.
When I call the train I pass the two data loaders

Can you replace

    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)

with

    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)

1 Like

The error still exists

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.

3 Likes

Oh stupid error. You are right man
Thank to all of you for your time and please forgive me!