Coverage for source/model/model_building_blocks/xception_block.py: 100%
25 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +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 .. code-block:: text
15 Input Tensor --> +-----------------------+ +----------------------+ +--------------------+ +-----+
16 | | SeparableConv2D | | SeparableConv2D | | MaxPooling2D | | Add |
17 | | Filters: N1 |-->| Filters: N2 |-->| Pool Size: K3xK3 |-->| |
18 | | Kernel Size: K1xK1 | | Kernel Size: K2xK2 | | Stride: S1xS1 | | |
19 | +-----------------------+ +----------------------+ +--------------------+ | |
20 | | |
21 +----------> +-----------------------+ | |
22 | Conv2D | | |
23 | Filters: N3 | | |
24 | Kernel Size: K4xK4 |------------------------------------------------------>| |
25 | Stride: S2xS2 | | |
26 | | +-----+ --> Output Tensor
27 +-----------------------+
28 """
30 def __init__(self, kernels: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]],
31 filters: tuple[int, int, int], steps: tuple[tuple[int, int], tuple[int, int]]) -> None:
32 """
33 Class constructor.
35 Parameters:
36 kernels (tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]]):
37 Sizes of all kernels used within this block.
38 filters (tuple[int, int, int]): Number of filters used in the convolutional layers.
39 steps (tuple[tuple[int, int], tuple[int, int]]): Strides for the max pooling and
40 convolutional layers.
41 """
43 self.__separable_conv_2d_1_kernel_size: tuple[int, int] = kernels[0]
44 self.__separable_conv_2d_2_kernel_size: tuple[int, int] = kernels[1]
45 self.__max_pooling_2d_kernel_size: tuple[int, int] = kernels[2]
46 self.__conv_2d_kernel_size: tuple[int, int] = kernels[3]
47 self.__separable_conv_2d_1_nr_of_filters: int = filters[0]
48 self.__separable_conv_2d_2_nr_of_filters: int = filters[1]
49 self.__conv_2d_nr_of_filters: int = filters[2]
50 self.__max_pooling_2d_step: tuple[int, int] = steps[0]
51 self.__conv_2d_step: tuple[int, int] = steps[1]
53 def __call__(self, input_tensor: tf.Tensor) -> tf.Tensor:
54 """
55 Applies depthwise separable convolutions with max pooling, and a residual connection to
56 the input tensor.
58 Parameters:
59 input_tensor (tf.Tensor): Input tensor to which the transformations should be applied.
61 Returns:
62 tf.Tensor: Output tensor after the transformations have been applied.
63 """
65 # Depthwise separable convolution
66 x_1 = SeparableConv2D(self.__separable_conv_2d_1_nr_of_filters,
67 self.__separable_conv_2d_1_kernel_size,
68 padding = 'same', use_bias = False)(input_tensor)
69 x_1 = BatchNormalization()(x_1)
70 x_1 = Activation('relu')(x_1)
71 x_1 = SeparableConv2D(self.__separable_conv_2d_2_nr_of_filters,
72 self.__separable_conv_2d_2_kernel_size,
73 padding = 'same', use_bias = False)(x_1)
74 x_1 = BatchNormalization()(x_1)
75 x_1 = Activation('relu')(x_1)
76 x_1 = MaxPooling2D(self.__max_pooling_2d_kernel_size, strides=self.__max_pooling_2d_step, padding = 'same')(x_1)
78 # Residual connection
79 x_2 = Conv2D(self.__conv_2d_nr_of_filters, self.__conv_2d_kernel_size, strides = self.__conv_2d_step,
80 padding = 'same', use_bias = False)(input_tensor)
81 x_2 = BatchNormalization()(x_2)
83 output_tensor = Add()([x_1, x_2])
85 return output_tensor