Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
unsky committed Sep 12, 2017
1 parent b6122b1 commit 4ab6530
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 23 deletions.
17 changes: 11 additions & 6 deletions kitti.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,42 @@ network:
p3_RPN_FEAT_STRIDE: 4

p3_ANCHOR_RATIOS:
- 0.25
- 0.5
- 1
- 2
- 3
p3_ANCHOR_SCALES:
- 8
p3_NUM_ANCHORS: 3
p3_NUM_ANCHORS: 5


p4_RCNN_FEAT_STRIDE: 8
p4_RPN_FEAT_STRIDE: 8

p4_ANCHOR_RATIOS:
- 0.25
- 0.5
- 1
- 2
- 3
p4_ANCHOR_SCALES:
- 8
p4_NUM_ANCHORS: 3
p4_NUM_ANCHORS: 5


p5_RCNN_FEAT_STRIDE: 16
p5_RPN_FEAT_STRIDE: 16

p5_ANCHOR_RATIOS:
- 0.25
- 0.5
- 1
- 2
- 3
p5_ANCHOR_SCALES:
- 8
p5_NUM_ANCHORS: 3

p5_NUM_ANCHORS: 5

dataset:
NUM_CLASSES: 4
Expand All @@ -84,14 +89,14 @@ dataset:
proposal: rpn
TRAIN:
lr: 0.0001
lr_step: '5.333'
lr_step: '50.333'

warmup: false
warmup_lr: 0.02
# typically we will use 8000 warmup step for single GPU for COCO
warmup_step: 1000
begin_epoch: 0
end_epoch: 40
end_epoch: 100
model_prefix: 'rcnn_kitti'
# whether resume training
RESUME: false
Expand Down
24 changes: 12 additions & 12 deletions lib/rpn/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def assign_anchor(feat_shape_p3,feat_shape_p4,feat_shape_p5,
'bbox_inside_weight': *todo* mark the assigned anchors
'bbox_outside_weight': used to normalize the bbox_loss, all weights sums to RPN_POSITIVE_WEIGHT
"""
allowed_border=10

feat_shape = [feat_shape_p3,feat_shape_p4,feat_shape_p5]
feat_stride=[4,8,16]
scales=(8,)
ratios=(0.5, 1, 2)
ratios=(0.25,0.5, 1, 2,3)


def _unmap(data, count, inds, fill=0):
Expand Down Expand Up @@ -138,14 +138,14 @@ def _unmap(data, count, inds, fill=0):

# keep only inside anchors
anchors = all_anchors
inds_inside = np.where((all_anchors[:, 0] >= -allowed_border) &
(all_anchors[:, 1] >= -allowed_border) &
(all_anchors[:, 2] < im_info[1] + allowed_border) &
(all_anchors[:, 3] < im_info[0] + allowed_border))[0]
# inds_inside = np.where((all_anchors[:, 0] >= -allowed_border) &
# (all_anchors[:, 1] >= -allowed_border) &
# (all_anchors[:, 2] < im_info[1] + allowed_border) &
# (all_anchors[:, 3] < im_info[0] + allowed_border))[0]
# label: 1 is positive, 0 is negative, -1 is dont care
total_anchors = len(inds_inside)#3*w*h
anchors = all_anchors[inds_inside, :]
labels = np.empty((len(inds_inside),), dtype=np.float32)
total_anchors = len(anchors)#3*w*h
# anchors = all_anchors[inds_inside, :]
labels = np.empty((total_anchors,), dtype=np.float32)
labels.fill(-1)

if gt_boxes.size > 0:
Expand Down Expand Up @@ -185,9 +185,9 @@ def _unmap(data, count, inds, fill=0):


# map up to original set of anchors
labels = _unmap(labels, int(K * A), inds_inside, fill=-1)
bbox_targets = _unmap(bbox_targets, int(K * A), inds_inside, fill=0)
bbox_weights = _unmap(bbox_weights, int(K * A), inds_inside, fill=0)
labels = _unmap(labels, int(K * A), range(total_anchors), fill=-1)
bbox_targets = _unmap(bbox_targets, int(K * A), range(total_anchors), fill=0)
bbox_weights = _unmap(bbox_weights, int(K * A), range(total_anchors), fill=0)



Expand Down
Binary file modified lib/rpn/rpn.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions retinanet/core/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def update(self, labels, preds):
# keep_inds = np.where(label != -1)
pred_label = pred_label[keep_inds]
label = label[keep_inds]

self.sum_metric += np.sum(pred_label.flat == label.flat)
self.num_inst += len(pred_label.flat)

Expand Down
Binary file modified retinanet/core/metric.pyc
Binary file not shown.
9 changes: 5 additions & 4 deletions retinanet/operator_py/focal_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def forward(self, is_train, req, in_data, out_data, aux):

self._labels = labels
pro_ = (mx.nd.sigmoid(cls_score) + 1e-14).asnumpy()
# print "pro:", pro_[0]
print "pro:", pro_[0]

# pro_ = np.exp(1e-14+cls_score - cls_score.max(axis=1).reshape((cls_score.shape[0], 1)))
# pro_ /= pro_.sum(axis=1).reshape((cls_score.shape[0], 1))
Expand All @@ -49,7 +49,7 @@ def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
pt = self._pt + 1e-14

pt = pt.reshape(len(pt),1)
dx = (1-self._alpha)*np.power(1 - pt, self._gamma - 1) * (self._gamma * (-1 * pt * pro_) * np.log(pt) + pro_ * (1 - pt)) * 1.0
dx = (1-self._alpha)*np.power(1 - pt, self._gamma - 1) * (self._gamma * (-1 * pt * pro_) * np.log(pt) + pro_ * (1 - pt)) * 1.0

####i==j
#reload pt
Expand All @@ -58,12 +58,13 @@ def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
negative_inds = np.where(labels>-100)

if self.use_ignore:

ig_inds= np.where(labels==self.ignore_label)
dx[ig_inds,:] =0
negative_inds = np.where(labels!=self.ignore_label)
pt = pt[negative_inds]
dx[negative_inds, labels[negative_inds].astype('int')] = (self._alpha)*(-1)*dx[negative_inds, labels[negative_inds].astype('int')]
dx[negative_inds, labels[negative_inds].astype('int')] = (self._alpha)*np.power(1 - pt, self._gamma) * (self._gamma * pt * np.log(pt) + pt -1) * (1.0)
#0.25*(dx[negative_inds, labels[negative_inds].astype('int')]-1)
# (self._alpha)*(-1)*dx[negative_inds, labels[negative_inds].astype('int')]
#(self._alpha)*np.power(1 - pt, self._gamma) * (self._gamma * pt * np.log(pt) + pt -1) * (1.0)

# print 'label:',labels[negative_inds][0]
Expand Down
Binary file modified retinanet/operator_py/focal_loss.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion retinanet/symbols/retina_resnet_101.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ def get_symbol(self, cfg, is_train=True):
return group

def init_weight(self, cfg, arg_params, aux_params):
pi = 0.01
pi = 0.00001


arg_params['cls_conv1_3x3_weight'] = mx.random.normal(0, 0.01, shape=self.arg_shape_dict['cls_conv1_3x3_weight'])
Expand All @@ -1054,6 +1054,7 @@ def init_weight(self, cfg, arg_params, aux_params):
arg_params['box_conv4_bias'] = mx.nd.zeros(shape=self.arg_shape_dict['box_conv4_bias'])
arg_params['box_pred_weight'] = mx.random.normal(0, 0.01, shape=self.arg_shape_dict['box_pred_weight'])
arg_params['box_pred_bias'] = mx.nd.zeros(shape=self.arg_shape_dict['box_pred_bias'])
#*(-np.log((1-pi)/pi))



Expand Down
Binary file modified retinanet/symbols/retina_resnet_101.pyc
Binary file not shown.

0 comments on commit 4ab6530

Please sign in to comment.