Conditions | 3 |
Total Lines | 75 |
Code Lines | 47 |
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 |
||
27 | def __init__( |
||
28 | self, |
||
29 | image_size: tuple, |
||
30 | out_channels: int, |
||
31 | num_channel_initial: int, |
||
32 | extract_levels: List[int], |
||
33 | out_kernel_initializer: str, |
||
34 | out_activation: str, |
||
35 | name: str = "GlobalNet", |
||
36 | **kwargs, |
||
37 | ): |
||
38 | """ |
||
39 | Image is encoded gradually, i from level 0 to E. |
||
40 | Then, a densely-connected layer outputs an affine |
||
41 | transformation. |
||
42 | |||
43 | :param image_size: tuple, such as (dim1, dim2, dim3) |
||
44 | :param out_channels: int, number of channels for the output |
||
45 | :param num_channel_initial: int, number of initial channels |
||
46 | :param extract_levels: list, which levels from net to extract |
||
47 | :param out_kernel_initializer: not used |
||
48 | :param out_activation: not used |
||
49 | :param name: name of the backbone. |
||
50 | :param kwargs: additional arguments. |
||
51 | """ |
||
52 | super().__init__( |
||
53 | image_size=image_size, |
||
54 | out_channels=out_channels, |
||
55 | num_channel_initial=num_channel_initial, |
||
56 | out_kernel_initializer=out_kernel_initializer, |
||
57 | out_activation=out_activation, |
||
58 | name=name, |
||
59 | **kwargs, |
||
60 | ) |
||
61 | |||
62 | # save parameters |
||
63 | assert out_channels == 3 |
||
64 | self._extract_levels = extract_levels |
||
65 | self._extract_max_level = max(self._extract_levels) # E |
||
66 | self.reference_grid = layer_util.get_reference_grid(image_size) |
||
67 | self.transform_initial = tf.constant_initializer( |
||
68 | value=list(np.eye(4, 3).reshape((-1))) |
||
69 | ) |
||
70 | # init layer variables |
||
71 | num_channels = [ |
||
72 | num_channel_initial * (2 ** level) |
||
73 | for level in range(self._extract_max_level + 1) |
||
74 | ] # level 0 to E |
||
75 | self._downsample_convs = [ |
||
76 | tf.keras.Sequential( |
||
77 | [ |
||
78 | layer.Conv3dBlock( |
||
79 | filters=num_channels[i], |
||
80 | kernel_size=7 if i == 0 else 3, |
||
81 | padding="same", |
||
82 | ), |
||
83 | layer.ResidualConv3dBlock( |
||
84 | filters=num_channels[i], |
||
85 | kernel_size=7 if i == 0 else 3, |
||
86 | padding="same", |
||
87 | ), |
||
88 | ] |
||
89 | ) |
||
90 | for i in range(self._extract_max_level) |
||
91 | ] # level 0 to E-1 |
||
92 | self._downsample_pools = [ |
||
93 | tfkl.MaxPool3D(pool_size=2, strides=2, padding="same") |
||
94 | for _ in range(self._extract_max_level) |
||
95 | ] # level 0 to E-1 |
||
96 | self._conv3d_block = layer.Conv3dBlock( |
||
97 | filters=num_channels[-1], kernel_size=3, padding="same" |
||
98 | ) # level E |
||
99 | self._flatten = tfkl.Flatten() |
||
100 | self._dense_layer = tfkl.Dense( |
||
101 | units=12, bias_initializer=self.transform_initial |
||
102 | ) |
||
133 |