Coverage for source/model/model_building_blocks/inception_block.py: 22%

23 statements  

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

1# model/model_building_blocks/inception_block.py 

2 

3import tensorflow as tf 

4from tensorflow.keras.layers import Conv2D, MaxPooling2D, Concatenate 

5 

6class InceptionBlock: 

7 """ 

8 Class implementing an Inception block compatible with the TensorFlow API. This block uses 

9 parallel convolutions with different kernel sizes and a max-pooling layer, followed by a 

10 concatenation of the results, as seen in the Inception architecture. 

11 

12 Diagram: 

13 

14 :: 

15 

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

17 | | Conv2D - typ. 1xY | +-------------+ 

18 | | Kernel Size: K1xK1 |------------------------------>| Concatenate | 

19 | | Filters: N1 | | | 

20 | +-----------------------+ | | 

21 | | | 

22 +----------> +-----------------------+ +-----------------------+ | | 

23 | | Conv2D - typ. 3xY | | Conv2D - typ. 3xY | | | 

24 | | Kernel Size: K1xK1 |-->| Kernel Size: K2xK2 |-->| | 

25 | | Filters: N2 | | Filters: N2 | | | 

26 | +-----------------------+ +-----------------------+ | | 

27 | | | 

28 +----------> +-----------------------+ +-----------------------+ | | 

29 | | Conv2D - typ. 5xY | | Conv2D - typ. 5xY | | | 

30 | | Kernel Size: K1xK1 |-->| Kernel Size: K3xK3 |-->| | 

31 | | Filters: N3 | | Filters: N3 | | | 

32 | +-----------------------+ +-----------------------+ | | 

33 | | | 

34 +----------> +-----------------------+ +-----------------------+ | | 

35 | MaxPooling2D | | Conv2D | | | 

36 | Kernel Size: K4xK4 |-->| Kernel Size: K1xK1 |-->| | 

37 | Stride: S1xS1 | | Filters: N4 | +-------------+ --> Output Tensor 

38 +-----------------------+ +-----------------------+ 

39 """ 

40 

41 def __init__(self, kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]], 

42 filters: tuple[int, int, int, int], steps: tuple[int, int]) -> None: 

43 """ 

44 Class constructor. 

45 

46 Parameters: 

47 kernels (tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]]): 

48 Sizes of all kernels used within this block. 

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

50 steps (tuple[int, int]): Strides for the max pooling layer. 

51 """ 

52 

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

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

55 self.__conv_2d_3_kernel_size: tuple[int, int] = kernels[2] 

56 self.__max_pooling_2d_kernel_size: tuple[int, int] = kernels[3] 

57 self.__conv_2d_1_nr_of_filters: int = filters[0] 

58 self.__conv_2d_2_nr_of_filters: int = filters[1] 

59 self.__conv_2d_3_nr_of_filters: int = filters[2] 

60 self.__conv_2d_4_nr_of_filters: int = filters[3] 

61 self.__max_pooling_2d_step: tuple[int, int] = steps 

62 

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

64 """ 

65 Applies parallel convolutions with different kernel sizes and a max-pooling layer, 

66 followed by concatenation of the results. 

67 

68 Parameters: 

69 input_tensor (tf.Tensor): Input tensor to which the transformations should be applied. 

70 

71 Returns: 

72 tf.Tensor: Output tensor after the transformations have been applied. 

73 """ 

74 

75 # 1xY convolution 

76 x_1 = Conv2D(self.__conv_2d_1_nr_of_filters, self.__conv_2d_1_kernel_size, padding = 'same', 

77 activation = 'relu')(input_tensor) 

78 

79 # 3xY convolution 

80 x_2 = Conv2D(self.__conv_2d_2_nr_of_filters, self.__conv_2d_1_kernel_size, padding = 'same', 

81 activation = 'relu')(input_tensor) 

82 x_2 = Conv2D(self.__conv_2d_2_nr_of_filters, self.__conv_2d_2_kernel_size, padding = 'same', 

83 activation = 'relu')(x_2) 

84 

85 # 5xY convolution 

86 x_3 = Conv2D(self.__conv_2d_3_nr_of_filters, self.__conv_2d_1_kernel_size, padding = 'same', 

87 activation = 'relu')(input_tensor) 

88 x_3 = Conv2D(self.__conv_2d_3_nr_of_filters, self.__conv_2d_3_kernel_size, padding = 'same', 

89 activation = 'relu')(x_3) 

90 

91 # Pooling 

92 x_4 = MaxPooling2D(self.__max_pooling_2d_kernel_size, strides=self.__max_pooling_2d_step, 

93 padding = 'same')(input_tensor) 

94 x_4 = Conv2D(self.__conv_2d_4_nr_of_filters, self.__conv_2d_1_kernel_size, 

95 padding = 'same', activation = 'relu')(x_4) 

96 

97 output_tensor = Concatenate()([x_1, x_2, x_3, x_4]) 

98 

99 return output_tensor