text_plain_output_1.png
from torchvision import models, transforms, datasets

import matplotlib.pyplot as plt

import numpy as np

import os

import torch

import torchvision

torch.__version__

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

data_dir = '../input/dogscats/dogscats/dogscats/'

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
vgg_format = transforms.Compose([transforms.CenterCrop(224), transforms.ToTensor(), normalize])

dsets = {x: datasets.ImageFolder(os.path.join(data_dir, x), vgg_format) for x in ['train', 'valid']}

dset_classes = dsets['valid'].classes
dset_classes

load_train = torch.utils.data.DataLoader(dsets['train'], batch_size=64, shuffle=True, num_workers=6)

load_test = torch.utils.data.DataLoader(dsets['valid'], batch_size=5, shuffle=True, num_workers=6)

inputs_try.shape

def imshow(inp, title=None):
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = np.clip(std * inp + mean, 0, 1)
    plt.pause(0.001)

out = torchvision.utils.make_grid(inputs_try)

inputs, classes = next(iter(load_train))
n_images = 8
out = torchvision.utils.make_grid(inputs[0:n_images])

inputs, classes = next(iter(load_test))
n_images = 8
out = torchvision.utils.make_grid(inputs[0:n_images])

model_vgg = models.vgg16(pretrained=True)

inputs_try, lables_try = (inputs_try.to(device), labels_try.to(device))
model_vgg = model_vgg.to(device)

out = torchvision.utils.make_grid(inputs_try.data.cpu())
imshow(out, title=[dset_classes[x] for x in labels_try.data.cpu()])