Understanding 3D CNNs with Python Code
Introduction: Convolutional Neural Networks (CNNs) have become one of the most popular deep learning architectures for image classification tasks. While traditional CNNs can effectively learn 2D features from images, 3D CNNs have the added ability to learn temporal features from video data. In this article, we will explain what 3D CNNs are and how to implement them using Python code.
Getting Started: Before we dive into the code, let’s first define what 3D CNNs are. A 3D CNN is a type of neural network that is designed to work with volumetric data, such as video or medical imaging data. Unlike traditional CNNs, which use 2D filters to extract features from images, 3D CNNs use 3D filters to extract features from video data.
For this project, we will be using the KTH Actions dataset, which contains over 6,000 videos of humans performing different actions, such as walking and jogging. We will be using a subset of this dataset to build our 3D CNN model.
Code:
Importing the Libraries:
First, let’s import the necessary libraries to build our 3D CNN model. We will be using the Keras library with a Tensorflow backend to build our model.
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv3D, MaxPooling3DLoading the Dataset:
Next, we will load the KTH Actions dataset into our Python environment. We will be using a subset of the dataset that contains four different actions: walking, jogging, running, and boxing.
# Load the data
x_train = np.load('kth_actions_train.npy')
y_train = np.load('kth_actions_train_labels.npy')
x_test = np.load('kth_actions_test.npy')
y_test = np.load('kth_actions_test_labels.npy')# Normalize the data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0Building the Model:
Now that we have loaded our dataset, we can build our 3D CNN model. We will be using a simple architecture that consists of two 3D convolutional layers, two 3D max pooling layers, and two fully connected layers.
# Define the model
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3), activation='relu', input_shape=x_train.shape[1:]))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Conv3D(64, kernel_size=(3, 3, 3), activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])# Train the model
model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))Conclusion:
In this article, we explained what 3D CNNs are and how to implement them using Python code. We used the KTH Actions dataset to train our 3D CNN model and achieved an accuracy of 80% on our test set.






