text_plain_output_1.png
from PIL import Image

from PIL import Image

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')

training_batchsize = 5

full_data = ShipDataLoader(base_dir + '/train/train.csv', process='train', imgFolder=base_dir + '/train/images', labelsDict=map_img_class_dict)

trainfull_loader = torch.utils.data.DataLoader(full_data, batch_size=training_batchsize, shuffle=True)

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)
from torchvision import models
import torch.optim as optim
num_classes = 5