Coverage for source/model/model_building_blocks/vgg16_block.py: 100%
14 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +0000
1# model/model_building_blocks/vgg16_block.py
3import tensorflow as tf
4from tensorflow.keras.layers import Conv2D, MaxPooling2D
6class Vgg16Block:
7 """
8 Class implementing Vgg16 block compatible with tensorflow API. This block is a core component of
9 the VGG16 architecture, applying two convolutional layers followed by a max pooling layer to
10 downsample and extract features from the input tensor.
12 Diagram:
14 .. code-block:: text
15 Input Tensor --> +-----------------------+ +-----------------------+ +-----------------------+
16 | Conv2D | | Conv2D | | MaxPooling2D |
17 | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 |
18 | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | |
19 +-----------------------+ +-----------------------+ +-----------------------+ --> Output Tensor
20 """
22 def __init__(self, kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], filters: tuple[int, int]) -> None:
23 """
24 Class constructor.
26 Parameters:
27 kernels (tuple[tuple[int, int], tuple[int, int], tuple[int, int]]): Sizes of all kernels used within this block.
28 filters (tuple[int, int]): Number of filters used in convolutional layers.
29 """
31 self.__conv_2d_1_kernel_size: tuple[int, int] = kernels[0]
32 self.__conv_2d_2_kernel_size: tuple[int, int] = kernels[1]
33 self.__max_pooling_2d_kernel_size: tuple[int, int] = kernels[2]
34 self.__conv_2d_1_nr_of_filters: int = filters[0]
35 self.__conv_2d_2_nr_of_filters: int = filters[1]
37 def __call__(self, input_tensor: tf.Tensor) -> tf.Tensor:
38 """
39 Applies convolutional transformation with max pooling to input tensor.
41 Parameters:
42 input_tensor (tf.Tensor): Input tensor that transformations should be applied to.
44 Returns:
45 (tf.Tensor): Output tensor with applied transformations.
46 """
48 x = Conv2D(self.__conv_2d_1_nr_of_filters, self.__conv_2d_1_kernel_size,
49 activation = 'relu', padding = 'same')(input_tensor)
50 x = Conv2D(self.__conv_2d_2_nr_of_filters, self.__conv_2d_2_kernel_size,
51 activation = 'relu', padding = 'same')(x)
53 output_tensor = MaxPooling2D(self.__max_pooling_2d_kernel_size)(x)
55 return output_tensor