
Video classification is a machine learning process that involves categorizing video scenes into classes by assigning labels depending on the video content. The video classification pipeline inputs a video file and generates a prediction based on a list of predefined classes. The process is commonly used in surveillance systems to help identify and classify specific actions or behaviors captured in the input video footage.
This article explains how to implement a video classification pipeline on a Vultr Cloud GPU server to identify human actions using the Video Masked Autoencoders VideoMAE pre-trained model from the Kinetics 400 Dataset. You will load input videos, sample the frames, feed the video frames into VideoMAE processors, and get the action class for each classified element in the input video clip.
Before you begin:
Follow the steps below to set up the server by installing all necessary dependency packages, download sample videos, and move them to the Notebook directory to use as the input files for the video classification tasks.
Install all dependency library packages using Pip.
The above command installs the following packages on the server.
transformers: Offers APIs and pipelines for downloading download and using pretrained transformer-based models. The VideoMAE image processor and classification models are part of the transformers library.pytorchvideo: Provides reusable, modular and efficient components for running video understanding tasks. VideoMAE internally uses torchvision functions to optimize the video classification process.opencv-python: Provides a wide range of image processing functionalities. You will use Opencv to load videos and extract frames.matplotlib: Creates static or interactive visualizations.ipywidgets: Provides interactive HTML widgets for Jupyter Notebook sessions using IPython kernel.gdown: Downloads files from public links such as Google Drive.Download a sample input file. For example, use the gdown module to download a sample video from a Google Drive link
You can use any other video file for classification in your session. For purposes of this article, use the sample video provided in the Google Drive URL.
Extract sample videos from the videos.zip archive to the notebooks directory using the Jupyter user home path.
Select Notebook within the Jupyter interface to create a new Python3 Kernel file.
In a new notebook code cell, import all required libraries.
Press Shift + Enter to run the code cell and import libraries.
Load the 4.mp4 sample video from the downloaded files using the openCV VideoCapture function.
Define the number of sample frames to use with the VideoMAE model. For its pre-trained Kinetics 400 model, VideoMAE uses 16 frames along the timeline as inputs.
Extract the number of total frames from the loaded video clip.
Get the indices of all sampled frames that are evenly spaced over a specified interval across the entire video.
Get the sampled frames into a list to use as the inputs of the VideoMAE classification model.
In the above function:
video.set: Locates the specific video frame based on the frame index.video.read()[1]: Returns the image raw data in the BGR order used by the openCV package.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB): Converts the image data from the BGR order to the normal RGB order for processing by VideoMAE.Create a new figure to visualize the sampled frames using Matplotlib.
Define the number of frames to display per row, for example, 4 and the total number of rows.
Visualize and view the sample frames.
Verify that all sampled images in a series display in your notebook. You will feed the sample images to the VideoMAE model for further classification.
Define a new function to return all sampled frames to ease the downstreaming steps.
Load the VideoMAE image processor pretrained from the Kinetics 400 dataset.
Load the VideoMAE pretrained video classification model from the same dataset.
Preprocess the frames by resizing, scaling, and normalizing the frame raw data using the Video image processor.
Get the classification model output using the preprocessed inputs
Extract the logits from the model outputs.
Get the indices of the top 3 predicted classes among all 400 predefined classes in the Kinetics 400 dataset.
Get the normalized prediction probability for each class using softmax.
Check the top 1 predicted label with its probability.
Output:
Based on the above prediction output, the classification aligns well with the content of the input video.
Define a new function to perform video classification using the sample video frames as the input and output top-k predicted classes with the highest probabilities.
You can also use the above function to scale up the video classification operation to a list of videos in batches.
Define a new function to display the video frame against its top-k predicted class with the probability for each.
The above function displays the video frame against its top-k predicted class that contains all the necessary key information. It includes the frame index cnt, the raw sampled image data frame, the video filename, the predicted top-k labels and probs for each label.
Set up the directory path to store your sample video files. For example, use videos/ in the Jupyter user home directory.
Create a new Matplotlib figure to visualize the video classification results for all available videos in the directory.
Output:
Visualize the results.
Verify that the visualization results display in your session with all videos classified to the most suitable classes.
You have set up a video classification pipeline using VideoMAE on a Vultr Cloud GPU server. You prepared the sample input video frames by extracting the right frames in each time sequence. Then, you applied the input frames to the VideoMAE image processor and video classification models for scaling to a list of video files in batches. For more information, visit the VideoMAE model page.
Download the model pipeline Jupyter Notebook file to view the implementations applied in this article.
0 Comments
Be the first to comment and share your perspective with the community.