Coverage for source/model/model_blue_prints/vggception_cnn_blue_print.py: 100%
41 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-06 12:00 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-06 12:00 +0000
1# model/model_blue_prints/basic_cnn_blue_print.py
3# global imports
4import math
5from tensorflow.keras import Model, layers
7# local imports
8from source.model import BluePrintBase
9from source.model import Vgg16Block
10from source.model import XceptionBlock
11from source.model import ModelAdapterBase
12from source.model import TFModelAdapter
14class VGGceptionCnnBluePrint(BluePrintBase):
15 """
16 Blueprint for creating a hybrid CNN architecture combining VGG and Xception patterns.
18 This class implements a model blueprint that constructs a neural network with a
19 combined architecture inspired by VGG16 and Xception networks. It's designed to
20 process both spatial and non-spatial features by separating the input vector
21 and processing them through different network components before combining them.
22 """
24 def instantiate_model(self, input_shape: tuple[int, int], output_length: int, spatial_data_shape: tuple[int, int],
25 number_of_filters: int = 32, cnn_squeezing_coeff: int = 2, dense_squeezing_coeff: int = 2,
26 dense_repetition_coeff: int = 1, filters_number_coeff: int = 2) -> ModelAdapterBase:
27 """
28 Creates and returns a hybrid VGG-Xception CNN model according to specified parameters.
30 The method constructs a neural network that:
31 1. Separates the input into spatial and non-spatial components
32 2. Processes the spatial data through VGG16 and Xception blocks
33 3. Flattens the CNN output and concatenates with non-spatial features
34 4. Passes the combined features through a series of dense layers
35 5. Produces a softmax output for classification
37 Parameters:
38 input_shape (tuple[int, int]): Shape of the input tensor
39 output_length (int): Number of output classes/actions
40 spatial_data_shape (tuple[int, int]): Rows and columns to reshape spatial data
41 number_of_filters (int): Initial number of convolutional filters
42 cnn_squeezing_coeff (int): Factor by which CNN dimensions are reduced
43 dense_squeezing_coeff (int): Factor by which dense layer sizes are reduced
44 dense_repetition_coeff (int): Number of dense layers of the same size to use
45 filters_number_coeff (int): Factor by which filter count increases in convolutional layers
47 Returns:
48 Model: Keras model implementing the hybrid VGG-Xception architecture to be compiled further.
49 """
51 spatial_data_rows, spatial_data_cols = spatial_data_shape
52 spatial_data_length = spatial_data_rows * spatial_data_cols
54 input_vector = layers.Input((1, input_shape[0]))
55 reshaped_input_vector = layers.Reshape((input_shape[0],))(input_vector)
56 spatial_part = layers.Lambda(lambda x: x[:, :spatial_data_length])(reshaped_input_vector)
57 non_spatial_part = layers.Lambda(lambda x: x[:, spatial_data_length:])(reshaped_input_vector)
58 reshaped_spatial_part = layers.Reshape((spatial_data_rows, spatial_data_cols, 1))(spatial_part)
60 cnn_part = Vgg16Block([(3, 1), (3, 1), (2, 1)],
61 [number_of_filters, number_of_filters])(reshaped_spatial_part)
62 cnn_part = layers.BatchNormalization()(cnn_part)
64 nr_of_xceptions_blocks = int(math.ceil(math.log(spatial_data_rows // 2, cnn_squeezing_coeff)))
65 for _ in range(nr_of_xceptions_blocks):
66 number_of_filters *= filters_number_coeff
67 cnn_part = XceptionBlock([(3, 1), (3, 1), (3, 1), (1, 1)],
68 [number_of_filters, number_of_filters, number_of_filters],
69 [(cnn_squeezing_coeff, 1), (cnn_squeezing_coeff, 1)])(cnn_part)
70 cnn_part = layers.BatchNormalization()(cnn_part)
72 flatten_cnn_part = layers.Flatten()(cnn_part)
73 concatenated_parts = layers.Concatenate()([flatten_cnn_part, non_spatial_part])
75 closest_smaller_power_of_coeff = int(math.pow(dense_squeezing_coeff,
76 int(math.log(concatenated_parts.shape[-1],
77 dense_squeezing_coeff))))
78 dense = layers.Dense(closest_smaller_power_of_coeff, activation='relu')(concatenated_parts)
79 dense = layers.BatchNormalization()(dense)
81 number_of_nodes = closest_smaller_power_of_coeff // dense_squeezing_coeff
82 nr_of_dense_layers = int(math.log(closest_smaller_power_of_coeff, dense_squeezing_coeff))
83 for _ in range(nr_of_dense_layers):
84 for _ in range(dense_repetition_coeff):
85 dense = layers.Dense(number_of_nodes, activation='relu')(dense)
86 dense = layers.BatchNormalization()(dense)
87 number_of_nodes //= dense_squeezing_coeff
88 if int(math.log(number_of_nodes, 10)) == int(math.log(output_length, 10)) + 1:
89 dense = layers.Dropout(0.3)(dense)
90 elif int(math.log(number_of_nodes, 10)) == int(math.log(output_length, 10)):
91 break
93 output = layers.Dense(output_length, activation='softmax')(dense)
95 return TFModelAdapter(Model(inputs = input_vector, outputs = output))