This tutorial will demonstrate how to use the RCDT-NS classifier in the PyTransKit package.
Functions:
Constructor function: rcdt_ns_obj = RCDT_NS(num_classes, thetas, rm_edge)
Inputs:
----------------
num_classes : integer value
totale number of classes in the dataset.
thetas : 1d array
angles in degrees for taking radon projections. Example: thetas=numpy.linspace(0,180,45)
rm_edge : boolean
IF TRUE the first and last points of RCDTs will be removed.
Outputs:
----------------
rcdt_ns_obj : class object
Instance of the class RCDT_NS.
Fit function: rcdt_ns_obj.fit(Xtrain, Ytrain, no_deform_model)
Inputs:
----------------
Xtrain : 3d array, shape (n_samples, n_rows, n_columns)
Image data for training.
Ytrain : 1d array, shape (n_samples,)
Labels of the training images.
no_deform_model : boolean
IF TRUE, no deformation model will be added
Predict function: preds = rcdt_ns_obj.predict(Xtest, use_gpu)
Inputs:
----------------
Xtest : 3d array, shape (n_samples, n_rows, n_columns)
Image data for testing.
use_gpu : boolean
IF TRUE, use gpu for calculations.
Outputs:
----------------
preds : 1d array, shape (n_samples,)
Predicted labels for test samples.
The following example will demonstrate how to:
In this example we have used MNIST dataset stored in the data folder
import numpy as np
from sklearn.metrics import accuracy_score
from pathlib import Path
import sys
sys.path.append('../')
from pytranskit.classification.utils import *
use_gpu = False
from pytranskit.classification.rcdt_ns import RCDT_NS
For loading data we have used load_data function from the pytranskit/classifier/utils.py script. It takes name and directory of the dataset, and total number of classes as input. Returns both train and test images in two separate 3d arrays of shape (n_samples, n_rows, n_columns), and corresponding class labels. User can use there own implementation to load data, just need to make sure that the output arrays are consistent.
datadir = './data'
dataset = 'MNIST'
num_classes = 10 # total number of classes in the dataset
(x_train, y_train), (x_test, y_test) = load_data(dataset, num_classes, datadir) # load_data function from utils.py
loading data from mat files x_train.shape (60000, 28, 28) x_test.shape (10000, 28, 28) saved to ./data/MNIST/dataset.hdf5
In this example we have used 512 randomly chosen samples per class to train the model. We have used another function take_train_samples function from utils.py script for this. User can use their own script.
n_samples_perclass = 512 # total number of training samples per class used in this example
x_train_sub, y_train_sub = take_train_samples(x_train, y_train, n_samples_perclass,
num_classes, repeat=0) # function from utils.py
theta = np.linspace(0, 176, 45) # choose the angles in degrees that will be used to calculate Radon projections
rcdt_ns_obj = RCDT_NS(num_classes, theta, rm_edge=True)
This function takes the train samples and labels as input, and stores the basis vectors for corresponding classes in a private variable. This variable will be used in the predict function in the test phase
rcdt_ns_obj.fit(x_train_sub, y_train_sub)
Calculating RCDTs for training images ... Generating basis vectors for each class ...
predict function takes the train samples as input and returns the predicted class labels
preds = rcdt_ns_obj.predict(x_test, use_gpu)
Calculating RCDTs for testing images ... Finding nearest subspace for each test sample ...
print('\nTest accuracy: {}%'.format(100*accuracy_score(y_test, preds)))
Test accuracy: 95.41%