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

from torchvision import models

import numpy as np

import os

import pandas as pd

import random

import torch

import torch.nn as nn

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')
from torchvision import models
import torch.optim as optim
num_classes = 5

Training_transforms = transforms.Compose([transforms.Resize((224, 224)), transforms.RandomHorizontalFlip(), transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1), transforms.RandomAffine(degrees=15, translate=(0.3, 0.3), scale=(0.5, 1.5), shear=None, resample=False, fillcolor=0), transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])

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

traindataset = ShipDataLoader('../input/train/train.csv', 'train', Training_transforms, '../input/train/images', map_img_class_dict)
train_loader = torch.utils.data.DataLoader(traindataset, batch_size=training_batchsize, sampler=train_sampler, num_workers=num_workers)

dataiter = iter(train_loader)
images, labels = dataiter.next()

model_ft = models.resnet50(pretrained=True)

model_ft.fc.in_features

from collections import OrderedDict
fc = nn.Sequential(nn.Linear(model_ft.fc.in_features, 720), nn.ReLU(), nn.Dropout(0.5), nn.Linear(720, 256), nn.ReLU(), nn.Dropout(0.4), nn.Linear(256, 64), nn.ReLU(), nn.Dropout(0.3), nn.Linear(64, 5), nn.Softmax(dim=1))

model_ft.fc = fc

out = model_ft(images)

out