Assignment 1 Import Error

There is a bug in the import statements in this initial notebook.

The line

from utilities import get_dataloaders

is trying to load a local package which is not included with the tutorial i believe. If there is a pip package for it, it would be nice to replace this with it instead.

1 Like

Hi there. I am also having the same issue.

Conda install for this didn’t work, and from what I found it’s not a pip install either.

I tried signing up to Weights & Biases in case there was something there that could help but couldn’t find anything (though there are some other good set up tips).

Is there a module or script we should be importing this from, or maybe it’s a typo?

I have the same problem with from utilities import get_dataloaders

I guess this one might work:

from torch.utils.data import DataLoader

It seems like you’re referring to 01_intro_starter.ipynb notebook. Imports work there as expected. Here’s the actual code from the file utilities.py:

def get_dataloaders(data_dir, batch_size, slice_size=None, valid_pct=0.2):
    "Get train/val dataloaders for classification on sprites dataset"
    dataset = CustomDataset.from_np(Path(data_dir), argmax=True)
    if slice_size:
        dataset = dataset.subset(slice_size)

    train_ds, valid_ds = dataset.split(valid_pct)

    train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True, num_workers=1)    
    valid_dl = DataLoader(valid_ds, batch_size=batch_size, shuffle=False, num_workers=1)

    return train_dl, valid_dl

I think it still depends on CustomDataset class, so looks like it needs more than just the above function for the notebook to work independently. (it works in the course environment)

From line 341 in utilities.py:

class CustomDataset(Dataset):
    def __init__(self, sprites, slabels, transform=default_tfms, null_context=False, argmax=False):
        self.sprites = sprites
        if argmax:
            self.slabels = np.argmax(slabels, axis=1)
        else:
            self.slabels = slabels
        self.transform = transform
        self.null_context = null_context

    @classmethod
    def from_np(cls, 
                path, 
                sfilename="sprites_1788_16x16.npy", lfilename="sprite_labels_nc_1788_16x16.npy", transform=default_tfms, null_context=False, argmax=False):
        sprites = np.load(Path(path)/sfilename)
        slabels = np.load(Path(path)/lfilename)
        return cls(sprites, slabels, transform, null_context, argmax)

    # Return the number of images in the dataset
    def __len__(self):
        return len(self.sprites)
    
    # Get the image and label at a given index
    def __getitem__(self, idx):
        # Return the image and label as a tuple
        if self.transform:
            image = self.transform(self.sprites[idx])
            if self.null_context:
                label = torch.tensor(0).to(torch.int64)
            else:
                label = torch.tensor(self.slabels[idx]).to(torch.int64)
        return (image, label)
    

    def subset(self, slice_size=1000):
        # return a subset of the dataset
        indices = random.sample(range(len(self)), slice_size)
        return CustomDataset(self.sprites[indices], self.slabels[indices], self.transform, self.null_context)

    def split(self, pct=0.2):
        "split dataset into train and test"
        train_size = int((1-pct)*len(self))
        test_size = len(self) - train_size
        train_dataset, test_dataset = torch.utils.data.random_split(self, [train_size, test_size])
        return train_dataset, test_dataset

It still misses default_tfms.

Not sure why you think I don’t know python. I was just trying to help the material.

In my 1st reply, the line number and file name were provided.
When you continued questioning about symbols defined in utilities.py, you appeared to be less familiar with the jupyter interface and python programming.

Please go ahead and help away. Happy learning btw.

The code is not working for me.

Throwing an error on no module named utilities.

Please provide the link of the utilities.py . As it’s not available in the course material.

Here’s the link to a notebook:

Please click on the jupyter icon on top left of the notebook to view the files in the workspace.
image

After that , select utilities.py and download it.

3 Likes

Thank you Balaji :pray: