Skip to content

Commit 08f4e8d

Browse files
authored
Merge pull request #1081 from tiancheng2000/feature-layers-deconv-build
Feature: DeConv2d,3d build enabled
2 parents 0392dec + 9f3c7dc commit 08f4e8d

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

tensorlayer/layers/convolution/simplified_deconv.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ def __init__(
8282
self.b_init = b_init
8383
self.in_channels = in_channels
8484

85-
# Attention: To build, we need not only the in_channels!
86-
# if self.in_channels:
87-
# self.build(None)
88-
# self._built = True
85+
# Attention: To build, we need not only the in_channels! Solved.
86+
if self.in_channels is not None:
87+
self.build(None)
88+
self._built = True
8989

9090
logging.info(
9191
"DeConv2d {}: n_filters: {} strides: {} padding: {} act: {} dilation: {}".format(
@@ -132,10 +132,13 @@ def build(self, inputs_shape):
132132
# dtype=tf.float32,
133133
name=self.name,
134134
)
135-
if self.data_format == "channels_first":
136-
self.in_channels = inputs_shape[1]
135+
if inputs_shape is not None:
136+
self.in_channels = inputs_shape[1 if self.data_format == "channels_first" else -1]
137+
elif self.in_channels is not None:
138+
inputs_shape = [1, self.in_channels, 1, 1
139+
] if self.data_format == "channels_first" else [1, 1, 1, self.in_channels]
137140
else:
138-
self.in_channels = inputs_shape[-1]
141+
raise ValueError("Either inputs_shape or in_channels must be specified for build.")
139142
_out = self.layer(
140143
tf.convert_to_tensor(np.random.uniform(size=inputs_shape), dtype=np.float32)
141144
) #np.random.uniform([1] + list(inputs_shape))) # initialize weights
@@ -206,12 +209,12 @@ def __init__(
206209
self.data_format = data_format
207210
self.W_init = W_init
208211
self.b_init = b_init
209-
self.in_channels = in_channels,
212+
self.in_channels = in_channels
210213

211-
# Attention: To build, we need not only the in_channels!
212-
# if self.in_channels:
213-
# self.build(None)
214-
# self._built = True
214+
# Attention: To build, we need not only the in_channels! Solved.
215+
if self.in_channels is not None:
216+
self.build(None)
217+
self._built = True
215218

216219
logging.info(
217220
"DeConv3d %s: n_filters: %s strides: %s pad: %s act: %s" % (
@@ -252,16 +255,17 @@ def build(self, inputs_shape):
252255
bias_initializer=self.b_init,
253256
name=self.name,
254257
)
255-
if self.data_format == "channels_first":
256-
self.in_channels = inputs_shape[1]
258+
if inputs_shape is not None:
259+
self.in_channels = inputs_shape[1 if self.data_format == "channels_first" else -1]
260+
elif self.in_channels is not None:
261+
inputs_shape = [1, self.in_channels, 1, 1, 1
262+
] if self.data_format == "channels_first" else [1, 1, 1, 1, self.in_channels]
257263
else:
258-
self.in_channels = inputs_shape[-1]
259-
264+
raise ValueError("Either inputs_shape or in_channels must be specified for build.")
260265
_out = self.layer(
261266
tf.convert_to_tensor(np.random.uniform(size=inputs_shape), dtype=np.float32)
262267
) #self.layer(np.random.uniform([1] + list(inputs_shape))) # initialize weights
263268
outputs_shape = _out.shape
264-
# self._add_weights(self.layer.weights)
265269
self._trainable_weights = self.layer.weights
266270

267271
def forward(self, inputs):

tensorlayer/layers/normalization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ def forward(self, inputs):
291291
if self.axes is None:
292292
self.axes = [i for i in range(len(inputs.shape)) if i != self.channel_axis]
293293

294-
mean, var = tf.nn.moments(inputs, self.axes, keepdims=False)
295294
if self.is_train:
296295
# update moving_mean and moving_var
296+
mean, var = tf.nn.moments(inputs, self.axes, keepdims=False)
297297
self.moving_mean = moving_averages.assign_moving_average(
298298
self.moving_mean, mean, self.decay, zero_debias=False
299299
)

0 commit comments

Comments
 (0)