Knowledge Base

Convolutional Neural Networks

Glossary

Augmentation*: A technique which is used to artificially expand a dataset by transforming existing images. The changes are only applied to training sets, while test and validation sets remain the same. Augmentation transforms the original image while still preserving its core features.

Сonvolution: A function that applies the same operations to all pixels to extract image elements that are important for classification.

Convolution layer: A layer where the convolution operation is applied to input images.

Convolution neural network: A class of neural networks that use convolutional layers. They perform most of the computation within the network.

Filter: A component of the convolutional layer, a set of weights that are applied to the image.

Padding: This setting adds zeros to the edges of the matrix (zero padding) so that the outermost pixels participate in the convolution at least as many times as the central pixels.

Pooling: Compacting a group of pixels to one pixel using some transformation: for example, calculating the maximum or arithmetic mean.

Striding, or Stride: This setting shifts the filter by more than one pixel and generates a smaller output image.

Practice

1# one-dimensional convolution
2
3def convolve(sequence, weights):
4 convolution = np.zeros(len(sequence) - len(weights) + 1)
5 for i in range(convolution.shape[0]):
6 convolution[i] = np.sum(weights * sequence[i:i + len(weights)])

1# two-dimensional convolutional layer in keras
2# filters - the number of filters which corresponds to the size of the output tensor.
3# kernel_size - the spatial size of the filter *K.*
4# strides - determines how far the filter shifts over the input matrix. (Set to 1 by default.)
5# padding - determines the width of the zero padding.
6# There are two types of padding: valid **and same*.
7#* The default type of padding is — valid, and is equal to zero.
8# Same sets the size of the padding automatically,
9# so that the width and height of the output tensor is equal to the width and height of the input tensor.
10# activation - activation function which is applied immediately after the convolution.
11
12keras.layers.Conv2D(
13 filters,
14 kernel_size,
15 strides,
16 padding,
17 activation
18)

1# Layer Flatten that makes the multidimensional tensor one-dimensional
2
3keras.layers.Flatten()

1# Pooling layer
2# pool_size — pooling size.
3# strides — determines how far the filter shifts over the input matrix. If *None* is specified, then the stride is equal to the pooling size.
4# padding — determines the width of the zero padding.
5
6keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', ...)
7keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', ...)

1# Data generator
2
3from tensorflow.keras.preprocessing.image import ImageDataGenerator
4datagen = ImageDataGenerator()
5
6# extract data from a folder
7datagen_flow = datagen.flow_from_directory(
8 # the folder with the dataset
9 '/dataset/',
10 # the target image size
11 target_size=(150, 150),
12 # the batch size
13 batch_size=16,
14 # class mode
15 class_mode='sparse',
16 # indicate that this is the data generator for the training set (if necessary)
17 subset='training',
18 # indicate that this is the data generator for the validation set (if necessary)
19 subset='validation',
20 # set a random number generator
21 seed=12345)
22
23# obtain the next batch
24features, target = next(datagen_flow)
25
26# training the model using data generators
27# using full dataset
28model.fit(datagen_flow, steps_per_epoch=len(datagen_flow))
29
30# with only training and validation sets
31model.fit(train_datagen_flow,
32 validation_data=val_datagen_flow,
33 steps_per_epoch=len(train_datagen_flow),
34 validation_steps=len(val_datagen_flow))

1# Adding augmentation in data generators
2# horizontal_flip - horizontal rotation
3# vertical_flip - vertical rotation
4
5datagen = ImageDataGenerator(validation_split=0.25,
6 rescale=1./255,
7 horizontal_flip=True,
8 vertical_flip=True)

1# Implementing ResNet architecture
2# input_shape — the size of the input image
3# classes=1000 — the number of neurons in the last fully connected layer where classification takes place
4# weights='imagenet' — the initialization of weights
5# ImageNet — the name of a large image database which
6# was used to train the network to sort pictures into 1000 classes
7# To initialize the weights at random, write weights=None.
8# include_top=True — indicates that there are two layers at the end of ResNet:
9# GlobalAveragePooling2D and Dense. If you set it to False, these layers will be missing.
10
11from tensorflow.keras.applications.resnet import ResNet50
12
13model = ResNet50(input_shape=None,
14 classes=1000,
15 include_top=True,
16 weights='imagenet')

1# Creating your own network based on ResNet50
2
3backbone = ResNet50(input_shape=(150, 150, 3),
4 weights='imagenet',
5 include_top=False)
6
7# freeze ResNet50 with the top removed (optional)
8backbone.trainable = False
9
10model = Sequential()
11model.add(backbone)
12model.add(GlobalAveragePooling2D())
13model.add(Dense(12, activation='softmax'))
Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,