Skip to content
Related Articles

Related Articles

Python Tensorflow – tf.keras.layers.Conv3D() Function

Improve Article
Save Article
  • Last Updated : 10 Jun, 2022
Improve Article
Save Article

In this article, we will cover Tensorflow tf.keras.layers.Conv3D() function.

TensorFlow is a free and open-source machine learning library. TensorFlow was created by Google Brain Team researchers and engineers as part of Google’s Machine Intelligence research group with the aim of performing machine learning and deep neural network research, although the technology is broad enough to be used in a number of different disciplines.

Difference between Conv2D and Conv3D

Let us start by first examining what convolution exactly is. In image processing, convolution is the technique of altering an image by applying a kernel to each pixel and its local neighbors over the entire picture. The kernel is a value matrix whose size and values affect the convolution process’ transformation impact. The output size will then depend on the following: a number of filters, filter spatial extend, stride, and amount of zero padding.

The conv filter is dragged horizontally and vertically (in x and y) over a picture, resulting in a Conv2D, regardless of whether the image is greyscaled (1 channel), color (3 channels), or anything else. The sole difference is that the Conv2D filter now requires an equal number of in-channels in the third dimension, whereas depth is the third dimension in a 3D convolution process, and the convolution filter is moved along that dimension as well, thus a 2x2x2 filter is moved in x, y, and z across the volume. In that scenario, the input has more than three dimensions.

The tf.keras.layers.Conv3D() function is used to apply the 3D convolution operation on data. This layer generates a tensor of outputs by convolving the layer input with a convolution kernel.

Syntax: tf.keras.layers.Conv3D( 

filters, kernel_size, strides=(1, 1, 1), padding=’valid’, data_format=None,

dilation_rate=(1, 1, 1), groups=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,

bias_constraint=None, **kwargs)

Input Shape: A 5+D tensor of shape: batch_shape + (channels, conv_dim1, conv_dim2, conv_dim3)

Output Shape: A 5+D tensor of shape: batch_shape + (filters, new_conv_dim1, new_conv_dim2, new_conv_dim3)

Parameters:

  • filters (Integer): The output space’s dimensionality (i.e. the number of output filters in the convolution).
  • kernel_size (Integer | [Integer,Integer,Integer]): The depth, height, and width of the 3D convolution window. 
  • strides (Integer | [Integer,Integer,Integer]): The convolutional strides in each dimension.
  • data_format: The data format. This specifies the order in which the dimensions in the inputs are ordered. channels_last is the default value.
  • dilation_rate: In each dimension, the dilation rate to utilize for the dilated convolution. It should be an integer or a three-int list/tuple.
  • groups (Integer): A positive integer indicating how many groups the input is divided into along the channel axis.
  • activation: The layer’s activation function.
  • use_bias (Boolean): If the layer has a bias vector or not. True is the default value.
  • kernel_initializer: The convolutional kernel weights matrix’s initializer.
  • bias_initializer: The bias vector’s initializer.
  • kernel_regularizer: The regularizer function applied to the kernel weights matrix.
  • bias_regularizer: The regularizer function applied to the bias vector.
  • activity_regularizer: The regularizer function applied to the activation.
  • kernel_constraint: The constraint for the convolutional kernel weights.
  • bias_constraint: The constraint for the bias vector.

Returns: A 5+D tensor representing activation(conv3d(inputs, kernel) + bias).

Example 1:

In this example, When using this layer as the first layer in a model, provide the keyword argument tensor_shape of integers, e.g. tensor_shape=(4, 28, 28, 28, 1) for 28x28x28 volumes with a single channel, in data_format=”channels_last”. In Cov3D w passed filters=8, which is responsible for space dimensionality, whereas kernel_size Is responsible for depth, height, and width of the 3D convolution window. After that, we defined models.Model function that can be created in the easiest way by using the Model class. Only the input and output layers need to be defined. The model.pridict return a trained model and predict the label of a new set of data. This method accepts one argument, (e.g. model. predict(X) ), and returns the learned label for each object in the array.

Python3




import tensorflow as tf
  
tensor_shape = (4, 28, 28, 28, 1)
input_shape = tensor_shape[1:]
X = tf.random.normal(tensor_shape)
  
  
def model(input_shape):
    X_input = tf.keras.layers.Input(shape=input_shape)
    X_output = tf.keras.layers.Conv3D(
        filters=8, kernel_size=4, strides=2)(X_input)
    model = tf.keras.models.Model(inputs=X_input, outputs=X_output)
    return model
  
  
model = model(input_shape)
  
Y = model.predict(X, steps=4)
print(Y.shape)


Output:

(4, 13, 13, 13, 8)

Example 2:

In this example, When using this layer as the first layer in a model, provide the keyword argument tensor_shape of integers, e.g. tensor_shape=(1, 4, 4, 4, 1) for 4x4x4 volumes with a single channel, in data_format=”channels_last”. In Cov3D w passed filters=3, which is responsible for space dimensionality, whereas kernel_size Is responsible for the depth, height, and width of the 3D convolution window. After that, we defined models.Model function that can be created in the easiest way by using the Model class. Only the input and output layers need to be defined. The model.pridict return a trained model and predict the label of a new set of data. This method accepts one argument, (e.g. model. predict(X) ), and returns the learned label for each object in the array.

Python3




import tensorflow as tf
  
tensor_shape = (1, 4, 4, 4, 1)
input_shape = tensor_shape[1:]
X = tf.random.normal(tensor_shape)
  
  
def model(input_shape):
    X_input = tf.keras.layers.Input(shape=input_shape)
    X_output = tf.keras.layers.Conv3D(
        filters=3, kernel_size=3, strides=1)(X_input)
    model = tf.keras.models.Model(inputs=X_input, outputs=X_output)
    return model
  
  
model = model(input_shape)
  
Y = model.predict(X, steps=1)
print(Y)


Output:

[[[[[-1.2114613   0.576858    0.29229757]
    [ 0.83879304 -0.20926164  0.8637264 ]]

   [[ 0.03376093  0.1571547   0.13753639]
    [-0.2992745   0.73341274  0.46030152]]]


  [[[ 0.61261517 -0.1008687  -0.64397895]
    [-0.5964341  -0.9971596  -0.18633988]]

   [[ 1.5544158   0.27557844 -1.2441761 ]
    [-0.21357535  0.51095605 -1.8175783 ]]]]]

CNN on 3D MNIST Dataset

Let’s use the above knowledge to create a Convolutional Neural Network. For this purpose, we have used the MNIST 3D dataset. 

Step 1: Let’s start by importing all the desired libraries and the dataset. After that we will print the shape of one of the arrays, we will notice that the information is condensed at the moment.

Python3




# Importing libraries
# Importing libraries
import h5py
import numpy as np
import tensorflow as tf
  
with h5py.File("full_dataset_vectors.h5", "r") as hf:
    # Importing MNIST 3D dataset
    X_train = hf["X_train"][:]
    Y_train = hf["y_train"][:]
    X_test = hf["X_test"][:]
    Y_test = hf["y_test"][:]
  
print(X_train.shape)


Output:

(10000, 4096)

Step 2: Reshape the dataset

Python3




# Reshaping dataset
X_train = X_train.reshape(X_train.shape[0], 1, 16, 16, 16)
X_test = X_test.reshape(X_test.shape[0], 1, 16, 16, 16)
  
print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)
print(Y_test.shape)


Output:

(10000, 1, 16, 16, 16)
(10000,)
(2000, 1, 16, 16, 16)
(2000,)

Step 3: Now, Before feeding the dataset to the model, we will do a few steps of data preprocessing like normalization and one-hot encoding.

Python3




def data_preprocessing():
    global X_train, X_test, Y_train, Y_test
    X_train = X_train/255.0
    X_test = X_test/255.0
    Y_train = tf.keras.utils.to_categorical(Y_train)
    Y_test = tf.keras.utils.to_categorical(Y_test)
    num_classes = Y_train.shape[1]
    return num_classes
  
  
num_classes = data_preprocessing()


Step 4: Now, finally we will create the model and fit it to the training dataset. I have used a very basic model here for the demonstration consisting of a Conv3D, MaxPool3D layer followed by two Dense layers.

Python3




def model(input_shape, num_classes):
    X_input = tf.keras.Input(input_shape)
    X = tf.keras.layers.Conv3D(filters=32, kernel_size=(
        2, 2, 2), activation='relu', data_format='channels_first')(X_input)
    X = tf.keras.layers.MaxPool3D(data_format='channels_first')(X)
    X = tf.keras.layers.Dropout(rate=0.25)(X)
    X = tf.keras.layers.Flatten()(X)
    X = tf.keras.layers.Dense(128, activation='softmax')(X)
    X = tf.keras.layers.Dropout(rate=0.5)(X)
    X_output = tf.keras.layers.Dense(num_classes, activation='softmax')(X)
    model = tf.keras.models.Model(inputs=X_input, outputs=X_output)
    return model
  
  
model = model(X_train.shape[1:], num_classes)
model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss='categorical_crossentropy', metrics='accuracy')
  
print(model.summary())


Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!