focal.hyperparameter_tuning module

Hyperparameter tuning module for the FOCAL application.

This module provides classes for hyperparameter optimization using Keras Tuner for both CNN and MLP models.

class focal.hyperparameter_tuning.BuildHyperModel(image_shape: Tuple[int, int, int], param_shape: Tuple[int, ...], backbone: str | None = 'mobilenet')[source]

Bases: HyperModel

HyperModel for determining optimal hyperparameters for the combined CNN+MLP model.

This class builds a model architecture that combines image features from MobileNetV2 with numerical parameters for fiber cleave classification.

build(hp)[source]

Build hypermodel to perform hyperparameter search.

Parameters:

hp – keras_tuner.engine.hyperparameters.HyperParameters Hyperparameters to be tuned

Returns:

Compiled model with hyperparameters

Return type:

tf.keras.Model

class focal.hyperparameter_tuning.BuildMLPHyperModel(model_path: str)[source]

Bases: HyperModel

HyperModel for MLP-based tension prediction.

This class builds a model that uses features extracted from a pre- trained CNN to predict optimal tension values.

build(hp)[source]

Build model with hyperparameters.

Parameters:

hp – keras_tuner.HyperParameters - Hyperparameters to be used for tuning

Returns:

Model to be trained

Return type:

tf.keras.Model

class focal.hyperparameter_tuning.HyperParameterTuning(image_shape: Tuple[int, int, int], feature_shape: Tuple[int, ...], max_epochs: int = 20, objective: str = 'val_accuracy', directory: str = './tuner_logs', project_name: str = 'Cleave_Tuner', backbone: str | None = 'mobilenet', class_weights: str | None = None)[source]

Bases: object

Class for tuning hyperparameters for the combined CNN+MLP model.

get_best_hyperparameters() object[source]

Get best hyperparameters from hyperparameter search.

Returns:

Best hyperparameters from hyperparameter search

Return type:

keras_tuner.engine.hyperparameters.HyperParameters

get_best_model() Model[source]

Get best model from hyperparameter search.

Returns:

Best model from hyperparameter search

Return type:

tf.keras.Model

Run hyperparameter search.

Parameters:
  • train_ds – tf.data.Dataset - Training dataset

  • test_ds – tf.data.Dataset - Testing dataset

  • max_epochs – maximum number of training epochs

save_best_model(pathname: str) None[source]

Save the best model from hyperparameter search.

Parameters:

pathname – Path where to save the model

tuner

self.tuner = Hyperband( hypermodel, objective=objective, max_epochs=max_epochs, directory=directory,

project_name=project_name )

class focal.hyperparameter_tuning.ImageHyperparameterTuning(image_shape: Tuple[int, int, int], max_epochs: int = 20, objective: str = 'val_accuracy', directory: str = './tuner_logs', project_name: str = 'CNN_Image_Only', backbone: str | None = 'mobilenet', num_classes: int | None = 5, class_weights: str | None = None)[source]

Bases: HyperParameterTuning

Hyperparameter tuning specifically for image-only models.

tuner

self.tuner = Hyperband( hypermodel, objective=objective, max_epochs=max_epochs, directory=directory,

project_name=project_name )

class focal.hyperparameter_tuning.ImageOnlyHyperModel(image_shape: Tuple[int, int, int], num_classes: int = 5, backbone: str | None = 'mobilenet', classification_type: str | None = 'binary')[source]

Bases: HyperModel

HyperModel for image-only classification (no numerical parameters).

build(hp)[source]

Build the image-only model with hyperparameters.

Parameters:

hp – Hyperparameters to tune

Returns:

Compiled model

Return type:

tf.keras.Model

class focal.hyperparameter_tuning.MLPHyperparameterTuning(cnn_path: str, max_epochs: int = 20, objective: str = 'val_mae', directory: str = './tuner_logs', project_name: str = 'MLPTuner', class_weights=None)[source]

Bases: HyperParameterTuning

Hyperparameter tuning specifically for MLP models.