Skip to content

Commit

Permalink
Fix the problem that emanet cannot train with a single card.
Browse files Browse the repository at this point in the history
  • Loading branch information
haoyuying authored May 8, 2021
1 parent 956cedc commit 10428e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions configs/emanet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Expectation-Maximization Attention Networks for Semantic Segmentation. ICCV 2019

| Model | Backbone | Resolution | Training Iters | mIoU | mIoU (flip) | mIoU (ms+flip) |Links |
|-|-|-|-|-|-|-|-|
|EMANet|ResNet50_OS8|1024x512|80000|77.64%|77.98%|78.23%|[model](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet50_os8_cityscapes_1024x512_80k/model.pdparams) \| [log](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet50_os8_cityscapes_1024x512_80k/train.log) \| [vdl](https://paddlepaddle.org.cn/paddle/visualdl/service/app?id=3e053a214d60822d6e65445b8614d052)|
|EMANet|ResNet101_OS8|1024x512|80000|79.41%|79.83%|80.33%|[model](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet101_os8_cityscapes_1024x512_80k/model.pdparams) \| [log](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet101_os8_cityscapes_1024x512_80k/train.log) \| [vdl](https://paddlepaddle.org.cn/paddle/visualdl/service/app?id=87be6389cdada711f5c6ada21d9ef6cd)|
|EMANet|ResNet50_OS8|1024x512|80000|79.05%|79.34%|79.69%|[model](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet50_os8_cityscapes_1024x512_80k/model.pdparams) \| [log](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet50_os8_cityscapes_1024x512_80k/train.log) \| [vdl](https://paddlepaddle.org.cn/paddle/visualdl/service/app?id=0a05a0c4cd7d785b9707bdc59f55f585)|
|EMANet|ResNet101_OS8|1024x512|80000|80.00%|80.23%|80.53%|[model](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet101_os8_cityscapes_1024x512_80k/model.pdparams) \| [log](https://bj.bcebos.com/paddleseg/dygraph/cityscapes/emanet_resnet101_os8_cityscapes_1024x512_80k/train.log) \| [vdl](https://paddlepaddle.org.cn/paddle/visualdl/service/app?id=ee6926322b8e292ce23ce62ecdaa3439)|

### Pascal VOC 2012 + Aug

Expand Down
18 changes: 9 additions & 9 deletions paddleseg/models/emanet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -128,8 +128,6 @@ def __init__(self,
out_channels=ema_channels,
kernel_size=3)
self.ema_mid_conv = nn.Conv2D(ema_channels, ema_channels, kernel_size=1)
for param in self.ema_mid_conv.parameters():
param.stop_gradient = True
self.ema_out_conv = layers.ConvBNReLU(
in_channels=ema_channels, out_channels=ema_channels, kernel_size=1)
self.bottleneck = layers.ConvBNReLU(
Expand Down Expand Up @@ -184,8 +182,8 @@ def __init__(self, c, k, stage_num=3, momentum=0.1):
tmp_mu = self.create_parameter(
shape=[1, c, k],
default_initializer=paddle.nn.initializer.KaimingNormal(k))
self.mu = F.normalize(paddle.to_tensor(tmp_mu), axis=1, p=2)
self.register_buffer('bases', self.mu)
mu = F.normalize(paddle.to_tensor(tmp_mu), axis=1, p=2)
self.register_buffer('mu', mu)

def forward(self, x):
x_shape = paddle.shape(x)
Expand All @@ -207,9 +205,11 @@ def forward(self, x):

if self.training:
mu = paddle.mean(mu, 0, keepdim=True)
if paddle.distributed.get_world_size() > 1:
paddle.distributed.reduce(
mu / paddle.distributed.get_world_size(), 0)
mu = F.normalize(mu, axis=1, p=2)
self.mu = self.mu * (1 - self.momentum) + mu * self.momentum
mu = self.mu * (1 - self.momentum) + mu * self.momentum
if paddle.distributed.get_world_size() > 1:
mu = paddle.distributed.all_reduce(mu)
mu /= paddle.distributed.get_world_size()
self.mu = mu

return x

0 comments on commit 10428e4

Please sign in to comment.