application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
from PIL import Image

from io import BytesIO

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

from torch.optim import lr_scheduler

from torch.utils.data import DataLoader, Dataset

from torchvision import datasets, models, transforms

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

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

import torch

import torch.nn as nn

import torch.optim as optim

train_on_gpu = torch.cuda.is_available()

labels = pd.read_csv('../input/train.csv')

num_classes = len(labels['Id'].unique())

data_dir = '../input'
train_dir = data_dir + '/train'
test_dir = data_dir + '/test'

pil2tensor = transforms.ToTensor()
tensor2pil = transforms.ToPILImage()
pil_image = Image.open(train_dir + '/0a750c2e8.jpg')
rgb_image = pil2tensor(pil_image)
def plot_image(tensor):
    pass

from io import BytesIO
import IPython.display
r_image = rgb_image[0]
g_image = rgb_image[1]
b_image = rgb_image[2]
def show_grayscale_image(tensor):
    f = BytesIO()
    a = np.uint8(tensor.mul(255).numpy())
    Image.fromarray(a).save(f, 'png')

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
train_data = transforms.Compose([transforms.Resize(256), transforms.RandomResizedCrop(224), transforms.RandomRotation(25), transforms.RandomHorizontalFlip(), transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0), transforms.RandomAffine(degrees=4, translate=None, scale=None, shear=None, resample=False, fillcolor=0), transforms.ToTensor(), transforms.Normalize(mean, std)])
test_data = transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean, std)])

def encode_labels(y):
    values = np.array(y)
    label_encoder = LabelEncoder()
    integer_encoded = label_encoder.fit_transform(values)
    onehot_encoder = OneHotEncoder(sparse=False)
    integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
    onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
    y = onehot_encoded
    return (y, label_encoder)
y, label_encoder = encode_labels(labels['Id'])

class WhaleTailDataset(Dataset):
    def __init__(self, image_folder, data_type='train', df=None, transform=None, y=None):
        self.image_folder = image_folder
        self.imgs_list = [img for img in os.listdir(image_folder)]
        self.data_type = data_type
        self.transform = transform
        self.y = y
        if self.data_type == 'train':
            self.df = df.values
    def __len__(self):
        return len(self.imgs_list)
    def __getitem__(self, idx):
        if self.data_type == 'train':
            img_name = os.path.join(self.image_folder, self.df[idx][0])
            label = self.y[idx]
        elif self.data_type == 'test':
            img_name = os.path.join(self.image_folder, self.imgs_list[idx])
            label = np.zeros((num_classes,))
        img = Image.open(img_name).convert('RGB')
        img = self.transform(img)
        if self.data_type == 'train':
            return (img, label)
        elif self.data_type == 'test':
            return (img, label, self.imgs_list[idx])

image_datasets = dict()
image_datasets['train'] = WhaleTailDataset(image_folder=train_dir, data_type='train', df=labels, transform=train_data, y=y)
image_datasets['test'] = WhaleTailDataset(image_folder=test_dir, data_type='test', transform=test_data)

train_size = 512
test_size = 32
num_workers = 0
dataloaders = dict()
dataloaders['train'] = torch.utils.data.DataLoader(image_datasets['train'], batch_size=train_size, num_workers=num_workers)
dataloaders['test'] = torch.utils.data.DataLoader(image_datasets['test'], batch_size=test_size, num_workers=num_workers)

model = models.resnet152(pretrained=True)
for param in model.parameters():
    param.requires_grad = False

classifier = nn.Sequential(nn.Linear(2048, 1024), nn.ReLU(), nn.Dropout(0.5), nn.Linear(1024, 512), nn.ReLU(), nn.Dropout(0.5), nn.Linear(512, 5005), nn.LogSoftmax(dim=1))
model.fc = classifier

from torch.optim import lr_scheduler
num_epochs = 6
learning_rate = 0.001
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.fc.parameters(), lr=learning_rate)
scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)

import matplotlib.pyplot as plt
model = model.cuda()
train_loss = []
for epoch in range(1, num_epochs + 1):
    for batch_i, (data, target) in tqdm(enumerate(dataloaders['train']), total=len(dataloaders['train'])):
        data, target = (data.cuda(), target.cuda())
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target.float())
        train_loss.append(loss.item())
        loss.backward()
        optimizer.step()
    scheduler.step()

sub = pd.read_csv('../input/sample_submission.csv')
model.eval()
for data, target, name in tqdm(dataloaders['test']):
    data = data.cuda()
    output = model(data)
    output = output.cpu().detach().numpy()
    for i, (e, n) in enumerate(list(zip(output, name))):
        sub.loc[sub['Image'] == n, 'Id'] = ' '.join(label_encoder.inverse_transform(e.argsort()[-5:][::-1]))
sub.to_csv('submission.csv', index=False)