Skip to content

Commit 73083fd

Browse files
committed
Code formatting
Code formatting
1 parent e18a62b commit 73083fd

File tree

3 files changed

+176
-129
lines changed

3 files changed

+176
-129
lines changed

tensorlayer/layers/convolution/oct_conv.py

+77-68
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22
# -*- coding: utf-8 -*-
33

44
import tensorflow as tf
5-
65
import tensorlayer as tl
76
from tensorlayer import logging
8-
from tensorlayer.decorators import deprecated_alias
97
from tensorlayer.layers.core import Layer
108

11-
# from tensorlayer.layers.core import LayersConfig
12-
13-
14-
159
__all__ = [
1610
'OctConv2dIn',
1711
'OctConv2d',
@@ -21,9 +15,12 @@
2115
'OctConv2dConcat',
2216
]
2317

18+
2419
class OctConv2dIn(Layer):
2520
"""
26-
The :class:`OctConv2dIn` class is a preprocessing layer for 2D image [batch, height, width, channel], see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
21+
The :class:`OctConv2dIn` class is a preprocessing layer for 2D image
22+
[batch, height, width, channel], see `Drop an Octave: Reducing Spatial Redundancy in
23+
Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
2724
2825
Parameters
2926
----------
@@ -68,19 +65,22 @@ def __repr__(self):
6865
s += ')'
6966
return s.format(classname=self.__class__.__name__, **self.__dict__)
7067

71-
def build(self, inputs):
68+
def build(self):
7269
pass
7370

7471
def forward(self, inputs):
75-
high_out=tf.identity(inputs,name=(self.name+'_high_out'))
76-
low_out = tf.nn.avg_pool2d(inputs, (2,2), strides=(2,2),padding='SAME',name=self.name+'_low_out')
77-
outputs=[high_out,low_out]
72+
high_out = tf.identity(inputs, name=(self.name + '_high_out'))
73+
low_out = tf.nn.avg_pool2d(inputs, (2, 2), strides=(2, 2),
74+
padding='SAME', name=self.name + '_low_out')
75+
outputs = [high_out, low_out]
7876
return outputs
7977

8078

8179
class OctConv2d(Layer):
8280
"""
83-
The :class:`OctConv2d` class is a 2D CNN layer for OctConv2d layer output, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__. Use this layer to process tensor list.
81+
The :class:`OctConv2d` class is a 2D CNN layer for OctConv2d layer output, see `Drop an Octave:
82+
Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution
83+
<https://arxiv.org/abs/1904.05049>`__. Use this layer to process tensor list.
8484
8585
Parameters
8686
----------
@@ -104,7 +104,8 @@ class OctConv2d(Layer):
104104
105105
Notes
106106
-----
107-
- The input should be a list with shape [high_res_tensor , low_res_tensor], the height and width of high_res should be twice of the low_res_tensor.
107+
- The input should be a list with shape [high_res_tensor , low_res_tensor],
108+
the height and width of high_res should be twice of the low_res_tensor.
108109
- If you do not which tensor is larger, use OctConv2dConcat layer.
109110
- The output will be a list which contains 2 tensor.
110111
- You should not use the output directly.
@@ -128,12 +129,12 @@ def __init__(
128129
filter=32,
129130
alpha=0.5,
130131
filter_size=(2, 2),
131-
strides=(1,1),
132+
strides=(1, 1),
132133
W_init=tl.initializers.truncated_normal(stddev=0.02),
133134
b_init=tl.initializers.constant(value=0.0),
134135
act=None,
135136
in_channels=None,
136-
name=None # 'cnn2d_layer',
137+
name=None
137138
):
138139
super().__init__(name)
139140
self.filter = filter
@@ -156,7 +157,6 @@ def __init__(
156157
self.build(None)
157158
self._built = True
158159

159-
160160
logging.info(
161161
"OctConv2d %s: filter_size: %s strides: %s high_out: %s low_out: %s act: %s" % (
162162
self.name, str(filter_size), str(strides), str(self.high_out), str(self.low_out),
@@ -166,8 +166,8 @@ def __init__(
166166

167167
def __repr__(self):
168168
actstr = self.act.__name__ if self.act is not None else 'No Activation'
169-
s = ('{classname}(in_channels={in_channels}, out_channels={filter} kernel_size={filter_size}'
170-
', strides={strides}')
169+
s = ('{classname}(in_channels={in_channels}, out_channels={filter} ,'
170+
'kernel_size={filter_size}, strides={strides}')
171171
if self.b_init is None:
172172
s += ', bias=False'
173173
s += (', ' + actstr)
@@ -179,11 +179,11 @@ def __repr__(self):
179179

180180
def build(self, inputs_shape):
181181
if not self.in_channels:
182-
high_ch=inputs_shape[0][-1]
183-
low_ch=inputs_shape[1][-1]
182+
high_ch = inputs_shape[0][-1]
183+
low_ch = inputs_shape[1][-1]
184184
else:
185-
high_ch=self.in_channels[0]
186-
low_ch=self.in_channels[1]
185+
high_ch = self.in_channels[0]
186+
low_ch = self.in_channels[1]
187187
self.high_high_filter_shape = (
188188
self.filter_size[0], self.filter_size[1], high_ch, self.high_out
189189
)
@@ -218,36 +218,38 @@ def build(self, inputs_shape):
218218

219219
def forward(self, inputs):
220220
high_input = inputs[0]
221-
low_input=inputs[1]
221+
low_input = inputs[1]
222222
high_to_high = tf.nn.conv2d(high_input, self.high_high__W,
223223
strides=self.strides, padding="SAME")
224-
high_to_low =tf.nn.avg_pool2d(high_input, (2,2), strides=(2,2),padding='SAME')
225-
high_to_low=tf.nn.conv2d(high_to_low, self.high_low__W,
226-
strides=self.strides, padding="SAME")
224+
high_to_low = tf.nn.avg_pool2d(high_input, (2, 2), strides=(2, 2), padding='SAME')
225+
high_to_low = tf.nn.conv2d(high_to_low, self.high_low__W,
226+
strides=self.strides, padding="SAME")
227227
low_to_low = tf.nn.conv2d(low_input, self.low_low_W,
228-
strides=self.strides, padding="SAME")
228+
strides=self.strides, padding="SAME")
229229
low_to_high = tf.nn.conv2d(low_input, self.low_high_W,
230-
strides=self.strides, padding="SAME")
231-
low_to_high=tf.keras.layers.UpSampling2D(size=(2,2), interpolation='nearest')(low_to_high)
232-
high_out=high_to_high+low_to_high
233-
low_out=low_to_low+high_to_low
230+
strides=self.strides, padding="SAME")
231+
low_to_high = tf.keras.layers.UpSampling2D(size=(2, 2),
232+
interpolation='nearest')(low_to_high)
233+
high_out = high_to_high + low_to_high
234+
low_out = low_to_low + high_to_low
234235
if self.b_init:
235236
high_out = tf.nn.bias_add(high_out, self.high_b, data_format="NHWC")
236237
low_out = tf.nn.bias_add(low_out, self.low_b, data_format="NHWC")
237238
if self.act:
238-
high_out = self.act(high_out,name=self.name+'_high_out')
239-
low_out= self.act(low_out,name=self.name+'_low_out')
239+
high_out = self.act(high_out, name=self.name + '_high_out')
240+
low_out = self.act(low_out, name=self.name + '_low_out')
240241
else:
241-
high_out=tf.identity(high_out,name=self.name+'_high_out')
242-
low_out=tf.identity(low_out,name=self.name+'_low_out')
243-
outputs=[high_out,low_out]
242+
high_out = tf.identity(high_out, name=self.name + '_high_out')
243+
low_out = tf.identity(low_out, name=self.name + '_low_out')
244+
outputs = [high_out, low_out]
244245
return outputs
245246

246247

247-
248248
class OctConv2dOut(Layer):
249249
"""
250-
The :class:`OctConv2dOut` class is a 2D CNN layer for OctConv2d layer output to get only a tensor, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__. Use this layer after other Octconv layers and get a normal tensor output.
250+
The :class:`OctConv2dOut` class is a 2D CNN layer for OctConv2d layer output to get
251+
only a tensor, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural
252+
Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
251253
252254
Parameters
253255
----------
@@ -292,7 +294,7 @@ def __init__(
292294
self,
293295
n_filter=32,
294296
filter_size=(2, 2),
295-
strides=(1,1),
297+
strides=(1, 1),
296298
W_init=tl.initializers.truncated_normal(stddev=0.02),
297299
b_init=tl.initializers.constant(value=0.0),
298300
act=None,
@@ -322,8 +324,8 @@ def __init__(
322324

323325
def __repr__(self):
324326
actstr = self.act.__name__ if self.act is not None else 'No Activation'
325-
s = ('{classname}(in_channels={in_channels}, out_channels={low_out}, kernel_size={filter_size}'
326-
', strides={strides}')
327+
s = ('{classname}(in_channels={in_channels}, out_channels={low_out},'
328+
' kernel_size={filter_size}, strides={strides}')
327329
if self.b_init is None:
328330
s += ', bias=False'
329331
s += (', ' + actstr)
@@ -334,11 +336,11 @@ def __repr__(self):
334336

335337
def build(self, inputs_shape):
336338
if not self.in_channels:
337-
high_ch=inputs_shape[0][-1]
338-
low_ch=inputs_shape[1][-1]
339+
high_ch = inputs_shape[0][-1]
340+
low_ch = inputs_shape[1][-1]
339341
else:
340-
high_ch=self.in_channels[0]
341-
low_ch=self.in_channels[1]
342+
high_ch = self.in_channels[0]
343+
low_ch = self.in_channels[1]
342344
self.high_low_filter_shape = (
343345
self.filter_size[0], self.filter_size[1], high_ch, self.high_out
344346
)
@@ -358,27 +360,27 @@ def build(self, inputs_shape):
358360

359361
def forward(self, inputs):
360362
high_input = inputs[0]
361-
low_input=inputs[1]
362-
high_to_low =tf.nn.avg_pool2d(high_input, (2,2), strides=(2,2),padding='SAME')
363-
high_to_low=tf.nn.conv2d(high_to_low, self.high_low__W,
364-
strides=self.strides, padding="SAME")
363+
low_input = inputs[1]
364+
high_to_low = tf.nn.avg_pool2d(high_input, (2, 2), strides=(2, 2), padding='SAME')
365+
high_to_low = tf.nn.conv2d(high_to_low, self.high_low__W,
366+
strides=self.strides, padding="SAME")
365367
low_to_low = tf.nn.conv2d(low_input, self.low_low_W,
366-
strides=self.strides, padding="SAME")
367-
low_out=low_to_low+high_to_low
368+
strides=self.strides, padding="SAME")
369+
low_out = low_to_low + high_to_low
368370
if self.b_init:
369371
low_out = tf.nn.bias_add(low_out, self.low_b, data_format="NHWC")
370372
if self.act:
371-
outputs= self.act(low_out,name=self.name+'_low_out')
373+
outputs = self.act(low_out, name=self.name + '_low_out')
372374
else:
373-
outputs=tf.identity(low_out,name=self.name+'_low_out')
375+
outputs = tf.identity(low_out, name=self.name + '_low_out')
374376
return outputs
375377

376378

377-
378-
379379
class OctConv2dHighOut(Layer):
380380
"""
381-
The :class:`OctConv2dHighOut` class is a slice layer for Octconv tensor list, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
381+
The :class:`OctConv2dHighOut` class is a slice layer for Octconv tensor list,
382+
see `Drop an Octave: Reducing Spatial Redundancy in Convolutional
383+
Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
382384
383385
Parameters
384386
----------
@@ -388,7 +390,8 @@ class OctConv2dHighOut(Layer):
388390
Notes
389391
-----
390392
- Use this layer to get high resolution tensor.
391-
- If you want to do some customized normalization ops, use this layer with OctConv2dLowOut and OctConv2dConcat layers to implement your idea.
393+
- If you want to do some customized normalization ops, use this layer with
394+
OctConv2dLowOut and OctConv2dConcat layers to implement your idea.
392395
393396
Examples
394397
--------
@@ -426,17 +429,19 @@ def __repr__(self):
426429
s += ')'
427430
return s.format(classname=self.__class__.__name__, **self.__dict__)
428431

429-
def build(self, inputs):
432+
def build(self):
430433
pass
431434

432435
def forward(self, inputs):
433-
outputs=tf.identity(inputs[0],self.name)
436+
outputs = tf.identity(inputs[0], self.name)
434437
return outputs
435438

436439

437-
class OctConv2dLowOut (Layer):
440+
class OctConv2dLowOut(Layer):
438441
"""
439-
The :class:`OctConv2dLowOut` class is a slice layer for Octconv tensor list, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
442+
The :class:`OctConv2dLowOut` class is a slice layer for Octconv tensor list, see
443+
`Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks
444+
with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
440445
441446
Parameters
442447
----------
@@ -446,7 +451,8 @@ class OctConv2dLowOut (Layer):
446451
Notes
447452
-----
448453
- Use this layer to get low resolution tensor.
449-
- If you want to do some customized normalization ops, use this layer with OctConv2dHighOut and OctConv2dConcat layers to implement your idea.
454+
- If you want to do some customized normalization ops, use this layer with
455+
OctConv2dHighOut and OctConv2dConcat layers to implement your idea.
450456
451457
Examples
452458
--------
@@ -485,16 +491,19 @@ def __repr__(self):
485491
s += ')'
486492
return s.format(classname=self.__class__.__name__, **self.__dict__)
487493

488-
def build(self, inputs):
494+
def build(self):
489495
pass
490496

491497
def forward(self, inputs):
492-
outputs=tf.identity(inputs[1],self.name)
498+
outputs = tf.identity(inputs[1], self.name)
493499
return outputs
494500

501+
495502
class OctConv2dConcat(Layer):
496503
"""
497-
The :class:`OctConv2dConcat` class is a concat layer for two 2D image batches, see `Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
504+
The :class:`OctConv2dConcat` class is a concat layer for two 2D image batches, see
505+
`Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks
506+
with Octave Convolution <https://arxiv.org/abs/1904.05049>`__.
498507
499508
Parameters
500509
----------
@@ -544,12 +553,12 @@ def __repr__(self):
544553
s += ')'
545554
return s.format(classname=self.__class__.__name__, **self.__dict__)
546555

547-
def build(self, inputs):
556+
def build(self):
548557
pass
549558

550559
def forward(self, inputs):
551-
if inputs[0].shape[1]>inputs[1].shape[1]:
552-
outputs=[inputs[0],inputs[1]]
560+
if inputs[0].shape[1] > inputs[1].shape[1]:
561+
outputs = [inputs[0], inputs[1]]
553562
else:
554563
outputs = [inputs[1], inputs[0]]
555564
return outputs

0 commit comments

Comments
 (0)