basic convolution block Args: inputs: input to the conv layer conv_type: type of convolution. "conv" for Conv2D and "ds" for depthwise separable convolution filters: number of filters kernel_size: kernel size used in convolution strides: strides used
(inputs,
conv_type="conv",
filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding="same",
relu=True,
upsampling=False,
up_sample_size=2,
skip_layer=None
)
| 10 | import sys |
| 11 | |
| 12 | def conv_block(inputs, |
| 13 | conv_type="conv", |
| 14 | filters=64, |
| 15 | kernel_size=(3, 3), |
| 16 | strides=(1, 1), |
| 17 | padding="same", |
| 18 | relu=True, |
| 19 | upsampling=False, |
| 20 | up_sample_size=2, |
| 21 | skip_layer=None |
| 22 | ): |
| 23 | """ |
| 24 | basic convolution block |
| 25 | Args: |
| 26 | inputs: input to the conv layer |
| 27 | conv_type: type of convolution. "conv" for Conv2D and "ds" for depthwise separable convolution |
| 28 | filters: number of filters |
| 29 | kernel_size: kernel size used in convolution |
| 30 | strides: strides used in convolution |
| 31 | padding: padding used in convolution |
| 32 | relu: if relu is active or not. |
| 33 | upsampling: if need to upsample |
| 34 | skip_layer: if skip layer is added |
| 35 | Returns: |
| 36 | out: output of basic convolution block |
| 37 | """ |
| 38 | if conv_type == "ds": |
| 39 | out = tf.keras.layers.SeparableConv2D(filters=filters, |
| 40 | kernel_size=kernel_size, |
| 41 | padding=padding, |
| 42 | strides=strides)(inputs) |
| 43 | elif conv_type == "conv": |
| 44 | out = tf.keras.layers.Conv2D(filters=filters, |
| 45 | kernel_size=kernel_size, |
| 46 | padding=padding, |
| 47 | strides=strides)(inputs) |
| 48 | else: |
| 49 | sys.exit("Wrong choice of convolution type.") |
| 50 | |
| 51 | out = tf.keras.layers.BatchNormalization()(out) |
| 52 | |
| 53 | if relu: |
| 54 | out = tf.keras.activations.relu(out) |
| 55 | |
| 56 | if upsampling: |
| 57 | out = tf.keras.layers.UpSampling2D(size=(up_sample_size, up_sample_size), |
| 58 | data_format="channels_last")(out) |
| 59 | skip_layer = tf.keras.layers.Conv2D(filters=out.shape[3], kernel_size=(1, 1), padding='same', strides=(1, 1))(skip_layer) |
| 60 | out = tf.keras.layers.concatenate([out, skip_layer], axis=3) |
| 61 | if conv_type == "ds": |
| 62 | out = tf.keras.layers.SeparableConv2D(filters=filters, |
| 63 | kernel_size=kernel_size, |
| 64 | padding=padding, |
| 65 | strides=strides)(out) |
| 66 | elif conv_type == "conv": |
| 67 | out = tf.keras.layers.Conv2D(filters=filters, |
| 68 | kernel_size=kernel_size, |
| 69 | padding=padding, |
no outgoing calls
no test coverage detected