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

23 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-07-27 17:11 +0000

1# model/model_building_blocks/inception_block.py 

2 

3# global imports 

4import tensorflow as tf 

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

6 

7# local imports 

8 

9class InceptionBlock: 

10 """ 

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

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

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

14 

15 Diagram: 

16 

17 :: 

18 

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

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

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

22 | | Filters: N1 | | | 

23 | +-----------------------+ | | 

24 | | | 

25 +----------> +-----------------------+ +-----------------------+ | | 

26 | | Conv2D - typ. 3xY | | Conv2D - typ. 3xY | | | 

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

28 | | Filters: N2 | | Filters: N2 | | | 

29 | +-----------------------+ +-----------------------+ | | 

30 | | | 

31 +----------> +-----------------------+ +-----------------------+ | | 

32 | | Conv2D - typ. 5xY | | Conv2D - typ. 5xY | | | 

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

34 | | Filters: N3 | | Filters: N3 | | | 

35 | +-----------------------+ +-----------------------+ | | 

36 | | | 

37 +----------> +-----------------------+ +-----------------------+ | | 

38 | MaxPooling2D | | Conv2D | | | 

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

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

41 +-----------------------+ +-----------------------+ 

42 """ 

43 

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

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

46 """ 

47 Class constructor. 

48 

49 Parameters: 

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

51 Sizes of all kernels used within this block. 

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

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

54 """ 

55 

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

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

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

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

60 self.__conv_2d_1_nr_of_filters: int = filters[0] 

61 self.__conv_2d_2_nr_of_filters: int = filters[1] 

62 self.__conv_2d_3_nr_of_filters: int = filters[2] 

63 self.__conv_2d_4_nr_of_filters: int = filters[3] 

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

65 

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

67 """ 

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

69 followed by concatenation of the results. 

70 

71 Parameters: 

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

73 

74 Returns: 

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

76 """ 

77 

78 # 1xY convolution 

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

80 activation = 'relu')(input_tensor) 

81 

82 # 3xY convolution 

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

84 activation = 'relu')(input_tensor) 

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

86 activation = 'relu')(x_2) 

87 

88 # 5xY convolution 

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

90 activation = 'relu')(input_tensor) 

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

92 activation = 'relu')(x_3) 

93 

94 # Pooling 

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

96 padding = 'same')(input_tensor) 

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

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

99 

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

101 

102 return output_tensor