Skip to content

Commit

Permalink
fix save_model bug and remove inputs[mode] in rcnn (PaddlePaddle#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
yghstill authored Jan 11, 2021
1 parent 2851025 commit 7042801
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 45 deletions.
5 changes: 3 additions & 2 deletions dygraph/ppdet/modeling/architectures/cascade_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def model_arch(self, ):
self.inputs,
self.rpn_head_out,
self.anchor_out,
self.training,
i,
rois,
bbox_head_out,
Expand All @@ -110,7 +111,7 @@ def model_arch(self, ):
spatial_scale, i)
self.bbox_head_list.append(bbox_head_out)

if self.inputs['mode'] == 'infer':
if not self.training:
bbox_pred, bboxes = self.bbox_head.get_cascade_prediction(
self.bbox_head_list, rois_list)
self.bboxes = self.bbox_post_process(bbox_pred, bboxes,
Expand All @@ -120,7 +121,7 @@ def model_arch(self, ):
if self.with_mask:
rois = rois_list[-1]
rois_has_mask_int32 = None
if self.inputs['mode'] == 'train':
if self.training:
bbox_targets = self.proposal.get_targets()[-1]
self.bboxes, rois_has_mask_int32 = self.mask(self.inputs, rois,
bbox_targets)
Expand Down
5 changes: 3 additions & 2 deletions dygraph/ppdet/modeling/architectures/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ def model_arch(self):

# Proposal RoI
# compute targets here when training
rois = self.proposal(self.inputs, self.rpn_head_out, self.anchor_out)
rois = self.proposal(self.inputs, self.rpn_head_out, self.anchor_out,
self.training)
# BBox Head
bbox_feat, self.bbox_head_out, self.bbox_head_feat_func = self.bbox_head(
body_feats, rois, spatial_scale)

if self.inputs['mode'] == 'infer':
if not self.training:
bbox_pred, bboxes = self.bbox_head.get_prediction(
self.bbox_head_out, rois)
# Refine bbox by the output from bbox_head at test stage
Expand Down
5 changes: 3 additions & 2 deletions dygraph/ppdet/modeling/architectures/mask_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ def model_arch(self):

# Proposal RoI
# compute targets here when training
rois = self.proposal(self.inputs, self.rpn_head_out, self.anchor_out)
rois = self.proposal(self.inputs, self.rpn_head_out, self.anchor_out,
self.training)
# BBox Head
bbox_feat, self.bbox_head_out, bbox_head_feat_func = self.bbox_head(
body_feats, rois, spatial_scale)

rois_has_mask_int32 = None
if self.inputs['mode'] == 'infer':
if not self.training:
bbox_pred, bboxes = self.bbox_head.get_prediction(
self.bbox_head_out, rois)
# Refine bbox by the output from bbox_head at test stage
Expand Down
10 changes: 3 additions & 7 deletions dygraph/ppdet/modeling/architectures/meta_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ class BaseArch(nn.Layer):
def __init__(self):
super(BaseArch, self).__init__()

def forward(self, inputs, mode='infer'):
def forward(self, inputs):
self.inputs = inputs
self.inputs['mode'] = mode
self.model_arch()

if mode == 'train':
if self.training:
out = self.get_loss()
elif mode == 'infer':
out = self.get_pred()
else:
out = None
raise "Now, only support train and infer mode!"
out = self.get_pred()
return out

def build_inputs(self, data, input_def):
Expand Down
9 changes: 5 additions & 4 deletions dygraph/ppdet/modeling/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, proposal_generator, proposal_target_generator):
self.proposal_generator = proposal_generator
self.proposal_target_generator = proposal_target_generator

def generate_proposal(self, inputs, rpn_head_out, anchor_out):
def generate_proposal(self, inputs, rpn_head_out, anchor_out, is_train):
# TODO: delete im_info
try:
im_shape = inputs['im_info']
Expand All @@ -97,7 +97,7 @@ def generate_proposal(self, inputs, rpn_head_out, anchor_out):
anchors=anchor,
variances=var,
im_shape=im_shape,
mode=inputs['mode'])
is_train=is_train)
if len(rpn_head_out) == 1:
return rpn_rois, rpn_rois_num
rpn_rois_list.append(rpn_rois)
Expand Down Expand Up @@ -164,21 +164,22 @@ def __call__(self,
inputs,
rpn_head_out,
anchor_out,
is_train=False,
stage=0,
proposal_out=None,
bbox_head_out=None,
max_overlap=None):
if stage == 0:
roi, rois_num = self.generate_proposal(inputs, rpn_head_out,
anchor_out)
anchor_out, is_train)
self.targets_list = []
self.max_overlap = None

else:
bbox_delta = bbox_head_out[1]
roi = self.refine_bbox(proposal_out[0], bbox_delta, stage)
rois_num = proposal_out[1]
if inputs['mode'] == 'train':
if is_train:
roi, rois_num, targets, self.max_overlap = self.generate_proposal_target(
inputs, roi, rois_num, stage, self.max_overlap)
self.targets_list.append(targets)
Expand Down
29 changes: 8 additions & 21 deletions dygraph/ppdet/modeling/heads/mask_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ def forward(self,
mask_index,
spatial_scale,
stage=0,
bbox_head_feat_func=None,
mode='train'):
bbox_head_feat_func=None):
if self.share_bbox_feat and mask_index is not None:
rois_feat = paddle.gather(bbox_feat, mask_index)
else:
rois_feat = self.mask_roi_extractor(body_feats, bboxes,
spatial_scale)
if self.share_bbox_feat and bbox_head_feat_func is not None and mode == 'infer':
if self.share_bbox_feat and bbox_head_feat_func is not None and not self.training:
rois_feat = bbox_head_feat_func(rois_feat)

# upsample
Expand Down Expand Up @@ -136,14 +135,8 @@ def forward_train(self,
spatial_scale,
stage=0):
# feat
mask_feat = self.mask_feat(
body_feats,
bboxes,
bbox_feat,
mask_index,
spatial_scale,
stage,
mode='train')
mask_feat = self.mask_feat(body_feats, bboxes, bbox_feat, mask_index,
spatial_scale, stage)
# logits
mask_head_out = self.mask_fcn_logits[stage](mask_feat)
return mask_head_out
Expand Down Expand Up @@ -174,15 +167,9 @@ def forward_test(self,
scale_factor_list = paddle.reshape(scale_factor_list, shape=[-1, 1])
scaled_bbox = paddle.multiply(bbox[:, 2:], scale_factor_list)
scaled_bboxes = (scaled_bbox, bbox_num)
mask_feat = self.mask_feat(
body_feats,
scaled_bboxes,
bbox_feat,
mask_index,
spatial_scale,
stage,
bbox_head_feat_func,
mode='infer')
mask_feat = self.mask_feat(body_feats, scaled_bboxes, bbox_feat,
mask_index, spatial_scale, stage,
bbox_head_feat_func)
mask_logit = self.mask_fcn_logits[stage](mask_feat)
mask_head_out = F.sigmoid(mask_logit)
return mask_head_out
Expand All @@ -196,7 +183,7 @@ def forward(self,
spatial_scale,
bbox_head_feat_func=None,
stage=0):
if inputs['mode'] == 'train':
if self.training:
mask_head_out = self.forward_train(body_feats, bboxes, bbox_feat,
mask_index, spatial_scale, stage)
else:
Expand Down
6 changes: 3 additions & 3 deletions dygraph/ppdet/modeling/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ def __call__(self,
anchors,
variances,
im_shape,
mode='train'):
pre_nms_top_n = self.train_pre_nms_top_n if mode == 'train' else self.infer_pre_nms_top_n
post_nms_top_n = self.train_post_nms_top_n if mode == 'train' else self.infer_post_nms_top_n
is_train=False):
pre_nms_top_n = self.train_pre_nms_top_n if is_train else self.infer_pre_nms_top_n
post_nms_top_n = self.train_post_nms_top_n if is_train else self.infer_post_nms_top_n
# TODO delete im_info
if im_shape.shape[1] > 2:
import paddle.fluid as fluid
Expand Down
4 changes: 3 additions & 1 deletion dygraph/ppdet/utils/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def load_pretrain_weight(model,
def save_model(model, optimizer, save_dir, save_name, last_epoch):
"""
save model into disk.
Args:
model (paddle.nn.Layer): the Layer instalce to save parameters.
optimizer (paddle.optimizer.Optimizer): the Optimizer instance to
Expand All @@ -159,6 +159,8 @@ def save_model(model, optimizer, save_dir, save_name, last_epoch):
save_name (str): the path to be saved.
last_epoch (int): the epoch index.
"""
if paddle.distributed.get_rank() != 0:
return
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, save_name)
Expand Down
2 changes: 1 addition & 1 deletion dygraph/tools/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def run(FLAGS, cfg, place):
for iter_id, data in enumerate(eval_loader):
# forward
model.eval()
outs = model(data, mode='infer')
outs = model(data)
for key in extra_key:
outs[key] = data[key]
for key, value in outs.items():
Expand Down
2 changes: 1 addition & 1 deletion dygraph/tools/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def run(FLAGS, cfg, place):
for iter_id, data in enumerate(test_loader):
# forward
model.eval()
outs = model(data, mode='infer')
outs = model(data)
for key in extra_key:
outs[key] = data[key]
for key, value in outs.items():
Expand Down
2 changes: 1 addition & 1 deletion dygraph/tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def no_grad(param):
data_time.update(time.time() - end_time)
# Model Forward
model.train()
outputs = model(data, mode='train')
outputs = model(data)
loss = outputs['loss']
# Model Backward
loss.backward()
Expand Down

0 comments on commit 7042801

Please sign in to comment.