def forward(self, x): x = self.fc0(x) x = relu(x) # Should use tanh but it's not stable now. x = self.fc1(x) x = relu(x) # Should use tanh but it's not stable now. x = self.fc2(x) return x
def forward(self, old_x): if self.stride == 1: x_proj, x = self.channel_shuffle(old_x) elif self.stride == 2: x_proj = old_x x = old_x x_gap = x.mean(axis=2, keepdims=True).mean(axis=3, keepdims=True) x_gap = self.reduce(x_gap) x = self.wnet1(x, x_gap) x = self.bn1(x) x = F.relu(x) x = self.wnet2(x, x_gap) x = self.bn2(x) x = self.wnet3(x, x_gap) x = self.bn3(x) x = F.relu(x) if self.stride == 2: x_proj = self.wnet_proj_1(x_proj, x_gap) x_proj = self.bn_proj_1(x_proj) x_proj = self.wnet_proj_2(x_proj, x_gap) x_proj = self.bn_proj_2(x_proj) x_proj = F.relu(x_proj) return F.concat((x_proj, x), 1)
def forward(self, x): x = self.fc1(x) x = F.relu(x) x = self.fc2(x) x = F.relu(x) x = self.fc3(x) x = F.relu(x) x = self.fc4(x) prob = F.softmax(x) return prob
def forward(self, x): identity = x x = self.conv1(x) x = self.bn1(x) x = F.relu(x) x = self.conv2(x) x = self.bn2(x) identity = self.downsample(identity) x += identity x = F.relu(x) return x
def get_stats(self, x): activations, flops = 0, 0 identity = x in_x = deepcopy(x) x = self.conv1(x) tmp_flops, tmp_acts = cal_conv_stats(self.conv1, in_x, x) activations += tmp_acts flops += tmp_flops in_x = deepcopy(x) x = self.bn1(x) tmp_flops, tmp_acts = cal_norm_stats(self.bn1, in_x, x) activations += tmp_acts flops += tmp_flops x = F.relu(x) in_x = deepcopy(x) x = self.conv2(x) tmp_flops, tmp_acts = cal_conv_stats(self.conv2, in_x, x) activations += tmp_acts flops += tmp_flops in_x = deepcopy(x) x = self.bn2(x) tmp_flops, tmp_acts = cal_norm_stats(self.bn2, in_x, x) activations += tmp_acts flops += tmp_flops if self.tmp_in_channels == self.tmp_channels and self.stride == 1: identity = self.downsample_id(identity) else: in_x = deepcopy(identity) identity = self.downsample_conv(identity) tmp_flops, tmp_acts = cal_conv_stats(self.downsample_conv, in_x, identity) activations += tmp_acts flops += tmp_flops in_x = deepcopy(identity) identity = self.downsample_norm(identity) tmp_flops, tmp_acts = cal_norm_stats(self.downsample_norm, in_x, identity) activations += tmp_acts flops += tmp_flops x += identity x = F.relu(x) return x, flops, activations
def two_layer_conv(x): # (8, 3, 3, 3) 代表(输出信道数,输入信道数,卷积核高度,卷积核宽度) conv_weight = mge.Parameter(np.random.randn(8, 3, 3, 3).astype(np.float32)) # 对于 8 个卷积核,提供 8 个 bias conv_bias = mge.Parameter(np.zeros((1, 8, 1, 1), dtype=np.float32)) x = F.conv2d(x, conv_weight, conv_bias) x = F.relu(x) conv_weight = mge.Parameter( np.random.randn(16, 8, 3, 3).astype(np.float32)) conv_bias = mge.Parameter(np.zeros((1, 16, 1, 1), dtype=np.float32)) x = F.conv2d(x, conv_weight, conv_bias) x = F.relu(x) return x
def forward(self, x): x1 = self.conv(x) x1 = self.bn(x1) x1 = F.relu(x1) x1 = x1 * self.scale[0] x2 = self.conv2(x) x2 = self.bn2(x2) x2 = F.relu(x2) x2 = x2 * self.scale[1] y = x1 + x2 y = y + 4 y = self.scale[0] + y y = F.relu(y) * 3 return y
def forward(self, fpn_fms, rcnn_rois, im_info=None, gt_boxes=None): rcnn_rois, labels, bbox_targets = self.get_ground_truth( rcnn_rois, im_info, gt_boxes) fpn_fms = [fpn_fms[x] for x in self.in_features] pool_features = layers.roi_pool( fpn_fms, rcnn_rois, self.stride, self.pooling_size, self.pooling_method, ) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_cls = self.pred_cls(roi_feature) pred_delta = self.pred_delta(roi_feature) if self.training: # loss for classification loss_rcnn_cls = layers.softmax_loss(pred_cls, labels) # loss for regression pred_delta = pred_delta.reshape(-1, self.cfg.num_classes + 1, 4) vlabels = labels.reshape(-1, 1).broadcast((labels.shapeof(0), 4)) pred_delta = F.indexing_one_hot(pred_delta, vlabels, axis=1) loss_rcnn_loc = layers.get_smooth_l1_loss( pred_delta, bbox_targets, labels, self.cfg.rcnn_smooth_l1_beta, norm_type="all", ) loss_dict = { 'loss_rcnn_cls': loss_rcnn_cls, 'loss_rcnn_loc': loss_rcnn_loc } return loss_dict else: # slice 1 for removing background pred_scores = F.softmax(pred_cls, axis=1)[:, 1:] pred_delta = pred_delta[:, 4:].reshape(-1, 4) target_shape = (rcnn_rois.shapeof(0), self.cfg.num_classes, 4) # rois (N, 4) -> (N, 1, 4) -> (N, 80, 4) -> (N * 80, 4) base_rois = F.add_axis(rcnn_rois[:, 1:5], 1).broadcast(target_shape).reshape(-1, 4) pred_bbox = self.box_coder.decode(base_rois, pred_delta) return pred_bbox, pred_scores
def forward(self, x): identity = x x = self.conv1(x) x = self.bn1(x) x = F.relu(x) x = self.conv2(x) x = self.bn2(x) if self.tmp_in_channels == self.tmp_channels and self.stride == 1: identity = self.downsample_id(identity) else: identity = self.downsample_conv(identity) identity = self.downsample_norm(identity) x += identity x = F.relu(x) return x
def forward(self, x): """ Residual block feedforward function. """ # NOTE: to completely reproduce pytorch, we use F.relu(x) to replace x in shortcut # since pytorch use inplace relu in residual branch. return self._residual(x) + self._shortcut(F.relu(x))
def forward(self, x): x = self.conv0(x) if self.bn0: x = self.bn0(x) x = F.relu(x) x = self.pool0(x) x = self.conv1(x) if self.bn1: x = self.bn1(x) x = F.relu(x) x = self.pool1(x) x = F.flatten(x, 1) x = self.fc0(x) x = F.relu(x) x = self.fc1(x) return x
def forward(self, x): x = self.t1(x) x = F.relu(x) x = self.t2(x) x = F.relu(x) x = self.t3(x) x = F.relu(x) x = self.t4(x) x = F.relu(x) logit = self.fc(x) logit = logit.reshape(-1, 28) #print('logit', logit.shape) #print('logit0', logit[0]) #print('logit1', logit[1]) return logit
def test_replace_var(): a = Tensor([1, 2]) b = Tensor([3, 4]) @trace(symbolic=True, capture_as_const=True) def fwd(a, b): return (a + b) * 2 fwd(a, b) orig_model = io.BytesIO() fwd.dump(orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False) orig_model.seek(0) graph = Net.load(orig_model) vara = graph.var_filter.name("a").as_unique() varb = graph.var_filter.name("b").as_unique() out = F.mul(vara, varb) out = F.relu(out) opnode = list(graph.opr_filter.has_input(vara)) repl_dict = {opnode[0].outputs[0]: out} graph.replace_vars(repl_dict) modified_model = io.BytesIO() graph.dump(modified_model) modified_model.seek(0) load_graph = GraphInference(modified_model) out = load_graph.run(a, b) np.testing.assert_equal(out["o"], [6, 16])
def test_replace_opr(): a = Tensor([1, 2]) b = Tensor([3, 4]) @trace(symbolic=True, capture_as_const=True) def fwd(a, b): return (a + b) * 2 fwd(a, b) orig_model = io.BytesIO() fwd.dump(orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False) orig_model.seek(0) graph = Net.load(orig_model) vara = graph.var_filter.name("a").as_unique() varb = graph.var_filter.name("b").as_unique() out1 = F.sub(vara, varb) out1 = F.relu(out1) out1 = graph.add_dep_oprs(out1) orig_opr = graph.opr_filter.has_input(vara).as_unique() repl_dict = {orig_opr: out1[0].owner} graph.replace_oprs(repl_dict) modified_model1 = io.BytesIO() graph.dump(modified_model1) modified_model1.seek(0) load_graph = GraphInference(modified_model1) out = load_graph.run(a, b) np.testing.assert_equal(out["o"], [0, 0])
def forward(self, x): # x: bxlxi x = self.init_map(x) # bxlxe ori = x p = self.position_encoding(eye) x = x + p values = self.value_mapping(x) #bxlxe keys = self.key_mapping(x) #bxlxe querys = self.key_mapping(x) #print('transformer', values.shape, keys.shape, querys.shape) attention = F.softmax(F.batched_matrix_mul(querys, keys.dimshuffle(0, 2, 1)), axis=1) #bxlxl #print(attention[0]) #print(attention[0].sum(axis=0)) #print('attention', attention.shape) out = F.batched_matrix_mul(values.dimshuffle(0, 2, 1), attention) out = out.dimshuffle(0, 2, 1) out = out + ori out = F.relu(out) #a,b,c = out.shape[0], out.shape[1], out.shape[2] #tmp = out.reshape(-1, self.key_embedding) #i = tmp.shape[0] #out = self.norm(tmp) #out = out.reshape(a,b,c) return out
def forward(self, x): features = self.features(x) out = F.relu(features) out = F.avg_pool2d(out, kernel_size=7, stride=1).reshape(features.shape[0], -1) out = self.classifier(out) return out
def forward(self, fpn_fms, rcnn_rois, im_info=None, gt_boxes=None): rcnn_rois, labels, bbox_targets = self.get_ground_truth( rcnn_rois, im_info, gt_boxes) fpn_fms = [fpn_fms[x] for x in self.in_features] pool_features = layers.roi_pool( fpn_fms, rcnn_rois, self.stride, self.pooling_size, self.pooling_method, ) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_logits = self.pred_cls(roi_feature) pred_offsets = self.pred_delta(roi_feature) if self.training: # loss for rcnn classification loss_rcnn_cls = F.loss.cross_entropy(pred_logits, labels, axis=1) # loss for rcnn regression pred_offsets = pred_offsets.reshape(-1, self.cfg.num_classes, 4) num_samples = labels.shape[0] fg_mask = labels > 0 loss_rcnn_bbox = layers.smooth_l1_loss( pred_offsets[fg_mask, labels[fg_mask] - 1], bbox_targets[fg_mask], self.cfg.rcnn_smooth_l1_beta, ).sum() / F.maximum(num_samples, 1) loss_dict = { "loss_rcnn_cls": loss_rcnn_cls, "loss_rcnn_bbox": loss_rcnn_bbox, } return loss_dict else: # slice 1 for removing background pred_scores = F.softmax(pred_logits, axis=1)[:, 1:] pred_offsets = pred_offsets.reshape(-1, 4) target_shape = (rcnn_rois.shape[0], self.cfg.num_classes, 4) # rois (N, 4) -> (N, 1, 4) -> (N, 80, 4) -> (N * 80, 4) base_rois = F.broadcast_to( F.expand_dims(rcnn_rois[:, 1:5], axis=1), target_shape).reshape(-1, 4) pred_bbox = self.box_coder.decode(base_rois, pred_offsets) return pred_bbox, pred_scores
def forward(self, features, im_info, boxes=None): # prediction features = [features[x] for x in self.in_features] # get anchors all_anchors_list = [ self.anchors_generator(fm, stride) for fm, stride in zip(features, self.stride_list) ] pred_cls_logit_list = [] pred_bbox_offset_list = [] for x in features: t = F.relu(self.rpn_conv(x)) scores = self.rpn_cls_score(t) pred_cls_logit_list.append( scores.reshape( scores.shape[0], 2, self.num_cell_anchors, scores.shape[2], scores.shape[3], )) bbox_offsets = self.rpn_bbox_offsets(t) pred_bbox_offset_list.append( bbox_offsets.reshape( bbox_offsets.shape[0], self.num_cell_anchors, 4, bbox_offsets.shape[2], bbox_offsets.shape[3], )) # sample from the predictions rpn_rois = self.find_top_rpn_proposals(pred_bbox_offset_list, pred_cls_logit_list, all_anchors_list, im_info) if self.training: rpn_labels, rpn_bbox_targets = self.get_ground_truth( boxes, im_info, all_anchors_list) pred_cls_logits, pred_bbox_offsets = self.merge_rpn_score_box( pred_cls_logit_list, pred_bbox_offset_list) # rpn loss loss_rpn_cls = layers.softmax_loss(pred_cls_logits, rpn_labels) loss_rpn_loc = layers.get_smooth_l1_loss( pred_bbox_offsets, rpn_bbox_targets, rpn_labels, self.cfg.rpn_smooth_l1_beta, norm_type="all", ) loss_dict = { "loss_rpn_cls": loss_rpn_cls, "loss_rpn_loc": loss_rpn_loc } return rpn_rois, loss_dict else: return rpn_rois
def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = out.mean(3).mean(2) out = self.linear(out) return out
def forward(self, x): identity = x x = self.conv_bn_relu1(x) x = self.conv_bn2(x) identity = self.downsample(identity) x = self.add(x, identity) x = F.relu(x) return x
def forward(self, features: List[Tensor]): logits, offsets, ctrness = [], [], [] for feature, scale, stride in zip(features, self.scale_list, self.stride_list): logits.append(self.cls_score(self.cls_subnet(feature))) bbox_subnet = self.bbox_subnet(feature) offsets.append(F.relu(self.bbox_pred(bbox_subnet) * scale) * stride) ctrness.append(self.ctrness(bbox_subnet)) return logits, offsets, ctrness
def func(x, y): a = x + y a1 = F.relu(a) a2 = F.abs(a) a3 = F.ceil(a) * 2 a4 = F.floor(a) r = a1 - a2 r1 = a3 / a4 return r, r1
def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = F.relu(x) x = self.avgpool(x) x = F.avg_pool2d(x, 22) x = F.flatten(x, 1) x = self.fc(x) return x
def forward(self, fpn_fms, rcnn_rois, labels=None, bbox_targets=None): # stride: 64,32,16,8,4 -> 4, 8, 16, 32 fpn_fms = fpn_fms[1:][::-1] stride = [4, 8, 16, 32] pool_features, rcnn_rois, labels, bbox_targets = roi_pool( fpn_fms, rcnn_rois, stride, (7, 7), 'roi_align', labels, bbox_targets) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_cls = self.pred_cls(roi_feature) pred_delta = self.pred_delta(roi_feature) if self.training: # loss for regression labels = labels.astype(np.int32).reshape(-1) # mulitple class to one pos_masks = labels > 0 pred_delta = pred_delta.reshape(-1, config.num_classes, 4) indexing_label = (labels * pos_masks).reshape(-1, 1) indexing_label = indexing_label.broadcast((labels.shapeof()[0], 4)) pred_delta = F.indexing_one_hot(pred_delta, indexing_label, 1) localization_loss = smooth_l1_loss(pred_delta, bbox_targets, config.rcnn_smooth_l1_beta) localization_loss = localization_loss * pos_masks # loss for classification valid_masks = labels >= 0 objectness_loss = softmax_loss(pred_cls, labels) objectness_loss = objectness_loss * valid_masks normalizer = 1.0 / (valid_masks.sum()) loss_rcnn_cls = objectness_loss.sum() * normalizer loss_rcnn_loc = localization_loss.sum() * normalizer loss_dict = {} loss_dict['loss_rcnn_cls'] = loss_rcnn_cls loss_dict['loss_rcnn_loc'] = loss_rcnn_loc return loss_dict else: pred_scores = F.softmax(pred_cls)[:, 1:].reshape(-1, 1) pred_delta = pred_delta[:, 4:].reshape(-1, 4) target_shape = (rcnn_rois.shapeof()[0], config.num_classes - 1, 4) base_rois = F.add_axis(rcnn_rois[:, 1:5], 1).broadcast(target_shape).reshape(-1, 4) pred_bbox = restore_bbox(base_rois, pred_delta, True) pred_bbox = F.concat([pred_bbox, pred_scores], axis=1) return pred_bbox
def forward(self, fpn_fms, rcnn_rois, labels=None, bbox_targets=None): # stride: 64,32,16,8,4 -> 4, 8, 16, 32 fpn_fms = fpn_fms[1:][::-1] stride = [4, 8, 16, 32] pool_features, rcnn_rois, labels, bbox_targets = roi_pool( fpn_fms, rcnn_rois, stride, (7, 7), 'roi_align', labels, bbox_targets) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_emd_pred_cls_0 = self.emd_pred_cls_0(roi_feature) pred_emd_pred_delta_0 = self.emd_pred_delta_0(roi_feature) pred_emd_pred_cls_1 = self.emd_pred_cls_1(roi_feature) pred_emd_pred_delta_1 = self.emd_pred_delta_1(roi_feature) if self.training: loss0 = emd_loss( pred_emd_pred_delta_0, pred_emd_pred_cls_0, pred_emd_pred_delta_1, pred_emd_pred_cls_1, bbox_targets, labels) loss1 = emd_loss( pred_emd_pred_delta_1, pred_emd_pred_cls_1, pred_emd_pred_delta_0, pred_emd_pred_cls_0, bbox_targets, labels) loss = F.concat([loss0, loss1], axis=1) indices = F.argmin(loss, axis=1) loss_emd = F.indexing_one_hot(loss, indices, 1) loss_emd = loss_emd.sum()/loss_emd.shapeof()[0] loss_dict = {} loss_dict['loss_rcnn_emd'] = loss_emd return loss_dict else: pred_scores_0 = F.softmax(pred_emd_pred_cls_0)[:, 1:].reshape(-1, 1) pred_scores_1 = F.softmax(pred_emd_pred_cls_1)[:, 1:].reshape(-1, 1) pred_delta_0 = pred_emd_pred_delta_0[:, 4:].reshape(-1, 4) pred_delta_1 = pred_emd_pred_delta_1[:, 4:].reshape(-1, 4) target_shape = (rcnn_rois.shapeof()[0], config.num_classes - 1, 4) base_rois = F.add_axis(rcnn_rois[:, 1:5], 1).broadcast(target_shape).reshape(-1, 4) pred_bbox_0 = restore_bbox(base_rois, pred_delta_0, True) pred_bbox_1 = restore_bbox(base_rois, pred_delta_1, True) pred_bbox_0 = F.concat([pred_bbox_0, pred_scores_0], axis=1) pred_bbox_1 = F.concat([pred_bbox_1, pred_scores_1], axis=1) #[{head0, pre1, tag1}, {head1, pre1, tag1}, {head0, pre1, tag2}, ...] pred_bbox = F.concat((pred_bbox_0, pred_bbox_1), axis=1).reshape(-1,5) return pred_bbox
def forward(self, fpn_fms, rcnn_rois, labels=None, bbox_targets=None): # stride: 64,32,16,8,4 -> 4, 8, 16, 32 fpn_fms = fpn_fms[1:] fpn_fms.reverse() stride = [4, 8, 16, 32] poo5, rcnn_rois, labels, bbox_targets = roi_pool( fpn_fms, rcnn_rois, stride, (7, 7), 'roi_align', labels, bbox_targets) poo5 = F.flatten(poo5, start_axis=1) fc1 = F.relu(self.fc1(poo5)) fc2 = F.relu(self.fc2(fc1)) cls_scores = self.cls(fc2) pred_boxes = self.bbox(fc2) # a = self.a(fc2) # b = self.b(fc2) # prob = F.stack([a, b], axis=1).reshape(-1, a.shape[1]) prob = F.concat([pred_boxes, cls_scores], axis=1) if self.training: # emd_loss = self.compute_gemini_loss(prob, bbox_targets, labels) bbox_targets, labels = bbox_targets.reshape(-1, 4), labels.flatten() cls_loss = softmax_loss(cls_scores, labels) pred_boxes = pred_boxes.reshape(-1, self.n, 4) bbox_loss = smooth_l1_loss_rcnn(pred_boxes, bbox_targets, labels, \ config.rcnn_smooth_l1_beta) loss_dict = {} loss_dict['cls_loss'] = cls_loss loss_dict['bbox_loss'] = bbox_loss return loss_dict else: offsets, cls_scores = prob[:, :-self.n], prob[:, -self.n:] pred_bbox = offsets.reshape(-1, self.n, 4) cls_prob = F.softmax(cls_scores, axis=1) n = rcnn_rois.shape[0] rois = F.broadcast_to(F.expand_dims(rcnn_rois[:, 1:5], axis=1), (n, 1, 4)).reshape(-1, 4) normalized = config.rcnn_bbox_normalize_targets pred_boxes = restore_bbox(rois, pred_bbox, normalized, config) pred_bbox = F.concat([pred_boxes, F.expand_dims(cls_prob, axis=2)], axis=2) return pred_bbox
def forward(self, fpn_fms, proposals, labels=None, bbox_targets=None): # input p2-p5 fpn_fms = fpn_fms[1:][::-1] stride = [4, 8, 16, 32] #pool_features = roi_pooler(fpn_fms, proposals, stride, (7, 7), "ROIAlignV2") pool_features, proposals, labels, bbox_targets = roi_pool( fpn_fms, proposals, stride, (7, 7), 'roi_align', labels, bbox_targets) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_cls = self.pred_cls(roi_feature) pred_delta = self.pred_delta(roi_feature) if self.training: # loss for regression labels = labels.astype(np.int32).reshape(-1) # mulitple class to one pos_masks = labels > 0 localization_loss = smooth_l1_loss( pred_delta, bbox_targets, config.rcnn_smooth_l1_beta) localization_loss = localization_loss * pos_masks # loss for classification valid_masks = labels >= 0 objectness_loss = softmax_loss( pred_cls, labels) objectness_loss = objectness_loss * valid_masks normalizer = 1.0 / (valid_masks.sum()) loss_rcnn_cls = objectness_loss.sum() * normalizer loss_rcnn_loc = localization_loss.sum() * normalizer loss_dict = {} loss_dict[self.stage_name + '_cls'] = loss_rcnn_cls loss_dict[self.stage_name + '_loc'] = loss_rcnn_loc pred_bbox = restore_bbox(proposals[:, 1:5], pred_delta, True) pred_proposals = F.zero_grad(F.concat([proposals[:, 0].reshape(-1, 1), pred_bbox], axis=1)) return pred_proposals, loss_dict else: pred_scores = F.softmax(pred_cls)[:, 1].reshape(-1, 1) pred_bbox = restore_bbox(proposals[:, 1:5], pred_delta, True) pred_proposals = F.concat([proposals[:, 0].reshape(-1, 1), pred_bbox], axis=1) return pred_proposals, pred_scores
def forward(self, fpn_fms, rcnn_rois, labels=None, bbox_targets=None): # stride: 64,32,16,8,4 -> 4, 8, 16, 32 fpn_fms = fpn_fms[1:] fpn_fms.reverse() stride = [4, 8, 16, 32] poo5, rcnn_rois, labels, bbox_targets = roi_pool( fpn_fms, rcnn_rois, stride, (7, 7), 'roi_align', labels, bbox_targets) poo5 = F.flatten(poo5, start_axis=1) fc1 = F.relu(self.fc1(poo5)) fc2 = F.relu(self.fc2(fc1)) a = self.a(fc2) b = self.b(fc2) prob = F.stack([a, b], axis=1).reshape(-1, a.shape[1]) if self.refinement: final_prob = self.refinement_module(prob, fc2) if self.training: emd_loss = self.compute_gemini_loss(prob, bbox_targets, labels) loss_dict = {} loss_dict['loss_rcnn_emd'] = emd_loss if self.refinement_module: final_emd_loss = self.compute_gemini_loss( final_prob, bbox_targets, labels) loss_dict['final_rcnn_emd'] = final_emd_loss return loss_dict else: offsets, cls_scores = prob[:, :-self.n], prob[:, -self.n:] pred_bbox = offsets.reshape(-1, self.n, 4) cls_prob = F.softmax(cls_scores, axis=1) n = rcnn_rois.shape[0] rois = F.broadcast_to(F.expand_dims(rcnn_rois[:, 1:5], axis=1), (n, 2, 4)).reshape(-1, 4) normalized = config.rcnn_bbox_normalize_targets pred_boxes = restore_bbox(rois, pred_bbox, normalized, config) pred_bbox = F.concat( [pred_boxes, F.expand_dims(cls_prob, axis=2)], axis=2) return pred_bbox
def run_conv2d(inp, w, b): O = F.conv2d( inp, w, b if has_bias else None, stride=(SH, SW), padding=(PH, PW), ) if nonlinear_mode == "relu": return F.relu(O) else: return O
def forward(self, fpn_fms, rcnn_rois, labels=None, bbox_targets=None): # stride: 64,32,16,8,4 -> 4, 8, 16, 32 fpn_fms = fpn_fms[1:][::-1] stride = [4, 8, 16, 32] pool_features, rcnn_rois, labels, bbox_targets = roi_pool( fpn_fms, rcnn_rois, stride, (7, 7), 'roi_align', labels, bbox_targets) flatten_feature = F.flatten(pool_features, start_axis=1) roi_feature = F.relu(self.fc1(flatten_feature)) roi_feature = F.relu(self.fc2(roi_feature)) pred_cls = self.pred_cls(roi_feature) pred_delta = self.pred_delta(roi_feature) if self.training: # loss for regression labels = labels.astype(np.int32).reshape(-1) pos_masks = labels > 0 localization_loss = smooth_l1_loss( pred_delta, bbox_targets, config.rcnn_smooth_l1_beta) localization_loss = localization_loss * pos_masks # loss for classification valid_masks = labels >= 0 objectness_loss = softmax_loss( pred_cls, labels) objectness_loss = objectness_loss * valid_masks normalizer = 1.0 / (valid_masks.sum()) loss_rcnn_cls = objectness_loss.sum() * normalizer loss_rcnn_loc = localization_loss.sum() * normalizer loss_dict = {} loss_dict['loss_rcnn_cls'] = loss_rcnn_cls loss_dict['loss_rcnn_loc'] = loss_rcnn_loc return loss_dict else: pred_scores = F.softmax(pred_cls) pred_bbox = restore_bbox(rcnn_rois[:, 1:5], pred_delta, True) pred_bbox = F.concat([pred_bbox, pred_scores[:, 1].reshape(-1,1)], axis=1) return pred_bbox