Model Module¶
- class ModelAdapterBase¶
Bases:
ABC
Implements a base class for model adapters. It provides an interface for loading, saving, printing summaries, fitting, predicting, and retrieving the model.
- abstract fit(input_data: Any, output_data: Any, **kwargs) Any ¶
Fits the model to the provided input and output data.
- Parameters:
input_data (Any) – The input data for fitting the model.
output_data (Any) – The output data for fitting the model.
(**kwargs) –
Additional keyword arguments for fitting the model.
- Returns:
The result of the fitting process, which may vary depending on the model.
- Return type:
(Any)
- abstract get_model() Any ¶
Retrieves the underlying model.
- Returns:
The underlying model instance.
- Return type:
(Any)
- abstract load_model(path: str) None ¶
Loads a model from the specified path.
- Parameters:
path (str) – The path to the model file.
- abstract predict(data: Any) Any ¶
Predicts the output for the given input data.
- Parameters:
data (Any) – The input data for prediction.
- Returns:
The predicted output data.
- Return type:
(Any)
- abstract print_summary(print_function: ~typing.Callable = <built-in function print>) None ¶
Prints a summary of the model’s architecture and parameters.
- Parameters:
print_function (Callable) – The function to use for printing the summary. Defaults to the built-in print function.
- report_parameters_needed_for_fitting() list[str] ¶
Reports the parameters needed for fitting the model.
- Returns:
A list of parameter names that are required for fitting.
- Return type:
(list[str])
- abstract save_model(path: str) None ¶
Saves the model to the specified path.
- Parameters:
path (str) – The path to the model file.
- class SklearnModelAdapter(model: BaseEstimator)¶
Bases:
ModelAdapterBase
Implements a model adapter for scikit-learn models. It provides methods for loading, saving, printing summaries, fitting, predicting, and retrieving the model.
- TAG: str = 'sklearn'¶
- fit(input_data: Any, output_data: Any, validation_data: Any, **kwargs) dict ¶
Fits the model to the provided input and output data.
- Parameters:
input_data (Any) – The input data for fitting the model.
output_data (Any) – The output data for fitting the model.
validation_data (Any) – The validation data for evaluating the model.
(**kwargs) –
Additional keyword arguments for fitting the model.
- Returns:
A dictionary containing the results of the fitting process.
- Return type:
(dict)
- get_model() BaseEstimator ¶
Retrieves the underlying model.
- Returns:
The underlying model instance.
- Return type:
(BaseEstimator)
- load_model(path: str) None ¶
Loads a scikit-learn model from a file.
- Parameters:
path (str) – The path to the model file.
- Raises:
ValueError – If the path does not end with the expected model file extension.
FileNotFoundError – If the model file does not exist at the specified path.
- predict(data: Any) dict ¶
Predicts probabilities for the output for the given input data.
- Parameters:
data (Any) – The input data for prediction.
- Returns:
The predicted output data.
- Return type:
(dict)
- print_summary(print_function: ~typing.Callable = <built-in function print>) None ¶
Prints a summary of the model’s name and parameters. Uses the provided print function to output the summary.
- Parameters:
print_function (Callable) – The function to use for printing the summary.
- report_parameters_needed_for_fitting() list[str] ¶
Reports the parameters needed for fitting the model.
- Returns:
A list of parameter names that are required for fitting.
- Return type:
(list[str])
- save_model(path: str) None ¶
Saves a scikit-learn model to a file.
- Parameters:
path (str) – The path to the model file.
- Raises:
ValueError – If the path does not end with the expected model file extension.
- class TFModelAdapter(model: Model, optimizer: Optional[OptimizerV2] = None, loss: Optional[str] = None, metrics: Optional[list[str]] = None)¶
Bases:
ModelAdapterBase
Implements a model adapter for TensorFlow models. It provides methods for loading, saving, printing summaries, fitting, predicting, and retrieving the model.
- TAG: str = 'tensorflow'¶
- fit(input_data: Any, output_data: Any, validation_data: Any, epochs: int, batch_size: int, callbacks: list[tensorflow.python.keras.callbacks.Callback], **kwargs) dict ¶
Fits the model to the provided input and output data.
- Parameters:
input_data (Any) – The input data for fitting the model.
output_data (Any) – The output data for fitting the model.
validation_data (Any) – The validation data for evaluating the model.
epochs (int) – The number of epochs to train the model.
batch_size (int) – The batch size to use for training.
callbacks (list[Callback]) – The list of callbacks to use during training.
(**kwargs) –
Additional keyword arguments for fitting the model.
- Returns:
A dictionary containing the results of the fitting process.
- Return type:
(dict)
- get_model() Model ¶
Retrieves the underlying model.
- Returns:
The underlying model instance.
- Return type:
(Model)
- load_model(path: str) None ¶
Loads a TensorFlow model from a file.
- Parameters:
path (str) – The path to the model file.
- Raises:
ValueError – If the path does not end with the expected weights file extension.
- predict(data: Any) dict ¶
Predicts probabilities for the output for the given input data.
- Parameters:
data (Any) – The input data for prediction.
- print_summary(print_function: ~typing.Callable = <built-in function print>) None ¶
Prints a summary of the model’s architecture.
- Parameters:
print_function (Callable) – The function to use for printing the summary.
- report_parameters_needed_for_fitting() list[str] ¶
Reports the parameters needed for fitting the model.
- Returns:
A list of parameter names that are required for fitting.
- Return type:
(list[str])
- save_model(path: str) None ¶
Saves a TensorFlow model to a file.
- Parameters:
path (str) – The path to the model file.
- Raises:
ValueError – If the path does not end with the expected weights file extension.
- class GeneralSklearnBluePrint(base_estimator_class: type, **kwargs)¶
Bases:
BluePrintBase
Implements a general blueprint for scikit-learn models. It provides a method to instantiate a model based on the provided base estimator class and keyword arguments.
- instantiate_model(**kwargs) ModelAdapterBase ¶
Instantiates a model based on the provided keyword arguments.
- Parameters:
(**kwargs) –
Optional keyword arguments for the model.
- Returns:
The model adapter for the instantiated model.
- Return type:
- report_parameters_needed_for_instantiation() list[str] ¶
Reports the parameters needed for model instantiation.
- Returns:
A list of parameter names that are required for instantiation.
- Return type:
(list[str])
- class VGGceptionCnnBluePrint(number_of_filters: int = 32, cnn_squeezing_coeff: int = 2, dense_squeezing_coeff: int = 2, dense_repetition_coeff: int = 1, filters_number_coeff: int = 2)¶
Bases:
BluePrintBase
Blueprint for creating a hybrid CNN architecture combining VGG and Xception patterns.
This class implements a model blueprint that constructs a neural network with a combined architecture inspired by VGG16 and Xception networks. It’s designed to process both spatial and non-spatial features by separating the input vector and processing them through different network components before combining them.
- instantiate_model(input_shape: tuple[int, int], output_length: int, spatial_data_shape: tuple[int, int], number_of_filters: Optional[int] = None, cnn_squeezing_coeff: Optional[int] = None, dense_squeezing_coeff: Optional[int] = None, dense_repetition_coeff: Optional[int] = None, filters_number_coeff: Optional[int] = None) ModelAdapterBase ¶
Creates and returns a hybrid VGG-Xception CNN model according to specified parameters.
The method constructs a neural network that: 1. Separates the input into spatial and non-spatial components 2. Processes the spatial data through VGG16 and Xception blocks 3. Flattens the CNN output and concatenates with non-spatial features 4. Passes the combined features through a series of dense layers 5. Produces a softmax output for classification
- Parameters:
input_shape (tuple[int, int]) – Shape of the input tensor
output_length (int) – Number of output classes/actions
spatial_data_shape (tuple[int, int]) – Rows and columns to reshape spatial data
number_of_filters (int) – Initial number of convolutional filters
cnn_squeezing_coeff (int) – Factor by which CNN dimensions are reduced
dense_squeezing_coeff (int) – Factor by which dense layer sizes are reduced
dense_repetition_coeff (int) – Number of dense layers of the same size to use
filters_number_coeff (int) – Factor by which filter count increases in convolutional layers
- Returns:
Keras model implementing the hybrid VGG-Xception architecture to be compiled further.
- Return type:
Model
- report_parameters_needed_for_instantiation() list[str] ¶
Reports the parameters needed for model instantiation.
- Returns:
A list of parameter names that are required for instantiation.
- Return type:
(list[str])
- class InceptionBlock(kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]], filters: tuple[int, int, int, int], steps: tuple[int, int])¶
Bases:
object
Class implementing an Inception block compatible with the TensorFlow API. This block uses parallel convolutions with different kernel sizes and a max-pooling layer, followed by a concatenation of the results, as seen in the Inception architecture.
Diagram:
Input Tensor --> +-----------------------+ | | Conv2D - typ. 1xY | +-------------+ | | Kernel Size: K1xK1 |------------------------------>| Concatenate | | | Filters: N1 | | | | +-----------------------+ | | | | | +----------> +-----------------------+ +-----------------------+ | | | | Conv2D - typ. 3xY | | Conv2D - typ. 3xY | | | | | Kernel Size: K1xK1 |-->| Kernel Size: K2xK2 |-->| | | | Filters: N2 | | Filters: N2 | | | | +-----------------------+ +-----------------------+ | | | | | +----------> +-----------------------+ +-----------------------+ | | | | Conv2D - typ. 5xY | | Conv2D - typ. 5xY | | | | | Kernel Size: K1xK1 |-->| Kernel Size: K3xK3 |-->| | | | Filters: N3 | | Filters: N3 | | | | +-----------------------+ +-----------------------+ | | | | | +----------> +-----------------------+ +-----------------------+ | | | MaxPooling2D | | Conv2D | | | | Kernel Size: K4xK4 |-->| Kernel Size: K1xK1 |-->| | | Stride: S1xS1 | | Filters: N4 | +-------------+ --> Output Tensor +-----------------------+ +-----------------------+
- class SEBlock(reduction_ratio: int = 16)¶
Bases:
object
Class implementing a Squeeze-and-Excitation (SE) block compatible with the TensorFlow API. This block applies global average pooling followed by a squeeze-and-excitation operation, as described in the SE-Net architecture.
Diagram:
Input Tensor last dimension length - ITldl Reduction rate - Rr Input Tensor --> +-----------------+ +---------------+ +--------------------+ +--------------+ | | GlobalAvgPool |-->| Reshape | | Dense | | Dense | +----------+ | +-----------------+ | Shape: ITldl |-->| Nodes: ITldl // Rr |-->| Nodes: ITldl |-->| Multiply | | | | | | | | | | | +---------------+ +--------------------+ +--------------+ | | | | | +------------------------------------------------------------------------------------------------->| | +----------+ --> Output Tensor
- class Vgg16Block(kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], filters: tuple[int, int])¶
Bases:
object
Class implementing Vgg16 block compatible with tensorflow API. This block is a core component of the VGG16 architecture, applying two convolutional layers followed by a max pooling layer to downsample and extract features from the input tensor.
Diagram:
Input Tensor --> +-----------------------+ +-----------------------+ +-----------------------+ | Conv2D | | Conv2D | | MaxPooling2D | | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 | | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | | +-----------------------+ +-----------------------+ +-----------------------+ --> Output Tensor
- class XceptionBlock(kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]], filters: tuple[int, int, int], steps: tuple[tuple[int, int], tuple[int, int]])¶
Bases:
object
Class implementing an Xception block compatible with the TensorFlow API. This block implements depthwise separable convolutions followed by max pooling and a residual connection, as seen in the Xception architecture.
Diagram:
Input Tensor --> +-----------------------+ +----------------------+ +--------------------+ +-----+ | | SeparableConv2D | | SeparableConv2D | | MaxPooling2D | | Add | | | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 |-->| | | | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | Stride: S1xS1 | | | | +-----------------------+ +----------------------+ +--------------------+ | | | | | +----------> +-----------------------+ | | | Conv2D | | | | Filters: N3 | | | | Kernel Size: K4xK4 |------------------------------------------------------>| | | Stride: S2xS2 | | | | | +-----+ --> Output Tensor +-----------------------+