conv2d keras tutorial with example

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.

conv2d keras Arguments:-

  1. Filters: It is the dimensionality of the output space.
  2. kernel_size: that will specify the height and width of the 2D convolution window.
  3. Strides: specifying the strides of the convolution along with the height and width. Padding: one of "valid" or "same" 
  4. data_format: It is the ordering of the dimensions in the inputs. 
  5. dilation_rate: this specifies the dilation rate to use for dilated convolution.
  6. depth_multiplier: The number of depth wise convolution output channels for each input channel.
  7. Activation: Activation function to use if you do not specify anything.
  8.  use_bias: the layer uses a bias vector.
  9. depthwise_initializer: Initialize for the depth-wise kernel matrix
  10. pointwise_initializer: Initialize for the point wise kernel matrix
  11. bias_initializer: Initialize for the bias vector
  12. depthwise_regularizer: the function applied to the depth-wise kernel matrix.
  13. pointwise_regularizer: Regularize function applied to the point-wise kernel matrix
  14. bias_regularizer: Regularize function applied to the bias vector.
  15. activity_regularizer: Regularize function applied to the output of the layer.
  16. depthwise_constraint: Constraint function applied to the depth-wise kernel matrix
  17. pointwise_constraint: Constraint function applied to the point-wise kernel matrix
  18. bias_constraint: Constraint function applied to the bias vector.

The constructor arguments as follows:-

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)

Codes for working parameters:-

           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])

conv2d keras Explanation :-

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.

kernel_size:-

conv2d keras tutorial

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.
keras conv2d example
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.
keras conv2d example

Model. add (conv2D (32(7, 7), activation=”relu”))
Model. add (conv2D (32(3, 3), activation=”relu”))

  1. The convolution filter is applied to the current location of the input volume.
  2. Then filter takes a 1-pixel step to the right and again applied to the input volume
  3. This process is performed until we reach the far-right border of the volume in which we move our filter one pixel down and then start again from the far left

keras conv2d Example :-

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.

Padding:-

keras conv2d example

The input volume is not zero-padded, spatial dimensions both are allowed to reduce by natural application of convolution.

keras conv2d Example :-


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.

data_format:-

conv2d keras example

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:-

  1. The Theano backend uses channels first ordering.
  2. The TensorFlow backend to Keras uses channels last ordering.

dilation_rate:-

keras conv2d tutorial

 

 

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

  1. You are working with higher resolution images but fine-grained details are still important.
  1. constructing a network with fewer parameters

Activation:-

conv2d keras tutorial
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

Kernel_initializer and bias_initializer:-

keras conv2d example

 

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.

  1. If we have to leave the bias_initialization alone by default filled with zeros.
  2. It works when your network has a large number of parameters

kernel_regularizer, bias_regularizer, and activity_regularizer

conv2d keras tutorial
The kernel_regularizer, bias_regularizer, and activity_regularizer will control the amount of regularization method applied to the Conv2D layer.

  1. It reduces the effect of over fitting
  2. And increase the ability of your model to generalize

Example:-
from keras.regularizers import l2
model. add (Conv2D (32, (3, 3), activation="relu"),
kernel_regularizer=l2 (0.0005))

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India