Feature Extraction from Images Module Tutorial
This Python module is used for extracting features from images using a Convolutional Autoencoder (CAE). The code for the CAE is adapted from a tutorial on the Paperspace blog.
Dependencies
Before starting, make sure that you have the following libraries installed. If not, you can install them using pip:
pip install numpy torch torchvision scikit-learn
Main Function
The main function in this script performs either of the following tasks based on the argument mode
:
If mode
is ‘train’, it creates an instance of the Convolutional Autoencoder, trains it using the given training set, and saves the trained model.
If mode
is not ‘train’, it loads a pre-trained model and uses it to generate and plot features for a set of test images.
Arguments
dataset_name
: The name of the dataset. The default is ‘action_recognition’.images_array
: The path to the file containing the images array. The default is ‘./results/ucf10.npz’.data_folder
: The folder that contains the images. The default is'../data/images_ucf10/'
.labels_file
: The path to the file containing the labels. The default is None.no_classes
: The number of classes in the dataset. The default is 10.mode
: The mode in which to run the script. The choices are ‘train’ and ‘test’. The default is ‘train’.batch_size
: The batch size to use during training. The default is 50.epochs
: The number of epochs to train the model for. The default is 50.
Example Usage
The typical usage from the command line would look like this:
python feature_extraction.py --dataset_name "action_recognition" --images_array "./results/ucf10.npz" --data_folder "../data/images_ucf10/" --labels_file "labels.txt" --no_classes 10 --mode "train" --batch_size 50 --epochs 50
This command will train the Convolutional Autoencoder using images in the ./results/ucf10.npz
file and labels in the labels.txt file. It will run for 50 epochs with a batch size of 50.
If you want to use it inside another Python script, here is how you would do it:
import argparse
from feature_extraction import main, device
options = argparse.Namespace(
dataset_name='action_recognition',
images_array='./results/ucf10.npz',
data_folder='../data/images_ucf10/',
labels_file='labels.txt',
no_classes=10,
mode='train',
batch_size=50,
epochs=50
)
main(options)
* This tutorial has been made with the help of ChatGPT 4.0.