未验证 提交 ca0448fd 编写于 作者: W wuzewu 提交者: GitHub

update api to 2.0-beta

......@@ -7,3 +7,6 @@ model:
num_classes: 19
backbone_channels: [270]
backbone_pretrained: pretrained_model/hrnet_w18_imagenet
optimizer:
weight_decay: 0.0005
_base_: '../_base_/cityscapes.yml'
batch_size: 2
iters: 40000
train_dataset:
type: Cityscapes
dataset_root: data/cityscapes
transforms:
- type: ResizeStepScaling
min_scale_factor: 0.5
max_scale_factor: 2.0
scale_step_size: 0.25
- type: RandomPaddingCrop
crop_size: [1024, 512]
- type: RandomHorizontalFlip
- type: Normalize
mode: train
val_dataset:
type: Cityscapes
dataset_root: data/cityscapes
transforms:
- type: Normalize
mode: val
model:
type: OCRNet
backbone:
type: HRNet_W18
backbone_pretrianed: None
num_classes: 19
in_channels: 270
backbone_channels: [270]
backbone_pretrained: pretrained_model/hrnet_w18_imagenet
model_pretrained: None
optimizer:
type: sgd
learning_rate:
value: 0.01
decay:
type: poly
power: 0.9
loss:
type: CrossEntropy
......@@ -15,11 +15,10 @@
import os
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.parallel import ParallelEnv
from paddle.fluid.io import DataLoader
# from paddle.incubate.hapi.distributed import DistributedBatchSampler
from paddle.distributed import ParallelEnv
from paddle.distributed import init_parallel_env
from paddle.io import DistributedBatchSampler
from paddle.io import DataLoader
import paddle.nn.functional as F
import paddleseg.utils.logger as logger
......@@ -79,11 +78,14 @@ def train(model,
os.makedirs(save_dir)
if nranks > 1:
strategy = fluid.dygraph.prepare_context()
ddp_model = fluid.dygraph.DataParallel(model, strategy)
# Initialize parallel training environment.
init_parallel_env()
strategy = paddle.distributed.prepare_context()
ddp_model = paddle.DataParallel(model, strategy)
batch_sampler = DistributedBatchSampler(
train_dataset, batch_size=batch_size, shuffle=True, drop_last=True)
loader = DataLoader(
train_dataset,
batch_sampler=batch_sampler,
......@@ -117,7 +119,6 @@ def train(model,
if nranks > 1:
logits = ddp_model(images)
loss = loss_computation(logits, labels, losses)
# loss = ddp_model(images, labels)
# apply_collective_grads sum grads over multiple gpus.
loss = ddp_model.scale_loss(loss)
loss.backward()
......@@ -127,10 +128,17 @@ def train(model,
loss = loss_computation(logits, labels, losses)
# loss = model(images, labels)
loss.backward()
optimizer.minimize(loss)
# optimizer.minimize(loss)
optimizer.step()
if isinstance(optimizer._learning_rate,
paddle.optimizer._LRScheduler):
optimizer._learning_rate.step()
model.clear_gradients()
# Sum loss over all ranks
if nranks > 1:
paddle.distributed.all_reduce(loss)
avg_loss += loss.numpy()[0]
lr = optimizer.current_step_lr()
lr = optimizer.get_lr()
train_batch_cost += timer.elapsed_time()
if (iter) % log_iters == 0 and ParallelEnv().local_rank == 0:
avg_loss /= log_iters
......@@ -143,10 +151,10 @@ def train(model,
logger.info(
"[TRAIN] epoch={}, iter={}/{}, loss={:.4f}, lr={:.6f}, batch_cost={:.4f}, reader_cost={:.4f} | ETA {}"
.format((iter - 1) // iters_per_epoch + 1, iter, iters,
avg_loss * nranks, lr, avg_train_batch_cost,
avg_loss, lr, avg_train_batch_cost,
avg_train_reader_cost, eta))
if use_vdl:
log_writer.add_scalar('Train/loss', avg_loss * nranks, iter)
log_writer.add_scalar('Train/loss', avg_loss, iter)
log_writer.add_scalar('Train/lr', lr, iter)
log_writer.add_scalar('Train/batch_cost',
avg_train_batch_cost, iter)
......@@ -160,10 +168,10 @@ def train(model,
"iter_{}".format(iter))
if not os.path.isdir(current_save_dir):
os.makedirs(current_save_dir)
fluid.save_dygraph(model.state_dict(),
os.path.join(current_save_dir, 'model'))
fluid.save_dygraph(optimizer.state_dict(),
os.path.join(current_save_dir, 'model'))
paddle.save(model.state_dict(),
os.path.join(current_save_dir, 'model'))
paddle.save(optimizer.state_dict(),
os.path.join(current_save_dir, 'model'))
if eval_dataset is not None:
mean_iou, avg_acc = evaluate(
......@@ -177,9 +185,8 @@ def train(model,
best_mean_iou = mean_iou
best_model_iter = iter
best_model_dir = os.path.join(save_dir, "best_model")
fluid.save_dygraph(
model.state_dict(),
os.path.join(best_model_dir, 'model'))
paddle.save(model.state_dict(),
os.path.join(best_model_dir, 'model'))
logger.info(
'Current evaluated best model in eval_dataset is iter_{}, miou={:4f}'
.format(best_model_iter, best_mean_iou))
......
......@@ -21,11 +21,11 @@ import math
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, Dropout
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import SyncBatchNorm as BatchNorm
from paddle.nn import Conv2d, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d
from paddleseg.utils import utils
from paddleseg.models.common import layer_libs, activation
......@@ -36,12 +36,12 @@ __all__ = [
]
class ConvBNLayer(fluid.dygraph.Layer):
class ConvBNLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
in_channels,
out_channels,
kernel_size,
stride=1,
dilation=1,
groups=1,
......@@ -52,31 +52,22 @@ class ConvBNLayer(fluid.dygraph.Layer):
super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode
self._pool2d_avg = Pool2D(
pool_size=2,
pool_stride=2,
pool_padding=0,
pool_type='avg',
ceil_mode=True)
self._conv = Conv2D(
num_channels=num_channels,
num_filters=num_filters,
filter_size=filter_size,
self._pool2d_avg = AvgPool2d(
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self._conv = Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=(filter_size - 1) // 2 if dilation == 1 else 0,
padding=(kernel_size - 1) // 2 if dilation == 1 else 0,
dilation=dilation,
groups=groups,
act=None,
param_attr=ParamAttr(name=name + "_weights"),
bias_attr=False)
if name == "conv1":
bn_name = "bn_" + name
else:
bn_name = "bn" + name[3:]
self._batch_norm = BatchNorm(
num_filters,
weight_attr=ParamAttr(name=bn_name + '_scale'),
bias_attr=ParamAttr(bn_name + '_offset'))
self._batch_norm = BatchNorm(out_channels)
self._act_op = activation.Activation(act=act)
def forward(self, inputs):
......@@ -89,10 +80,10 @@ class ConvBNLayer(fluid.dygraph.Layer):
return y
class BottleneckBlock(fluid.dygraph.Layer):
class BottleneckBlock(nn.Layer):
def __init__(self,
num_channels,
num_filters,
in_channels,
out_channels,
stride,
shortcut=True,
if_first=False,
......@@ -101,34 +92,34 @@ class BottleneckBlock(fluid.dygraph.Layer):
super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=1,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
act='relu',
name=name + "_branch2a")
self.dilation = dilation
self.conv1 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters,
filter_size=3,
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
stride=stride,
act='relu',
dilation=dilation,
name=name + "_branch2b")
self.conv2 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters * 4,
filter_size=1,
in_channels=out_channels,
out_channels=out_channels * 4,
kernel_size=1,
act=None,
name=name + "_branch2c")
if not shortcut:
self.short = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters * 4,
filter_size=1,
in_channels=in_channels,
out_channels=out_channels * 4,
kernel_size=1,
stride=1,
is_vd_mode=False if if_first or stride == 1 else True,
name=name + "_branch1")
......@@ -142,8 +133,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
# If given dilation rate > 1, using corresponding padding
if self.dilation > 1:
padding = self.dilation
y = fluid.layers.pad(
y, [0, 0, 0, 0, padding, padding, padding, padding])
y = F.pad(y, [0, 0, 0, 0, padding, padding, padding, padding])
#####################################################################
conv1 = self.conv1(y)
conv2 = self.conv2(conv1)
......@@ -153,15 +143,14 @@ class BottleneckBlock(fluid.dygraph.Layer):
else:
short = self.short(inputs)
y = fluid.layers.elementwise_add(x=short, y=conv2)
layer_helper = LayerHelper(self.full_name(), act='relu')
return layer_helper.append_activation(y)
y = paddle.elementwise_add(x=short, y=conv2, act='relu')
return y
class BasicBlock(fluid.dygraph.Layer):
class BasicBlock(nn.Layer):
def __init__(self,
num_channels,
num_filters,
in_channels,
out_channels,
stride,
shortcut=True,
if_first=False,
......@@ -169,24 +158,24 @@ class BasicBlock(fluid.dygraph.Layer):
super(BasicBlock, self).__init__()
self.stride = stride
self.conv0 = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=3,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=stride,
act='relu',
name=name + "_branch2a")
self.conv1 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters,
filter_size=3,
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3,
act=None,
name=name + "_branch2b")
if not shortcut:
self.short = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=1,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
is_vd_mode=False if if_first else True,
name=name + "_branch1")
......@@ -201,13 +190,12 @@ class BasicBlock(fluid.dygraph.Layer):
short = inputs
else:
short = self.short(inputs)
y = fluid.layers.elementwise_add(x=short, y=conv1)
y = paddle.elementwise_add(x=short, y=conv1, act='relu')
layer_helper = LayerHelper(self.full_name(), act='relu')
return layer_helper.append_activation(y)
return y
class ResNet_vd(fluid.dygraph.Layer):
class ResNet_vd(nn.Layer):
def __init__(self,
backbone_pretrained=None,
layers=50,
......@@ -243,28 +231,27 @@ class ResNet_vd(fluid.dygraph.Layer):
dilation_dict = {3: 2}
self.conv1_1 = ConvBNLayer(
num_channels=3,
num_filters=32,
filter_size=3,
in_channels=3,
out_channels=32,
kernel_size=3,
stride=2,
act='relu',
name="conv1_1")
self.conv1_2 = ConvBNLayer(
num_channels=32,
num_filters=32,
filter_size=3,
in_channels=32,
out_channels=32,
kernel_size=3,
stride=1,
act='relu',
name="conv1_2")
self.conv1_3 = ConvBNLayer(
num_channels=32,
num_filters=64,
filter_size=3,
in_channels=32,
out_channels=64,
kernel_size=3,
stride=1,
act='relu',
name="conv1_3")
self.pool2d_max = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max')
self.pool2d_max = MaxPool2d(kernel_size=3, stride=2, padding=1)
# self.block_list = []
self.stage_list = []
......@@ -296,9 +283,9 @@ class ResNet_vd(fluid.dygraph.Layer):
bottleneck_block = self.add_sublayer(
'bb_%d_%d' % (block, i),
BottleneckBlock(
num_channels=num_channels[block]
in_channels=num_channels[block]
if i == 0 else num_filters[block] * 4,
num_filters=num_filters[block],
out_channels=num_filters[block],
stride=2 if i == 0 and block != 0
and dilation_rate == 1 else 1,
shortcut=shortcut,
......@@ -318,9 +305,9 @@ class ResNet_vd(fluid.dygraph.Layer):
basic_block = self.add_sublayer(
'bb_%d_%d' % (block, i),
BasicBlock(
num_channels=num_channels[block]
in_channels=num_channels[block]
if i == 0 else num_filters[block],
num_filters=num_filters[block],
out_channels=num_filters[block],
stride=2 if i == 0 and block != 0 else 1,
shortcut=shortcut,
if_first=block == i == 0,
......@@ -329,23 +316,6 @@ class ResNet_vd(fluid.dygraph.Layer):
shortcut = True
self.stage_list.append(block_list)
self.pool2d_avg = Pool2D(
pool_size=7, pool_type='avg', global_pooling=True)
self.pool2d_avg_channels = num_channels[-1] * 2
stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
self.out = Linear(
self.pool2d_avg_channels,
class_dim,
param_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv),
name="fc_0.w_0"),
bias_attr=ParamAttr(name="fc_0.b_0"))
self.init_weight(backbone_pretrained)
def forward(self, inputs):
y = self.conv1_1(inputs)
y = self.conv1_2(y)
......@@ -357,34 +327,12 @@ class ResNet_vd(fluid.dygraph.Layer):
for i, stage in enumerate(self.stage_list):
for j, block in enumerate(stage):
y = block(y)
#print("stage {} block {}".format(i+1, j+1), y.shape)
feat_list.append(y)
y = self.pool2d_avg(y)
y = fluid.layers.reshape(y, shape=[-1, self.pool2d_avg_channels])
y = self.out(y)
return y, feat_list
# def init_weight(self, pretrained_model=None):
# if pretrained_model is not None:
# if os.path.exists(pretrained_model):
# utils.load_pretrained_model(self, pretrained_model)
def init_weight(self, pretrained_model=None):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if pretrained_model is not None:
if os.path.exists(pretrained_model):
utils.load_pretrained_model(self, pretrained_model)
else:
raise Exception('Pretrained model is not found: {}'.format(
pretrained_model))
return feat_list
@manager.BACKBONES.add_component
def ResNet18_vd(**args):
model = ResNet_vd(layers=18, **args)
return model
......
......@@ -16,17 +16,16 @@ import math
import os
import paddle
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear
from paddle.fluid.initializer import Normal
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2d
from paddle.nn import SyncBatchNorm as BatchNorm
from paddleseg.cvlibs import manager
from paddleseg import utils
from paddleseg.cvlibs import param_init
from paddleseg.utils import logger
from paddleseg.models.common import layer_libs, activation
__all__ = [
"fcn_hrnet_w18_small_v1", "fcn_hrnet_w18_small_v2", "fcn_hrnet_w18",
......@@ -36,7 +35,7 @@ __all__ = [
@manager.MODELS.add_component
class FCN(fluid.dygraph.Layer):
class FCN(nn.Layer):
"""
Fully Convolutional Networks for Semantic Segmentation.
https://arxiv.org/abs/1411.4038
......@@ -70,18 +69,18 @@ class FCN(fluid.dygraph.Layer):
self.model_pretrained = model_pretrained
self.backbone_indices = backbone_indices
if channels is None:
channels = backbone_channels[backbone_indices[0]]
channels = backbone_channels[0]
self.backbone = backbone
self.conv_last_2 = ConvBNLayer(
num_channels=backbone_channels[backbone_indices[0]],
num_filters=channels,
filter_size=1,
in_channels=backbone_channels[0],
out_channels=channels,
kernel_size=1,
stride=1)
self.conv_last_1 = Conv2D(
num_channels=channels,
num_filters=self.num_classes,
filter_size=1,
self.conv_last_1 = Conv2d(
in_channels=channels,
out_channels=self.num_classes,
kernel_size=1,
stride=1,
padding=0)
if self.training:
......@@ -93,7 +92,7 @@ class FCN(fluid.dygraph.Layer):
x = fea_list[self.backbone_indices[0]]
x = self.conv_last_2(x)
logit = self.conv_last_1(x)
logit = fluid.layers.resize_bilinear(logit, input_shape)
logit = F.resize_bilinear(logit, input_shape)
return [logit]
def init_weight(self):
......@@ -125,32 +124,31 @@ class FCN(fluid.dygraph.Layer):
logger.warning('No pretrained model to load, train from scratch')
class ConvBNLayer(fluid.dygraph.Layer):
class ConvBNLayer(nn.Layer):
def __init__(self,
num_channels,
num_filters,
filter_size,
in_channels,
out_channels,
kernel_size,
stride=1,
groups=1,
act="relu"):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
num_channels=num_channels,
num_filters=num_filters,
filter_size=filter_size,
self._conv = Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=(filter_size - 1) // 2,
padding=(kernel_size - 1) // 2,
groups=groups,
bias_attr=False)
self._batch_norm = BatchNorm(num_filters)
self.act = act
self._batch_norm = BatchNorm(out_channels)
self.act = activation.Activation(act=act)
def forward(self, input):
y = self._conv(input)
y = self._batch_norm(y)
if self.act == 'relu':
y = fluid.layers.relu(y)
y = self.act(y)
return y
......
......@@ -15,11 +15,14 @@
import codecs
import os
from typing import Any, Callable
import pprint
import yaml
import paddle.fluid as fluid
import paddle
import paddle.nn.functional as F
import paddleseg.cvlibs.manager as manager
from paddleseg.utils import logger
class Config(object):
......@@ -36,7 +39,7 @@ class Config(object):
if path.endswith('yml') or path.endswith('yaml'):
dic = self._parse_from_yaml(path)
print(dic)
logger.info('\n' + pprint.pformat(dic))
self._build(dic)
else:
raise RuntimeError('Config file should in yaml format!')
......@@ -127,18 +130,19 @@ class Config(object):
lr = self._learning_rate
args = self.decay_args
args.setdefault('decay_steps', self.iters)
return fluid.layers.polynomial_decay(lr, **args)
args.setdefault('end_lr', 0)
return paddle.optimizer.PolynomialLR(lr, **args)
else:
raise RuntimeError('Only poly decay support.')
@property
def optimizer(self) -> fluid.optimizer.Optimizer:
def optimizer(self) -> paddle.optimizer.Optimizer:
if self.optimizer_type == 'sgd':
lr = self.learning_rate
args = self.optimizer_args
args.setdefault('momentum', 0.9)
return fluid.optimizer.Momentum(
lr, parameter_list=self.model.parameters(), **args)
return paddle.optimizer.Momentum(
lr, parameters=self.model.parameters(), **args)
else:
raise RuntimeError('Only sgd optimizer support.')
......
......@@ -14,8 +14,8 @@
import argparse
import paddle.fluid as fluid
from paddle.fluid.dygraph.parallel import ParallelEnv
import paddle
from paddle.distributed import ParallelEnv
import paddleseg
from paddleseg.cvlibs import manager
......@@ -91,41 +91,40 @@ def main(args):
['-' * 48])
logger.info(info)
places = fluid.CUDAPlace(ParallelEnv().dev_id) \
places = paddle.CUDAPlace(ParallelEnv().dev_id) \
if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
else fluid.CPUPlace()
with fluid.dygraph.guard(places):
if not args.cfg:
raise RuntimeError('No configuration file specified.')
cfg = Config(args.cfg)
train_dataset = cfg.train_dataset
if not train_dataset:
raise RuntimeError(
'The training dataset is not specified in the configuration file.'
)
val_dataset = cfg.val_dataset if args.do_eval else None
losses = cfg.loss
train(
cfg.model,
train_dataset,
places=places,
eval_dataset=val_dataset,
optimizer=cfg.optimizer,
save_dir=args.save_dir,
iters=cfg.iters,
batch_size=cfg.batch_size,
save_interval_iters=args.save_interval_iters,
log_iters=args.log_iters,
num_classes=train_dataset.num_classes,
num_workers=args.num_workers,
use_vdl=args.use_vdl,
losses=losses,
ignore_index=losses['types'][0].ignore_index)
else paddle.CPUPlace()
paddle.disable_static(places)
if not args.cfg:
raise RuntimeError('No configuration file specified.')
cfg = Config(args.cfg)
train_dataset = cfg.train_dataset
if not train_dataset:
raise RuntimeError(
'The training dataset is not specified in the configuration file.')
val_dataset = cfg.val_dataset if args.do_eval else None
losses = cfg.loss
train(
cfg.model,
train_dataset,
places=places,
eval_dataset=val_dataset,
optimizer=cfg.optimizer,
save_dir=args.save_dir,
iters=cfg.iters,
batch_size=cfg.batch_size,
save_interval_iters=args.save_interval_iters,
log_iters=args.log_iters,
num_classes=train_dataset.num_classes,
num_workers=args.num_workers,
use_vdl=args.use_vdl,
losses=losses,
ignore_index=losses['types'][0].ignore_index)
if __name__ == '__main__':
......
......@@ -14,8 +14,8 @@
import argparse
import paddle.fluid as fluid
from paddle.fluid.dygraph.parallel import ParallelEnv
import paddle
from paddle.distributed import ParallelEnv
import paddleseg
from paddleseg.cvlibs import manager
......@@ -41,25 +41,25 @@ def parse_args():
def main(args):
env_info = get_environ_info()
places = fluid.CUDAPlace(ParallelEnv().dev_id) \
places = paddle.CUDAPlace(ParallelEnv().dev_id) \
if env_info['Paddle compiled with cuda'] and env_info['GPUs used'] \
else fluid.CPUPlace()
else paddle.CPUPlace()
with fluid.dygraph.guard(places):
if not args.cfg:
raise RuntimeError('No configuration file specified.')
paddle.disable_static(places)
if not args.cfg:
raise RuntimeError('No configuration file specified.')
cfg = Config(args.cfg)
val_dataset = cfg.val_dataset
if not val_dataset:
raise RuntimeError(
'The verification dataset is not specified in the configuration file.'
)
evaluate(
cfg.model,
val_dataset,
model_dir=args.model_dir,
num_classes=val_dataset.num_classes)
cfg = Config(args.cfg)
val_dataset = cfg.val_dataset
if not val_dataset:
raise RuntimeError(
'The verification dataset is not specified in the configuration file.'
)
evaluate(
cfg.model,
val_dataset,
model_dir=args.model_dir,
num_classes=val_dataset.num_classes)
if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册