Coverage for source/model/model_building_blocks/xception_block.py: 100%
25 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-06 12:00 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-06 12:00 +0000
1# model/model_building_blocks/vgg16_block.py
3import tensorflow as tf
4from tensorflow.keras.layers import SeparableConv2D, Conv2D, MaxPooling2D, BatchNormalization, Activation, Add
6class XceptionBlock:
7 """
8 Class implementing an Xception block compatible with the TensorFlow API. This block implements
9 depthwise separable convolutions followed by max pooling and a residual connection, as seen in
10 the Xception architecture.
12 Diagram:
14 ::
16 Input Tensor --> +-----------------------+ +----------------------+ +--------------------+ +-----+
17 | | SeparableConv2D | | SeparableConv2D | | MaxPooling2D | | Add |
18 | | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 |-->| |
19 | | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | Stride: S1xS1 | | |
20 | +-----------------------+ +----------------------+ +--------------------+ | |
21 | | |
22 +----------> +-----------------------+ | |
23 | Conv2D | | |
24 | Filters: N3 | | |
25 | Kernel Size: K4xK4 |------------------------------------------------------>| |
26 | Stride: S2xS2 | | |
27 | | +-----+ --> Output Tensor
28 +-----------------------+
29 """
31 def __init__(self, kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]],
32 filters: tuple[int, int, int], steps: tuple[tuple[int, int], tuple[int, int]]) -> None:
33 """
34 Class constructor.
36 Parameters:
37 kernels (tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]]):
38 Sizes of all kernels used within this block.
39 filters (tuple[int, int, int]): Number of filters used in the convolutional layers.
40 steps (tuple[tuple[int, int], tuple[int, int]]): Strides for the max pooling and
41 convolutional layers.
42 """
44 self.__separable_conv_2d_1_kernel_size: tuple[int, int] = kernels[0]
45 self.__separable_conv_2d_2_kernel_size: tuple[int, int] = kernels[1]
46 self.__max_pooling_2d_kernel_size: tuple[int, int] = kernels[2]
47 self.__conv_2d_kernel_size: tuple[int, int] = kernels[3]
48 self.__separable_conv_2d_1_nr_of_filters: int = filters[0]
49 self.__separable_conv_2d_2_nr_of_filters: int = filters[1]
50 self.__conv_2d_nr_of_filters: int = filters[2]
51 self.__max_pooling_2d_step: tuple[int, int] = steps[0]
52 self.__conv_2d_step: tuple[int, int] = steps[1]
54 def __call__(self, input_tensor: tf.Tensor) -> tf.Tensor:
55 """
56 Applies depthwise separable convolutions with max pooling, and a residual connection to
57 the input tensor.
59 Parameters:
60 input_tensor (tf.Tensor): Input tensor to which the transformations should be applied.
62 Returns:
63 tf.Tensor: Output tensor after the transformations have been applied.
64 """
66 # Depthwise separable convolution
67 x_1 = SeparableConv2D(self.__separable_conv_2d_1_nr_of_filters,
68 self.__separable_conv_2d_1_kernel_size,
69 padding = 'same', use_bias = False)(input_tensor)
70 x_1 = BatchNormalization()(x_1)
71 x_1 = Activation('relu')(x_1)
72 x_1 = SeparableConv2D(self.__separable_conv_2d_2_nr_of_filters,
73 self.__separable_conv_2d_2_kernel_size,
74 padding = 'same', use_bias = False)(x_1)
75 x_1 = BatchNormalization()(x_1)
76 x_1 = Activation('relu')(x_1)
77 x_1 = MaxPooling2D(self.__max_pooling_2d_kernel_size, strides=self.__max_pooling_2d_step, padding = 'same')(x_1)
79 # Residual connection
80 x_2 = Conv2D(self.__conv_2d_nr_of_filters, self.__conv_2d_kernel_size, strides = self.__conv_2d_step,
81 padding = 'same', use_bias = False)(input_tensor)
82 x_2 = BatchNormalization()(x_2)
84 output_tensor = Add()([x_1, x_2])
86 return output_tensor