model_components package

Subpackages

Submodules

model_components.export_helpers module

model_components.export_helpers.export(model, kwargs, results, base_path='exports', export_model=False, additive=True)

Exports the results of the training and testing process.

Args:

model (Model): The trained model. kwargs (dict): The configuration used for training. results (dict): The pipeline results of the training and testing process. base_path (str, optional): The base path for saving the exported files. Defaults to “exports”. export_model (bool, optional): Whether to save the model architecture and weights. Defaults to False. additive (bool, optional): Whether to generate a random folder inside the base path. If set to False, the export happens exactly at the user-specified folder. Defaults to True.

model_components.export_helpers.export_best_weights(model, file_path)

Exports the best weights of the model.

Args:

model (Model): The trained model. file_path (str): The path where the best weights will be saved.

model_components.export_helpers.export_history_to_csv(history, file_path)

Exports the training history to a CSV file.

Args:

history (History): The training history object. file_path (str): The path where the history CSV file will be saved.

model_components.export_helpers.export_kwargs(kwargs, file_path)

Exports the training configuration (kwargs) to a file.

Args:

kwargs (dict): The configuration used for training. file_path (str): The path where the configuration will be saved.

model_components.export_helpers.export_test_loss(test_loss, file_path)

Exports the test loss.

Args:

test_loss (float): The test loss value. file_path (str): The path where the test loss will be saved.

model_components.export_helpers.export_test_results(results, file_path, kwargs)

Exports the test results to a CSV file.

Args:

results (dict): The pipeline results of the training and testing process. file_path (str): The path where the test results CSV file will be saved. kwargs (dict): The configuration used for training.

model_components.model_creation module

model_components.model_creation.create_base_model(transfer_arch, image_size, pre_trained)

Create a base model using the specified transfer architecture and pretrained weights.

Args:

transfer_arch (str): Name of the transfer architecture (e.g., ‘VGG19’, ‘ResNet50’, etc.). image_size (tuple): Tuple containing the height and width of the input image (e.g., (224, 224)). pre_trained (str): Name of the dataset used to pre-train the model (e.g., ‘imagenet’).

Returns:

Tuple[Model, function]: A tuple containing the base model and the preprocess input function.

model_components.model_creation.create_model(base_model, before_dense, regularization, number_of_targets, dense_make, initializer, batch_norm, l1_strength, l2_strength, dropout_rate, activations)

Creates a model based on the provided arguments.

Args:

base_model (tf.keras.Model): The base model for transfer learning. before_dense (str): The layer type to add before dense layers (‘Flatten’ or ‘GlobalAveragePooling2D’). regularization (str): The regularization method to apply (‘L1’, ‘L2’, ‘Dropout+L1’, ‘Dropout+L2’, or None). number_of_targets (int): The number of target classes. dense_make (list): A list of integers representing the number of neurons in each dense layer. initializer (str): The initializer to use for dense layers. batch_norm (bool): Whether to use batch normalization in the model. l1_strength (float): The L1 regularization strength. l2_strength (float): The L2 regularization strength. dropout_rate (float): The dropout rate for dropout regularization. activations (str): The activation function to use for dense layers.

Returns:

tf.keras.Model: The created model based on the provided arguments.

model_components.model_creation.get_preprocess_input_function(model_name)

Returns the preprocessing function associated with the given transfer model name.

Args:

model_name (str): The name of the pre-trained model.

Returns:

function: The preprocess_input function associated with the given transfer model.

model_components.prediction_helpers module

model_components.prediction_helpers.create_predict_datagen(preprocess_input_func)

Create an ImageDataGenerator instance for prediction.

Args:

preprocess_input_func (function): Preprocessing function for input images.

Returns:

ImageDataGenerator: An ImageDataGenerator instance for prediction.

model_components.prediction_helpers.create_predict_generator(path_to_folder, image_size, target_type, batch_size, preprocess_input_func)

Create an image generator for prediction.

Args:

path_to_folder (str): Path to the directory containing images. image_size (tuple): Tuple of (height, width) for resizing images. target_type (str): Type of target labels (‘binary’ or ‘categorical’). batch_size (int): Number of samples per gradient update. preprocess_input_func (function): Preprocessing function for input images.

Returns:

DirectoryIterator: An iterator that yields (data, labels) tuples.

model_components.prediction_helpers.generate_predictions(model, predict_generator)

Generate predictions using a trained model and a predict_generator.

Args:

model (Model): A trained Keras model. predict_generator (DirectoryIterator): An iterator that yields (data, labels) tuples.

Returns:

tuple: A tuple containing the predictions and the filenames.

model_components.prediction_helpers.model_predict(model, preprocess_input_func, path_to_folder, class_labels, image_size, target_type, batch_size=32, verbose=True, sort_by='variance')

Predict the classes of images in a folder using a trained model.

Args:

model (Model): A trained Keras model. preprocess_input_func (function): Preprocessing function for input images. path_to_folder (str): Path to the directory containing images. class_labels (list): A list of class labels. image_size (tuple): Tuple of (height, width) for resizing images. target_type (str): Type of target labels (‘binary’ or ‘categorical’). batch_size (int, optional): Number of image samples per batch. Defaults to 32. verbose (bool, optional): Whether to print results. Defaults to True. sort_by (str, optional): Sorting criteria for results (‘confidence’ or ‘variance’). Defaults to ‘variance’.

Returns:

list: A list of dictionaries containing prediction results.

model_components.prediction_helpers.process_predictions(class_labels, path_to_folder, predictions, filenames, target_type, verbose, sort_by=None)

Process the predictions and generate a list of dictionaries containing the results.

Args:

class_labels (list): A list of class labels. path_to_folder (str): Path to the directory containing images. predictions (np.array): Predictions generated by the model. filenames (list): A list of filenames. target_type (str): Type of target labels (‘binary’ or ‘categorical’). verbose (bool): Whether to print results. sort_by (str, optional): Sorting criteria for results (‘confidence’ or ‘variance’).

Returns:

list: A list of dictionaries containing prediction results.

Module contents