Coverage for source/model/model_blue_prints/vggception_cnn_blue_print.py: 100%
54 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-01 20:51 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-01 20:51 +0000
1# model/model_blue_prints/vggception_cnn_blue_print.py
3# global imports
4import math
5from tensorflow.keras import layers, Model
6from typing import Optional
8# local imports
9from source.model import BluePrintBase, ModelAdapterBase, TFModelAdapter, \
10 Vgg16Block, XceptionBlock
12class VGGceptionCnnBluePrint(BluePrintBase):
13 """
14 Blueprint for creating a hybrid CNN architecture combining VGG and Xception patterns.
16 This class implements a model blueprint that constructs a neural network with a
17 combined architecture inspired by VGG16 and Xception networks. It's designed to
18 process both spatial and non-spatial features by separating the input vector
19 and processing them through different network components before combining them.
20 """
22 def __init__(self, number_of_filters: int = 32, cnn_squeezing_coeff: int = 2,
23 dense_squeezing_coeff: int = 2, dense_repetition_coeff: int = 1,
24 filters_number_coeff: int = 2) -> None:
25 """
26 Initializes the VGGceptionCnnBluePrint with the specified configuration parameters.
28 Parameters:
29 number_of_filters (int): Initial number of convolutional filters.
30 cnn_squeezing_coeff (int): Factor by which CNN dimensions are reduced.
31 dense_squeezing_coeff (int): Factor by which dense layer sizes are reduced.
32 dense_repetition_coeff (int): Number of dense layers of the same size to use.
33 filters_number_coeff (int): Factor by which filter count increases in convolutional layers.
34 """
36 self.__number_of_filters = number_of_filters
37 self.__cnn_squeezing_coeff = cnn_squeezing_coeff
38 self.__dense_squeezing_coeff = dense_squeezing_coeff
39 self.__dense_repetition_coeff = dense_repetition_coeff
40 self.__filters_number_coeff = filters_number_coeff
42 def instantiate_model(self, input_shape: tuple[int, int], output_length: int, spatial_data_shape: tuple[int, int],
43 number_of_filters: Optional[int] = None, cnn_squeezing_coeff: Optional[int] = None,
44 dense_squeezing_coeff: Optional[int] = None, dense_repetition_coeff: Optional[int] = None,
45 filters_number_coeff: Optional[int] = None) -> ModelAdapterBase:
46 """
47 Creates and returns a hybrid VGG-Xception CNN model according to specified parameters.
49 The method constructs a neural network that:
50 1. Separates the input into spatial and non-spatial components
51 2. Processes the spatial data through VGG16 and Xception blocks
52 3. Flattens the CNN output and concatenates with non-spatial features
53 4. Passes the combined features through a series of dense layers
54 5. Produces a softmax output for classification
56 Parameters:
57 input_shape (tuple[int, int]): Shape of the input tensor
58 output_length (int): Number of output classes/actions
59 spatial_data_shape (tuple[int, int]): Rows and columns to reshape spatial data
60 number_of_filters (int): Initial number of convolutional filters
61 cnn_squeezing_coeff (int): Factor by which CNN dimensions are reduced
62 dense_squeezing_coeff (int): Factor by which dense layer sizes are reduced
63 dense_repetition_coeff (int): Number of dense layers of the same size to use
64 filters_number_coeff (int): Factor by which filter count increases in convolutional layers
66 Returns:
67 Model: Keras model implementing the hybrid VGG-Xception architecture to be compiled further.
68 """
70 if number_of_filters is None:
71 number_of_filters = self.__number_of_filters
72 if cnn_squeezing_coeff is None:
73 cnn_squeezing_coeff = self.__cnn_squeezing_coeff
74 if dense_squeezing_coeff is None:
75 dense_squeezing_coeff = self.__dense_squeezing_coeff
76 if dense_repetition_coeff is None:
77 dense_repetition_coeff = self.__dense_repetition_coeff
78 if filters_number_coeff is None:
79 filters_number_coeff = self.__filters_number_coeff
81 spatial_data_rows, spatial_data_cols = spatial_data_shape
82 spatial_data_length = spatial_data_rows * spatial_data_cols
84 input_vector = layers.Input((1, input_shape[0]))
85 reshaped_input_vector = layers.Reshape((input_shape[0],))(input_vector)
86 spatial_part = layers.Lambda(lambda x: x[:, :spatial_data_length])(reshaped_input_vector)
87 non_spatial_part = layers.Lambda(lambda x: x[:, spatial_data_length:])(reshaped_input_vector)
88 reshaped_spatial_part = layers.Reshape((spatial_data_rows, spatial_data_cols, 1))(spatial_part)
90 cnn_part = Vgg16Block([(3, 1), (3, 1), (2, 1)],
91 [number_of_filters, number_of_filters])(reshaped_spatial_part)
92 cnn_part = layers.BatchNormalization()(cnn_part)
94 nr_of_xceptions_blocks = int(math.ceil(math.log(spatial_data_rows // 2, cnn_squeezing_coeff)))
95 for _ in range(nr_of_xceptions_blocks):
96 number_of_filters *= filters_number_coeff
97 cnn_part = XceptionBlock([(3, 1), (3, 1), (3, 1), (1, 1)],
98 [number_of_filters, number_of_filters, number_of_filters],
99 [(cnn_squeezing_coeff, 1), (cnn_squeezing_coeff, 1)])(cnn_part)
100 cnn_part = layers.BatchNormalization()(cnn_part)
102 flatten_cnn_part = layers.Flatten()(cnn_part)
103 concatenated_parts = layers.Concatenate()([flatten_cnn_part, non_spatial_part])
105 closest_smaller_power_of_coeff = int(math.pow(dense_squeezing_coeff,
106 int(math.log(concatenated_parts.shape[-1],
107 dense_squeezing_coeff))))
108 dense = layers.Dense(closest_smaller_power_of_coeff, activation='relu')(concatenated_parts)
109 dense = layers.BatchNormalization()(dense)
111 number_of_nodes = closest_smaller_power_of_coeff // dense_squeezing_coeff
112 nr_of_dense_layers = int(math.log(closest_smaller_power_of_coeff, dense_squeezing_coeff))
113 for _ in range(nr_of_dense_layers):
114 for _ in range(dense_repetition_coeff):
115 dense = layers.Dense(number_of_nodes, activation='relu')(dense)
116 dense = layers.BatchNormalization()(dense)
117 number_of_nodes //= dense_squeezing_coeff
118 if int(math.log(number_of_nodes, 10)) == int(math.log(output_length, 10)) + 1:
119 dense = layers.Dropout(0.3)(dense)
120 elif int(math.log(number_of_nodes, 10)) == int(math.log(output_length, 10)):
121 break
123 output = layers.Dense(output_length, activation='softmax')(dense)
125 return TFModelAdapter(Model(inputs = input_vector, outputs = output))