Keras.Conv2D Class

Python Methods and Functions

Kernel. In image processing, the kernel is a convolutional matrix or masks that can be used to blur, sharpen, emboss, detect edges, and more by performing convolution between the kernel and the image.

Keras Conv2D class constructor has the following arguments:

 keras.layers.Conv2D (filters, kernel_size, strides = (1, 1), padding = 'valid', data_format = None, dilation_rate = (1, 1), activation = None, use_bias = True, kernel_initializer = 'glorot_uniform', bias_initializer =' zeros', kernel_regularizer = None, bias_regularizer = None, activity_regularizer = None, kernel_constraint = None None) 

Now let's look at each of these parameters separately:
filters

  • Required parameter Conv2D — is the number of filters on which the convolutional layers are extracted.
  • This is an integer value that also determines the number of output filters in the convolution.
  model.add (Conv2D ( 32, (3, 3), padding = "same", activation = "relu")) model.add (MaxPooling2D (pool_size = (2, 2)))  
  • Here we learn a total of 32 filters, and then use Max Pooling to reduce the spatial dimensions of the output volume.
  • Regarding choosing an appropriate value for no. For filters, it is always recommended to use powers of 2 as values.
  • kernel_size

    • This parameter determines the size of the kernel. Common sizes include 1 × 1, 3 × 3, 5 × 5, and 7 × 7, which can be passed as (1, 1), (3, 3), (5, 5), or (7, 7) tuples .
    • This is an integer or a tuple / list of 2 integers defining the height and width of the 2D convolution window.
    • This parameter must be an odd integer.
      model.add (Conv2D (32, (7, 7), activation = "relu"))  

    success

    • This parameter is an integer or a tuple / list of 2 integers defining the "step" of the convolution along with the height and width of the input volume.
    • Its default value is always ( 1, 1), which means that this Conv2D filter is applied to the current location of the input volume, and this filter steps 1 pixel to the right, and again the filter is applied to the input. Volume and this is done until we reach the far right border of the volume in which we move our filter.
      model.add (Conv2D (128, (3, 3), strides = (1, 1), activation = "relu")) model.add (Conv2D (128, (3, 3), strides = (2, 2), activation = "relu"))  

    padding

    • Keras Conv2D class padding parameter can take one of two values: & # 39; valid & # 39; or & # 39; same & # 39 ;.
    • Setting the value to "valid" means that the input volume is not padded with zeros and the spatial dimensions can be reduced by naturally applying convolution.
      model.add (Conv2D (32, (3, 3), padding = "valid"))  

    Instead, you can preserve the spatial dimensions of the volume so that the size the output volume matched the size of the input volume, set to "same".

      model.add (Conv2D (32, (3, 3), padding = "same"))  

    data format

    • This parameter of the Conv2D class can be set to "channel_last" or "channel_first".
    • TensorFlow backend Keras uses the last channel order, while the Theano backend uses the first channel order.
    • Usually we will not touch this value like Keras, since in most cases we will use the TensorFlow backend for Keras.
    • The default is imag e_data_format, which is located in your Keras config file at ~ / .keras / Keras.json.

    dilation_rate

    • Parameter The dilation_rate of the Conv2D class is a tuple of two integers that defines the dilation rate for the extended convolution.
    • Dilated Convolution — this is a basic convolution applied to the input volume with specific gaps.
    • You can use this option when you are working with higher resolution images and fine details are important to you, or when you are building a mesh with fewer parameters.

    activation

    • Activation parameter for the Conv2D class — it's just an auxiliary parameter that lets you specify a string that specifies the name of the activation function you want to apply after the convolution is done.
      model.add (Conv2D (32, (3, 3 ), activation = "relu"))  

    OR

      model.add (Conv2D (32, (3, 3))) model.add ( Activation ("relu"))  
    • If you do not specify anything, no activation will be applied and will not affect the performance of your convolutional neural network.

    use_bias

    • This parameter of the Conv2D class is used to determine whether the bias vector is added to the convolutional layer.
    • By default, its value is set to True.

    kernel_initializer

    • This parameter controls the initialization method used for initialization of all values ​​in the Conv2D class before actually training the model.
    • This is the kernel weight matrix initializer.

    < strong> bias_initializer

    • Whereas bias_initializer controls how the bias vector is actually initialized before training starts.
    • This is the initializer for the bias vector.

    kernel_regularizer, bias_regularizer and activity_regularizer

    • kernel_regularizer — it is a Regularizer function that is applied to the kernel weights matrix.
    • bias_regularizer — this is a regularizer function that is applied to the offset vector.
    • activity_regularizer — it is a Regularizer function that is applied to the output of a layer (i.e. activation).
    • Regularization — it is a technique used to reduce error by properly fitting the function in a given training set and preventing overfitting.
    • It controls the type and amount of regularization method applied to the Conv2D layer.
    • The use of regularization is mandatory when working with large datasets and deep neural networks.
    • Using regularization helps us reduce the impact of overfitting and also improve our model's ability to generalize.
    • There are two types of regularization: L1 and L2 regularization , both are used to reduce the overfitting of our model.
      from keras.regularizers import l2 ... model.add (Conv2D (128, (3, 3), activation = "relu "), kernel_regularizer = l2 (0.0002))  
    • The regularization value you are applying is a hyper parameter that you will need to tweak for your own dataset, and its value usually fluctuates from 0.0001 to 0.001.
    •  It is always recommended to leave the bias_regularizer alone, as it has less effect on reducing overfitting.
    • It is also recommended to leave the default activity_regularizer.

    core-constraint and offset-constraint

    • kernel_constraint — it is a Constraint function that is applied to the kernel matrix.
    • bias_constraint — it is a constraint function that is applied to the offset vector.
    • Constraint — this is the condition of the optimization problem that the solution must satisfy. 
      There are several types of restrictions — primarily equality constraints, inequality constraints, and integer constraints.
    • These options allow you to place constraints on Conv2D layers.
    • These options are usually left alone unless you have a specific reasons to apply constraint to Con2D layers.

    Here's a simple code example that demonstrates how the various parameters of the Conv2D class work:

    # build model

    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 training

    model. compile (loss = keras.losses.categorical_crossentropy,

    optimizer = keras.optimizers.SGD (lr = 0.01 ),

    metrics = [ ' accuracy' ])

     
    # model fitting
    model.fit (x_train, y_train,

    batch_size = batch_size,

    epochs = epochs,

     

    verbose = 1 ,

    validation_data = (x_test, y_test),

    callbacks = [history])

     
    # grade and print results

    score = model.evaluate (x_test, y_test, verbose = 0 )

    print ( 'Test loss:' , score [ 0 ])

    print ( ' Test accuracy: ' , score [ 1 ])

    Understanding the Code:

    • When adding Conv2D layers using the Sequential.model.add () method, we can use a lot of parameters that we read about earlier on our blog.
    • The first parameter tells us about the number of filters used in our convolution operation.
    • Then the second parameter specifies the size of the convolutional filter in pixels. The filter size can be determined by the CNN architecture you are using — for example, VGGNet only uses (3, 3) filters. If not, use a 5 × 5 or 7 × 7 filter to examine larger objects, then quickly scale down to 3 × 3.
    • The third parameter specifies how the convolutional filter should move along the x-axis and y-axis original image. In most cases, you can leave the default step parameter (1, 1). However, you can increase it to (2, 2) to reduce the size of the output volume.
    • The fourth parameter — this is an activation parameter that specifies the name of the activation function that you want to apply after the convolution is done.



    Summary:

    • Most of the time you will be using filters, kernel_size , strides, padding.
    • How to properly use the Keras Conv2D class to create our own Convolution neural network and determine if we need to use a specific parameter for the Keras Conv2D class.




    Get Solution for free from DataCamp guru