In :
%reload_ext autoreload
%matplotlib inline


# Convolutions and basics of CNNs¶

How convolutions work

In :
from exp.nb_07 import *
from PIL import Image
import numpy as np


## Data¶

Let's grab out MNIST dataset.

In :
x_train, y_train, x_valid, y_valid = get_data()

In :
x_train.shape

Out:
torch.Size([50000, 784])

Each row of the dataset is an image,

In :
x_train.shape

Out:
torch.Size()

But in order to properly view them we'll reshape them for now into rank 3 tensors:

[channels, height, width]

In :
x_train = x_train.view(-1, 28,28)

In :
five = x_train

In :
plt.imshow(five)

Out:
<matplotlib.image.AxesImage at 0x2990cc70d30> ## Convolutions¶

In [ ]:


In [ ]:


In [ ]:



## Kernel from Scratch¶

Let's try to create a top edge detection kernel from scratch and convolve it over the image.

In :
k = tensor([
[1.,1.,1.],
[-1.,-1.,-1.,],
[0.,0.,0.]
]); k.shape

Out:
torch.Size([3, 3])

Pytorch F.conv2d requires [batch size, channels, height, width] so we'll reshape using view

Then we can pass it into our nn.Conv2d with our kernel and since the stride=1 the result will be a rank 3 tensor

[filters, channels, height, width]'

In :
top = F.conv2d(five.view(1,1,28,28), k[None, None])

In :
top.shape

Out:
torch.Size([1, 1, 26, 26])
In :
plt.imshow(top.squeeze())

Out:
<matplotlib.image.AxesImage at 0x2990d41e6a0> And if we transpose our kernel it looks like it will detect edges on the leftside of an object.

In :
k.t()

Out:
tensor([[ 1., -1.,  0.],
[ 1., -1.,  0.],
[ 1., -1.,  0.]])
In :
left = F.conv2d(five.view(1,1,28,28), k.t()[None, None])

In :
plt.imshow(left.squeeze())

Out:
<matplotlib.image.AxesImage at 0x2990e0458e0> So how do this basic

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:



F.adaptive_avg_pool2d

Applies a 2D adaptive average pooling over an input signal composed of several input planes.
In [ ]:
avg_pool_1d = F.adaptive_avg_pool2d(feature_map, 1); avg_pool_1d.shape

In [ ]:
feature_map.squeeze().view(-1).mean()

In [ ]:
avg_pool_1d.squeeze()


Applies a 2D adaptive max pooling over an input signal composed of several input planes.



Max pooling will return a tensor of the max of some specified shape.

In [ ]:
max_pool = F.adaptive_max_pool2d(feature_map, 1); max_pool.shape

In [ ]:
feature_map.squeeze().view(-1).max()

In [ ]:
max_pool.squeeze()


## Pre-Trained Model¶

Let's look at the activations of a pretrained model.

In [ ]:
from fastai.vision import *

In [ ]:
model = models.resnet34(pretrained=True)


To pass an image we need to: normalize, turn into a mini-batch, and put onto GPU

In [ ]:


In [ ]:


In [ ]:


In [ ]:

`