def _topk(self, scores): k = self.max_per_img shape_fm = paddle.shape(scores) shape_fm.stop_gradient = True cat, height, width = shape_fm[1], shape_fm[2], shape_fm[3] # batch size is 1 scores_r = paddle.reshape(scores, [cat, -1]) topk_scores, topk_inds = paddle.topk(scores_r, k) topk_scores, topk_inds = paddle.topk(scores_r, k) topk_ys = topk_inds // width topk_xs = topk_inds % width topk_score_r = paddle.reshape(topk_scores, [-1]) topk_score, topk_ind = paddle.topk(topk_score_r, k) k_t = paddle.full(paddle.shape(topk_ind), k, dtype='int64') topk_clses = paddle.cast(paddle.floor_divide(topk_ind, k_t), 'float32') topk_inds = paddle.reshape(topk_inds, [-1]) topk_ys = paddle.reshape(topk_ys, [-1, 1]) topk_xs = paddle.reshape(topk_xs, [-1, 1]) topk_inds = paddle.gather(topk_inds, topk_ind) topk_ys = paddle.gather(topk_ys, topk_ind) topk_xs = paddle.gather(topk_xs, topk_ind) return topk_score, topk_inds, topk_clses, topk_ys, topk_xs
def dynamic_k_matching(self, cost_matrix, pairwise_ious, num_gt): match_matrix = np.zeros_like(cost_matrix.numpy()) # select candidate topk ious for dynamic-k calculation topk_ious, _ = paddle.topk(pairwise_ious, self.candidate_topk, axis=0) # calculate dynamic k for each gt dynamic_ks = paddle.clip(topk_ious.sum(0).cast('int'), min=1) for gt_idx in range(num_gt): _, pos_idx = paddle.topk(cost_matrix[:, gt_idx], k=dynamic_ks[gt_idx], largest=False) match_matrix[:, gt_idx][pos_idx.numpy()] = 1.0 del topk_ious, dynamic_ks, pos_idx # match points more than two gts extra_match_gts_mask = match_matrix.sum(1) > 1 if extra_match_gts_mask.sum() > 0: cost_matrix = cost_matrix.numpy() cost_argmin = np.argmin(cost_matrix[extra_match_gts_mask, :], axis=1) match_matrix[extra_match_gts_mask, :] *= 0.0 match_matrix[extra_match_gts_mask, cost_argmin] = 1.0 # get foreground mask match_fg_mask_inmatrix = match_matrix.sum(1) > 0 match_gt_inds_to_fg = match_matrix[match_fg_mask_inmatrix, :].argmax(1) return match_gt_inds_to_fg, match_fg_mask_inmatrix
def _topk(scores, K=40): # batch, cat, height, width = scores.size() batch, cat, height, width = scores.shape # topk_scores, topk_inds = torch.topk(scores.view(batch, cat, -1), K) topk_scores, topk_inds = paddle.topk(scores.reshape([batch, cat, -1]), K) topk_inds = topk_inds % (height * width) # topk_ys = (topk_inds / width).int().float() topk_ys = (topk_inds / width).cast('int32').cast('float32') # topk_xs = (topk_inds % width).int().float() topk_xs = (topk_inds % width).cast('int32').cast('float32') # topk_score, topk_ind = torch.topk(topk_scores.view(batch, -1), K) topk_score, topk_ind = paddle.topk(topk_scores.reshape([batch, -1]), K) # topk_clses = (topk_ind / K).int() topk_clses = (topk_ind / K).cast('int32') # topk_inds = _gather_feat( # topk_inds.view(batch, -1, 1), topk_ind).view(batch, K) topk_inds = _gather_feat(topk_inds.reshape([batch, -1, 1]), topk_ind).reshape([batch, K]) # topk_ys = _gather_feat(topk_ys.view(batch, -1, 1), topk_ind).view(batch, K) topk_ys = _gather_feat(topk_ys.reshape([batch, -1, 1]), topk_ind).reshape([batch, K]) # topk_xs = _gather_feat(topk_xs.view(batch, -1, 1), topk_ind).view(batch, K) topk_xs = _gather_feat(topk_xs.reshape([batch, -1, 1]), topk_ind).reshape([batch, K]) return topk_score, topk_inds, topk_clses, topk_ys, topk_xs
def test_errors(self): with paddle.fluid.dygraph.guard(): x = paddle.to_tensor([1, 2, 3]) with self.assertRaises(BaseException): paddle.topk(x, k=-1) with self.assertRaises(BaseException): paddle.topk(x, k=0)
def test_errors(self): paddle.disable_static() x = paddle.to_tensor([1, 2, 3]) with self.assertRaises(BaseException): paddle.topk(x, k=-1) with self.assertRaises(BaseException): paddle.topk(x, k=0)
def __call__(self, head_out, im_shape, scale_factor): """ Decode the bbox. Args: head_out (tuple): bbox_pred, cls_logit and masks of bbox_head output. im_shape (Tensor): The shape of the input image. scale_factor (Tensor): The scale factor of the input image. Returns: bbox_pred (Tensor): The output prediction with shape [N, 6], including labels, scores and bboxes. The size of bboxes are corresponding to the input image, the bboxes may be used in other branch. bbox_num (Tensor): The number of prediction boxes of each batch with shape [bs], and is N. """ bboxes, logits, masks = head_out bbox_pred = bbox_cxcywh_to_xyxy(bboxes) origin_shape = paddle.floor(im_shape / scale_factor + 0.5) img_h, img_w = origin_shape.unbind(1) origin_shape = paddle.stack( [img_w, img_h, img_w, img_h], axis=-1).unsqueeze(0) bbox_pred *= origin_shape scores = F.sigmoid(logits) if self.use_focal_loss else F.softmax( logits)[:, :, :-1] if not self.use_focal_loss: scores, labels = scores.max(-1), scores.argmax(-1) if scores.shape[1] > self.num_top_queries: scores, index = paddle.topk( scores, self.num_top_queries, axis=-1) labels = paddle.stack( [paddle.gather(l, i) for l, i in zip(labels, index)]) bbox_pred = paddle.stack( [paddle.gather(b, i) for b, i in zip(bbox_pred, index)]) else: scores, index = paddle.topk( scores.reshape([logits.shape[0], -1]), self.num_top_queries, axis=-1) labels = index % logits.shape[2] index = index // logits.shape[2] bbox_pred = paddle.stack( [paddle.gather(b, i) for b, i in zip(bbox_pred, index)]) bbox_pred = paddle.concat( [ labels.unsqueeze(-1).astype('float32'), scores.unsqueeze(-1), bbox_pred ], axis=-1) bbox_num = paddle.to_tensor( bbox_pred.shape[1], dtype='int32').tile([bbox_pred.shape[0]]) bbox_pred = bbox_pred.reshape([-1, 6]) return bbox_pred, bbox_num
def test_errors(self): self.__class__.use_npu = True self.place = paddle.NPUPlace(0) paddle.disable_static() x = paddle.to_tensor([1, 2, 3]) with self.assertRaises(BaseException): paddle.topk(x, k=-1) with self.assertRaises(BaseException): paddle.topk(x, k=0)
def select_topk(heat_map, K=100): """ Args: heat_map: heat_map in [N, C, H, W] K: top k samples to be selected score: detection threshold Returns: """ #batch, c, height, width = paddle.shape(heat_map) batch, c = heat_map.shape[:2] height = paddle.shape(heat_map)[2] width = paddle.shape(heat_map)[3] # First select topk scores in all classes and batchs # [N, C, H, W] -----> [N, C, H*W] heat_map = paddle.reshape(heat_map, (batch, c, -1)) # Both in [N, C, K] topk_scores_all, topk_inds_all = paddle.topk(heat_map, K) # topk_inds_all = topk_inds_all % (height * width) # todo: this seems redudant topk_ys = (topk_inds_all // width).astype("float32") topk_xs = (topk_inds_all % width).astype("float32") # Select topK examples across channel # [N, C, K] -----> [N, C*K] topk_scores_all = paddle.reshape(topk_scores_all, (batch, -1)) # Both in [N, K] topk_scores, topk_inds = paddle.topk(topk_scores_all, K) topk_clses = (topk_inds // K).astype("float32") # First expand it as 3 dimension topk_inds_all = paddle.reshape( _gather_feat(paddle.reshape(topk_inds_all, (batch, -1, 1)), topk_inds), (batch, K)) topk_ys = paddle.reshape( _gather_feat(paddle.reshape(topk_ys, (batch, -1, 1)), topk_inds), (batch, K)) topk_xs = paddle.reshape( _gather_feat(paddle.reshape(topk_xs, (batch, -1, 1)), topk_inds), (batch, K)) return dict({ "topk_score": topk_scores, "topk_inds_all": topk_inds_all, "topk_clses": topk_clses, "topk_ys": topk_ys, "topk_xs": topk_xs })
def topk(input, k, dim=None, largest=True, sorted=True, out=None): vals, inds = paddle.topk(input, k, axis=dim, largest=largest, sorted=sorted) return vals, inds
def forward(self): fpn_rois = self.input('FpnRois', 0) areas = self.bbox_area(fpn_rois) scale = paddle.sqrt(areas) num_level = self.max_level - self.min_level + 1 target_level = paddle.log(scale / self.refer_scale + 1e-06) / np.log(2) target_level = paddle.floor(self.refer_level + target_level) target_level = paddle.clip(target_level, min=self.min_level, max=self.max_level) rois = list() rois_idx_order = list() for level in range(self.min_level, self.max_level + 1): level_tensor = paddle.full_like(target_level, fill_value=level) res = paddle.equal(target_level, level_tensor) res = paddle.squeeze(res, axis=1) res = paddle.cast(res, dtype='int32') index = paddle.nonzero(res) roi = paddle.gather(fpn_rois, index, axis=0) rois.append(roi) rois_idx_order.append(index) rois_idx_order = paddle.concat(rois_idx_order, axis=0) size = paddle.shape(rois_idx_order)[0] _, rois_idx_restore = paddle.topk(rois_idx_order, axis=0, sorted=True, largest=False, k=size) #rois_idx_restore = paddle.cast(rois_idx_restore, dtype='int32') return {'MultiFpnRois': rois, 'RestoreIndex': [rois_idx_restore]}
def forward(self, inp): score = self.gate(inp) if self.training: noise = paddle.rand(shape=score.shape) noise = noise * 2 * self.switch_eps + 1.0 - self.switch_eps score += noise score = F.softmax(score, axis=-1) top1_score, top1_idx = paddle.topk(score, k=1, axis=-1, largest=True) cap_rate = self.capacity[0 if self.training else 1] capacity = math.ceil(cap_rate * inp.shape[0]) _new_lec, _new_gec, top1_idx = limit_by_capacity(top1_idx, self.num_expert, self.world_size, capacity, group=self.group) valid_idx = top1_idx[top1_idx > -1] valid_idx_tmp = paddle.reshape(valid_idx, shape=[len(valid_idx), 1]) fraction_expert = paddle.scatter_nd_add( x=paddle.zeros(shape=[self.tot_expert]), index=valid_idx_tmp, updates=paddle.ones_like(valid_idx, dtype=paddle.float32).reshape( shape=[len(valid_idx)]), ) / valid_idx.numel() prob_expert = score.sum(axis=0) / valid_idx.numel() loss = (fraction_expert * prob_expert).sum() * self.tot_expert self.set_loss(loss) return top1_score, top1_idx
def infer_net(self, inputs): def embedding_layer(input, table_name, initializer_instance=None): emb = paddle.static.nn.embedding( input=input, size=[self.sparse_feature_number, self.sparse_feature_dim], param_attr=table_name) return emb all_label = np.arange(self.sparse_feature_number).reshape( self.sparse_feature_number).astype('int32') self.all_label = paddle.cast(x=paddle.nn.functional.assign(all_label), dtype='int64') emb_all_label = embedding_layer(self.all_label, "emb") emb_a = embedding_layer(inputs[0], "emb") emb_b = embedding_layer(inputs[1], "emb") emb_c = embedding_layer(inputs[2], "emb") target = paddle.add(x=paddle.fluid.layers.nn.elementwise_sub( emb_b, emb_a), y=emb_c) emb_all_label_l2 = paddle.fluid.layers.l2_normalize(x=emb_all_label, axis=1) dist = paddle.fluid.layers.matmul(x=target, y=emb_all_label_l2, transpose_y=True) values, pred_idx = paddle.topk(x=dist, k=1) label = paddle.fluid.layers.expand(paddle.unsqueeze(inputs[3], axis=[1]), expand_times=[1, 1]) label_ones = paddle.fluid.layers.fill_constant_batch_size_like( label, shape=[-1, 1], value=1.0, dtype='float32') right_cnt = paddle.sum( x=paddle.cast(paddle.equal(x=pred_idx, y=label), dtype='float32')) total_cnt = paddle.sum(x=label_ones) global_right_cnt = paddle.fluid.layers.create_global_var( name="global_right_cnt", persistable=True, dtype='float32', shape=[1], value=0) global_total_cnt = paddle.fluid.layers.create_global_var( name="global_total_cnt", persistable=True, dtype='float32', shape=[1], value=0) global_right_cnt.stop_gradient = True global_total_cnt.stop_gradient = True tmp1 = paddle.add(x=right_cnt, y=global_right_cnt) paddle.nn.functional.assign(tmp1, global_right_cnt) tmp2 = paddle.add(x=total_cnt, y=global_total_cnt) paddle.nn.functional.assign(tmp2, global_total_cnt) acc = paddle.divide(x=global_right_cnt, y=global_total_cnt, name="total_acc") self._infer_results['acc'] = acc
def infer_network(vocab_size, emb_size): analogy_a = paddle.static.data(name="analogy_a", shape=[None, 1], dtype='int64') analogy_b = paddle.static.data(name="analogy_b", shape=[None, 1], dtype='int64') analogy_c = paddle.static.data(name="analogy_c", shape=[None, 1], dtype='int64') all_label = paddle.static.data(name="all_label", shape=[vocab_size, 1], dtype='int64') all_label = paddle.reshape(all_label, [-1]) emb_all_label = paddle.static.nn.embedding(input=all_label, size=[vocab_size, emb_size], param_attr="emb") analogy_a = paddle.reshape(analogy_a, [-1]) emb_a = paddle.static.nn.embedding(input=analogy_a, size=[vocab_size, emb_size], param_attr="emb") analogy_b = paddle.reshape(analogy_b, [-1]) emb_b = paddle.static.nn.embedding(input=analogy_b, size=[vocab_size, emb_size], param_attr="emb") analogy_c = paddle.reshape(analogy_c, [-1]) emb_c = paddle.static.nn.embedding(input=analogy_c, size=[vocab_size, emb_size], param_attr="emb") target = paddle.add(paddle.add(emb_b, -emb_a), emb_c) emb_all_label_l2 = fluid.layers.l2_normalize(x=emb_all_label, axis=1) dist = fluid.layers.matmul(x=target, y=emb_all_label_l2, transpose_y=True) values, pred_idx = paddle.topk(x=dist, k=4) return values, pred_idx
def label_box(anchors, gt_boxes, positive_overlap, negative_overlap, allow_low_quality): iou = bbox_overlaps(gt_boxes, anchors) if iou.numel() == 0: default_matches = paddle.full((iou.shape[1], ), 0, dtype='int64') default_match_labels = paddle.full((iou.shape[1], ), -1, dtype='int32') return default_matches, default_match_labels matched_vals, matches = paddle.topk(iou, k=1, axis=0) match_labels = paddle.full(matches.shape, -1, dtype='int32') match_labels = paddle.where(matched_vals < negative_overlap, paddle.zeros_like(match_labels), match_labels) match_labels = paddle.where(matched_vals >= positive_overlap, paddle.ones_like(match_labels), match_labels) if allow_low_quality: highest_quality_foreach_gt = iou.max(axis=1, keepdim=True) pred_inds_with_highest_quality = paddle.logical_and( iou > 0, iou == highest_quality_foreach_gt).cast('int32').sum(0, keepdim=True) match_labels = paddle.where(pred_inds_with_highest_quality > 0, paddle.ones_like(match_labels), match_labels) matches = matches.flatten() match_labels = match_labels.flatten() return matches, match_labels
def _gather_topk_pyramid(self, gt2anchor_distances, num_anchors_list, pad_gt_mask): pad_gt_mask = pad_gt_mask.tile([1, 1, self.topk]).astype(paddle.bool) gt2anchor_distances_list = paddle.split(gt2anchor_distances, num_anchors_list, axis=-1) num_anchors_index = np.cumsum(num_anchors_list).tolist() num_anchors_index = [ 0, ] + num_anchors_index[:-1] is_in_topk_list = [] topk_idxs_list = [] for distances, anchors_index in zip(gt2anchor_distances_list, num_anchors_index): num_anchors = distances.shape[-1] topk_metrics, topk_idxs = paddle.topk(distances, self.topk, axis=-1, largest=False) topk_idxs_list.append(topk_idxs + anchors_index) topk_idxs = paddle.where(pad_gt_mask, topk_idxs, paddle.zeros_like(topk_idxs)) is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(axis=-2) is_in_topk = paddle.where(is_in_topk > 1, paddle.zeros_like(is_in_topk), is_in_topk) is_in_topk_list.append(is_in_topk.astype( gt2anchor_distances.dtype)) is_in_topk_list = paddle.concat(is_in_topk_list, axis=-1) topk_idxs_list = paddle.concat(topk_idxs_list, axis=-1) return is_in_topk_list, topk_idxs_list
def forward(self, inputs, targets=None): others = targets[-4:] encoder_word_pos = others[0] gsrm_word_pos = others[1] gsrm_slf_attn_bias1 = others[2] gsrm_slf_attn_bias2 = others[3] pvam_feature = self.pvam(inputs, encoder_word_pos, gsrm_word_pos) gsrm_feature, word_out, gsrm_out = self.gsrm(pvam_feature, gsrm_word_pos, gsrm_slf_attn_bias1, gsrm_slf_attn_bias2) final_out = self.vsfd(pvam_feature, gsrm_feature) if not self.training: final_out = F.softmax(final_out, axis=1) _, decoded_out = paddle.topk(final_out, k=1) predicts = OrderedDict([ ('predict', final_out), ('pvam_feature', pvam_feature), ('decoded_out', decoded_out), ('word_out', word_out), ('gsrm_out', gsrm_out), ]) return predicts
def gather_topk_anchors(metrics, topk, largest=True, topk_mask=None, eps=1e-9): r""" Args: metrics (Tensor, float32): shape[B, n, L], n: num_gts, L: num_anchors topk (int): The number of top elements to look for along the axis. largest (bool) : largest is a flag, if set to true, algorithm will sort by descending order, otherwise sort by ascending order. Default: True topk_mask (Tensor, bool|None): shape[B, n, topk], ignore bbox mask, Default: None eps (float): Default: 1e-9 Returns: is_in_topk (Tensor, float32): shape[B, n, L], value=1. means selected """ num_anchors = metrics.shape[-1] topk_metrics, topk_idxs = paddle.topk(metrics, topk, axis=-1, largest=largest) if topk_mask is None: topk_mask = (topk_metrics.max(axis=-1, keepdim=True) > eps).tile( [1, 1, topk]) topk_idxs = paddle.where(topk_mask, topk_idxs, paddle.zeros_like(topk_idxs)) is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(axis=-2) is_in_topk = paddle.where(is_in_topk > 1, paddle.zeros_like(is_in_topk), is_in_topk) return is_in_topk.astype(metrics.dtype)
def TopKProcess(probs, top_k, min_tokens_to_keep): top_k = min(max(top_k, min_tokens_to_keep), probs.shape[-1]) # Remove all tokens with a probability less than the last token of the top-k topk_probs, _ = paddle.topk(probs, k=top_k) probs = paddle.where(probs >= topk_probs[:, -1:], probs, paddle.full_like(probs, 0.0)) return probs
def beam_search_step(state, logits, eos_id, beam_width, is_first_step, length_penalty): """logits.shape == [B*W, V]""" _, vocab_size = logits.shape bsz, beam_width = state.log_probs.shape onehot_eos = P.cast(F.one_hot(P.ones([1], 'int64') * eos_id, vocab_size), 'int64') #[1, V] probs = P.log(F.softmax(logits)) #[B*W, V] probs = mask_prob(probs, onehot_eos, state.finished) #[B*W, V] allprobs = P.reshape(state.log_probs, [-1, 1]) + probs #[B*W, V] not_finished = 1 - P.reshape(state.finished, [-1, 1]) #[B*W,1] not_eos = 1 - onehot_eos length_to_add = not_finished * not_eos #[B*W,V] alllen = P.reshape(state.lengths, [-1, 1]) + length_to_add allprobs = P.reshape(allprobs, [-1, beam_width * vocab_size]) alllen = P.reshape(alllen, [-1, beam_width * vocab_size]) allscore = hyp_score(allprobs, alllen, length_penalty) if is_first_step: allscore = P.reshape( allscore, [bsz, beam_width, -1])[:, 0, :] # first step only consiter beam 0 scores, idx = P.topk(allscore, k=beam_width) #[B, W] next_beam_id = idx // vocab_size #[B, W] next_word_id = idx % vocab_size gather_idx = P.concat( [P.nonzero(idx != -1)[:, :1], P.reshape(idx, [-1, 1])], 1) next_probs = P.reshape(P.gather_nd(allprobs, gather_idx), idx.shape) next_len = P.reshape(P.gather_nd(alllen, gather_idx), idx.shape) gather_idx = P.concat([ P.nonzero(next_beam_id != -1)[:, :1], P.reshape(next_beam_id, [-1, 1]) ], 1) next_finished = P.reshape( P.gather_nd(state.finished, gather_idx), state.finished.shape ) #[gather new beam state according to new beam id] #log.debug(gather_idx.numpy()) #log.debug(state.finished.numpy()) #log.debug(next_finished.numpy()) next_finished += P.cast(next_word_id == eos_id, 'int64') next_finished = P.cast(next_finished > 0, 'int64') #log.debug(next_word_id.numpy()) #log.debug(next_beam_id.numpy()) next_state = BeamSearchState(log_probs=next_probs, lengths=next_len, finished=next_finished) output = BeamSearchOutput(scores=scores, predicted_ids=next_word_id, beam_parent_ids=next_beam_id) return output, next_state
def run_static(self, place): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): input_tensor = paddle.static.data(name="x", shape=[6, 7, 8], dtype="float64") large_input_tensor = paddle.static.data(name="large_x", shape=[2, 1030], dtype="float64") k_tensor = paddle.static.data(name="k", shape=[1], dtype="int32") result1 = paddle.topk(input_tensor, k=2) result2 = paddle.topk(input_tensor, k=2, axis=-1) result3 = paddle.topk(input_tensor, k=k_tensor, axis=1) result4 = paddle.topk(input_tensor, k=2, axis=1, largest=False) result5 = paddle.topk(input_tensor, k=2, axis=-1, largest=False) result6 = paddle.topk(large_input_tensor, k=1, axis=-1) result7 = paddle.topk(input_tensor, k=2, axis=1, sorted=False) exe = paddle.static.Executor(place) input_data = np.random.rand(10, 20).astype("float64") large_input_data = np.random.rand(2, 100).astype("float64") paddle_result = exe.run(feed={ "x": self.input_data, "large_x": self.large_input_data, "k": np.array([2]).astype("int32") }, fetch_list=[ result1[0], result1[1], result2[0], result2[1], result3[0], result3[1], result4[0], result4[1], result5[0], result5[1], result6[0], result6[1], result7[0], result7[1] ]) numpy_result = numpy_topk(self.input_data, k=2) self.assertTrue(np.allclose(paddle_result[0], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[1], numpy_result[1])) numpy_result = numpy_topk(self.input_data, k=2, axis=-1) self.assertTrue(np.allclose(paddle_result[2], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[3], numpy_result[1])) numpy_result = numpy_topk(self.input_data, k=2, axis=1) self.assertTrue(np.allclose(paddle_result[4], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[5], numpy_result[1])) numpy_result = numpy_topk(self.input_data, k=2, axis=1, largest=False) self.assertTrue(np.allclose(paddle_result[6], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[7], numpy_result[1])) numpy_result = numpy_topk(self.input_data, k=2, axis=-1, largest=False) self.assertTrue(np.allclose(paddle_result[8], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[9], numpy_result[1])) numpy_result = numpy_topk(self.large_input_data, k=1, axis=-1) self.assertTrue(np.allclose(paddle_result[10], numpy_result[0])) self.assertTrue(np.allclose(paddle_result[11], numpy_result[1])) sort_paddle = numpy_topk(paddle_result[12], axis=1, k=2) numpy_result = numpy_topk(self.input_data, k=2, axis=1) self.assertTrue(np.allclose(sort_paddle[0], numpy_result[0]))
def label_box(anchors, gt_boxes, positive_overlap, negative_overlap, allow_low_quality, ignore_thresh, is_crowd=None): iou = bbox_overlaps(gt_boxes, anchors) n_gt = gt_boxes.shape[0] if n_gt == 0 or is_crowd is None: n_gt_crowd = 0 else: n_gt_crowd = paddle.nonzero(is_crowd).shape[0] if iou.shape[0] == 0 or n_gt_crowd == n_gt: # No truth, assign everything to background default_matches = paddle.full((iou.shape[1], ), 0, dtype='int64') default_match_labels = paddle.full((iou.shape[1], ), 0, dtype='int32') return default_matches, default_match_labels # if ignore_thresh > 0, remove anchor if it is closed to # one of the crowded ground-truth if n_gt_crowd > 0: N_a = anchors.shape[0] ones = paddle.ones([N_a]) mask = is_crowd * ones if ignore_thresh > 0: crowd_iou = iou * mask valid = (paddle.sum((crowd_iou > ignore_thresh).cast('int32'), axis=0) > 0).cast('float32') iou = iou * (1 - valid) - valid # ignore the iou between anchor and crowded ground-truth iou = iou * (1 - mask) - mask matched_vals, matches = paddle.topk(iou, k=1, axis=0) match_labels = paddle.full(matches.shape, -1, dtype='int32') # set ignored anchor with iou = -1 neg_cond = paddle.logical_and(matched_vals > -1, matched_vals < negative_overlap) match_labels = paddle.where(neg_cond, paddle.zeros_like(match_labels), match_labels) match_labels = paddle.where(matched_vals >= positive_overlap, paddle.ones_like(match_labels), match_labels) if allow_low_quality: highest_quality_foreach_gt = iou.max(axis=1, keepdim=True) pred_inds_with_highest_quality = paddle.logical_and( iou > 0, iou == highest_quality_foreach_gt).cast('int32').sum(0, keepdim=True) match_labels = paddle.where(pred_inds_with_highest_quality > 0, paddle.ones_like(match_labels), match_labels) matches = matches.flatten() match_labels = match_labels.flatten() return matches, match_labels
def _gen_proposal(self, scores, bbox_deltas, anchors, inputs): """ scores (list[Tensor]): Multi-level scores prediction bbox_deltas (list[Tensor]): Multi-level deltas prediction anchors (list[Tensor]): Multi-level anchors inputs (dict): ground truth info """ prop_gen = self.train_proposal if self.training else self.test_proposal im_shape = inputs['im_shape'] # Collect multi-level proposals for each batch # Get 'topk' of them as final output bs_rois_collect = [] bs_rois_num_collect = [] batch_size = paddle.slice(paddle.shape(im_shape), [0], [0], [1]) # Generate proposals for each level and each batch. # Discard batch-computing to avoid sorting bbox cross different batches. for i in range(batch_size): rpn_rois_list = [] rpn_prob_list = [] rpn_rois_num_list = [] for rpn_score, rpn_delta, anchor in zip(scores, bbox_deltas, anchors): rpn_rois, rpn_rois_prob, rpn_rois_num, post_nms_top_n = prop_gen( scores=rpn_score[i:i + 1], bbox_deltas=rpn_delta[i:i + 1], anchors=anchor, im_shape=im_shape[i:i + 1]) if rpn_rois.shape[0] > 0: rpn_rois_list.append(rpn_rois) rpn_prob_list.append(rpn_rois_prob) rpn_rois_num_list.append(rpn_rois_num) if len(scores) > 1: rpn_rois = paddle.concat(rpn_rois_list) rpn_prob = paddle.concat(rpn_prob_list).flatten() if rpn_prob.shape[0] > post_nms_top_n: topk_prob, topk_inds = paddle.topk(rpn_prob, post_nms_top_n) topk_rois = paddle.gather(rpn_rois, topk_inds) else: topk_rois = rpn_rois topk_prob = rpn_prob else: topk_rois = rpn_rois_list[0] topk_prob = rpn_prob_list[0].flatten() bs_rois_collect.append(topk_rois) bs_rois_num_collect.append(paddle.shape(topk_rois)[0]) bs_rois_num_collect = paddle.concat(bs_rois_num_collect) return bs_rois_collect, bs_rois_num_collect
def get_points_train(self, seg_logits, uncertainty_func): # finish """ Sample points for training. Sample points in [0, 1] x [0, 1] coordinate space based on their uncertainty. The uncertainties are calculated for each point using 'uncertainty_func' function that takes point's logit prediction as input. Args: seg_logits (Tensor): Semantic segmentation logits, shape ( batch_size, num_classes, height, width). uncertainty_func (func): uncertainty calculation function. cfg (dict): Training config of point head. Returns: point_coords (Tensor): A tensor of shape (batch_size, num_points, 2) that contains the coordinates of ``num_points`` sampled points. """ num_points = self.num_points oversample_ratio = self.oversample_ratio importance_sample_ratio = self.importance_sample_ratio assert oversample_ratio >= 1 assert 0 <= importance_sample_ratio <= 1 batch_size = paddle.shape(seg_logits)[0] num_sampled = int(num_points * oversample_ratio) point_coords = paddle.rand([batch_size, num_sampled, 2]) point_logits = point_sample(seg_logits, point_coords) # It is crucial to calculate uncertainty based on the sampled # prediction value for the points. Calculating uncertainties of the # coarse predictions first and sampling them for points leads to # incorrect results. To illustrate this: assume uncertainty func( # logits)=-abs(logits), a sampled point between two coarse # predictions with -1 and 1 logits has 0 logits, and therefore 0 # uncertainty value. However, if we calculate uncertainties for the # coarse predictions first, both will have -1 uncertainty, # and sampled point will get -1 uncertainty. point_uncertainties = uncertainty_func(point_logits) num_uncertain_points = int(importance_sample_ratio * num_points) num_random_points = num_points - num_uncertain_points idx = paddle.topk(point_uncertainties[:, 0, :], k=num_uncertain_points, axis=1)[1] shift = num_sampled * paddle.arange(batch_size, dtype='int64') idx += shift.unsqueeze([-1]) idx = idx.reshape([-1]) point_coords = paddle.index_select(point_coords.reshape([-1, 2]), idx, axis=0) point_coords = point_coords.reshape( [batch_size, num_uncertain_points, 2]) if num_random_points > 0: rand_point_coords = paddle.rand([batch_size, num_random_points, 2]) point_coords = paddle.concat((point_coords, rand_point_coords), axis=1) return point_coords
def topk_sampling(self, probs): topk_probs, _ = paddle.topk(probs, self.topk) ge_cond = paddle.cast( paddle.greater_equal(probs, paddle.unsqueeze(topk_probs[:, -1], [1])), "float32") old_probs = probs probs = probs * ge_cond / paddle.sum(topk_probs, axis=-1, keepdim=True) sampling_ids = layers.sampling_id(probs, dtype="int") probs = old_probs return probs, sampling_ids
def decode(self, inputs, caches): tgt_ids = inputs['tgt_ids'] tgt_pos = inputs['tgt_pos'] tgt_generation_mask = inputs['tgt_generation_mask'] predictions = tgt_ids # TODO step = 0 while step < self.max_dec_len: # [-1, 1] append_mask = paddle.cast( tgt_ids != self.eos_id, dtype=tgt_generation_mask.dtype) tgt_generation_mask = paddle.concat( [tgt_generation_mask, paddle.unsqueeze(append_mask, 1)], axis=-1) tgt_sent = paddle.ones( [tgt_generation_mask.shape[0], 1], dtype=tgt_ids.dtype) # [-1, 1, hidden_size] out, caches = self.plato2_encoder(caches, tgt_ids, tgt_sent, tgt_pos, tgt_generation_mask) out = paddle.squeeze(out, axis=1) # [-1, hidden_size] trans = self.logits_fc_layer(out) trans = self.gelu_layer(trans) trans = self.logits_layer_norm(trans) # [-1, vocab_size] logits = paddle.matmul( trans, self.plato2_encoder.word_embedding_layer.weight, transpose_y=True) + self.logits_bias logits[:, self.unk_id] = -1e9 logits[:, self.bos_id] = -1e9 logits[:, self.mask_id] = -1e9 if step < self.min_dec_len: logits[:, self.eos_id] = -1e9 logits = logits * append_mask + (1 - append_mask) * self.after_eos probs = self.softmax(logits) # [-1, topk] topk_probs, _ = paddle.topk(probs, k=self.topk) mask = paddle.cast(probs >= topk_probs[:, -1:], 'float32') sums = paddle.sum(topk_probs, axis=-1, keepdim=True) new_probs = probs * mask / sums # [-1, 1] sampling_ids = paddle.multinomial(new_probs) step = step + 1 tgt_ids = sampling_ids tgt_pos = tgt_pos + 1 predictions = paddle.concat([predictions, tgt_ids], axis=1) return predictions
def forward(self, inputs): """ forward """ x, indices = paddle.topk(inputs, k=1, axis=None, largest=True, sorted=True, name=None) return x + indices
def forward(self, analogy_a, analogy_b, analogy_c, true_word, all_label): emb_a = self.embedding(analogy_a) emb_b = self.embedding(analogy_b) emb_c = self.embedding(analogy_c) emb_all_label = self.embedding(all_label) target = emb_b - emb_a + emb_c emb_all_label_l2 = F.normalize(emb_all_label, axis=1) dist = paddle.matmul(x=target, y=emb_all_label_l2, transpose_y=True) values, pred_idx = paddle.topk(x=dist, k=4) return values, pred_idx
def forward(self): multi_level_rois = self.input('MultiLevelRois') multi_level_scores = self.input('MultiLevelScores') multi_level_rois = paddle.concat(multi_level_rois, axis=0) multi_level_scores = paddle.concat(multi_level_scores, axis=0) proposal_num = paddle.shape(multi_level_scores)[0] post_nms_top_n_tensor = paddle.assign( np.array([self.post_nms_top_n]).astype('int32')) k_candidate = paddle.concat([proposal_num, post_nms_top_n_tensor]) k = paddle.min(k_candidate) scores, index = paddle.topk(multi_level_scores, k=k, axis=0) rois = paddle.gather(multi_level_rois, index, axis=0) return {"FpnRois": [rois]}
def get_bboxes(self, cls_score_list, bbox_pred_list, mlvl_anchors, nms_pre, cls_out_channels, use_sigmoid_cls): assert len(cls_score_list) == len(bbox_pred_list) == len(mlvl_anchors) mlvl_bboxes = [] mlvl_scores = [] idx = 0 for cls_score, bbox_pred, anchors in zip(cls_score_list, bbox_pred_list, mlvl_anchors): cls_score = paddle.reshape(cls_score, [-1, cls_out_channels]) if use_sigmoid_cls: scores = F.sigmoid(cls_score) else: scores = F.softmax(cls_score, axis=-1) # bbox_pred = bbox_pred.permute(1, 2, 0).reshape(-1, 5) bbox_pred = paddle.transpose(bbox_pred, [1, 2, 0]) bbox_pred = paddle.reshape(bbox_pred, [-1, 5]) anchors = paddle.reshape(anchors, [-1, 5]) if nms_pre > 0 and scores.shape[0] > nms_pre: # Get maximum scores for foreground classes. if use_sigmoid_cls: max_scores = paddle.max(scores, axis=1) else: max_scores = paddle.max(scores[:, 1:], axis=1) topk_val, topk_inds = paddle.topk(max_scores, nms_pre) anchors = paddle.gather(anchors, topk_inds) bbox_pred = paddle.gather(bbox_pred, topk_inds) scores = paddle.gather(scores, topk_inds) target_means = (.0, .0, .0, .0, .0) target_stds = (1.0, 1.0, 1.0, 1.0, 1.0) bboxes = bbox_utils.delta2rbox(anchors, bbox_pred, target_means, target_stds) mlvl_bboxes.append(bboxes) mlvl_scores.append(scores) idx += 1 mlvl_bboxes = paddle.concat(mlvl_bboxes, axis=0) mlvl_scores = paddle.concat(mlvl_scores) if use_sigmoid_cls: # Add a dummy background class to the front when using sigmoid padding = paddle.zeros([mlvl_scores.shape[0], 1], dtype=mlvl_scores.dtype) mlvl_scores = paddle.concat([padding, mlvl_scores], axis=1) return mlvl_scores, mlvl_bboxes
def _post_process_loss(self, logit, label, semantic_weights, loss): """ Consider mask and top_k to calculate the final loss. Args: logit (Tensor): Logit tensor, the data type is float32, float64. Shape is (N, C), where C is number of classes, and if shape is more than 2D, this is (N, C, D1, D2,..., Dk), k >= 1. label (Tensor): Label tensor, the data type is int64. Shape is (N), where each value is 0 <= label[i] <= C-1, and if shape is more than 2D, this is (N, D1, D2,..., Dk), k >= 1. semantic_weights (Tensor, optional): Weights about loss for each pixels, shape is the same as label. loss (Tensor): Loss tensor which is the output of cross_entropy. If soft_label is False in cross_entropy, the shape of loss should be the same as the label. If soft_label is True in cross_entropy, the shape of loss should be (N, D1, D2,..., Dk, 1). Returns: (Tensor): The average loss. """ mask = label != self.ignore_index mask = paddle.cast(mask, 'float32') label.stop_gradient = True mask.stop_gradient = True if loss.ndim > mask.ndim: loss = paddle.squeeze(loss, axis=-1) loss = loss * mask if semantic_weights is not None: loss = loss * semantic_weights if self.weight is not None: _one_hot = F.one_hot(label, logit.shape[-1]) coef = paddle.sum(_one_hot * self.weight, axis=-1) else: coef = paddle.ones_like(label) if self.top_k_percent_pixels == 1.0: avg_loss = paddle.mean(loss) / (paddle.mean(mask * coef) + self.EPS) else: loss = loss.reshape((-1, )) top_k_pixels = int(self.top_k_percent_pixels * loss.numel()) loss, indices = paddle.topk(loss, top_k_pixels) coef = coef.reshape((-1, )) coef = paddle.gather(coef, indices) coef.stop_gradient = True coef = coef.astype('float32') avg_loss = loss.mean() / (paddle.mean(coef) + self.EPS) return avg_loss