text_plain_output_1.png
from PIL import Image

from PIL import Image

from sklearn.model_selection import train_test_split

from torch.utils.data.sampler import SubsetRandomSampler

from torchvision import datasets, transforms

import numpy as np

import os

import pandas as pd

import random

import torch

transform_ship = transforms.Compose([transforms.ToTensor()])

SEED = 200
base_dir = '../input/'
def seed_everything(seed=SEED):
    random.seed(seed)
    os.environ['PYHTONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
seed_everything(SEED)

train_data = pd.read_csv(base_dir + '/train/train.csv')

map_img_class_dict = {k: v for k, v in zip(train_data.image, train_data.category)}

test_data = pd.read_csv(base_dir + '/test_ApKoW4T.csv')

from PIL import Image
class ShipDataLoader(torch.utils.data.DataLoader):
    def __init__(self, CSVfolder, process='train', transform=transforms.Compose([transforms.Resize(size=(224, 224)), transforms.ToTensor()]), imgFolder='../input/train/images/', labelsDict={}, y_labels=list(train_data.category)):
        self.process = process
        self.imgFolder = imgFolder
        self.CSVfolder = CSVfolder
        self.y = y_labels
        self.FileList = pd.read_csv(self.CSVfolder)['image'].tolist()
        self.transform = transform
        self.labelsDict = labelsDict
        if self.process == 'train':
            self.labels = [labelsDict[i] for i in self.FileList]
        else:
            self.labels = [0 for i in range(len(self.FileList))]
    def __len__(self):
        return len(self.FileList)
    def __getitem__(self, idx):
        file_name = self.FileList[idx]
        image_data = self.pil_loader(self.imgFolder + '/' + file_name)
        if self.transform:
            image_data = self.transform(image_data)
        if self.process == 'train':
            label = self.y[idx]
        else:
            label = file_name
        return (image_data, label)
    def pil_loader(self, path):
        with open(path, 'rb') as f:
            img = Image.open(f)
            return img.convert('RGB')

tr, val = train_test_split(train_data.category, stratify=train_data.category, test_size=0.15, random_state=10)
training_batchsize = 16
num_workers = 8
train_sampler = SubsetRandomSampler(list(tr.index))
valid_sampler = SubsetRandomSampler(list(val.index))

len(list(tr.index))