def forward(self, rcnn_cls_pred, rcnn_bbox_pred, rcnn_cls_gt, rcnn_bbox_gt): with autograd.pause(): ctx = rcnn_cls_pred.context roi_num = rcnn_cls_pred.shape[0] roi_idx = nd.arange(roi_num, ctx=ctx).reshape(-1, 1) fg_bbox_mask = (rcnn_cls_gt > 0).reshape(0, 1, 1) bbox_weights = nd.zeros_like(rcnn_bbox_gt).reshape(0, -1, 4) bbox_weights[roi_idx, rcnn_cls_gt[:], :] = \ self._bbox_weights.data(ctx).broadcast_to((roi_num, 1, 4)) * fg_bbox_mask bbox_weights = bbox_weights.reshape(0, -1) # rcnn_cls_pred.shape (roi_num, num_classes) rcnn_cls_log = nd.log(nd.clip(rcnn_cls_pred, 1e-14, 1)) cls_log_loss = -nd.sum(rcnn_cls_log[ roi_idx, rcnn_cls_gt]) / self._roi_batch_size.data(ctx) # rcnn_bbox_pred.shape (roi_num, num_classes*4) rcnn_bbox_smooth_l1 = nd.smooth_l1(rcnn_bbox_pred - rcnn_bbox_gt, scalar=1.0) bbox_smooth_l1_loss = nd.sum( rcnn_bbox_smooth_l1 * bbox_weights) / self._roi_batch_size.data(ctx) return cls_log_loss, bbox_smooth_l1_loss
def updata(self, labels, preds): rcnn_bbox_taregt, rcnn_bbox_weight = labels rcnn_bbox_reg = preds[0] num_inst = nd.sum(rcnn_bbox_weight) / 4 loss = nd.sum(rcnn_bbox_weight * nd.smooth_l1(rcnn_bbox_reg-rcnn_bbox_taregt, scalar=1)) self.sum_metric += loss.asscalar() self.num_inst += num_inst.asscalar()
def update(self, labels, preds): rpn_bbox_target, rpn_bbox_weight = labels rpn_bbox_reg = preds[0] num_inst = nd.sum(rpn_bbox_weight) / 4 loss = nd.sum(rpn_bbox_weight * nd.smooth_l1(rpn_bbox_reg-rpn_bbox_target, scalar=3)) self.sum_metric += loss.asscalar() self.num_inst += num_inst.asscalar()
def update(self, labels, preds): """Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` [rcnn_box_target,rcnn_box_weight] The labels of the data. preds : list of `NDArray` [rcnn_box_reg] Predicted values. """ rcnn_box_target, rcnn_box_weight = labels rcnn_box_reg = preds[0] num_inst = nd.sum(rcnn_box_weight) / 4 self.sum_metric += (nd.smooth_l1(rcnn_box_reg - rcnn_box_target, scalar=1) * rcnn_box_weight).sum().asscalar() self.num_inst += num_inst.asscalar()
def forward(self, rpn_cls_pred, rpn_bbox_pred, rpn_cls_gt, rpn_bbox_gt): with autograd.pause(): ctx = rpn_cls_pred.context batch_size = rpn_cls_pred.shape[0] # construct cls_mask to ignore label=-1 cls_mask = nd.stack(rpn_cls_gt == 0, rpn_cls_gt == 1, axis=1) bbox_weights = (rpn_cls_gt == 1).reshape( batch_size, 1, -1) * self._bbox_weights.data(ctx) # reshape -> (batch_size, 2, num_anchors*feat_h*feat_w) rpn_cls_log = nd.log( nd.clip(rpn_cls_pred.reshape((batch_size, 2, -1)), 1e-14, 1)) cls_log_loss = -nd.sum( rpn_cls_log * cls_mask) / self._rpn_batch_size.data(ctx) # reshape -> (batch_size, 4, num_anchors*feat_h*feat_w) rpn_bbox_smooth_l1 = nd.smooth_l1(rpn_bbox_pred.reshape( (batch_size, 4, -1)) - rpn_bbox_gt, scalar=3.0) bbox_smooth_l1_loss = nd.sum( rpn_bbox_smooth_l1 * bbox_weights) / self._rpn_batch_size.data(ctx) return cls_log_loss, bbox_smooth_l1_loss
continue h, w = img.shape[0:2] bbox = [row[2:6] * nd.array((w, h, w, h), ctx=row.context)] d2l.show_bboxes(fig.axes, bbox, '%.2f' % score, 'w') display(img, output, threshold=0.3) #损失函数 #平滑L1范数损失 sigmas = [10, 1, 0.5] lines = ['-', '--', '-.'] x = nd.arange(-2, 2, 0.1) d2l.set_figsize() for l, s in zip(lines, sigmas): y = nd.smooth_l1(x, scalar=s) d2l.plt.plot(x.asnumpy(), y.asnumpy(), l, label='sigma=%.1f' % s) d2l.plt.legend() #焦点损失 def focal_loss(gamma, x): return -(1 - x)**gamma * x.log() x = nd.arange(0.01, 1, 0.01) for l, gamma in zip(lines, [0, 1, 5]): y = d2l.plt.plot(x.asnumpy(), focal_loss(gamma, x).asnumpy(), l, label='gamma=%.1f' % gamma)
def compute_res_loss(output, target, maski): diff = nd.abs(output * maski - target * maski) return nd.smooth_l1(diff).mean()
def forward(self, output, label, mask): loss = nd.smooth_l1((output - label) * mask, scalar=1.0) return nd.mean(loss, self._batch_axis, exclude=True)