Hello sir,
Iam a beginnner in pytorch. I have a dataset of images that I want to split into train and validate datasets. I realized that the dataset is highly imbalanced containing 134 (mages) → label 0, 20(images)-> label 1,136 (images)->label 2, 74(images)->lable 3 and 49(images)->label 4. So after splitting that dataset into train and validate, I would want to take the training dataset and balance it, and after that I want to apply transformations to train and validate dataset separately and get the train and validate loader ready. How can I go about it and the right approach? What I have done is…
# Custom dataset
class DiabeticRetinopathy(Dataset):
def init(self, csv_file, root_dir, total=None,transform = None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def len(self):
return len(self.annotations)
def getitem(self, index):
#img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
img_path = os.path.join(self.root_dir, self.annotations.iloc[index].image +".jpg")
image = Image.open(img_path)
y_label = torch.tensor(int(self.annotations.iloc[index,1]))
if self.transform:
image = self.transform(image)
return(image, y_label)
# transform for the entire dataset
normalize = transforms.Normalize(mean=[x / 255.0 for x in [0.4346, 0.2110, 0.0705]],
std=[x / 255.0 for x in [0.3102, 0.1663, 0.0852]])
my_transforms = transforms.Compose([
transforms.ToTensor(),
normalize
])
full_dataset = DiabeticRetinopathy(
csv_file="/content/B.%20Disease%20Grading/B. Disease Grading/2. Groundtruths/a. IDRiD_Disease Grading_Training Labels.csv",
root_dir="/content/B.%20Disease%20Grading/B. Disease Grading/1. Original Images/a. Training Set", transform=my_transforms)
#Splitting the dataset into train and validate
train_set, val_set=torch.utils.data.random_split(full_dataset, [373,40 ])
# code for balancing

Get all targets
targets = []
for _, target in train_dataset:
targets.append(target)
targets = torch.stack(targets)

Compute samples weight (each sample should get its own weight)
class_sample_count = torch.tensor(
[(targets == t).sum() for t in torch.unique(targets, sorted=True)])
weight = 1. / class_sample_count.float()
samples_weight = torch.tensor([weight[t] for t in targets])

Create sampler, dataset, loader
sampler = WeightedRandomSampler(samples_weight, len(samples_weight))
train_loader = DataLoader(
train_dataset, batch_size=batch_size, num_workers=1, sampler=sampler)

Iterate DataLoader and check class balance for each batch
for i, (x, y) in enumerate(train_loader):
print(“batch index {}, 0/1: {}/{}”.format(
i, (y == 0).sum(), (y == 1).sum(), (y == 2).sum(), (y == 3).sum(), (y == 4).sum()))
#In the above code, how will I make my train dataset

after this I want to apply the train_tranform to the balanced training dataset and create train and validate dataloaders.
train_transform = transforms.Compose([
                         transforms.Resize((2500,2500)),
                         transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0, hue=0.1),
                         transforms.RandomCrop(1490,1490),
                         transforms.GaussianBlur(11, sigma=(0.1, 2.0)),
                         transforms.RandomRotation(degrees=90),
                         transforms.RandomHorizontalFlip(p=0.5),
                         transforms.RandomVerticalFlip(p=0.5),
                         transforms.ToTensor(), #converting the dimension from (height,weight,channel) to (channel,height,weight) convention of PyTorch
                  transforms.Normalize([0.4346, 0.2110, 0.0705],[0.3102, 0.1663, 0.0852])

])