Jovian
⭐️
Sign In
In [1]:
import numpy as np
import torch
from torchvision.datasets import CIFAR10

import matplotlib.pyplot as plt
%matplotlib inline

from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data.dataloader import DataLoader

import torch.nn as nn

import torch.nn.functional as F


In [2]:
dataset = CIFAR10(root = '/data', download = True)
Files already downloaded and verified
In [3]:
len(dataset)
Out[3]:
50000
In [4]:
test_data = CIFAR10(root = '/data', train = False)
len(test_data)
Out[4]:
10000
In [5]:
print(dataset.class_to_idx)
{'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}

Converting images to tensors

In [6]:
import torchvision.transforms as transform
In [7]:
#Dataset after converting into tensors
dataset = CIFAR10(root = '/data', train = True, transform=transform.Compose([transform.ToTensor(), 
                                                                             transform.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
In [8]:
img_tensor, label = dataset[0]
print(img_tensor.shape, label)
torch.Size([3, 32, 32]) 6
In [9]:
print(img_tensor[:, 10:20, 10:20])
print(torch.max(img_tensor), torch.min(img_tensor))
tensor([[[-0.3725, -0.2392, -0.2941, -0.2314, 0.0745, 0.0902, 0.1608, 0.0510, 0.0824, 0.0510], [-0.1294, 0.1451, 0.0667, 0.2784, 0.3255, 0.1922, 0.2627, 0.1608, 0.3882, 0.2627], [ 0.0824, 0.6706, 0.3961, 0.4980, 0.6549, 0.4824, 0.6078, 0.6235, 0.6706, 0.4980], [ 0.2941, 0.7961, 0.4353, 0.4980, 0.8745, 0.7176, 0.7882, 0.7647, 0.6784, 0.6941], [ 0.5059, 0.4510, 0.1373, 0.5922, 0.7490, 0.8980, 0.9137, 0.8667, 0.8902, 0.7804], [ 0.7255, 0.0824, 0.2706, 0.6157, 0.5373, 0.9373, 1.0000, 1.0000, 0.9216, 0.8510], [ 0.6157, 0.1373, 0.5373, 0.6000, 0.7255, 0.9059, 0.9216, 0.8745, 0.8353, 0.8118], [ 0.3725, 0.3725, 0.7255, 0.7725, 0.8039, 0.8275, 0.7569, 0.5765, 0.4431, 0.4196], [ 0.3333, 0.4510, 0.8902, 0.8039, 0.4667, 0.4118, 0.3020, 0.1451, 0.1686, 0.2314], [ 0.3569, 0.3255, 0.7255, 0.5216, -0.0353, 0.0588, -0.0039, 0.1843, 0.2941, 0.0353]], [[-0.6863, -0.5843, -0.6471, -0.5608, -0.2863, -0.3412, -0.3176, -0.4275, -0.3569, -0.3333], [-0.5294, -0.3255, -0.3882, -0.0902, -0.0980, -0.2157, -0.0902, -0.2392, -0.0510, -0.1373], [-0.3804, 0.1765, -0.0353, 0.1765, 0.3255, 0.1608, 0.2863, 0.2706, 0.2863, 0.1216], [-0.1686, 0.3804, 0.0745, 0.2392, 0.6941, 0.5059, 0.4745, 0.4745, 0.3882, 0.3647], [ 0.1922, 0.1608, -0.2078, 0.2706, 0.5686, 0.7804, 0.7804, 0.7255, 0.7176, 0.5451], [ 0.4980, -0.2627, -0.1765, 0.2235, 0.3020, 0.8353, 0.9843, 0.9765, 0.8353, 0.7020], [ 0.4275, -0.2941, 0.0431, 0.2314, 0.4745, 0.7725, 0.8588, 0.8275, 0.7569, 0.7020], [ 0.1529, -0.0667, 0.3804, 0.5451, 0.6235, 0.7098, 0.6627, 0.4588, 0.2784, 0.2392], [ 0.0510, 0.0353, 0.6784, 0.7098, 0.3255, 0.2549, 0.1451, -0.0667, -0.0902, -0.0275], [ 0.0667, -0.0510, 0.4275, 0.3255, -0.3020, -0.2314, -0.2863, -0.1059, -0.0039, -0.2235]], [[-0.8980, -0.8667, -0.8667, -0.7647, -0.5529, -0.6157, -0.5765, -0.7098, -0.6392, -0.5529], [-0.8902, -0.8275, -0.8196, -0.3961, -0.4588, -0.5922, -0.4275, -0.5529, -0.3569, -0.4431], [-0.8510, -0.5373, -0.6784, -0.2314, -0.0431, -0.2235, -0.1373, -0.0980, -0.0745, -0.2863], [-0.6078, -0.2784, -0.5529, -0.1922, 0.3804, 0.1686, 0.0039, -0.0588, -0.1216, -0.1216], [-0.1137, -0.1608, -0.6000, -0.0510, 0.3333, 0.5373, 0.4588, 0.2941, 0.2784, 0.1294], [ 0.3255, -0.4431, -0.5059, -0.1216, 0.0588, 0.6627, 0.8196, 0.7176, 0.5451, 0.4118], [ 0.2314, -0.5608, -0.3412, -0.1373, 0.2235, 0.6314, 0.7725, 0.6863, 0.5765, 0.5059], [-0.0980, -0.4275, 0.0118, 0.2863, 0.4039, 0.5373, 0.5294, 0.3020, 0.0824, 0.0039], [-0.1843, -0.3098, 0.3882, 0.5294, 0.1137, 0.0275, -0.0980, -0.3333, -0.3804, -0.3490], [-0.2157, -0.4275, 0.0824, 0.0588, -0.5686, -0.5294, -0.6235, -0.5059, -0.4196, -0.6078]]]) tensor(1.) tensor(-1.)

There are three channels for every image - Red, Green and Blue

Training and validation sets

In [10]:
def split_indices(n, val_pct):
    n_val = int(n * val_pct)
    idxs = np.random.permutation(n)
    return idxs[n_val:], idxs[:n_val]
In [11]:
train_idxs, validation_idxs = split_indices(len(dataset), 0.2)
In [12]:
print(len(train_idxs), len(validation_idxs))
40000 10000
In [13]:
batch_size = 100

train_sampler = SubsetRandomSampler(train_idxs)
train_loader = DataLoader(dataset,
                         batch_size,
                         sampler = train_sampler)

val_sampler = SubsetRandomSampler(validation_idxs)
val_loader = DataLoader(dataset, 
                       batch_size,
                       sampler = val_sampler)
In [21]:
classes = dataset.classes

Visualize batch of training data

In [14]:
def imshow(img):
    #Unnormalize
    img = img/2+0.5
    
    #Convert from tensor image
    plt.imshow(np.transpose(img, (1, 2, 0)))
In [15]:
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy()
In [34]:
fig = plt.figure(figsize = (25, 4))

#Display 20 images
for idx in np.arange(20):
    ax = fig.add_subplot(2, 20/2, idx+1, xticks = [], yticks = [])
    imshow(images[idx])
    ax.set_title(classes[labels[idx]])
Notebook Image

View image in more detail

In [37]:
rgb_image = np.squeeze(images[19])
channels = ['red', 'green', 'blue']
In [43]:
fig = plt.figure(figsize = (36, 36))
for idx in np.arange(rgb_image.shape[0]):
    ax = fig.add_subplot(1, 3, idx + 1)
    img = rgb_image[idx]
    ax.imshow(img, cmap = 'gray')
    ax.set_title(channels[idx])
    width, height = img.shape
    thresh = img.max()/2.5
    for x in range(width):
        for y in range(height):
            val = round(img[x][y], 2) if img[x][y] != 0 else 0
            ax.annotate(str(val), xy = (y, x),
                       horizontalalignment = 'center',
                       verticalalignment = 'center',
                       size = 8,
                       color = 'white' if img[x][y] < thresh else 'black')