text_plain_output_1.png
from torch.utils.data import Dataset, DataLoader, SubsetRandomSampler

from torchvision import transforms

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

gpu_status = torch.cuda.is_available()

train_df = pd.read_csv('../input/plant-pathology-2020-fgvc7/train.csv')

def get_label(row):
    for c in train_df.columns[1:]:
        if row[c] == 1:
            return c
train_df_copy = train_df.copy()
train_df_copy['label'] = train_df_copy.apply(get_label, axis=1)

sample_img = train_df.iloc[1, 0]
sample_labels = train_df.iloc[1, :]
sample_labels = np.asarray(sample_labels)

class LeafDataset(Dataset):
    def __init__(self, df, transform=None):
        self.df = df
        self.transform = transform
    def __len__(self):
        return self.df.shape[0]
    def __getitem__(self, idx):
        img_src = '../input/plant-pathology-2020-fgvc7/images/' + self.df.loc[idx, 'image_id'] + '.jpg'
        image = PIL.Image.open(img_src).convert('RGB')
        if self.transform:
            image = self.transform(image)
        if self.df.shape[1] == 5:
            labels = self.df.loc[idx, ['healthy', 'multiple_diseases', 'rust', 'scab']].values
            labels = torch.from_numpy(labels.astype(np.uint8))
            labels = labels.unsqueeze(-1).long()
            labels = labels.numpy().tolist().index([1])
            labels = torch.from_numpy(np.asarray(labels))
            return (image, labels)
        else:
            return image

leaf_sample_dataset = LeafDataset(df=train_df, transform=None)
fig, ax = plt.subplots(1,3)

for i in range(3):
    img, label = leaf_sample_dataset[i]
    
    ax[i].imshow(img)
    print(type(img), img.size,label)

leaf_transform = transforms.Compose([transforms.Resize((512, 512)), transforms.CenterCrop((384, 384)), transforms.RandomAffine(degrees=15), transforms.RandomHorizontalFlip(p=0.4), transforms.RandomVerticalFlip(p=0.3), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
leaf_train_dataset = LeafDataset(df=train_df, transform=leaf_transform)
leaf_train_loader = DataLoader(leaf_train_dataset, shuffle=True, batch_size=16)
images, labels = next(iter(leaf_train_loader))

dataset_size = len(leaf_train_dataset)
indices = list(range(dataset_size))
np.random.shuffle(indices)
split = int(np.floor(0.2 * dataset_size))
train_idx, val_idx = (indices[split:], indices[:split])
print(split)
print(len(train_idx), len(val_idx))
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(val_idx)