Conditions | 5 |
Total Lines | 133 |
Code Lines | 90 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # coding=utf-8 |
||
33 | def __init__( |
||
34 | self, |
||
35 | image_size: tuple, |
||
36 | out_channels: int, |
||
37 | num_channel_initial: int, |
||
38 | extract_levels: List[int], |
||
39 | out_kernel_initializer: str, |
||
40 | out_activation: str, |
||
41 | use_additive_upsampling: bool = True, |
||
42 | name: str = "LocalNet", |
||
43 | **kwargs, |
||
44 | ): |
||
45 | """ |
||
46 | Image is encoded gradually, i from level 0 to E, |
||
47 | then it is decoded gradually, j from level E to D. |
||
48 | Some of the decoded levels are used for generating extractions. |
||
49 | |||
50 | So, extract_levels are between [0, E] with E = max(extract_levels), |
||
51 | and D = min(extract_levels). |
||
52 | |||
53 | :param image_size: such as (dim1, dim2, dim3) |
||
54 | :param out_channels: number of channels for the extractions |
||
55 | :param num_channel_initial: number of initial channels. |
||
56 | :param extract_levels: number of extraction levels. |
||
57 | :param out_kernel_initializer: initializer to use for kernels. |
||
58 | :param out_activation: activation to use at end layer. |
||
59 | :param use_additive_upsampling: whether use additive up-sampling. |
||
60 | :param name: name of the backbone. |
||
61 | :param kwargs: additional arguments. |
||
62 | """ |
||
63 | super().__init__( |
||
64 | image_size=image_size, |
||
65 | out_channels=out_channels, |
||
66 | num_channel_initial=num_channel_initial, |
||
67 | out_kernel_initializer=out_kernel_initializer, |
||
68 | out_activation=out_activation, |
||
69 | name=name, |
||
70 | **kwargs, |
||
71 | ) |
||
72 | |||
73 | # save parameters |
||
74 | self._extract_levels = extract_levels |
||
75 | self._use_additive_upsampling = use_additive_upsampling |
||
76 | self._extract_max_level = max(self._extract_levels) # E |
||
77 | self._extract_min_level = min(self._extract_levels) # D |
||
78 | |||
79 | # init layer variables |
||
80 | num_channels = [ |
||
81 | num_channel_initial * (2 ** level) |
||
82 | for level in range(self._extract_max_level + 1) |
||
83 | ] # level 0 to E |
||
84 | kernel_sizes = [ |
||
85 | 7 if level == 0 else 3 for level in range(self._extract_max_level + 1) |
||
86 | ] |
||
87 | self._downsample_convs = [] |
||
88 | self._downsample_pools = [] |
||
89 | tensor_shape = image_size |
||
90 | self._tensor_shapes = [tensor_shape] |
||
91 | for i in range(self._extract_max_level): |
||
92 | downsample_conv = tf.keras.Sequential( |
||
93 | [ |
||
94 | layer.Conv3dBlock( |
||
95 | filters=num_channels[i], |
||
96 | kernel_size=kernel_sizes[i], |
||
97 | padding="same", |
||
98 | ), |
||
99 | layer.ResidualConv3dBlock( |
||
100 | filters=num_channels[i], |
||
101 | kernel_size=kernel_sizes[i], |
||
102 | padding="same", |
||
103 | ), |
||
104 | ] |
||
105 | ) |
||
106 | downsample_pool = tfkl.MaxPool3D(pool_size=2, strides=2, padding="same") |
||
107 | tensor_shape = tuple((x + 1) // 2 for x in tensor_shape) |
||
108 | self._downsample_convs.append(downsample_conv) |
||
109 | self._downsample_pools.append(downsample_pool) |
||
110 | self._tensor_shapes.append(tensor_shape) |
||
111 | |||
112 | self._conv3d_block = layer.Conv3dBlock( |
||
113 | filters=num_channels[-1], kernel_size=3, padding="same" |
||
114 | ) # level E |
||
115 | |||
116 | self._upsample_deconvs = [] |
||
117 | self._resizes = [] |
||
118 | self._upsample_convs = [] |
||
119 | for level in range( |
||
120 | self._extract_max_level - 1, self._extract_min_level - 1, -1 |
||
121 | ): # level D to E-1 |
||
122 | padding = deepreg.model.layer_util.deconv_output_padding( |
||
123 | input_shape=self._tensor_shapes[level + 1], |
||
124 | output_shape=self._tensor_shapes[level], |
||
125 | kernel_size=kernel_sizes[level], |
||
126 | stride=2, |
||
127 | padding="same", |
||
128 | ) |
||
129 | upsample_deconv = layer.Deconv3dBlock( |
||
130 | filters=num_channels[level], |
||
131 | output_padding=padding, |
||
132 | kernel_size=3, |
||
133 | strides=2, |
||
134 | padding="same", |
||
135 | ) |
||
136 | upsample_conv = tf.keras.Sequential( |
||
137 | [ |
||
138 | layer.Conv3dBlock( |
||
139 | filters=num_channels[level], kernel_size=3, padding="same" |
||
140 | ), |
||
141 | layer.ResidualConv3dBlock( |
||
142 | filters=num_channels[level], kernel_size=3, padding="same" |
||
143 | ), |
||
144 | ] |
||
145 | ) |
||
146 | self._upsample_deconvs.append(upsample_deconv) |
||
147 | self._upsample_convs.append(upsample_conv) |
||
148 | if self._use_additive_upsampling: |
||
149 | resize = layer.Resize3d(shape=self._tensor_shapes[level]) |
||
150 | self._resizes.append(resize) |
||
151 | self._extract_layers = [ |
||
152 | tf.keras.Sequential( |
||
153 | [ |
||
154 | tfkl.Conv3D( |
||
155 | filters=out_channels, |
||
156 | kernel_size=3, |
||
157 | strides=1, |
||
158 | padding="same", |
||
159 | kernel_initializer=out_kernel_initializer, |
||
160 | activation=out_activation, |
||
161 | ), |
||
162 | layer.Resize3d(shape=image_size), |
||
163 | ] |
||
164 | ) |
||
165 | for _ in self._extract_levels |
||
166 | ] |
||
217 |