Recognition¶
Submodules¶
cnn module¶
-
hardy.recognition.cnn.build_model(training_set, validation_set=None, config_path='./')[source]¶ Function that allows to build and fit a sequential convolutional neural network using Keras.
Parameters: - training_set: Keras image directory iterator
The set of files that will be used to train the CNN model
- validation_set: Keras image directory iterator
The set of files that will be used to validate the trained model after each epoch
- config_path : str
string containing the path to the yaml file representing the classifier hyperparameters
Returns: - model: Keras sequential model
The trained convolutional neural network
- history: Keras callbacks function
A function that retains information of the loss and performance of the training and validation sets in each epoch.
-
hardy.recognition.cnn.evaluate_model(model, testing_set)[source]¶ Function that returns the evaluation of the model based on the performance of the testing set previously separated from the rest of the learning dataset.
Parameters: - model : keras sequential model
the trained model we want to evaluate using a testing set
- testing_set: Keras image directory iterator
The testing set containg labelled images that was not part of the learning dataset. This will be used to evaluate the actual performance of the trained model.
Returns: - results[1] : float32
returns the classification accuracy of the model based on its performance on the testing set
-
hardy.recognition.cnn.feature_map(image, model, classes, size, layer_num=None, save=True, log_dir='./', image_path=None)[source]¶ The function outputs the feature map of given layer.
The function takes image path, model, number of classes, target size to ouput the feature maps for a particular neural network model.
Parameters: - image: str or numpy array
if string it opens the image from path provided. If numpy array, it directly feeds it into feature maps
- model: neural network model
trained neural network model to make prediction
- classes: int
number of classes used to train the model
- size: int
target size used to train the model
- layer_num: int or str
if int, provides output only from a single layer. If None, provides output from all the layers. If ‘last’, it provides provides probablity for classifications.
- save: bool
if True it saves the feature maps in the log_dir folder
- log_dir: str
log directory representing the location of logs
Returns: - feature_map: int
if layer_num = ‘last’, feature_map is probability for classfication
- pyplot: matplotlib.pyplot
if layer_num is int or None, pyplots are generated
-
hardy.recognition.cnn.feature_map_layers(img_feature_array, model, list_layer_pos, save, log_dir)[source]¶ Nested function for feature_map(). Returns the pyplots for if layer_num is int or None in feature_map().
Parameters: - image_feature_array: array
array in expanded dimension representing the image input in feature_map()
- model: neural network model
neural network model used to make prediction for the image
- list_layer_pos: list
list comprising of numbers representing the layer position
- save: bool
if True it saves the feature maps in the log_dir folder
- log_dir: str
log directory representing the location of logs
Returns: - pyplot: matplotlib.pyplot
pyplot representing the feature maps
-
hardy.recognition.cnn.k_fold_model(k, config_path='./', target_size=(80, 80), classes=['noisy', 'not_noisy'], batch_size=32, color_mode='rgb', iterator_mode='arrays', image_list=None, test_set=None, **kwargs)[source]¶
-
hardy.recognition.cnn.plot_history(model_history)[source]¶ Functions that returns plot of the performance of the learning set in each epoch.
Parameters: - model_history: Keras callbacks function
A function that retains information of the loss and performance of the training and validation sets in each epoch.
Returns: - fig: matplotlib plot
A figure containing two plots showing the change in the loss and accuracy during the training of the model
-
hardy.recognition.cnn.report_on_metrics(model, test_set, target_names=['noisy', 'not_noisy'])[source]¶ A function that prints the result of the model just trained
Parameters: - model: Keras sequential model
the trained convolutional neural network
- test_set: Keras image directory iterator
the test set to use to obtain the true performance of the model.
- target_names: list
list containing strings represnting the classes the data is classified in
Returns: - conf_matrix : array
A numpy array containing values for the true positives, false negatives, false positives and true negatives
- report : str
a string containg the overall report of the performance of the model. Accuracy, recall and F1 scores are reported.
-
hardy.recognition.cnn.save_load_model(filepath, model=None, save=None, load=None)[source]¶ Function to save and load the NN model
Function that can save or load model depending on given parameters.
Parameters: - filepath : str
string indicating the filename for saving or loading model.
- model : neural_network
trained neural network variable that is to be saved or loaded.
- save : bool
boolean value if true saves the neural network model.
- load : bool
boolean value if true loads the neural network model.
Returns: - loaded_model : model
model that is loaded from the specified location
tuner module¶
-
hardy.recognition.tuner.best_model(tuner, training_set, validation_set, test_set)[source]¶ Function that takes the tuner and builds up the model on the basis on best hyperparameters in the tuner
Parameters: - tuner: keras tuner
tuner generated by specifications from tuner_build_model function
- training_set: keras pointer
training set data generated through flow from directory
- validation_set: keras pointer
validation set data generated through flow from directory
- test_set: keras point
test_set data generated through flow from directory. Used for cross validation of model
- epochs: int
the number of times model is executed to be trained over training set & validation set
Returns: - model: keras model
model built up using the best hyperparameters in the tuner
- history: dict
dictionary containing result from fitting model oveer training and validation set
- metrics: np.float64
np array containing loss and accuracy for cross-validation of data
-
hardy.recognition.tuner.build_tuner_model(hp)[source]¶ Functions that builds a convolutional keras model with tunable hyperparameters
Parameters: - hp: keras tuner class
A class that is used to define the parameter search space
Returns: - model: Keras sequential model
The trained convolutional neural network
-
hardy.recognition.tuner.report_generation(model, history, metrics, log_dir, tuner=None, save_model=True, config_path=None, k_fold=False, k=None)[source]¶ Function that generates the report based on tuner search and hyperparameters
Parameters: - tuner: keras tuner
tuner generated by the run_tuner function
- model: keras model
model built up using the best hyperparameters in the tuner generated by the tuner ‘best_model’ function
- history: history: dict
dictionary containing result from fitting model over training and validation set generated by best_model function
- metrics: np.float64
np array containing loss and accuracy for cross-validation of data generated by best_model function
- log_dir: str
string representing the location where the report needs to be stored
- save_model: bool
If true saves the model with best hyperparameters in the defined location
- config_path: str
location of configuration file for the convolutional neural network
Returns: - .yaml file containing the hyperparameters, performance and history
- of the trained CNN
-
hardy.recognition.tuner.run_tuner(training_set, validation_set, project_name='untransformed')[source]¶ Function that runs the tuner using training set, validation set and hyperparameters defined in the config file
Parameters: - training_set: keras pointer
training set data generated through keras flow from directory function
- validation_set: keras pointer
validation set data generated through keras flow from directory function
- project_name: str
name to use for the log files of the tuner run
Returns: - tuner: keras tuner