我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用network.np_to_variable()。
def forward(self, im_data, gt_data=None): im_data = network.np_to_variable(im_data, is_cuda=True, is_training=self.training) density_map = self.DME(im_data) if self.training: gt_data = network.np_to_variable(gt_data, is_cuda=True, is_training=self.training) self.loss_mse = self.build_loss(density_map, gt_data) return density_map
def forward(self, im_data, im_info, gt_boxes=None, gt_ishard=None, dontcare_areas=None): im_data = network.np_to_variable(im_data, is_cuda=True) im_data = im_data.permute(0, 3, 1, 2) features = self.features(im_data) rpn_conv1 = self.conv1(features) # rpn score rpn_cls_score = self.score_conv(rpn_conv1) rpn_cls_score_reshape = self.reshape_layer(rpn_cls_score, 2) rpn_cls_prob = F.softmax(rpn_cls_score_reshape) rpn_cls_prob_reshape = self.reshape_layer(rpn_cls_prob, len(self.anchor_scales)*3*2) # rpn boxes rpn_bbox_pred = self.bbox_conv(rpn_conv1) # proposal layer cfg_key = 'TRAIN' if self.training else 'TEST' rois = self.proposal_layer(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, self._feat_stride, self.anchor_scales) # generating training labels and build the rpn loss if self.training: assert gt_boxes is not None rpn_data = self.anchor_target_layer(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas, im_info, self._feat_stride, self.anchor_scales) self.cross_entropy, self.loss_box = self.build_loss(rpn_cls_score_reshape, rpn_bbox_pred, rpn_data) return features, rois
def proposal_layer(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales): rpn_cls_prob_reshape = rpn_cls_prob_reshape.data.cpu().numpy() rpn_bbox_pred = rpn_bbox_pred.data.cpu().numpy() x = proposal_layer_py(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales) x = network.np_to_variable(x, is_cuda=True) return x.view(-1, 5)
def anchor_target_layer(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas, im_info, _feat_stride, anchor_scales): """ rpn_cls_score: for pytorch (1, Ax2, H, W) bg/fg scores of previous conv layer gt_boxes: (G, 5) vstack of [x1, y1, x2, y2, class] gt_ishard: (G, 1), 1 or 0 indicates difficult or not dontcare_areas: (D, 4), some areas may contains small objs but no labelling. D may be 0 im_info: a list of [image_height, image_width, scale_ratios] _feat_stride: the downsampling ratio of feature map to the original input image anchor_scales: the scales to the basic_anchor (basic anchor is [16, 16]) ---------- Returns ---------- rpn_labels : (1, 1, HxA, W), for each anchor, 0 denotes bg, 1 fg, -1 dontcare rpn_bbox_targets: (1, 4xA, H, W), distances of the anchors to the gt_boxes(may contains some transform) that are the regression objectives rpn_bbox_inside_weights: (1, 4xA, H, W) weights of each boxes, mainly accepts hyper param in cfg rpn_bbox_outside_weights: (1, 4xA, H, W) used to balance the fg/bg, beacuse the numbers of bgs and fgs mays significiantly different """ rpn_cls_score = rpn_cls_score.data.cpu().numpy() rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = \ anchor_target_layer_py(rpn_cls_score, gt_boxes, gt_ishard, dontcare_areas, im_info, _feat_stride, anchor_scales) rpn_labels = network.np_to_variable(rpn_labels, is_cuda=True, dtype=torch.LongTensor) rpn_bbox_targets = network.np_to_variable(rpn_bbox_targets, is_cuda=True) rpn_bbox_inside_weights = network.np_to_variable(rpn_bbox_inside_weights, is_cuda=True) rpn_bbox_outside_weights = network.np_to_variable(rpn_bbox_outside_weights, is_cuda=True) return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def proposal_target_layer(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes): """ ---------- rpn_rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] gt_boxes: (G, 5) [x1 ,y1 ,x2, y2, class] int # gt_ishard: (G, 1) {0 | 1} 1 indicates hard dontcare_areas: (D, 4) [ x1, y1, x2, y2] num_classes ---------- Returns ---------- rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] labels: (1 x H x W x A, 1) {0,1,...,_num_classes-1} bbox_targets: (1 x H x W x A, K x4) [dx1, dy1, dx2, dy2] bbox_inside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss bbox_outside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss """ rpn_rois = rpn_rois.data.cpu().numpy() rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = \ proposal_target_layer_py(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes) # print labels.shape, bbox_targets.shape, bbox_inside_weights.shape rois = network.np_to_variable(rois, is_cuda=True) labels = network.np_to_variable(labels, is_cuda=True, dtype=torch.LongTensor) bbox_targets = network.np_to_variable(bbox_targets, is_cuda=True) bbox_inside_weights = network.np_to_variable(bbox_inside_weights, is_cuda=True) bbox_outside_weights = network.np_to_variable(bbox_outside_weights, is_cuda=True) return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
def proposal_layer(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales, anchor_ratios, is_region): rpn_cls_prob_reshape = rpn_cls_prob_reshape.data.cpu().numpy() rpn_bbox_pred = rpn_bbox_pred.data.cpu().numpy() x = proposal_layer_py(rpn_cls_prob_reshape, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchor_scales, anchor_ratios, is_region=is_region) x = network.np_to_variable(x, is_cuda=True) return x.view(-1, 5)
def anchor_target_layer(rpn_cls_score, gt_boxes, dontcare_areas, im_info, _feat_stride, anchor_scales, anchor_rotios, is_region=False): """ rpn_cls_score: for pytorch (1, Ax2, H, W) bg/fg scores of previous conv layer gt_boxes: (G, 5) vstack of [x1, y1, x2, y2, class] #gt_ishard: (G, 1), 1 or 0 indicates difficult or not dontcare_areas: (D, 4), some areas may contains small objs but no labelling. D may be 0 im_info: a list of [image_height, image_width, scale_ratios] _feat_stride: the downsampling ratio of feature map to the original input image anchor_scales: the scales to the basic_anchor (basic anchor is [16, 16]) ---------- Returns ---------- rpn_labels : (1, 1, HxA, W), for each anchor, 0 denotes bg, 1 fg, -1 dontcare rpn_bbox_targets: (1, 4xA, H, W), distances of the anchors to the gt_boxes(may contains some transform) that are the regression objectives rpn_bbox_inside_weights: (1, 4xA, H, W) weights of each boxes, mainly accepts hyper param in cfg rpn_bbox_outside_weights: (1, 4xA, H, W) used to balance the fg/bg, beacuse the numbers of bgs and fgs mays significiantly different """ rpn_cls_score = rpn_cls_score.data.cpu().numpy() rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = \ anchor_target_layer_py(rpn_cls_score, gt_boxes, dontcare_areas, im_info, _feat_stride, anchor_scales, anchor_rotios, is_region=is_region) rpn_labels = network.np_to_variable(rpn_labels, is_cuda=True, dtype=torch.LongTensor) rpn_bbox_targets = network.np_to_variable(rpn_bbox_targets, is_cuda=True) rpn_bbox_inside_weights = network.np_to_variable(rpn_bbox_inside_weights, is_cuda=True) rpn_bbox_outside_weights = network.np_to_variable(rpn_bbox_outside_weights, is_cuda=True) return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def forward(self, im_data, gt_data=None, gt_cls_label=None, ce_weights=None): im_data = network.np_to_variable(im_data, is_cuda=True, is_training=self.training) density_map, density_cls_score = self.CCN(im_data) density_cls_prob = F.softmax(density_cls_score) if self.training: gt_data = network.np_to_variable(gt_data, is_cuda=True, is_training=self.training) gt_cls_label = network.np_to_variable(gt_cls_label, is_cuda=True, is_training=self.training,dtype=torch.FloatTensor) self.loss_mse, self.cross_entropy = self.build_loss(density_map, density_cls_prob, gt_data, gt_cls_label, ce_weights) return density_map
def proposal_target_layer(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes): """ ---------- rpn_rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] gt_boxes: (G, 5) [x1 ,y1 ,x2, y2, class] int # gt_ishard: (G, 1) {0 | 1} 1 indicates hard dontcare_areas: (D, 4) [x1, y1, x2, y2] num_classes ---------- Returns ---------- rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] labels: (1 x H x W x A, 1) {0, 1, ..., _num_classes-1} bbox_targets: (1 x H x W x A, K x 4) [dx1, dy1, dx2, dy2] bbox_inside_weights: (1 x H x W x A, K x 4) 0, 1 masks for the computing loss bbox_outside_weights: (1 x H x W x A, K x 4) 0, 1 masks for the computing loss """ rpn_rois = rpn_rois.data.cpu().numpy() rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = \ proposal_target_layer_py(rpn_rois, gt_boxes, gt_ishard, dontcare_areas, num_classes) # print labels.shape, bbox_targets.shape, bbox_inside_weights.shape rois = network.np_to_variable(rois, is_cuda=True) labels = network.np_to_variable(labels, is_cuda=True, dtype=torch.LongTensor) bbox_targets = network.np_to_variable(bbox_targets, is_cuda=True) bbox_inside_weights = network.np_to_variable(bbox_inside_weights, is_cuda=True) bbox_outside_weights = network.np_to_variable(bbox_outside_weights, is_cuda=True) return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
def proposal_target_layer(object_rois, region_rois, gt_objects, gt_relationships, gt_regions, n_classes_obj, voc_sign, is_training=False, graph_generation=False): """ ---------- object_rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] region_rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] gt_objects: (G_obj, 5) [x1 ,y1 ,x2, y2, obj_class] int gt_relationships: (G_obj, G_obj) [pred_class] int (-1 for no relationship) gt_regions: (G_region, 4+40) [x1, y1, x2, y2, word_index] (-1 for padding) # gt_ishard: (G_region, 4+40) {0 | 1} 1 indicates hard # dontcare_areas: (D, 4) [ x1, y1, x2, y2] n_classes_obj n_classes_pred is_training to indicate whether in training scheme ---------- Returns ---------- rois: (1 x H x W x A, 5) [0, x1, y1, x2, y2] labels: (1 x H x W x A, 1) {0,1,...,_num_classes-1} bbox_targets: (1 x H x W x A, K x4) [dx1, dy1, dx2, dy2] bbox_inside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss bbox_outside_weights: (1 x H x W x A, Kx4) 0, 1 masks for the computing loss """ object_rois = object_rois.data.cpu().numpy() region_rois = region_rois.data.cpu().numpy() object_labels, object_rois, bbox_targets, bbox_inside_weights, bbox_outside_weights, mat_object, \ phrase_label, phrase_rois, mat_phrase, region_seq, region_rois, \ bbox_targets_region, bbox_inside_weights_region, bbox_outside_weights_region, mat_region= \ proposal_target_layer_py(object_rois, region_rois, gt_objects, gt_relationships, gt_regions, n_classes_obj, voc_sign, is_training, graph_generation=graph_generation) # print labels.shape, bbox_targets.shape, bbox_inside_weights.shape if is_training: object_labels = network.np_to_variable(object_labels, is_cuda=True, dtype=torch.LongTensor) bbox_targets = network.np_to_variable(bbox_targets, is_cuda=True) bbox_inside_weights = network.np_to_variable(bbox_inside_weights, is_cuda=True) bbox_outside_weights = network.np_to_variable(bbox_outside_weights, is_cuda=True) phrase_label = network.np_to_variable(phrase_label, is_cuda=True, dtype=torch.LongTensor) region_seq = network.np_to_variable(region_seq, is_cuda=True, dtype=torch.LongTensor) bbox_targets_region = network.np_to_variable(bbox_targets_region, is_cuda=True) bbox_inside_weights_region = network.np_to_variable(bbox_inside_weights_region, is_cuda=True) bbox_outside_weights_region = network.np_to_variable(bbox_outside_weights_region, is_cuda=True) object_rois = network.np_to_variable(object_rois, is_cuda=True) phrase_rois = network.np_to_variable(phrase_rois, is_cuda=True) region_rois = network.np_to_variable(region_rois, is_cuda=True) return (object_rois, object_labels, bbox_targets, bbox_inside_weights, bbox_outside_weights), \ (phrase_rois, phrase_label), \ (region_rois, region_seq, bbox_targets_region, bbox_inside_weights_region, bbox_outside_weights_region), \ mat_object, mat_phrase, mat_region