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

14 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-07-30 20:59 +0000

1# model/model_building_blocks/vgg16_block.py 

2 

3# global imports 

4import tensorflow as tf 

5from tensorflow.keras.layers import Conv2D, MaxPooling2D 

6 

7# local imports 

8 

9class Vgg16Block: 

10 """ 

11 Class implementing Vgg16 block compatible with tensorflow API. This block is a core component of 

12 the VGG16 architecture, applying two convolutional layers followed by a max pooling layer to 

13 downsample and extract features from the input tensor. 

14 

15 Diagram: 

16 

17 :: 

18 

19 Input Tensor --> +-----------------------+ +-----------------------+ +-----------------------+ 

20 | Conv2D | | Conv2D | | MaxPooling2D | 

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

22 | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | | 

23 +-----------------------+ +-----------------------+ +-----------------------+ --> Output Tensor 

24 """ 

25 

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

27 """ 

28 Class constructor. 

29 

30 Parameters: 

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

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

33 """ 

34 

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

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

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

38 self.__conv_2d_1_nr_of_filters: int = filters[0] 

39 self.__conv_2d_2_nr_of_filters: int = filters[1] 

40 

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

42 """ 

43 Applies convolutional transformation with max pooling to input tensor. 

44 

45 Parameters: 

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

47 

48 Returns: 

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

50 """ 

51 

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

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

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

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

56 

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

58 

59 return output_tensor