1
- # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
1
+ # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
@@ -128,8 +128,6 @@ def __init__(self,
128
128
out_channels = ema_channels ,
129
129
kernel_size = 3 )
130
130
self .ema_mid_conv = nn .Conv2D (ema_channels , ema_channels , kernel_size = 1 )
131
- for param in self .ema_mid_conv .parameters ():
132
- param .stop_gradient = True
133
131
self .ema_out_conv = layers .ConvBNReLU (
134
132
in_channels = ema_channels , out_channels = ema_channels , kernel_size = 1 )
135
133
self .bottleneck = layers .ConvBNReLU (
@@ -184,8 +182,8 @@ def __init__(self, c, k, stage_num=3, momentum=0.1):
184
182
tmp_mu = self .create_parameter (
185
183
shape = [1 , c , k ],
186
184
default_initializer = paddle .nn .initializer .KaimingNormal (k ))
187
- self . mu = F .normalize (paddle .to_tensor (tmp_mu ), axis = 1 , p = 2 )
188
- self .register_buffer ('bases ' , self . mu )
185
+ mu = F .normalize (paddle .to_tensor (tmp_mu ), axis = 1 , p = 2 )
186
+ self .register_buffer ('mu ' , mu )
189
187
190
188
def forward (self , x ):
191
189
x_shape = paddle .shape (x )
@@ -207,9 +205,11 @@ def forward(self, x):
207
205
208
206
if self .training :
209
207
mu = paddle .mean (mu , 0 , keepdim = True )
210
- if paddle .distributed .get_world_size () > 1 :
211
- paddle .distributed .reduce (
212
- mu / paddle .distributed .get_world_size (), 0 )
213
208
mu = F .normalize (mu , axis = 1 , p = 2 )
214
- self .mu = self .mu * (1 - self .momentum ) + mu * self .momentum
209
+ mu = self .mu * (1 - self .momentum ) + mu * self .momentum
210
+ if paddle .distributed .get_world_size () > 1 :
211
+ mu = paddle .distributed .all_reduce (mu )
212
+ mu /= paddle .distributed .get_world_size ()
213
+ self .mu = mu
214
+
215
215
return x
0 commit comments