How to solve problem of memory error in multi label classification?

I have 5529 labels,
and each image has variable number of labels.
In my final result, i can have upto 100 labels.
I made my custom dataset following this code.
class myCustomDataset(Dataset):
“”“my dataset.”""

def __init__(self, csv_file, root_dir,img_ext,transform=None):
        csv_file (string): Path to the csv file with annotations.
        root_dir (string): Directory with all the images.
        transform (callable, optional): Optional transform to be applied
            on a sample.
    tmp_df = pd.read_csv(csv_file,sep=';',header=None)
    assert tmp_df.iloc[:,0].apply(lambda x: os.path.isfile(root_dir + x + img_ext)).all(), \

“Some images referenced in the CSV file were not found” = MultiLabelBinarizer()
    self.root_dir = root_dir
    self.img_ext = img_ext
    self.transform = transform

    self.X_train = tmp_df.iloc[:,0]
    self.y_train =[:,0].str.split()).astype(np.float32)


def __len__(self):
    return len(self.X_train.index)

def __getitem__(self, index):
    img = + self.X_train[index] + self.img_ext)
    img = img.convert('RGB')
    if self.transform is not None:
        img = self.transform(img)
    label = torch.from_numpy(self.y_train[index])
    return img, label

now,i give my validationset to the dataset and create dataloader.

transformedvalid_dataset= myCustomDataset(csv_file=’/home/nis/Downloads/trialdata/Validation-Concepts.csv’,
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
I am able to create my custom dataset ,and make validation loader.

trainloader =, batch_size=32, shuffle=True)
dataiter = iter(trainloader)
images, labels =

<class ‘torch.Tensor’>
torch.Size([32, 3, 224, 224])
torch.Size([32, 14157])

But when i do same for train images(56638 images), it gives memory error when i provide my data set. I am far from making data loader work in case of train images as it gives memory error while giving images to dataset. Data loader would be the next step and batch size is provided at data loader.
i could not figure out or find any links to correct the error.

how can i solve it? Also,my labels shape is equal to numbers of images in validation set,is it correct?
Also, how to know how data looks at
self.y_train =[:,0].str.split()).astype(np.float32)
label = torch.from_numpy(self.y_train[index])

What kind of memory error do you get?
Could you post the stack trace so that we can have a look?

I solved the problem. I had to put comma inside split function.

Memory errors happens a lot with python when using the 32bit Windows version . This is because 32bit processes only gets 2GB of memory to play with by default.

The solution for this error is that pandas.read_csv() function takes an option called dtype. This lets pandas know what types exist inside your csv data.

For example: by specifying dtype={‘age’:int} as an option to the .read_csv() will let pandas know that age should be interpreted as a number. This saves you lots of memory.


Or try the solution below: