Coverage for source/model/model_building_blocks/vgg16_block.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-06-06 12:00 +0000

1# model/model_building_blocks/vgg16_block.py 

2 

3import tensorflow as tf 

4from tensorflow.keras.layers import Conv2D, MaxPooling2D 

5 

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. 

11 

12 Diagram: 

13 

14 :: 

15 

16 Input Tensor --> +-----------------------+ +-----------------------+ +-----------------------+ 

17 | Conv2D | | Conv2D | | MaxPooling2D | 

18 | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 | 

19 | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | | 

20 +-----------------------+ +-----------------------+ +-----------------------+ --> Output Tensor 

21 """ 

22 

23 def __init__(self, kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int]], filters: tuple[int, int]) -> None: 

24 """ 

25 Class constructor. 

26 

27 Parameters: 

28 kernels (tuple[tuple[int, int], tuple[int, int], tuple[int, int]]): Sizes of all kernels used within this block. 

29 filters (tuple[int, int]): Number of filters used in convolutional layers. 

30 """ 

31 

32 self.__conv_2d_1_kernel_size: tuple[int, int] = kernels[0] 

33 self.__conv_2d_2_kernel_size: tuple[int, int] = kernels[1] 

34 self.__max_pooling_2d_kernel_size: tuple[int, int] = kernels[2] 

35 self.__conv_2d_1_nr_of_filters: int = filters[0] 

36 self.__conv_2d_2_nr_of_filters: int = filters[1] 

37 

38 def __call__(self, input_tensor: tf.Tensor) -> tf.Tensor: 

39 """ 

40 Applies convolutional transformation with max pooling to input tensor. 

41 

42 Parameters: 

43 input_tensor (tf.Tensor): Input tensor that transformations should be applied to. 

44 

45 Returns: 

46 (tf.Tensor): Output tensor with applied transformations. 

47 """ 

48 

49 x = Conv2D(self.__conv_2d_1_nr_of_filters, self.__conv_2d_1_kernel_size, 

50 activation = 'relu', padding = 'same')(input_tensor) 

51 x = Conv2D(self.__conv_2d_2_nr_of_filters, self.__conv_2d_2_kernel_size, 

52 activation = 'relu', padding = 'same')(x) 

53 

54 output_tensor = MaxPooling2D(self.__max_pooling_2d_kernel_size)(x) 

55 

56 return output_tensor