def bbox_transform(self, deltas, weights=[0.1, 0.1, 0.2, 0.2]): wx, wy, ww, wh = weights deltas = paddle.reshape(deltas, shape=(0, -1, 4)) dx = paddle.slice(deltas, axes=[2], starts=[0], ends=[1]) * wx dy = paddle.slice(deltas, axes=[2], starts=[1], ends=[2]) * wy dw = paddle.slice(deltas, axes=[2], starts=[2], ends=[3]) * ww dh = paddle.slice(deltas, axes=[2], starts=[3], ends=[4]) * wh dw = paddle.clip(dw, -1.e10, np.log(1000. / 16)) dh = paddle.clip(dh, -1.e10, np.log(1000. / 16)) pred_ctr_x = dx pred_ctr_y = dy pred_w = paddle.exp(dw) pred_h = paddle.exp(dh) x1 = pred_ctr_x - 0.5 * pred_w y1 = pred_ctr_y - 0.5 * pred_h x2 = pred_ctr_x + 0.5 * pred_w y2 = pred_ctr_y + 0.5 * pred_h x1 = paddle.reshape(x1, shape=(-1, )) y1 = paddle.reshape(y1, shape=(-1, )) x2 = paddle.reshape(x2, shape=(-1, )) y2 = paddle.reshape(y2, shape=(-1, )) return paddle.concat([x1, y1, x2, y2])
def net(self, input, is_infer=False): sparse = paddle.cast(input[0], "int64") price = paddle.slice(input[0], [1], [264], [265]) label = paddle.slice(input[0], [1], [266], [267]) label = paddle.cast(label, "int64") inputs = [sparse, price] DMR_model = DMRLayer( self.user_size, self.cms_segid_size, self.cms_group_id_size, self.final_gender_code_size, self.age_level_size, self.pvalue_level_size, self.shopping_level_size, self.occupation_size, self.new_user_class_level_size, self.adgroup_id_size, self.cate_size, self.campaign_id_size, self.customer_size, self.brand_size, self.btag_size, self.pid_size, self.main_embedding_size, self.other_embedding_size) pred, loss = DMR_model(inputs, is_infer) predict_2d = paddle.concat(x=[1 - pred, pred], axis=1) auc, batch_auc, _ = paddle.static.auc(input=predict_2d, label=label, num_thresholds=2**12, slide_steps=20) auc = paddle.cast(auc, "float32") if is_infer: fetch_dict = {"auc": auc} return fetch_dict self._cost = loss fetch_dict = {'auc': auc, 'cost': loss} return fetch_dict
def subsample_labels(labels, num_samples, fg_fraction, bg_label=0, use_random=True): positive = paddle.nonzero( paddle.logical_and(labels != -1, labels != bg_label)) negative = paddle.nonzero(labels == bg_label) positive = positive.cast('int32').flatten() negative = negative.cast('int32').flatten() fg_num = int(num_samples * fg_fraction) fg_num = min(positive.numel(), fg_num) bg_num = num_samples - fg_num bg_num = min(negative.numel(), bg_num) # randomly select positive and negative examples fg_perm = paddle.randperm(positive.numel(), dtype='int32') fg_perm = paddle.slice(fg_perm, axes=[0], starts=[0], ends=[fg_num]) bg_perm = paddle.randperm(negative.numel(), dtype='int32') bg_perm = paddle.slice(bg_perm, axes=[0], starts=[0], ends=[bg_num]) if use_random: fg_inds = paddle.gather(positive, fg_perm) bg_inds = paddle.gather(negative, bg_perm) else: fg_inds = paddle.slice(positive, axes=[0], starts=[0], ends=[fg_num]) bg_inds = paddle.slice(negative, axes=[0], starts=[0], ends=[bg_num]) return fg_inds, bg_inds
def forward(self, inputs): emb = [] # input feature data for data in inputs: feat_emb = self.embedding(data) # sum pooling feat_emb = paddle.sum(feat_emb, axis=1) emb.append(feat_emb) concat_emb = paddle.concat(x=emb, axis=1) ctr_output = concat_emb for n_layer in self._ctr_mlp_layers: ctr_output = n_layer(ctr_output) ctr_out = F.softmax(ctr_output) cvr_output = concat_emb for n_layer in self._cvr_mlp_layers: cvr_output = n_layer(cvr_output) cvr_out = F.softmax(cvr_output) ctr_prop_one = paddle.slice(ctr_out, axes=[1], starts=[1], ends=[2]) cvr_prop_one = paddle.slice(cvr_out, axes=[1], starts=[1], ends=[2]) ctcvr_prop_one = paddle.multiply(x=ctr_prop_one, y=cvr_prop_one) ctcvr_prop = paddle.concat(x=[1 - ctcvr_prop_one, ctcvr_prop_one], axis=1) return ctr_out, ctr_prop_one, cvr_out, cvr_prop_one, ctcvr_prop, ctcvr_prop_one
def net(self, input, is_infer=False): pyramid_model = MatchPyramidLayer( self.emb_path, self.vocab_size, self.emb_size, self.kernel_num, self.conv_filter, self.conv_act, self.hidden_size, self.out_size, self.pool_size, self.pool_stride, self.pool_padding, self.pool_type, self.hidden_act) prediction = pyramid_model(input) if is_infer: fetch_dict = {'prediction': prediction} return fetch_dict # calculate hinge loss pos = paddle.slice(prediction, axes=[0, 1], starts=[0, 0], ends=[64, 1]) neg = paddle.slice(prediction, axes=[0, 1], starts=[64, 0], ends=[128, 1]) loss_part1 = paddle.subtract( paddle.full(shape=[64, 1], fill_value=1.0, dtype='float32'), pos) loss_part2 = paddle.add(loss_part1, neg) loss_part3 = paddle.maximum( paddle.full(shape=[64, 1], fill_value=0.0, dtype='float32'), loss_part2) avg_cost = paddle.mean(loss_part3) self.inference_target_var = avg_cost self._cost = avg_cost fetch_dict = {'cost': avg_cost} return fetch_dict
def forward(self, sequence_output): r""" The IpuBertForQuestionAnswering forward method, overrides the __call__() special method. Args: sequence_output (Tensor): See :class:`BertModel`. Returns: tuple: Returns tuple (`start_logits`, `end_logits`). With the fields: - `start_logits` (Tensor): A tensor of the input token classification logits, indicates the start position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length]. - `end_logits` (Tensor): A tensor of the input token classification logits, indicates the end position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length]. """ logits = self.classifier(sequence_output) start_logits = paddle.slice(input=logits, axes=[1], starts=[0], ends=[1]) end_logits = paddle.slice(input=logits, axes=[1], starts=[1], ends=[2]) start_logits = paddle.reshape(start_logits, [-1, self.seq_len]) end_logits = paddle.reshape(end_logits, [-1, self.seq_len]) return start_logits, end_logits
def net(self, inputs, is_infer=False): pyramid_model = MatchPyramidLayer( self.emb_path, self.vocab_size, self.emb_size, self.kernel_num, self.conv_filter, self.conv_act, self.hidden_size, self.out_size, self.pool_size, self.pool_stride, self.pool_padding, self.pool_type, self.hidden_act) prediction = pyramid_model(inputs) if is_infer: self._infer_results["prediction"] = prediction return pos = paddle.slice( prediction, axes=[0, 1], starts=[0, 0], ends=[64, 1]) neg = paddle.slice( prediction, axes=[0, 1], starts=[64, 0], ends=[128, 1]) loss_part1 = paddle.subtract( paddle.full( shape=[64, 1], fill_value=1.0, dtype='float32'), pos) loss_part2 = paddle.add(loss_part1, neg) loss_part3 = paddle.maximum( paddle.full( shape=[64, 1], fill_value=0.0, dtype='float32'), loss_part2) avg_cost = paddle.mean(loss_part3) self._cost = avg_cost
def test_1(self): input = np.random.random([3, 4, 5, 6]).astype("float64") minus_1 = fluid.layers.fill_constant([1], "int32", -1) minus_3 = fluid.layers.fill_constant([1], "int64", -3) starts = fluid.layers.data(name='starts', shape=[1, 3], append_batch_size=False) ends = fluid.layers.data(name='ends', shape=[3], append_batch_size=False) x = fluid.layers.data(name="x", shape=[3, 4, 5, 6], append_batch_size=False, dtype="float64") # value_int64 is greater than 2147483647 which is the max of int32 value_int64 = fluid.layers.fill_constant([1], "int64", 2147483648) out_1 = paddle.slice(x, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[value_int64, 100, -1]) out_2 = paddle.slice(x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, -1]) out_3 = paddle.slice(x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, minus_1]) out_4 = paddle.slice(x, axes=[0, 1, 2], starts=starts, ends=ends) out_5 = x[-3:3, 0:100, 2:-1] out_6 = x[minus_3:3, 0:100, :, 2:-1] out_7 = x[minus_1, 0:100, :, 2:minus_1] exe = fluid.Executor(place=fluid.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( fluid.default_main_program(), feed={ "x": input, 'starts': np.array([-3, 0, 2]).astype("int32"), 'ends': np.array([3, 100, -1]).astype("int32") }, fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7]) assert np.array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) assert np.array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) assert np.array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) assert np.array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) assert np.array_equal(res_5, input[-3:3, 0:100, 2:-1, :]) assert np.array_equal(res_6, input[-3:3, 0:100, :, 2:-1]) assert np.array_equal(res_7, input[-1, 0:100, :, 2:-1])
def finalize(beam_size, output_ids, parent_ids, out_seq_lens, max_seq_len=None): if max_seq_len is None: max_seq_len = paddle.max(out_seq_lens) output_ids = paddle.slice(output_ids, [0], [0], [max_seq_len]) parent_ids = paddle.slice(parent_ids, [0], [0], [max_seq_len]) % beam_size ids = paddle.nn.functional.gather_tree(output_ids, parent_ids) return ids
def create_loss(prediction): pos = paddle.slice(prediction, axes=[0, 1], starts=[0, 0], ends=[64, 1]) neg = paddle.slice(prediction, axes=[0, 1], starts=[64, 0], ends=[128, 1]) loss_part1 = paddle.subtract( paddle.full(shape=[64, 1], fill_value=1.0, dtype='float32'), pos) loss_part2 = paddle.add(loss_part1, neg) loss_part3 = paddle.maximum( paddle.full(shape=[64, 1], fill_value=0.0, dtype='float32'), loss_part2) avg_cost = paddle.mean(loss_part3) return avg_cost
def test_starts_ends_is_tensor(self): with paddle.fluid.dygraph.guard(): a = paddle.rand(shape=[4, 5, 6], dtype='float32') axes = [0, 1, 2] starts = [-3, 0, 2] ends = [3, 2, 4] a_1 = paddle.slice(a, axes=axes, starts=paddle.to_tensor(starts, dtype='int32'), ends=paddle.to_tensor(ends, dtype='int32')) a_2 = paddle.slice(a, axes=axes, starts=starts, ends=ends) self.assertTrue(np.array_equal(a_1.numpy(), a_2.numpy()))
def net(self, inputs, is_infer=False): input_data = inputs[0] label_income = inputs[1] label_marital = inputs[2] PLE = PLELayer(self.feature_size, self.task_num, self.exp_per_task, self.shared_num, self.expert_size, self.tower_size, self.level_number) pred_income, pred_marital = PLE.forward(input_data) pred_income_1 = paddle.slice(pred_income, axes=[1], starts=[1], ends=[2]) pred_marital_1 = paddle.slice(pred_marital, axes=[1], starts=[1], ends=[2]) auc_income, batch_auc_1, auc_states_1 = paddle.static.auc( #auc_income = AUC( input=pred_income, label=paddle.cast(x=label_income, dtype='int64')) #auc_marital = AUC( auc_marital, batch_auc_2, auc_states_2 = paddle.static.auc( input=pred_marital, label=paddle.cast(x=label_marital, dtype='int64')) if is_infer: fetch_dict = {'auc_income': auc_income, 'auc_marital': auc_marital} return fetch_dict cost_income = paddle.nn.functional.log_loss(input=pred_income_1, label=paddle.cast( label_income, dtype="float32")) cost_marital = paddle.nn.functional.log_loss(input=pred_marital_1, label=paddle.cast( label_marital, dtype="float32")) avg_cost_income = paddle.mean(x=cost_income) avg_cost_marital = paddle.mean(x=cost_marital) cost = avg_cost_income + avg_cost_marital self._cost = cost fetch_dict = { 'cost': cost, 'auc_income': auc_income, 'auc_marital': auc_marital } return fetch_dict
def finalize(beam_size, output_ids, parent_ids, out_seq_lens, max_seq_len=None, decoding_strategy="beam_search"): if max_seq_len is None: max_seq_len = paddle.max(out_seq_lens) ids = paddle.slice(output_ids, [0], [0], [max_seq_len]) if "beam_search" == decoding_strategy: parent_ids = paddle.slice(parent_ids, [0], [0], [max_seq_len]) % beam_size ids = paddle.nn.functional.gather_tree(ids, parent_ids) return ids
def create_loss(pred_income, pred_marital, label_income, label_marital): pred_income_1d = paddle.slice(pred_income, axes=[1], starts=[1], ends=[2]) pred_marital_1d = paddle.slice( pred_marital, axes=[1], starts=[1], ends=[2]) cost_income = paddle.nn.functional.log_loss( input=pred_income_1d, label=label_income) cost_marital = paddle.nn.functional.log_loss( input=pred_marital_1d, label=label_marital) avg_cost_income = paddle.mean(x=cost_income) avg_cost_marital = paddle.mean(x=cost_marital) cost = avg_cost_income + avg_cost_marital return cost
def test_device_guard_with_id(self): main_program = paddle.static.Program() startup_program = paddle.static.Program() with paddle.static.program_guard(main_program, startup_program): data1 = paddle.full(shape=[1, 3, 8, 8], fill_value=0.5, dtype='float32') data2 = paddle.full(shape=[1, 3, 5, 5], fill_value=0.5, dtype='float32') shape = paddle.shape(data2) with paddle.static.device_guard("cpu"): shape = paddle.slice(shape, axes=[0], starts=[0], ends=[4]) with paddle.static.device_guard("gpu:1"): out = fluid.layers.crop_tensor(data1, shape=shape) # check if the device attr is set correctly all_ops = main_program.global_block().ops device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName() for op in all_ops: if op.type == 'slice': self.assertEqual(op.desc.attr(device_attr_name), "cpu") if op.type == 'crop_tensor': self.assertEqual(op.desc.attr(device_attr_name), "gpu:1") execute(main_program, startup_program)
def create_prediction_model(self, with_att=False): self.prediction_model = Program() startup = Program() with paddle.fluid.framework.program_guard(self.prediction_model, startup): user_input = [ fluid.layers.data( name="user_emb_{}".format(i), shape=[-1, 1, self.node_emb_size], dtype="float32", ) for i in range(69) ] unit_id_emb = fluid.layers.data(name="unit_id_emb", shape=[-1, 1, self.node_emb_size], dtype="float32") dout = dnn_model_define(user_input, unit_id_emb, self.node_emb_size, with_att=with_att) softmax_prob = paddle.nn.functional.softmax(dout) positive_prob = paddle.slice(softmax_prob, axes=[1], starts=[1], ends=[2]) prob = paddle.reshape(positive_prob, [-1]) #print(str(self.prediction_model)) self.prediction_model_fetch_vars = [prob.name] self.exe.run(startup)
def compute(self, pred, label, *args): """ Compute the top-k (maxinum value in `topk`) indices. Args: pred (Tensor): The predicted value is a Tensor with dtype float32 or float64. Shape is [batch_size, d0, ..., dN]. label (Tensor): The ground truth value is Tensor with dtype int64. Shape is [batch_size, d0, ..., 1], or [batch_size, d0, ..., num_classes] in one hot representation. Return: Tensor: Correct mask, a tensor with shape [batch_size, topk]. """ pred = paddle.argsort(pred, descending=True) pred = paddle.slice(pred, axes=[len(pred.shape) - 1], starts=[0], ends=[self.maxk]) if (len(label.shape) == 1) or \ (len(label.shape) == 2 and label.shape[-1] == 1): # In static mode, the real label data shape may be different # from shape defined by paddle.static.InputSpec in model # building, reshape to the right shape. label = paddle.reshape(label, (-1, 1)) elif label.shape[-1] != 1: # one-hot label label = paddle.argmax(label, axis=-1, keepdim=True) correct = pred == label return paddle.cast(correct, dtype='float32')
def forward(self, fatoms, fbonds, agraph, bgraph, scope, tree_message): """Forward""" fatoms = paddle.to_tensor(fatoms) fbonds = paddle.to_tensor(fbonds) agraph = paddle.to_tensor(agraph) bgraph = paddle.to_tensor(bgraph) binput = self.W_i(fbonds) graph_message = F.relu(binput) for i in range(self.depth - 1): message = paddle.concat([tree_message, graph_message], axis=0) nei_message = index_select_ND(message, 0, bgraph) nei_message = paddle.sum(nei_message, axis=1) nei_message = self.W_h(nei_message) graph_message = F.relu(binput + nei_message) message = paddle.concat([tree_message, graph_message], axis=0) nei_message = index_select_ND(message, 0, agraph) nei_message = paddle.sum(nei_message, axis=1) ainput = paddle.concat([fatoms, nei_message], axis=1) atom_hiddens = F.relu(self.W_o(ainput)) mol_vecs = [] for st, le in scope: mol_vec = paddle.sum( paddle.slice(atom_hiddens, [0], [st], [st + le]), axis=0) / le mol_vecs.append(mol_vec) mol_vecs = paddle.stack(mol_vecs, axis=0) return mol_vecs
def forward(self, data, target, *mems): if not mems: batch_size = data.shape[0] mems = self.init_mems(batch_size, self.d_model) hidden, new_mems = self._forward(data, mems=mems) # TODO(FrostML): use getitem. tgt_len = target.shape[1] pred_hid = paddle.slice(hidden, [1], [-tgt_len], [hidden.shape[1]]) if self.sample_softmax > 0 and self.training: assert self.tie_weight, "tie_weight must be True if sample_softmax > 0" logit = sample_logits(self.word_emb, self.out_layer.bias, target, pred_hid, self.sampler) loss = -paddle.log(F.softmax(logit, axis=-1))[:, :, 0] else: loss = self.crit( paddle.reshape( pred_hid, shape=[-1, pred_hid.shape[-1]]), paddle.reshape( target, shape=[-1])) if new_mems is None: return [loss.mean()] else: return [loss.mean()] + new_mems
def test(self): x = paddle.ones(shape=[3, 4, 5]) x.desc.set_shape([3, -1, 5]) self.assertEqual(x.shape, (3, -1, 5)) out0 = paddle.slice(x, axes=[1], starts=[0], ends=[3]) self.assertEqual(out0.shape, (3, 3, 5))
def crop(x, audio_start, audio_length): """Crop the upsampled condition to match audio_length. The upsampled condition has the same time steps as the whole audio does. But since audios are sliced to 0.5 seconds randomly while conditions are not, upsampled conditions should also be sliced to extactly match the time steps of the audio slice. Parameters ---------- x : Tensor [shape=(B, C, T)] The upsampled condition. audio_start : Tensor [shape=(B,), dtype:int] The index of the starting point of the audio clips. audio_length : int The length of the audio clip(number of samples it contaions). Returns ------- Tensor [shape=(B, C, audio_length)] Cropped condition. """ # crop audio slices = [] # for each example # paddle now supports Tensor of shape [1] in slice # starts = audio_start.numpy() for i in range(x.shape[0]): start = audio_start[i] end = start + audio_length slice = paddle.slice(x[i], axes=[1], starts=[start], ends=[end]) slices.append(slice) out = paddle.stack(slices) return out
def forward(self, inputs): x = self.bn1(inputs) x = paddle.reshape(x, [1, 3 * 16 * 16]) x = self.fc1(x) x = paddle.fluid.layers.unsqueeze(input=x, axes=[2]) x = self.relu1(x) y = paddle.fluid.layers.fill_constant(x.shape, dtype=paddle.float32, value=1) # x = paddle.stack([x, y], axis=3) x = paddle.slice(x, axes=[0], starts=[0], ends=[1]) x = paddle.exp(x) # y += paddle.fluid.layers.uniform_random(y.shape) y = paddle.expand(y, shape=[1, 768, 768, 2]) x = paddle.expand(x, shape=[1, 768, 768, 2]) out = paddle.concat([x, y]) out = self.dp(out) out = channel_shuffle(out, 2) out1, out2 = paddle.split(out, num_or_sections=2, axis=1) outshape = out1.shape max_idx = paddle.argmax(out1.reshape( (outshape[0], outshape[1], outshape[2] * outshape[3])), axis=-1) out2 = out2.reshape( (outshape[0], outshape[1], outshape[2] * outshape[3])) res, _ = self.lstm(out2) return res, max_idx
def forward(self, input_data, is_infer): query_fc = input_data[0] for n_layer in self._query_layers: query_fc = n_layer(query_fc) doc_pos_fc = input_data[1] for n_layer in self._pos_layers: doc_pos_fc = n_layer(doc_pos_fc) R_Q_D_p = F.cosine_similarity(query_fc, doc_pos_fc, axis=1, eps=0).reshape([-1, 1]) if is_infer: return R_Q_D_p, paddle.ones(shape=[self.slice_end, 1]) R_Q_D_ns = [] for i in range(len(input_data) - 2): doc_neg_fc_i = input_data[i + 2] for n_layer in self._neg_layers: doc_neg_fc_i = n_layer(doc_neg_fc_i) R_Q_D_n = F.cosine_similarity(query_fc, doc_neg_fc_i, axis=1, eps=0).reshape([-1, 1]) R_Q_D_ns.append(R_Q_D_n) concat_Rs = paddle.concat(x=[R_Q_D_p] + R_Q_D_ns, axis=1) prob = F.softmax(concat_Rs, axis=1) hit_prob = paddle.slice(prob, axes=[0, 1], starts=[0, 0], ends=[self.slice_end, -1]) return R_Q_D_p, hit_prob
def net(self, inputs, is_infer=False): input_data = inputs[0] label_income = inputs[1] label_marital = inputs[2] MMoE = MMoELayer(self.feature_size, self.expert_num, self.expert_size, self.tower_size, self.gate_num) pred_income, pred_marital = MMoE(input_data) pred_income_1 = paddle.slice(pred_income, axes=[1], starts=[1], ends=[2]) pred_marital_1 = paddle.slice(pred_marital, axes=[1], starts=[1], ends=[2]) auc_income, batch_auc_1, auc_states_1 = paddle.fluid.layers.auc( #auc_income = AUC( input=pred_income, label=paddle.cast(x=label_income, dtype='int64')) #auc_marital = AUC( auc_marital, batch_auc_2, auc_states_2 = paddle.fluid.layers.auc( input=pred_marital, label=paddle.cast(x=label_marital, dtype='int64')) if is_infer: self._infer_results["AUC_income"] = auc_income self._infer_results["AUC_marital"] = auc_marital return # 1.8 cross_entropy cost_income = paddle.nn.functional.log_loss(input=pred_income_1, label=label_income) cost_marital = paddle.nn.functional.log_loss(input=pred_marital_1, label=label_marital) avg_cost_income = paddle.mean(x=cost_income) avg_cost_marital = paddle.mean(x=cost_marital) cost = avg_cost_income + avg_cost_marital self._cost = cost self._metrics["AUC_income"] = auc_income self._metrics["BATCH_AUC_income"] = batch_auc_1 self._metrics["AUC_marital"] = auc_marital self._metrics["BATCH_AUC_marital"] = batch_auc_2
def __call__(self, x): start = self.point[0] if len(self.point) == 2: end = self.point[1] else: end = self.input_shape[self.axis] out = paddle.slice(x=x, start=start, end=end, axes=[self.axis]) return out
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 forward(ctx, inp, rank, world_size, group): B = inp.shape[0] local_batch_size = B // world_size batch_start = local_batch_size * rank batch_end = min(batch_start + local_batch_size, B) inp = paddle.slice(inp, axes=[0], starts=[batch_start], ends=[batch_end]) ctx.args = world_size, group return inp
def __call__(self, indices, depth, values): indices_shape = indices.shape rank = len(indices.shape) real_axis = self.axis if self.axis < 0: real_axis = self.axis + rank + 1 depth_range = paddle.arange(end=depth) ls = tuple(indices_shape[0:real_axis]) rs = tuple(indices_shape[real_axis:rank]) targets = paddle.reshape(depth_range, (1, ) * (real_axis - 0) + tuple(depth_range.shape) + (1, ) * (rank - real_axis)) mod = paddle.mod(indices, depth) v = paddle.reshape(mod, ls + (1, ) + rs) out = targets == v out = paddle.cast(out, "float32") on_value = paddle.slice(values, axes=[0], starts=[1], ends=[2]) off_value = paddle.slice(values, axes=[0], starts=[0], ends=[1]) out = out * (on_value - off_value) + off_value return out
def forward(self, inputs: paddle.Tensor): shape_nchw = paddle.to_tensor(inputs.shape) shape_hw = paddle.slice(shape_nchw, axes=[0], starts=[2], ends=[4]) shape_hw.stop_gradient = True in_shape = paddle.cast(shape_hw, dtype='int32') out_shape = in_shape * self.scale out_shape.stop_gradient = True out = F.resize_nearest(input=inputs, scale=self.scale, actual_shape=out_shape) return out
def forward(self, inputs): """ forward """ axes = self.config['axes'] starts = self.config['starts'] starts = [1, 0, paddle.to_tensor(0), 0] ends = self.config['ends'] ends = [10, 10, paddle.to_tensor(10), 10] x = paddle.slice(inputs, axes=axes, starts=starts, ends=ends) return x