text_plain_output_1.png
from PIL import Image

from io import BytesIO

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

from torchvision import datasets, models, transforms

import matplotlib.pyplot as plt

import numpy as np # linear algebra

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

import torch

train_on_gpu = torch.cuda.is_available()

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

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

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)

dataiter = iter(dataloaders['train'])
images, labels = dataiter.next()
print('Batch shape: ', images.size())