提交 455cf5d3 编写于 作者: C chenguowei01

update to beta version

上级 c6064306
......@@ -15,11 +15,9 @@
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.io import DistributedBatchSampler
from paddle.io import DataLoader
import paddle.nn.functional as F
import paddleseg.utils.logger as logger
......@@ -79,8 +77,8 @@ def train(model,
os.makedirs(save_dir)
if nranks > 1:
strategy = fluid.dygraph.prepare_context()
ddp_model = fluid.dygraph.DataParallel(model, strategy)
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)
......@@ -130,7 +128,7 @@ def train(model,
optimizer.minimize(loss)
model.clear_gradients()
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
......@@ -160,10 +158,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 +175,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))
......
......@@ -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,24 +130,18 @@ class Config(object):
lr = self._learning_rate
args = self.decay_args
args.setdefault('decay_steps', self.iters)
return fluid.layers.polynomial_decay(lr, **args)
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)
weight_decay = args.get('weight_decay', None)
if weight_decay is not None:
args.pop('weight_decay')
regularization = fluid.regularizer.L2DecayRegularizer(
regularization_coeff=weight_decay)
args.setdefault('regularization', None)
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.
先完成此消息的编辑!
想要评论请 注册