0
$\begingroup$

I am trying to solve this problem by using a convolutional NN to classify an image data set to check the type of disease it is. I have reached task 1b and trying to implement the training loop. However, I am getting an error and can't understand how to implement the training loop.

I am sharing the google drive link.

The code in task 1b where I am getting an error:

import datasets

ds = datasets.LesionDataset('/content/data/img',
                            '/content/data/img/train.csv')
input, label = ds[0]
print(input)
print(label)

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-dcb680f1f34a> in <module>()
      4 ds = datasets.LesionDataset('/content/data/img',
      5                             '/content/data/img/train.csv')
----> 6 input, label = ds[0]
      7 print(input)
      8 print(label)

/content/drive/MyDrive/DL Assignment/datasets.py in __getitem__(self, idx)
     34   def __getitem__(self, idx):
     35 
---> 36         image = self.img_dir[idx]
     37         label = self.labels_fname[idx]
     38 

'datasets' is a .py file currently in the same folder in the link shared which has the following code: You can open the link to understand it better.

import collections
import csv
from pathlib import Path

import numpy as np
import torch
import torchvision.transforms as transforms
from PIL import Image
import pandas as pd


# TODO Task 1b - Implement LesionDataset
from torch.utils.data import Dataset
class LesionDataset(Dataset):
#The __init__ function should have the following prototype
  def __init__(self, img_dir, labels_fname):
    # is the directory path with all the image files
    # is the csv file with image ids and their corresponding labels
      self.img_dir = Path(img_dir)

      #image = Image.open('ISIC_0024306.jpg')
      # 
      self.labels_fname = pd.read_csv(labels_fname)


      image = self.labels_fname['image']


#list of images store in list 
  def __len__(self):
  
        return len(self.labels_fname)

  def __getitem__(self, idx):
  
        image = self.img_dir[idx]
        label = self.labels_fname[idx]

        return image, label

# TODO Task 1e - Add augment flag to LesionDataset, so the __init__ function
#                now look like this:
#                   def __init__(self, img_dir, labels_fname, augment=False):



# TODO Task 2b - Implement TextDataset
#               The __init__ function should have the following prototype
#                   def __init__(self, fname, sentence_len)
#                   - fname is the filename of the cvs file that contains each
#                     news headlines text and its corresponding label.
#                   - sentence_len the maximum sentence length you want the
#                     tokenized to return. Any sentence longer than that should
#                     be truncated by the tokenizer. Any shorter sentence should
#                     padded by the tokenizer.
#                We will be using the pretrained 'distilbert-base-uncased' transform,
#                so please use the appropriate tokenizer for it. NOTE: You will need
#                to include the relevant import statement.

I am stuck at this part. Can anyone suggest how to implement it?

$\endgroup$
1
1
$\begingroup$

In the __getitem__ method you should return both the image and the label, in your current example you are trying to get the image by indexing the img_dir variable or your class, which won't work since this is of type Path. The general steps for the __getitem__ method is that you should (1) get the path of image, (2) load the image and (3) convert the image to a tensor for pytorch to use. This would something like this:

from torch.utils.data import Dataset
import torchvision.transforms as transforms
from pathlib import Path
import pandas as pd
from PIL import Image

class LesionDataset(Dataset):
    def __init__(self, img_dir, labels_fname):
        self.img_dir = Path(img_dir)
        self.img_paths = self.img_dir.glob("*")
        self.labels_fname = pd.read_csv(labels_fname)

    def __len__(self):
        return len(self.labels_fname)

    def __getitem__(self, idx):
        img_path = self.img_paths[idx]
        image = Image.open(img_path)
        image = transforms.ToTensor()(image)
        label = self.labels_fname[idx]
        return image, label
$\endgroup$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.