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

import numpy as np # linear algebra

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

import torch

import torch.nn as nn

import torch.nn.functional as F

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

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 8, 3, padding=1)
        self.conv2 = nn.Conv2d(8, 16, 3, padding=1)
        self.conv3 = nn.Conv2d(16, 32, 3, padding=1)
        self.conv4 = nn.Conv2d(32, 64, 3, padding=1)
        self.conv5 = nn.Conv2d(64, 128, 3, padding=1)
        self.conv6 = nn.Conv2d(128, 256, 2, padding=1)
        self.conv7 = nn.Conv2d(256, 512, 2, padding=1)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(12 * 12 * 512, 2048)
        self.fc2 = nn.Linear(2048, 4)
        self.dropout = nn.Dropout(0.2)
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = self.pool2(F.relu(self.conv2(x)))
        x = F.relu(self.conv3(x))
        x = self.pool2(F.relu(self.conv4(x)))
        x = self.pool2(F.relu(self.conv5(x)))
        x = self.pool2(F.relu(self.conv6(x)))
        x = self.pool2(F.relu(self.conv7(x)))
        x = x.view(-1, 12 * 12 * 512)
        x = self.dropout(x)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x
model = Net()
print(model)
if gpu_status:
    model.cuda()