Safe lab preview
Conv Nets TF
This is a sanitized, read-only preview. Nothing executes in this page.
Read-only
Notebook preview
Conv Nets TF
> **Integrated runtime note:** This preview uses a small deterministic, rights-safe offline fixture for reproducible learning. Full-scale results require the lesson's documented dataset or model in an approved external environment.
# Convolutional neural networks
We have seen before that neural networks are quite good at dealing with images, and even one-layer perceptron is able to recognize handwritten digits from MNIST dataset with reasonable accuracy. However, MNIST dataset is very special, and all digits are centered inside the image, which makes the task simpler.
In real life, we want to be able to recognize objects on the picture regardless of their exact location in the image. Computer vision is different from generic classification, because when we are trying to find a certain object in the picture, we are scanning the image looking for some specific **patterns** and their combinations. For example, when looking for a cat, we first may look for horizontal lines, which can form whiskers, and then certain combination of whiskers can tell us that it is actually a picture of a cat. Relative position and presence of certain patterns is important, and not their exact position on the image.
To extract patterns, we will use the notion of **convolutional filters**. But first, let us load all dependencies and functions that we have defined in the previous units. We will also import `tfcv` helper library that contain some useful functions that we do not want to define inside this notebook to keep the code short and clean.
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
# Self-contained course computer-vision helpers.
from pathlib import Path
import glob
from PIL import Image
def plot_convolution(data, kernel, title=""):
fig, axes = plt.subplots(2, len(data) + 1, figsize=(10, 4))
fig.suptitle(title, fontsize=16)
tensor_kernel = np.asarray(kernel, dtype=np.float32)[:, :, None, None]
for index, image in enumerate(data):
axes[0, index].imshow(image, cmap="gray")
tensor_image = np.asarray(image, dtype=np.float32)[None, :, :, None]
convolved = tf.nn.conv2d(tensor_image, tensor_kernel, strides=1, padding="SAME")
axes[1, index].imshow(convolved[0, :, :, 0], cmap="gray")
axes[0, index].axis("off")
axes[1, index].axis("off")
axes[0, -1].imshow(kernel, cmap="gray")
axes[0, -1].axis("off")
axes[1, -1].axis("off")
plt.tight_layout()
plt.show()
def plot_results(history):
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
axes[0].set_title("Accuracy")
axes[1].set_title("Loss")
for key in ("accuracy", "val_accuracy"):
if key in history.history:
axes[0].plot(history.history[key], label=key)
for key in ("loss", "val_loss"):
if key in history.history:
axes[1].plot(history.history[key], label=key)
axes[0].legend()
axes[1].legend()
plt.show()
def display_dataset(dataset, labels=None, n=10, classes=None):
count = min(n, len(dataset))
fig, axes = plt.subplots(1, count, figsize=(1.8 * count, 3))
axes = np.atleast_1d(axes)
for index in range(count):
axes[index].imshow(dataset[index])
axes[index].axis("off")
if classes is not None and labels is not None:
label = int(np.asarray(labels[index]).reshape(-1)[0])
axes[index].set_title(classes[label])
plt.show()
def check_image(filename):
try:
with Image.open(filename) as image:
image.verify()
return True
except (OSError, SyntaxError):
return False
def check_image_dir(pattern):
invalid = [filename for filename in glob.glob(pattern) if not check_image(filename)]
if invalid:
names = ", ".join(Path(filename).name for filename in invalid[:5])
raise ValueError(f"Invalid images detected ({len(invalid)}): {names}")
return 0In this example, we will focus on the MNIST dataset that we have seen before, and on image classification. We will start by loading the dataset using Keras built-in functions.
# Bundled, deterministic handwritten-digits fixture.
from sklearn.datasets import load_digits as _course_load_digits
def _course_digits_load_data():
course_digits = _course_load_digits()
images = np.kron(
course_digits.images.astype(np.float32),
np.ones((3, 3), dtype=np.float32),
)
images = np.pad(images, ((0, 0), (2, 2), (2, 2))) * (255.0 / 16.0)
labels = np.asarray(course_digits.target, dtype=np.int64)
split = int(0.8 * len(images))
return (images[:split], labels[:split]), (images[split:], labels[split:])
(x_train,y_train),(x_test,y_test) = _course_digits_load_data()
x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0## Convolutional filters
Convolutional filters are small windows that run over each pixel of the image and compute weighted average of the neighboring pixels.
They are defined by matrices of weight coefficients. Let's see the examples of applying two different convolutional filters over our MNIST handwritten digits:
plot_convolution(x_train[:5],[[-1.,0.,1.],[-1.,0.,1.],[-1.,0.,1.]],'Vertical edge filter')
plot_convolution(x_train[:5],[[-1.,-1.,-1.],[0.,0.,0.],[1.,1.,1.]],'Horizontal edge filter')First filter is called a **vertical edge filter**, and it is defined by the following matrix:
$$
\left(
\begin{matrix}
-1 & 0 & 1 \cr
-1 & 0 & 1 \cr
-1 & 0 & 1 \cr
\end{matrix}
\right)
$$
When this filter goes over relatively uniform pixel field, all values add up to 0. However, when it encounters a vertical edge in the image, high spike value is generated. That's why in the images above you can see vertical edges represented by high and low values, while horizontal edges are averaged out.
An opposite thing happens when we apply horizontal edge filter - horizontal lines are amplified, and vertical are averaged out.
In classical computer vision, multiple filters were applied to the image to generate features, which then were used by machine learning algorithm to build a classifier. Those filters are in fact similar to neural structures that are available in the vision system of some animals.
> **Figure description:** The original was excluded because its reuse rights are not documented.
However, in deep learning we construct networks that **learn** best convolutional filters to solve classification problem. To do that, we introduce **convolutional layers**.
## Covolutional layers
To make the weights of convolutional layer trainable, we need somehow to reduce the process of applying convolutional filter window to the image to the matrix operations, which can then be subject to backward propagation training. To do this, we use a clever matrix transformation, which we call **im2col**.
Suppose we have a small image $\mathbf{x}$, with the following pixels:
$$
\mathbf{x} = \left(
\begin{array}{ccccc}
a & b & c & d & e \\
f & g & h & i & j \\
k & l & m & n & o \\
p & q & r & s & t \\
u & v & w & x & y \\
\end{array}
\right)
$$
And we want to apply two conv filters, with the following weights:
$$
W^{(i)} = \left(\begin{array}{ccc}
w^{(i)}_{00} & w^{(i)}_{01} & w^{(i)}_{02} \\
w^{(i)}_{10} & w^{(i)}_{11} & w^{(i)}_{12} \\
w^{(i)}_{20} & w^{(i)}_{21} & w^{(i)}_{22} \\
\end{array}\right)
$$
When applying the convolution, the first pixel of the result would be obtained by element-wise multiplication of
$\left(\begin{array}{ccc}
a & b & c \\
f & g & h \\
k & l & m \\
\end{array}\right)$ and $W^{(i)}$, the second element - by multiplying by $\left(\begin{array}{ccc}
b & c & d \\
g & h & i \\
l & m & n \\
\end{array}\right)$ by $W^{(i)}$, and so on.
To formalize this process, let's extract all $3\times3$ fragments of the original image $x$ into the following matrix:
$$
\mathrm{im2col}(x) = \left[
\begin{array}{cccccc}
a & b & \ldots & g & \ldots & m \\
b & c & \ldots & h & \ldots & n \\
c & d & \ldots & i & \ldots & o \\
f & g & \ldots & l & \ldots & r \\
g & h & \ldots & m & \ldots & s \\
h & i & \ldots & n & \ldots & t \\
k & l & \ldots & q & \ldots & w \\
l & m & \ldots & r & \ldots & x \\
m & n & \ldots & s & \ldots & y \\
\end{array}
\right]
$$
Each column of this matrix corresponds to each $3\times3$ subregion of the original image. Now, to get the result of the convolution, we just need to multiply this matrix by the matrix or weights
$$
\mathbf{W} = \left[
\begin{array}{cccccccc}
w^{(0)}_{00} & w^{(0)}_{01} & w^{(0)}_{02} & w^{(0)}_{10} & w^{(0)}_{11} & \ldots & w^{(0)}_{21} & w^{(0)}_{22} \\
w^{(1)}_{00} & w^{(1)}_{01} & w^{(1)}_{02} & w^{(1)}_{10} & w^{(1)}_{11} & \ldots & w^{(1)}_{21} & w^{(1)}_{22} \\
\end{array}
\right]
$$
(each row of this matrix contains weights of $i$-th filter, flattened into one row)
So the application of a convolution filter to the original image can be replaced by matrix multiplication, which we already know how to handle using back prop:
$$
C(x) = W\times\mathbf{im2col}(x)
$$
Convolutional layers are defined using `Conv2d` class. We need to specify the following:
* `filters` - number of filters to use. We will use 9 different filters, which will give the network plenty of opportunities to explore which filters work best for our scenario.
* `kernel_size` is the size of the sliding window. Usually 3x3 or 5x5 filters are used.
Simplest CNN will contain one convolutional layer. Given the input size 28x28, after applying nine 5x5 filters we will end up with a tensor of 24x24x9. The spatial dimension is smaller, because there are only 24 positions where a sliding interval of length 5 can fit into 28 pixels).
After convolution, we flatten 24x24x9 tensor into one vector of size 5184, and then add linear layer, to produce 10 classes. We also use `relu` activation function in between layers.
model = keras.models.Sequential([
keras.layers.Conv2D(filters=9, kernel_size=(5,5), input_shape=(28,28,1),activation='relu'),
keras.layers.Flatten(),
keras.layers.Dense(10)
])
model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['acc'])
model.summary()You can see that this network contains around 50k trainable parameters, compared to around 80k in fully-connected multi-layered networks. This allows us to achieve good results even on smaller datasets, because convolutional networks generalize much better.
> **Note**: In most of the practical cases, we want to apply convolutional layers to color images. Thus, `Conv2D` layer expects the input to be of the shape $W\times H\times C$, where $W$ and $H$ are width and height of the image, and $C$ is the number of color channels. For grayscale images, we need the same shape with $C=1$.
We need to reshape our data before starting training:
x_train_c = np.expand_dims(x_train,3)
x_test_c = np.expand_dims(x_test,3)
hist = model.fit(x_train_c,y_train,validation_data=(x_test_c,y_test),epochs=2)plot_results(hist)As you can see, we are able to achieve higher accuracy, and much faster (in terms of number of epochs), compared to the fully-connected networks from previous unit. However, the training itself requires more resources, and may be slower on non-GPU computers.
## Visualizing Convolutional Layers
We can also visualize the weights of our trained convolutional layers, to try and make some more sense of what is going on:
fig,ax = plt.subplots(1,9)
l = model.layers[0].weights[0]
for i in range(9):
ax[i].imshow(l[...,0,i])
ax[i].axis('off')You can see that some of those filters look like they can recognize some oblique strokes, while others look pretty random.
> **Task**: Train the same network with 3x3 filters and visualize them. Do you see more familiar patterns?
## Multi-layered CNNs and pooling layers
First convolutional layers looks for primitive patterns, such as horizontal or vertical lines, but we can apply further convolutional layers on top of them to look for higher-level patterns, such as primitive shapes. Then more convolutional layers can combine those shapes into some parts of the picture, up to the final object that we are trying to classify.
When doing so, we may also apply one trick: reducing the spatial size of the image. Once we have detected there is a horizontal stoke within sliding 3x3 window, it is not so important at which exact pixel it occurred. Thus we can "scale down" the size of the image, which is done using one of the **pooling layers**:
* **Average Pooling** takes a sliding window (for example, 2x2 pixels) and computes an average of values within the window
* **Max Pooling** replaces the window with the maximum value. The idea behind max pooling is to detect a presence of a certain pattern within the sliding window.
Thus, in a typical CNN there would be several convolutional layers, with pooling layers in between them to decrease dimensions of the image. We would also increase the number of filters, because as patterns become more advanced - there are more possible interesting combinations that we need to be looking for.
> **Figure description:** An image showing several convolutional layers with pooling layers.
Because of decreasing spatial dimensions and increasing feature/filters dimensions, this architecture is also called **pyramid architecture**.
model = keras.models.Sequential([
keras.layers.Conv2D(filters=10, kernel_size=(5,5), input_shape=(28,28,1),activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(filters=20, kernel_size=(5,5), activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Flatten(),
keras.layers.Dense(10)
])
model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['acc'])
model.summary()Notice that the number of trainable parameters (~8.5K) is dramatically smaller than in previous cases. This happens because convolutional layers in general have few parameters, and dimensionality of the image before applying final dense layer is significantly reduced. Small number of parameters have positive impact on our models, because it helps to prevent overfitting even on smaller dataset sizes.
hist = model.fit(x_train_c,y_train,validation_data=(x_test_c,y_test),epochs=2)plot_results(hist)What you should probably observe is that we are able to achieve higher accuracy than with just one layer, and much faster in terms of number of epochs - just with 1 or 2 epochs. It means that sophisticated network architecture needs much fewer data to figure out what is going on, and to extract generic patterns from our images. However, training also takes longer, and requires a GPU.
## Playing with real images from the CIFAR-10 dataset
While our handwritten digit recognition problem may seem like a toy problem, we are now ready to do something more serious. Let's explore more advanced dataset of pictures of different objects, called [CIFAR-10](https://www.cs.toronto.edu/~kriz/cifar.html). It contains 60k 32x32 images, divided into 10 classes.
# Generated RGB patterns for deterministic, rights-safe execution.
def _course_rgb_load_data():
yy, xx = np.mgrid[:32, :32]
images, labels = [], []
for label in range(10):
base = np.zeros((32, 32, 3), dtype=np.float32)
base[..., label % 3] = ((xx * (label + 2) + yy * ((label % 4) + 1)) % 32) / 31.0
base[..., (label + 1) % 3] = ((yy + label * 3) % 32) / 31.0
for sample in range(24):
images.append(np.roll(base, shift=(sample % 5) - 2, axis=sample % 2))
labels.append(label)
images = (255.0 * np.asarray(images)).astype(np.float32)
labels = np.asarray(labels, dtype=np.int64).reshape(-1, 1)
split = 200
return (images[:split], labels[:split]), (images[split:], labels[split:])
(x_train,y_train),(x_test,y_test) = _course_rgb_load_data()
x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')display_dataset(x_train,y_train,classes=classes)A well-known architecture for CIFAR-10 is called [LeNet](https://en.wikipedia.org/wiki/LeNet), and has been proposed by *Yann LeCun*. It follows the same principles as we have outlined above, the main difference being 3 input color channels instead of 1.
model = keras.models.Sequential([
keras.layers.Conv2D(filters = 6, kernel_size = 5, strides = 1, activation = 'relu', input_shape = (32,32,3)),
keras.layers.MaxPooling2D(pool_size = 2, strides = 2),
keras.layers.Conv2D(filters = 16, kernel_size = 5, strides = 1, activation = 'relu'),
keras.layers.MaxPooling2D(pool_size = 2, strides = 2),
keras.layers.Flatten(),
keras.layers.Dense(120, activation = 'relu'),
keras.layers.Dense(84, activation = 'relu'),
keras.layers.Dense(10, activation = 'softmax')])
model.summary()Training this network properly will take significant amount of time, and should preferably be done on GPU-enabled compute.
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['acc'])
hist = model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=2)plot_results(hist)The accuracy that we have been able to achieve with few epochs of training does not seem too great. However, remember that bling guessing would only give us 10% accuracy, and that our problem is actually significantly more difficult than MNIST digit classification. Getting above 50% accuracy in such a short training time seems like a good accomplishment.
## Takeaways
In this unit, we have learned the main concept behind computer vision neural networks - convolutional networks. Real-life architectures that power image classification, object detection, and even image generation networks are all based on CNNs, just with more layers and some additional training tricks.
Outputs, execution counts, widgets, and active content were removed during import. Run notebooks only in an external environment you trust.
Record your practice
Optional self-reporting helps you remember what you practiced and never gates course completion.