application_vnd.jupyter.stderr_output_1.png
from PIL import Image

import cv2 as cv

import numpy as np # linear algebra

import os

import os

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

import torch

import torch.utils.data.sampler as sampler

import torchvision.transforms as transforms

train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
target_dict = {0: 'No DR(Diabetic Retinopathy)', 1: 'Mild', 2: 'Moderate', 3: 'Severe', 4: 'Proliferative DR(Diabetic Retinopathy)'}

class MyDataset(torch.utils.data.Dataset):
    def __init__(self, dir_img_path=None, dir_csv_path=None, Usage=None, transform=None):
        super().__init__()
        '\n        Usage:\n        '
        self.DIR_PATH = '../input'
        self.df = pd.read_csv(dir_csv_path)
        self.dir_img_path = dir_img_path
        self.images = self.loadImage(self.dir_img_path)
        self.Usage = Usage
        self.transform = transform
    def loadImage(self, path):
        return os.listdir(path)
    def __getitem__(self, pos):
        obj = self.df.loc[pos]
        img_id = obj['id_code']
        if self.Usage == 'Training':
            label = obj['diagnosis']
        img_id = '{}.png'.format(img_id)
        img = cv.imread(os.path.join(self.dir_img_path, img_id))
        img = Image.fromarray(img)
        if self.transform:
            img = self.transform(img)
        if self.Usage == 'Training':
            return (img, label)
        return (img, obj['id_code'])
    def change_type(self, img, label):
        return (img.astype(np.float32), label.astype(np.long))
    def read(self, image):
        return cv.imread(image)
    def reshape(self, image):
        return cv.resize(image, (244, 244))
    def __len__(self):
        return len(self.df)
transformation = transforms.Compose([transforms.Resize((224, 224)), transforms.ColorJitter(0.1), transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip(), transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
myDataset = MyDataset('../input/train_images', '../input/train.csv', transform=transformation, Usage='Training')

test_size = 0.2
samples = len(myDataset)
indices = list(range(samples))
np.random.shuffle(indices)
train_len = int(np.floor(samples * test_size))
train_idx, valid_idx = (indices[train_len:], indices[:train_len])
train_sampler = sampler.SubsetRandomSampler(train_idx)
valid_sampler = sampler.SubsetRandomSampler(valid_idx)
print(len(train_sampler), len(valid_sampler))
train_loader = torch.utils.data.DataLoader(myDataset, sampler=train_sampler, batch_size=32, shuffle=False)
test_loader = torch.utils.data.DataLoader(myDataset, sampler=valid_sampler, batch_size=32, shuffle=False)
use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')