def test_attr_tensor_API(self): startup_program = Program() train_program = Program() with program_guard(train_program, startup_program): fill_value = 2.0 input = paddle.fluid.data(name='input', dtype='float32', shape=[2, 3]) output = paddle.full_like(input, fill_value) output_dtype = paddle.full_like(input, fill_value, dtype='float32') place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) exe = paddle.static.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) res = exe.run(train_program, feed={'input': img}, fetch_list=[output]) out_np = np.array(res[0]) self.assertTrue(not (out_np - np.full_like(img, fill_value)).any(), msg="full_like output is wrong, out = " + str(out_np))
def to_tensor(pic): """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. See :class:`~paddlevision.transforms.ToTensor` for more details. Args: pic (PIL Image or numpy.ndarray): Image to be converted to tensor. Returns: Tensor: Converted image. """ if not (F_pil._is_pil_image(pic) or _is_numpy(pic)): raise TypeError('pic should be PIL Image or ndarray. Got {}'.format( type(pic))) if _is_numpy(pic) and not _is_numpy_image(pic): raise ValueError( 'pic should be 2/3 dimensional. Got {} dimensions.'.format( pic.ndim)) default_float_dtype = paddle.get_default_dtype() if isinstance(pic, np.ndarray): # handle numpy array if pic.ndim == 2: pic = pic[:, :, None] img = paddle.to_tensor(pic.transpose((2, 0, 1))) # backward compatibility if not img.dtype == default_float_dtype: img = img.astype(dtype=default_float_dtype) return img.divide(paddle.full_like(img, 255)) else: return img if accimage is not None and isinstance(pic, accimage.Image): nppic = np.zeros([pic.channels, pic.height, pic.width], dtype=np.float32) pic.copyto(nppic) return paddle.to_tensor(nppic).astype(dtype=default_float_dtype) # handle PIL Image mode_to_nptype = {'I': np.int32, 'I;16': np.int16, 'F': np.float32} img = paddle.to_tensor( np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True)) if pic.mode == '1': img = 255 * img img = img.reshape([pic.size[1], pic.size[0], len(pic.getbands())]) if not img.dtype == default_float_dtype: img = img.astype(dtype=default_float_dtype) # put it from HWC to CHW format img = img.transpose((2, 0, 1)) return img.divide(paddle.full_like(img, 255)) else: # put it from HWC to CHW format img = img.transpose((2, 0, 1)) return img
def build_model(self): x = paddle.static.data(name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32') x_fill = paddle.full_like(x, **self.attrs) out = paddle.fluid.layers.elementwise_add(x_fill, x_fill) self.fetch_list = [out.name]
def fill_any_like(name: str, x, value, dtype=None): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): data = paddle.static.data(name='x', shape=x.shape, dtype=data_type) out = paddle.full_like(data, value, dtype=dtype) out = paddle.cast(out, np.float32) cpu = paddle.static.cpu_places(1) exe = paddle.static.Executor(cpu[0]) # startup program will call initializer to initialize the parameters. exe.run(paddle.static.default_startup_program()) outs = exe.run(feed={'x': x}, fetch_list=[out]) saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1]) return outs[0]
def relative_position_bucket(relative_position, bidirectional=True, num_buckets=32, max_distance=128): ret = 0 if bidirectional: num_buckets //= 2 ret += (relative_position > 0).astype(paddle.int64) * num_buckets n = paddle.abs(relative_position) else: n = paddle.max(-relative_position, paddle.zeros_like(relative_position)) # now n is in the range [0, inf) # half of the buckets are for exact increments in positions max_exact = num_buckets // 2 is_small = n < max_exact # The other half of the buckets are for logarithmically bigger bins in positions up to max_distance val_if_large = max_exact + (paddle.log( n.astype(paddle.float32) / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact)).astype(paddle.int64) val_if_large = paddle.minimum( val_if_large, paddle.full_like(val_if_large, num_buckets - 1)) ret += paddle.where(is_small, n, val_if_large) return ret
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 TopPProcess(probs, top_p, min_tokens_to_keep): sorted_probs = paddle.sort(probs, descending=True) sorted_indices = paddle.argsort(probs, descending=True) cumulative_probs = paddle.cumsum(sorted_probs, axis=-1) # Remove tokens with cumulative probs above the top_p, But keep at # least min_tokens_to_keep tokens sorted_indices_to_remove = cumulative_probs > top_p if min_tokens_to_keep > 1: # Set 'min_tokens_to_keep - 1' because the first token is kept sorted_indices_to_remove[:, :min_tokens_to_keep - 1] = 0 # Keep the first token sorted_indices_to_remove = paddle.cast(sorted_indices_to_remove, dtype='int64') sorted_indices_to_remove[:, 1:] = ( sorted_indices_to_remove[:, :-1].clone()) sorted_indices_to_remove[:, 0] = 0 # Scatter sorted tensors to original indexing sorted_indices = sorted_indices + paddle.arange( probs.shape[0]).unsqueeze(-1) * probs.shape[-1] condition = paddle.scatter(sorted_indices_to_remove.flatten(), sorted_indices.flatten(), sorted_indices_to_remove.flatten()) condition = paddle.cast(condition, 'bool').reshape(probs.shape) probs = paddle.where(condition, paddle.full_like(probs, 0.0), probs) return probs
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 test_full_like_fill_inf(self): paddle.disable_static() input = paddle.arange(6, 10, dtype='float32') out = paddle.full_like(input, fill_value=float('inf')) out_numpy = np.random.random((4)).astype("float32") out_numpy.fill(float('inf')) self.assertTrue((out.numpy() == out_numpy).all(), True) paddle.enable_static()
def forward(self, x): if self.training and self.drop > 0: y = paddle.rand(shape=[x.shape[0], 1, 1]).__ge__( self.drop).astype("float32") y = y.divide(paddle.full_like(y, 1 - self.drop)) return paddle.add(x, y) else: return paddle.add(x, self.m(x))
def test_skip_data_transform(self): paddle.disable_static() with _test_eager_guard(): x = paddle.to_tensor([1., 2., 3., 4.], place=paddle.CUDAPinnedPlace()) out = paddle.full_like(x, 1.) self.assertTrue( (out.numpy() == np.ones([4]).astype(np.float32)).all(), True) paddle.enable_static()
def forward(self, src, mask, query_embed, pos_embed): bs, c, h, w = src.shape src = src.flatten(2).permute(2, 0, 1) pos_embed = pos_embed.flatten(2).permute(2, 0, 1) query_embed = query_embed.unsqueeze(1).repeat(1, bs, 1) mask = mask.flatten(1) tgt = paddle.full_like(query_embed).requires_grad_(False) memory = self.encoder(src, src_key_padding_mask=mask, pos=pos_embed) hs = self.decoder(tgt, memory, memory_key_padding_mask=mask, pos=\ pos_embed, query_pos=query_embed) return hs.transpose(1, 2), memory.permute(1, 2, 0).view(bs, c, h, w)
def forward(self, x1, x2, target): similarity = paddle.fluid.layers.reduce_sum(x1 * x2, dim=-1) / ( paddle.norm(x1, axis=-1) * paddle.norm(x2, axis=-1) + self.epsilon) one_list = paddle.full_like(target, fill_value=1) out = paddle.fluid.layers.reduce_mean( paddle.where( paddle.equal(target, one_list), 1. - similarity, paddle.maximum(paddle.zeros_like(similarity), similarity - self.margin))) return out
def reset_parameters(self): for i in range(self.T_max): running_mean = getattr(self, 'running_mean_{}'.format(i)) running_var = getattr(self, 'running_var_{}'.format(i)) # print(running_mean) # running_mean.zero_() # running_var.fill_(1) running_mean = paddle.zeros_like(running_mean, dtype='float32') running_var = paddle.full_like(running_var, fill_value=1., dtype='float32')
def _get_index_updates(self, num_query_objects, target, match_indices): batch_idx = paddle.concat([ paddle.full_like(src, i) for i, (src, _) in enumerate(match_indices) ]) src_idx = paddle.concat([src for (src, _) in match_indices]) src_idx += (batch_idx * num_query_objects) target_assign = paddle.concat([ paddle.gather(t, dst, axis=0) for t, (_, dst) in zip(target, match_indices) ]) return src_idx, target_assign
def greedy_search(self, input_ids, logits_processors, max_length, pad_token_id, eos_token_id, **model_kwargs): batch_size, cur_len = input_ids.shape origin_len = cur_len unfinished_flag = paddle.full([batch_size, 1], True, dtype='bool') scores = paddle.full([batch_size, 1], 0.0, dtype=paddle.get_default_dtype()) while cur_len < max_length: # prepare model inputs & get model output model_inputs = self.prepare_inputs_for_generation( input_ids, **model_kwargs) outputs = self(**model_inputs) logits = outputs[0] if isinstance(outputs, tuple) else outputs # [batch_size, vocab_size] logits = logits[:, -1, :] # pre-process distribution logits = self.adjust_logits_during_generation(logits) logits = logits_processors(input_ids, logits) # greedy probs = F.softmax(logits) probs = paddle.log(probs) next_tokens = paddle.argmax(probs, axis=-1).unsqueeze(-1) next_scores = paddle.index_sample(probs, next_tokens) if eos_token_id is not None: next_tokens = paddle.where( unfinished_flag, next_tokens, paddle.full_like(next_tokens, pad_token_id)) scores = self.update_scores_for_generation(scores, next_scores, cur_len - origin_len, unfinished_flag) cur_len += 1 input_ids = paddle.concat([input_ids, next_tokens], axis=1) if eos_token_id is not None: unfinished_flag = paddle.logical_and( unfinished_flag, next_tokens != eos_token_id) # Stop when there is a </s> in all sentences if not paddle.any(unfinished_flag): break model_kwargs = self.update_model_kwargs_for_generation( outputs, model_kwargs) return input_ids[:, origin_len:], scores
def _init_weights(self, module): """ Initialize the weights """ if isinstance(module, (nn.Linear, nn.Embedding)): module.weight.set_value( paddle.tensor.normal(mean=0.0, std=self.initializer_range if hasattr( self, "initializer_range") else self.electra.config["initializer_range"], shape=module.weight.shape)) elif isinstance(module, nn.LayerNorm): module.bias.set_value(paddle.zeros_like(module.bias)) module.weight.set_value(paddle.full_like(module.weight, 1.0)) if isinstance(module, nn.Linear) and module.bias is not None: module.bias.set_value(paddle.zeros_like(module.bias))
def _init_weights(self, layer): """ Initialize the weights """ if isinstance(layer, (nn.Linear, nn.Embedding)): layer.weight.set_value( paddle.tensor.normal(mean=0.0, std=self.initializer_range if hasattr( self, "initializer_range") else self.electra.config["initializer_range"], shape=layer.weight.shape)) elif isinstance(layer, nn.LayerNorm): layer.bias.set_value(paddle.zeros_like(layer.bias)) layer.weight.set_value(paddle.full_like(layer.weight, 1.0)) layer._epsilon = 1e-12 if isinstance(layer, nn.Linear) and layer.bias is not None: layer.bias.set_value(paddle.zeros_like(layer.bias))
def forward(self, landmark_out, landmark_target, label): # 只保留landmark数据 -2 ones = paddle.ones_like(label) zeros = paddle.zeros_like(label) valid_label = paddle.where( paddle.equal(label, paddle.full_like(label, fill_value=-2)), ones, zeros) valid_label = paddle.squeeze(valid_label) # 获取有效值的总数 keep_num = int(paddle.sum(valid_label).numpy()[0] * self.keep_ratio) loss = self.square_loss(input=landmark_out, label=landmark_target) loss = paddle.sum(loss, axis=1) loss = loss * valid_label # 取有效数据计算损失 loss, _ = paddle.topk(loss, k=keep_num, axis=0) return paddle.mean(loss)
def forward(self, class_out, label): # 保留neg 0 和pos 1 的数据,忽略掉part -1, landmark -2 zeros = paddle.zeros_like(label) ignore_label = paddle.full_like(label, fill_value=-100) label = paddle.where(paddle.less_than(label, zeros), ignore_label, label) # 求neg 0 和pos 1 的数据70%数据 ones = paddle.ones_like(label) valid_label = paddle.where(paddle.greater_equal(label, zeros), ones, zeros) num_valid = paddle.sum(valid_label) keep_num = int((num_valid * self.keep_ratio).numpy()[0]) # 计算交叉熵损失 loss = self.entropy_loss(input=class_out, label=label) # 取有效数据的70%计算损失 loss, _ = paddle.topk(paddle.squeeze(loss), k=keep_num) return paddle.mean(loss)
def test_errors(self): with program_guard(Program(), Program()): #for ci coverage input_data = paddle.fluid.data(name='input', dtype='float32', shape=[2, 3]) output = paddle.full_like(input_data, 2.0) def test_input_dtype(): paddle.full_like self.assertRaises(TypeError, paddle.full_like, x=input_data, fill_value=2, dtype='uint4')
def net(self, input, is_infer=False): embedding = paddle.nn.Embedding( self.node_nums, self.node_emb_size, sparse=True, padding_idx=0, weight_attr=paddle.framework.ParamAttr( name="tdm.bw_emb.weight", initializer=paddle.nn.initializer.Normal(std=0.001))) # embedding = paddle.nn.Embedding( # self.node_nums, # self.node_emb_size, # sparse=True, # padding_idx=0, # weight_attr=paddle.framework.ParamAttr( # name="TDM_Tree_Emb", # initializer=paddle.nn.initializer.Constant(0.01))) user_feature = input[0:self.item_nums] user_feature_emb = list(map(embedding, user_feature)) # [(bs, emb)] unit_id_emb = embedding(input[-2]) dout = dnn_model_define( user_feature_emb, unit_id_emb, node_emb_size=self.node_emb_size, fea_groups=self.fea_group, with_att=self.with_att) cost, softmax_prob = paddle.nn.functional.softmax_with_cross_entropy( logits=dout, label=input[-1], return_softmax=True, ignore_index=-1) ignore_label = paddle.full_like(input[-1], fill_value=-1) avg_cost = paddle.sum(cost) / paddle.sum( paddle.cast( paddle.not_equal(input[-1], ignore_label), dtype='int32')) self._cost = avg_cost self.inference_target_var = softmax_prob fetch_dict = {'cost': avg_cost} return fetch_dict
def _init_weights(self, layer): # Initialize the weights. if isinstance(layer, (nn.Linear, nn.Embedding)): if isinstance(layer.weight, paddle.Tensor): layer.weight.set_value( paddle.tensor.normal( mean=0.0, std=self.initializer_range if hasattr( self, "initializer_range") else self.transformer.config["initializer_range"], shape=layer.weight.shape)) if isinstance(layer, nn.Linear) and layer.bias is not None: layer.bias.set_value(paddle.zeros_like(layer.bias)) elif isinstance(layer, nn.LayerNorm): layer.bias.set_value(paddle.zeros_like(layer.bias)) layer.weight.set_value(paddle.full_like(layer.weight, 1.0)) elif isinstance(layer, XLNetRelativeAttention): for param in [ layer.q, layer.k, layer.v, layer.o, layer.r, layer.r_r_bias, layer.r_s_bias, layer.r_w_bias, layer.seg_embed, ]: param.set_value( paddle.tensor.normal( mean=0.0, std=self.initializer_range if hasattr( self, "initializer_range") else self.transformer.config["initializer_range"], shape=param.shape)) elif isinstance(layer, XLNetModel): layer.mask_emb.set_value( paddle.tensor.normal( mean=0.0, std=self.initializer_range if hasattr( self, "initializer_range") else self.transformer.config["initializer_range"], shape=layer.mask_emb.shape))
def beam_search(self, x, beam_width, eos, embed): def _inflate(tensor, times, dim): repeat_dims = [1] * tensor.dim() repeat_dims[dim] = times output = paddle.tile(tensor, repeat_dims) return output # https://github.com/IBM/pytorch-seq2seq/blob/fede87655ddce6c94b38886089e05321dc9802af/seq2seq/models/TopKDecoder.py batch_size, l, d = x.shape x = paddle.tile(paddle.transpose(x.unsqueeze(1), perm=[1, 0, 2, 3]), [beam_width, 1, 1, 1]) inflated_encoder_feats = paddle.reshape( paddle.transpose(x, perm=[1, 0, 2, 3]), [-1, l, d]) # Initialize the decoder state = self.decoder.get_initial_state(embed, tile_times=beam_width) pos_index = paddle.reshape(paddle.arange(batch_size) * beam_width, shape=[-1, 1]) # Initialize the scores sequence_scores = paddle.full(shape=[batch_size * beam_width, 1], fill_value=-float('Inf')) index = [i * beam_width for i in range(0, batch_size)] sequence_scores[index] = 0.0 # Initialize the input vector y_prev = paddle.full(shape=[batch_size * beam_width], fill_value=self.num_classes) # Store decisions for backtracking stored_scores = list() stored_predecessors = list() stored_emitted_symbols = list() for i in range(self.max_len_labels): output, state = self.decoder(inflated_encoder_feats, state, y_prev) state = paddle.unsqueeze(state, axis=0) log_softmax_output = paddle.nn.functional.log_softmax(output, axis=1) sequence_scores = _inflate(sequence_scores, self.num_classes, 1) sequence_scores += log_softmax_output scores, candidates = paddle.topk(paddle.reshape( sequence_scores, [batch_size, -1]), beam_width, axis=1) # Reshape input = (bk, 1) and sequence_scores = (bk, 1) y_prev = paddle.reshape(candidates % self.num_classes, shape=[batch_size * beam_width]) sequence_scores = paddle.reshape( scores, shape=[batch_size * beam_width, 1]) # Update fields for next timestep pos_index = paddle.expand_as(pos_index, candidates) predecessors = paddle.cast(candidates / self.num_classes + pos_index, dtype='int64') predecessors = paddle.reshape(predecessors, shape=[batch_size * beam_width, 1]) state = paddle.index_select(state, index=predecessors.squeeze(), axis=1) # Update sequence socres and erase scores for <eos> symbol so that they aren't expanded stored_scores.append(sequence_scores.clone()) y_prev = paddle.reshape(y_prev, shape=[-1, 1]) eos_prev = paddle.full_like(y_prev, fill_value=eos) mask = eos_prev == y_prev mask = paddle.nonzero(mask) if mask.dim() > 0: sequence_scores = sequence_scores.numpy() mask = mask.numpy() sequence_scores[mask] = -float('inf') sequence_scores = paddle.to_tensor(sequence_scores) # Cache results for backtracking stored_predecessors.append(predecessors) y_prev = paddle.squeeze(y_prev) stored_emitted_symbols.append(y_prev) # Do backtracking to return the optimal values #====== backtrak ======# # Initialize return variables given different types p = list() l = [[self.max_len_labels] * beam_width for _ in range(batch_size) ] # Placeholder for lengths of top-k sequences # the last step output of the beams are not sorted # thus they are sorted here sorted_score, sorted_idx = paddle.topk( paddle.reshape(stored_scores[-1], shape=[batch_size, beam_width]), beam_width) # initialize the sequence scores with the sorted last step beam scores s = sorted_score.clone() batch_eos_found = [0] * batch_size # the number of EOS found # in the backward loop below for each batch t = self.max_len_labels - 1 # initialize the back pointer with the sorted order of the last step beams. # add pos_index for indexing variable with b*k as the first dimension. t_predecessors = paddle.reshape(sorted_idx + pos_index.expand_as(sorted_idx), shape=[batch_size * beam_width]) while t >= 0: # Re-order the variables with the back pointer current_symbol = paddle.index_select(stored_emitted_symbols[t], index=t_predecessors, axis=0) t_predecessors = paddle.index_select( stored_predecessors[t].squeeze(), index=t_predecessors, axis=0) eos_indices = stored_emitted_symbols[t] == eos eos_indices = paddle.nonzero(eos_indices) if eos_indices.dim() > 0: for i in range(eos_indices.shape[0] - 1, -1, -1): # Indices of the EOS symbol for both variables # with b*k as the first dimension, and b, k for # the first two dimensions idx = eos_indices[i] b_idx = int(idx[0] / beam_width) # The indices of the replacing position # according to the replacement strategy noted above res_k_idx = beam_width - (batch_eos_found[b_idx] % beam_width) - 1 batch_eos_found[b_idx] += 1 res_idx = b_idx * beam_width + res_k_idx # Replace the old information in return variables # with the new ended sequence information t_predecessors[res_idx] = stored_predecessors[t][idx[0]] current_symbol[res_idx] = stored_emitted_symbols[t][idx[0]] s[b_idx, res_k_idx] = stored_scores[t][idx[0], 0] l[b_idx][res_k_idx] = t + 1 # record the back tracked results p.append(current_symbol) t -= 1 # Sort and re-order again as the added ended sequences may change # the order (very unlikely) s, re_sorted_idx = s.topk(beam_width) for b_idx in range(batch_size): l[b_idx] = [ l[b_idx][k_idx.item()] for k_idx in re_sorted_idx[b_idx, :] ] re_sorted_idx = paddle.reshape( re_sorted_idx + pos_index.expand_as(re_sorted_idx), [batch_size * beam_width]) # Reverse the sequences and re-order at the same time # It is reversed because the backtracking happens in reverse time order p = [ paddle.reshape(paddle.index_select(step, re_sorted_idx, 0), shape=[batch_size, beam_width, -1]) for step in reversed(p) ] p = paddle.concat(p, -1)[:, 0, :] return p, paddle.ones_like(p)
def _get_tgt_permutation_idx(self, indices): # permute targets following indices batch_idx = paddle.concat( [paddle.full_like(tgt, i) for i, (_, tgt) in enumerate(indices)]) tgt_idx = paddle.concat([tgt for (_, tgt) in indices]) return batch_idx, tgt_idx
def fill_(tensor: Tensor, value): return tensor.set_value(paddle.full_like(tensor, value))
def _no_grad_fill_(tensor, val): with paddle.no_grad(): tensor.set_value(paddle.full_like(tensor, fill_value=val)) return tensor
def _no_grad_fill_(tensor, value=0.): with paddle.no_grad(): tensor.set_value(paddle.full_like(tensor, value, dtype=tensor.dtype)) return tensor
def forward(self, pred_scores, pred_bboxes, anchor_points, gt_labels, gt_bboxes, bg_index, gt_scores=None): r"""This code is based on https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/assigners/task_aligned_assigner.py The assignment is done in following steps 1. compute alignment metric between all bbox (bbox of all pyramid levels) and gt 2. select top-k bbox as candidates for each gt 3. limit the positive sample's center in gt (because the anchor-free detector only can predict positive distance) 4. if an anchor box is assigned to multiple gts, the one with the highest iou will be selected. Args: pred_scores (Tensor, float32): predicted class probability, shape(B, L, C) pred_bboxes (Tensor, float32): predicted bounding boxes, shape(B, L, 4) anchor_points (Tensor, float32): pre-defined anchors, shape(L, 2), "cxcy" format gt_labels (Tensor|List[Tensor], int64): Label of gt_bboxes, shape(B, n, 1) gt_bboxes (Tensor|List[Tensor], float32): Ground truth bboxes, shape(B, n, 4) bg_index (int): background index gt_scores (Tensor|List[Tensor]|None, float32) Score of gt_bboxes, shape(B, n, 1), if None, then it will initialize with one_hot label Returns: assigned_labels (Tensor): (B, L) assigned_bboxes (Tensor): (B, L, 4) assigned_scores (Tensor): (B, L, C) """ assert pred_scores.ndim == pred_bboxes.ndim gt_labels, gt_bboxes, pad_gt_scores, pad_gt_mask = pad_gt( gt_labels, gt_bboxes, gt_scores) assert gt_labels.ndim == gt_bboxes.ndim and \ gt_bboxes.ndim == 3 batch_size, num_anchors, num_classes = pred_scores.shape _, num_max_boxes, _ = gt_bboxes.shape # negative batch if num_max_boxes == 0: assigned_labels = paddle.full([batch_size, num_anchors], bg_index) assigned_bboxes = paddle.zeros([batch_size, num_anchors, 4]) assigned_scores = paddle.zeros( [batch_size, num_anchors, num_classes]) return assigned_labels, assigned_bboxes, assigned_scores # compute iou between gt and pred bbox, [B, n, L] ious = iou_similarity(gt_bboxes, pred_bboxes) # gather pred bboxes class score pred_scores = pred_scores.transpose([0, 2, 1]) batch_ind = paddle.arange( end=batch_size, dtype=gt_labels.dtype).unsqueeze(-1) gt_labels_ind = paddle.stack( [batch_ind.tile([1, num_max_boxes]), gt_labels.squeeze(-1)], axis=-1) bbox_cls_scores = paddle.gather_nd(pred_scores, gt_labels_ind) # compute alignment metrics, [B, n, L] alignment_metrics = bbox_cls_scores.pow(self.alpha) * ious.pow( self.beta) # check the positive sample's center in gt, [B, n, L] is_in_gts = check_points_inside_bboxes(anchor_points, gt_bboxes) # select topk largest alignment metrics pred bbox as candidates # for each gt, [B, n, L] is_in_topk = gather_topk_anchors( alignment_metrics * is_in_gts, self.topk, topk_mask=pad_gt_mask.tile([1, 1, self.topk]).astype(paddle.bool)) # select positive sample, [B, n, L] mask_positive = is_in_topk * is_in_gts * pad_gt_mask # if an anchor box is assigned to multiple gts, # the one with the highest iou will be selected, [B, n, L] mask_positive_sum = mask_positive.sum(axis=-2) if mask_positive_sum.max() > 1: mask_multiple_gts = (mask_positive_sum.unsqueeze(1) > 1).tile( [1, num_max_boxes, 1]) is_max_iou = compute_max_iou_anchor(ious) mask_positive = paddle.where(mask_multiple_gts, is_max_iou, mask_positive) mask_positive_sum = mask_positive.sum(axis=-2) assigned_gt_index = mask_positive.argmax(axis=-2) assert mask_positive_sum.max() == 1, \ ("one anchor just assign one gt, but received not equals 1. " "Received: %f" % mask_positive_sum.max().item()) # assigned target assigned_gt_index = assigned_gt_index + batch_ind * num_max_boxes assigned_labels = paddle.gather( gt_labels.flatten(), assigned_gt_index.flatten(), axis=0) assigned_labels = assigned_labels.reshape([batch_size, num_anchors]) assigned_labels = paddle.where( mask_positive_sum > 0, assigned_labels, paddle.full_like(assigned_labels, bg_index)) assigned_bboxes = paddle.gather( gt_bboxes.reshape([-1, 4]), assigned_gt_index.flatten(), axis=0) assigned_bboxes = assigned_bboxes.reshape([batch_size, num_anchors, 4]) assigned_scores = F.one_hot(assigned_labels, num_classes) # rescale alignment metrics alignment_metrics *= mask_positive max_metrics_per_instance = alignment_metrics.max(axis=-1, keepdim=True) max_ious_per_instance = (ious * mask_positive).max(axis=-1, keepdim=True) alignment_metrics = alignment_metrics / ( max_metrics_per_instance + self.eps) * max_ious_per_instance alignment_metrics = alignment_metrics.max(-2).unsqueeze(-1) assigned_scores = assigned_scores * alignment_metrics return assigned_labels, assigned_bboxes, assigned_scores
def sample(self, input_ids, logits_processors, max_length, pad_token_id, eos_token_id, top_k=None, top_p=None, temperature=None, min_tokens_to_keep=1, **model_kwargs): 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 TopPProcess(probs, top_p, min_tokens_to_keep): sorted_probs = paddle.sort(probs, descending=True) sorted_indices = paddle.argsort(probs, descending=True) cumulative_probs = paddle.cumsum(sorted_probs, axis=-1) # Remove tokens with cumulative probs above the top_p, But keep at # least min_tokens_to_keep tokens sorted_indices_to_remove = cumulative_probs > top_p if min_tokens_to_keep > 1: # Set 'min_tokens_to_keep - 1' because the first token is kept sorted_indices_to_remove[:, :min_tokens_to_keep - 1] = 0 # Keep the first token sorted_indices_to_remove = paddle.cast(sorted_indices_to_remove, dtype='int64') sorted_indices_to_remove[:, 1:] = ( sorted_indices_to_remove[:, :-1].clone()) sorted_indices_to_remove[:, 0] = 0 # Scatter sorted tensors to original indexing sorted_indices = sorted_indices + paddle.arange( probs.shape[0]).unsqueeze(-1) * probs.shape[-1] condition = paddle.scatter(sorted_indices_to_remove.flatten(), sorted_indices.flatten(), sorted_indices_to_remove.flatten()) condition = paddle.cast(condition, 'bool').reshape(probs.shape) probs = paddle.where(condition, paddle.full_like(probs, 0.0), probs) return probs batch_size, cur_len = input_ids.shape origin_len = cur_len unfinished_flag = paddle.full([batch_size, 1], True, dtype='bool') scores = paddle.full([batch_size, 1], 0.0, dtype=paddle.get_default_dtype()) while cur_len < max_length: # prepare model inputs & get model output model_inputs = self.prepare_inputs_for_generation( input_ids, **model_kwargs) outputs = self(**model_inputs) logits = outputs[0] if isinstance(outputs, tuple) else outputs # [batch_size, vocab_size] logits = logits[:, -1, :] # pre-process distribution logits = self.adjust_logits_during_generation(logits) logits = logits_processors(input_ids, logits) # sample origin_probs = F.softmax(logits) origin_probs = paddle.log(origin_probs) if temperature is not None and temperature != 1.0: logits = logits / temperature probs = F.softmax(logits) if top_k is not None and top_k != 0: probs = TopKProcess(probs, top_k, min_tokens_to_keep) if top_p is not None and top_p < 1.0: probs = TopPProcess(probs, top_p, min_tokens_to_keep) next_tokens = paddle.multinomial(probs) next_scores = paddle.index_sample(origin_probs, next_tokens) if eos_token_id is not None: next_tokens = paddle.where( unfinished_flag, next_tokens, paddle.full_like(next_tokens, pad_token_id)) scores = self.update_scores_for_generation(scores, next_scores, cur_len - origin_len, unfinished_flag) cur_len += 1 input_ids = paddle.concat([input_ids, next_tokens], axis=1) if eos_token_id is not None: unfinished_flag = paddle.logical_and( unfinished_flag, next_tokens != eos_token_id) # Stop when there is a </s> in all sentences if not paddle.any(unfinished_flag): break model_kwargs = self.update_model_kwargs_for_generation( outputs, model_kwargs) return input_ids[:, origin_len:], scores