conv2d keras tutorial with example : In this tutorial, we are going to study the Keras conv2d in detail with example.
The layer creates a convolution kernel that wind and helps to produce a tensor output.
The matrix is used for blurring, edge detection and convolution between images.
Keras.l.conv2D (filters, kernel_size, strides= (1, 1),
Padding=’valid, data_format=none, dilation_rate= (1, 1)’
Activation=none, use_bias=True, kernel_initalizer=’glorot_uniform’,
Bias_)
Bias_initalizer=’zeros’, kernel_regulaizer=none,
Bias_regulizer=none, activity_regulaizer=none,
Bias_constraint=none, bias_constraint=none)
model = Sequential ()
model. add (Conv2D (32, kernel_size = (5, 5), strides = (1, 1),
activation ='relu'))
model. add (Maxpooling2D (pool_size= (2, 2), strides= (2, 2)))
model. add (Conv2D (64, (5, 5), activation ='relu'))
model. add (MaxPooling2D (pool_size = (2, 2)))
model. add (Flatten ())
model. add(Dense (1000, activation ='relu'))
model. add (Dense (num_classes, activation ='softmax'))
model. compile (loss = keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.SGD (lr = 0.01),
metrics = ['accuracy'])
model.fit (x_train, y_train,
batch_size = batch_size,
epochs = epochs,
verbose = 1,
validation_data = (x_test, y_test),
callbacks = [history])
score = model. evaluate (x_test, y_test, verbose = 0)
print ('Test loss:’ score [0])
Print('Test accuracy:’ score [1])
Here adding conv2D use sequential.model.add () method we use many parameters.
The parameter tells several filters used in convolution operation.
Then the second parameter will specify the size of the convolution filter in pixels.
The third parameter will tell the filter along with x-axis and y-axis of the source image.
The fourth parameter specifies a name and applies after performing convolution.
Keras Conv2D and Convolution Layers:-
model. add (Conv2D (32, (3, 3), padding="same", activation="relu"))
model.add (Maxpooling2D (pool_size= (2, 2)))
model. add (Conv2D (64, (3, 3), padding="same", activation="relu"))
model. add (MaxPooling2D (pool_size= (2, 2)))
model. add (Conv2D (128, (3, 3), padding="same", activation="relu"))
model. add (Maxpooling2D (pool_size= (2, 2)))
model. add (Activation ("softmax"))
The maxpooling is used to reduce the spatial dimensions of the output volume and the final Conv2D layer learns 128 filters.
The second parameter to provide to the Keras Conv2D class is the kernel_size.
The values for kernel_size will include: (1, 1) , (3, 3) , (5, 5) , (7, 7) .
Suppose the input images are greater than 128×128 then use a kernel size > 3 to help (1) learn larger spatial filters and (2) to help reduce volume size.
The modules inside the network will learn local features at different scales and combine the outputs.
Use 1×1 and 3×3 filters as a form of dimensionality and keep the number of parameters in the network low.
Model. add (conv2D (32(7, 7), activation=”relu”))
Model. add (conv2D (32(3, 3), activation=”relu”))
model. add (Conv2D (128, (3, 3), strides= (1, 1), activation="relu"))
model. add (Conv2D (128, (3, 3), strides= (1, 1), activation="relu"))
model. add (Conv2D (128, (3, 3), strides= (2, 2), activation="relu"))
The strides of 1×1 and the final Conv2D layer take place of a maxpooling layer and will reduce the spatial dimensions of the output volume convolution.
The input volume is not zero-padded, spatial dimensions both are allowed to reduce by natural application of convolution.
model. add (Conv2D (32, (3, 3), padding="valid"))
If you want to preserve the spatial dimensions of the volume as output volume size matches the input volume size then you supply a value of same for the padding:
Example:-
model. add (Conv2D (32, (3, 3), padding="same"))
Set it to the same for the majority of the layers in the network and then reduces spatial dimensions.
They will include support for both “channels last” and “channels first” channel ordering.
And data format value in the Conv2D class can be the channels_last or channels_first:-
The dilation_rate parameter of the Conv2D class is a 2-tuple that controls the dilation rate for dilated convolution.
It is convolution applied to the input volume with defined
The activation parameter will allow the activation function for use after convolution to be specified.
It simply a convenience parameter that allows you to supply a string specifies the name of the activation function.
Keras Conv2D and Convolution Layers
It controls the initialization method used to initialize the values in the Conv2D class.
It controls how the bias vector is initialized before training starts.
The kernel_regularizer, bias_regularizer, and activity_regularizer will control the amount of regularization method applied to the Conv2D layer.
Example:-
from keras.regularizers import l2
model. add (Conv2D (32, (3, 3), activation="relu"),
kernel_regularizer=l2 (0.0005))