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 convolution23def 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 keras2# 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.1112keras.layers.Conv2D(13 filters,14 kernel_size,15 strides,16 padding,17 activation18)
1# Layer Flatten that makes the multidimensional tensor one-dimensional23keras.layers.Flatten()
1# Pooling layer2# 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.56keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', ...)7keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', ...)
1# Data generator23from tensorflow.keras.preprocessing.image import ImageDataGenerator4datagen = ImageDataGenerator()56# extract data from a folder7datagen_flow = datagen.flow_from_directory(8 # the folder with the dataset9 '/dataset/',10 # the target image size11 target_size=(150, 150),12 # the batch size13 batch_size=16,14 # class mode15 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 generator21 seed=12345)2223# obtain the next batch24features, target = next(datagen_flow)2526# training the model using data generators27# using full dataset28model.fit(datagen_flow, steps_per_epoch=len(datagen_flow))2930# with only training and validation sets31model.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 generators2# horizontal_flip - horizontal rotation3# vertical_flip - vertical rotation45datagen = ImageDataGenerator(validation_split=0.25,6 rescale=1./255,7 horizontal_flip=True,8 vertical_flip=True)
1# Implementing ResNet architecture2# input_shape — the size of the input image3# classes=1000 — the number of neurons in the last fully connected layer where classification takes place4# weights='imagenet' — the initialization of weights5# ImageNet — the name of a large image database which6# was used to train the network to sort pictures into 1000 classes7# 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.1011from tensorflow.keras.applications.resnet import ResNet501213model = ResNet50(input_shape=None,14 classes=1000,15 include_top=True,16 weights='imagenet')
1# Creating your own network based on ResNet5023backbone = ResNet50(input_shape=(150, 150, 3),4 weights='imagenet',5 include_top=False)67# freeze ResNet50 with the top removed (optional)8backbone.trainable = False910model = Sequential()11model.add(backbone)12model.add(GlobalAveragePooling2D())13model.add(Dense(12, activation='softmax'))