def forward(self, x): """Inputs of forward function Args: x: the sequence fed to the positional encoder model (required). Shape: x: [sequence length, batch size, embed dim] output: [sequence length, batch size, embed dim] Examples: >>> output = pos_encoder(x) """ w_pe = self.pe[:paddle.shape(x)[-1], :] w1 = self.linear1(self.avg_pool_1(x).squeeze()).unsqueeze(0) w_pe = w_pe * w1 w_pe = paddle.transpose(w_pe, [1, 2, 0]) w_pe = paddle.unsqueeze(w_pe, 2) h_pe = self.pe[:paddle.shape(x).shape[-2], :] w2 = self.linear2(self.avg_pool_2(x).squeeze()).unsqueeze(0) h_pe = h_pe * w2 h_pe = paddle.transpose(h_pe, [1, 2, 0]) h_pe = paddle.unsqueeze(h_pe, 3) x = x + w_pe + h_pe x = paddle.transpose( paddle.reshape(x, [x.shape[0], x.shape[1], x.shape[2] * x.shape[3]]), [2, 0, 1]) return self.dropout(x)
def scatter_paddle(self, refined_seg_logits, point_indices, point_logits): """ paddle version scatter : equal to pytorch version scatter(-1,point_indices,point_logits). Args: refined_seg_logits(Tensor): shape=[batch_size, channels, height * width] point_indices(Tensor): shape=[batch_size, channels, height * width] point_logits(Tensor): shape[batch_size, channels, height * width] Returns: scattered refined_seg_logits(Tensor). """ original_shape = paddle.shape( refined_seg_logits) # [batch_size, channels, height * width] new_refined_seg_logits = refined_seg_logits.flatten(0, 1) # [N*C,H*W] offsets = (paddle.arange(paddle.shape(new_refined_seg_logits)[0]) * paddle.shape(new_refined_seg_logits)[1]).unsqueeze( -1) # [N*C,1] point_indices = point_indices.flatten(0, 1) # [N*C,H*W] new_point_indices = (point_indices + offsets).flatten() point_logits = point_logits.flatten() # [N*C*H*W] refined_seg_logits = paddle.scatter(refined_seg_logits.flatten(), new_point_indices, point_logits, overwrite=True) return refined_seg_logits.reshape(shape=original_shape)
def paste_mask(self, masks, boxes, im_h, im_w): """ Paste the mask prediction to the original image. """ x0_int, y0_int = 0, 0 x1_int, y1_int = im_w, im_h x0, y0, x1, y1 = paddle.split(boxes, 4, axis=1) N = masks.shape[0] img_y = paddle.arange(y0_int, y1_int) + 0.5 img_x = paddle.arange(x0_int, x1_int) + 0.5 img_y = (img_y - y0) / (y1 - y0) * 2 - 1 img_x = (img_x - x0) / (x1 - x0) * 2 - 1 # img_x, img_y have shapes (N, w), (N, h) if self.assign_on_cpu: paddle.set_device('cpu') gx = img_x[:, None, :].expand( [N, paddle.shape(img_y)[1], paddle.shape(img_x)[1]]) gy = img_y[:, :, None].expand( [N, paddle.shape(img_y)[1], paddle.shape(img_x)[1]]) grid = paddle.stack([gx, gy], axis=3) img_masks = F.grid_sample(masks, grid, align_corners=False) return img_masks[:, 0]
def forward(self, x): feats = self.backbone(x) feats = [feats[i] for i in self.backbone_indices] fpn_feats = self.neck(feats) # [n,256,64,128]*3 & [n,256,128,256] pfn_logits = self.fpnhead( fpn_feats ) # segmainoutput decode_head[0] 512*1024->[n, 19, 64, 128] point_logits = self.pointhead( fpn_feats, pfn_logits) # segpointoutput decode_head[1] if self.training: logit_list = [ F.interpolate(logit, paddle.shape(x)[2:], mode='bilinear', align_corners=self.align_corners) for logit in pfn_logits ] logit_list.append(point_logits) else: logit_list = [ F.interpolate(logit, paddle.shape(x)[2:], mode='bilinear', align_corners=self.align_corners) for logit in point_logits ] return logit_list
def forward(self, input, mask=None): """ Args: input (obj: `paddle.Tensor`) of shape (batch, seq_len, input_size): Tensor containing the features of the input sequence. mask (obj: `paddle.Tensor`, optional, defaults to `None`) of shape (batch, seq_len) : Tensor is a bool tensor, whose each element identifies whether the input word id is pad token or not. """ weight = self.input_weight.tile( repeat_times=(paddle.shape(input)[0], 1, 1)) bias = self.bias.tile(repeat_times=(paddle.shape(input)[0], 1, 1)) # Shape: (batch_size, max_seq_len, hidden_size) word_squish = paddle.bmm(input, weight) + bias att_context_vector = self.att_context_vector.tile( repeat_times=(paddle.shape(input)[0], 1, 1)) # Shape: (batch_size, max_seq_len, 1) att_score = paddle.bmm(word_squish, att_context_vector) if mask is not None: # mask, remove the effect of 'PAD' mask = paddle.cast(mask, dtype='float32') mask = mask.unsqueeze(axis=-1) inf_tensor = paddle.full( shape=paddle.shape(mask), dtype='float32', fill_value=-INF) att_score = paddle.multiply(att_score, mask) + paddle.multiply( inf_tensor, (1 - mask)) att_weight = F.softmax(att_score, axis=1) # Shape: (batch_size, hidden_size) reps = paddle.bmm(input.transpose(perm=(0, 2, 1)), att_weight).squeeze(-1) return reps, att_weight
def get_target_tensor(self, prediction, target_is_real): """Create label tensors with the same size as the input. Parameters: prediction (tensor) - - tpyically the prediction from a discriminator target_is_real (bool) - - if the ground truth label is for real images or fake images Returns: A label tensor filled with ground truth label, and with the size of the input """ if target_is_real: if not hasattr(self, 'target_real_tensor'): self.target_real_tensor = paddle.full( shape=paddle.shape(prediction), fill_value=self.target_real_label, dtype='float32') target_tensor = self.target_real_tensor else: if not hasattr(self, 'target_fake_tensor'): self.target_fake_tensor = paddle.full( shape=paddle.shape(prediction), fill_value=self.target_fake_label, dtype='float32') target_tensor = self.target_fake_tensor # target_tensor.stop_gradient = True return target_tensor
def forward(self, inputs): """ Get SOLOv2MaskHead output. Args: inputs(list[Tensor]): feature map from each necks with shape of [N, C, H, W] Returns: ins_pred(Tensor): Output of SOLOv2MaskHead head """ feat_all_level = F.relu(self.convs_all_levels[0](inputs[0])) for i in range(1, self.range_level): input_p = inputs[i] if i == (self.range_level - 1): input_feat = input_p x_range = paddle.linspace( -1, 1, paddle.shape(input_feat)[-1], dtype='float32') y_range = paddle.linspace( -1, 1, paddle.shape(input_feat)[-2], dtype='float32') y, x = paddle.meshgrid([y_range, x_range]) x = paddle.unsqueeze(x, [0, 1]) y = paddle.unsqueeze(y, [0, 1]) y = paddle.expand( y, shape=[paddle.shape(input_feat)[0], 1, -1, -1]) x = paddle.expand( x, shape=[paddle.shape(input_feat)[0], 1, -1, -1]) coord_feat = paddle.concat([x, y], axis=1) input_p = paddle.concat([input_p, coord_feat], axis=1) feat_all_level = paddle.add(feat_all_level, self.convs_all_levels[i](input_p)) ins_pred = F.relu(self.conv_pred(feat_all_level)) return ins_pred
def forward(self, x): # Encoder input_shape = paddle.shape(x)[2:] x = self.conv_bn0(x) # 1/2 shortcut = self.conv_bn1(x) # shortcut x = F.max_pool2d(x, kernel_size=3, stride=2, padding=1) # 1/4 x = self.block1(x) # 1/8 x = self.block2(x) # 1/16 # Decoder x = self.depthwise_separable0(x) shortcut_shape = paddle.shape(shortcut)[2:] x = F.interpolate( x, shortcut_shape, mode='bilinear', align_corners=self.align_corners) x = paddle.concat(x=[shortcut, x], axis=1) x = self.depthwise_separable1(x) logit = self.depthwise_separable2(x) logit = F.interpolate( logit, input_shape, mode='bilinear', align_corners=self.align_corners) return [logit]
def forward_test(self, src): bs = paddle.shape(src)[0] if self.encoder is not None: src = self.positional_encoding(paddle.transpose(src, [1, 0, 2])) memory = self.encoder(src) else: memory = paddle.transpose(paddle.squeeze(src, 2), [2, 0, 1]) dec_seq = paddle.full((bs, 1), 2, dtype=paddle.int64) dec_prob = paddle.full((bs, 1), 1., dtype=paddle.float32) for len_dec_seq in range(1, 25): dec_seq_embed = paddle.transpose(self.embedding(dec_seq), [1, 0, 2]) dec_seq_embed = self.positional_encoding(dec_seq_embed) tgt_mask = self.generate_square_subsequent_mask( paddle.shape(dec_seq_embed)[0]) output = self.decoder(dec_seq_embed, memory, tgt_mask=tgt_mask, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None) dec_output = paddle.transpose(output, [1, 0, 2]) dec_output = dec_output[:, -1, :] word_prob = F.softmax(self.tgt_word_prj(dec_output), axis=1) preds_idx = paddle.argmax(word_prob, axis=1) if paddle.equal_all( preds_idx, paddle.full(paddle.shape(preds_idx), 3, dtype='int64')): break preds_prob = paddle.max(word_prob, axis=1) dec_seq = paddle.concat( [dec_seq, paddle.reshape(preds_idx, [-1, 1])], axis=1) dec_prob = paddle.concat( [dec_prob, paddle.reshape(preds_prob, [-1, 1])], axis=1) return [dec_seq, dec_prob]
def forward(self, conv_out): last_out = self.lateral_convs[-1](conv_out[-1]) f = last_out fpn_feature_list = [last_out] for i in reversed(range(len(conv_out) - 1)): conv_x = conv_out[i] conv_x = self.lateral_convs[i](conv_x) prev_shape = paddle.shape(conv_x)[2:] f = conv_x + F.interpolate( f, prev_shape, mode='bilinear', align_corners=True) fpn_feature_list.append(self.fpn_out[i](f)) output_size = paddle.shape(fpn_feature_list[-1])[2:] x = self.scale_heads[0](fpn_feature_list[-1]) for index in range(len(self.scale_heads) - 2, 0, -1): x = x + F.interpolate(self.scale_heads[index]( fpn_feature_list[index]), size=output_size, mode='bilinear', align_corners=self.align_corners) x = self.cls_seg(x) if self.enable_auxiliary_loss: dsn = self.dsn(conv_out[2]) return [x, dsn] else: return [x]
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 __call__(self, x, index): if self.dim < 0: self.dim += len(x.shape) x_range = list(range(len(x.shape))) x_range[0] = self.dim x_range[self.dim] = 0 x_swaped = paddle.transpose(x, perm=x_range) index_range = list(range(len(index.shape))) index_range[0] = self.dim index_range[self.dim] = 0 index_swaped = paddle.transpose(index, perm=index_range) dtype = index.dtype x_shape = paddle.shape(x_swaped) index_shape = paddle.shape(index_swaped) prod = paddle.cast(paddle.prod(x_shape), dtype=dtype) / x_shape[0] x_swaped_flattend = paddle.flatten(x_swaped) index_swaped_flattend = paddle.flatten(index_swaped) index_swaped_flattend *= prod bias = paddle.arange(start=0, end=prod, dtype=dtype) bias = paddle.reshape(bias, x_shape[1:]) bias = paddle.crop(bias, index_shape[1:]) bias = paddle.flatten(bias) bias = paddle.tile(bias, [index_shape[0]]) index_swaped_flattend += bias gathered = paddle.index_select(x_swaped_flattend, index_swaped_flattend) gathered = paddle.reshape(gathered, index_swaped.shape) out = paddle.transpose(gathered, perm=x_range) return out
def forward(self, input_ids, position_ids=None, attention_mask=None, use_cache=False, cache=None): self.checkpoints = [] if position_ids is None: past_length = 0 if cache is not None: past_length = paddle.shape(cache[0].k)[-2] position_ids = paddle.arange(past_length, paddle.shape(input_ids)[-1] + past_length, dtype='int64') position_ids = position_ids.unsqueeze(0) # .expand_as(input_ids) position_ids = paddle.fluid.layers.expand_as( position_ids, input_ids) embedding_output = self.embeddings(input_ids=input_ids, position_ids=position_ids) encoder_outputs = self.decoder(embedding_output, memory=None, tgt_mask=None, use_cache=use_cache, cache=cache) self.checkpoints.extend(self.decoder.checkpoints) return encoder_outputs
def forward(self, x): outputs = [] if self.data_format == 'NCHW': interpolate_shape = paddle.shape(x)[2:] axis = 1 else: interpolate_shape = paddle.shape(x)[1:3] axis = -1 for block in self.aspp_blocks: y = block(x) outputs.append(y) if self.image_pooling: img_avg = self.global_avg_pool(x) img_avg = F.interpolate( img_avg, interpolate_shape, mode='bilinear', align_corners=self.align_corners, data_format=self.data_format) outputs.append(img_avg) x = paddle.concat(outputs, axis=axis) x = self.conv_bn_relu(x) x = self.dropout(x) return x
def net(self, inputs, is_infer=False): word2vec_model = Word2VecLayer(self.sparse_feature_number, self.sparse_feature_dim, self.neg_num, emb_name="emb", emb_w_name="emb_w", emb_b_name="emb_b") true_logits, neg_logits = word2vec_model.forward(inputs) label_ones = paddle.full(shape=[paddle.shape(true_logits)[0], 1], fill_value=1.0) label_zeros = paddle.full( shape=[paddle.shape(true_logits)[0], self.neg_num], fill_value=0.0) true_logits = paddle.nn.functional.sigmoid(true_logits) true_xent = paddle.nn.functional.binary_cross_entropy( true_logits, label_ones) neg_logits = paddle.nn.functional.sigmoid(neg_logits) neg_xent = paddle.nn.functional.binary_cross_entropy( neg_logits, label_zeros) cost = paddle.add(true_xent, neg_xent) avg_cost = paddle.mean(x=cost) self._cost = avg_cost fetch_dict = {'loss': avg_cost} return fetch_dict
def forward(self, decoder_input_ids=None, decoder_attention_mask=None, encoder_output=None, memory_mask=None, cache=None): if decoder_attention_mask is None: decoder_length = paddle.shape(decoder_input_ids)[-1] decoder_attention_mask = paddle.tensor.triu( (paddle.full( (decoder_length, decoder_length), -np.inf, dtype=paddle.get_default_dtype())), 1) decoder_inputs_embeds = self.embed_tokens(decoder_input_ids) past_key_values_length = paddle.shape(cache[0][0].k)[ 2] if cache is not None else 0 decoder_inputs_embed_pos = self.decoder_embed_positions( decoder_input_ids.shape, past_key_values_length) hidden_states = decoder_inputs_embeds + decoder_inputs_embed_pos hidden_states = self.decoder_layernorm_embedding(hidden_states) decoder_input = self.decoder_dropout(hidden_states) decoder_output = self.decoder( tgt=decoder_input, memory=encoder_output, tgt_mask=decoder_attention_mask, memory_mask=memory_mask, cache=cache) return decoder_output
def forward(self, x, patch_embed_size): """ Forward function. Args: x (Tensor): Input tensor of decoder. patch_embed_size (Tensor): The height and width of the patch embed tensor. Returns: list[Tensor]: Segmentation results. """ x = self.proj_input(x) cls_token = self.cls_token.expand((paddle.shape(x)[0], -1, -1)) x = paddle.concat([x, cls_token], axis=1) for block in self.blocks: x = block(x) x = self.decoder_norm(x) patches, masks = x[:, :-self.num_classes], x[:, -self.num_classes:] patches = self.proj_patch(patches) masks = self.proj_class(masks) patches = patches / paddle.norm(patches, axis=-1, keepdim=True) masks = masks / paddle.norm(masks, axis=-1, keepdim=True) masks = patches @ masks.transpose((0, 2, 1)) masks = masks.reshape( (0, 0, self.num_classes)) # For export inference model masks = self.mask_norm(masks) #[b, (h w), c] -> [b, c, h, w] h, w = patch_embed_size[0], patch_embed_size[1] masks = masks.reshape((0, h, w, paddle.shape(masks)[-1])) masks = masks.transpose((0, 3, 1, 2)) return [masks]
def row_column_shuffle(embedding): embedding = paddle.transpose(embedding, perm=[1, 0]) corrupted_embedding = paddle.transpose(embedding[paddle.randperm( paddle.shape(embedding)[0])], perm=[1, 0]) return corrupted_embedding[paddle.randperm( paddle.shape(corrupted_embedding)[0])]
def __call__(self, hm, wh, reg, im_shape, scale_factor): heat = self._simple_nms(hm) scores, inds, clses, ys, xs = self._topk(heat) scores = paddle.tensor.unsqueeze(scores, [1]) clses = paddle.tensor.unsqueeze(clses, [1]) reg_t = paddle.transpose(reg, [0, 2, 3, 1]) # Like TTFBox, batch size is 1. # TODO: support batch size > 1 reg = paddle.reshape(reg_t, [-1, paddle.shape(reg_t)[-1]]) reg = paddle.gather(reg, inds) xs = paddle.cast(xs, 'float32') ys = paddle.cast(ys, 'float32') xs = xs + reg[:, 0:1] ys = ys + reg[:, 1:2] wh_t = paddle.transpose(wh, [0, 2, 3, 1]) wh = paddle.reshape(wh_t, [-1, paddle.shape(wh_t)[-1]]) wh = paddle.gather(wh, inds) if self.regress_ltrb: x1 = xs - wh[:, 0:1] y1 = ys - wh[:, 1:2] x2 = xs + wh[:, 2:3] y2 = ys + wh[:, 3:4] else: x1 = xs - wh[:, 0:1] / 2 y1 = ys - wh[:, 1:2] / 2 x2 = xs + wh[:, 0:1] / 2 y2 = ys + wh[:, 1:2] / 2 n, c, feat_h, feat_w = hm.shape[:] padw = (feat_w * self.down_ratio - im_shape[0, 1]) / 2 padh = (feat_h * self.down_ratio - im_shape[0, 0]) / 2 x1 = x1 * self.down_ratio y1 = y1 * self.down_ratio x2 = x2 * self.down_ratio y2 = y2 * self.down_ratio x1 = x1 - padw y1 = y1 - padh x2 = x2 - padw y2 = y2 - padh bboxes = paddle.concat([x1, y1, x2, y2], axis=1) scale_y = scale_factor[:, 0:1] scale_x = scale_factor[:, 1:2] scale_expand = paddle.concat([scale_x, scale_y, scale_x, scale_y], axis=1) boxes_shape = paddle.shape(bboxes) boxes_shape.stop_gradient = True scale_expand = paddle.expand(scale_expand, shape=boxes_shape) bboxes = paddle.divide(bboxes, scale_expand) if self.for_mot: results = paddle.concat([bboxes, scores, clses], axis=1) return results, inds else: results = paddle.concat([clses, scores, bboxes], axis=1) return results, paddle.shape(results)[0:1]
def _dice_loss(self, input, target): input = paddle.reshape(input, shape=(paddle.shape(input)[0], -1)) target = paddle.reshape(target, shape=(paddle.shape(target)[0], -1)) a = paddle.sum(input * target, axis=1) b = paddle.sum(input * input, axis=1) + 0.001 c = paddle.sum(target * target, axis=1) + 0.001 d = (2 * a) / (b + c) return 1 - d
def test_positive_attr_shape(self): x = paddle.zeros([1, 3, 5, 7]) self.assertEqual( paddle.jit.dy2static.choose_shape_attr_or_api( x.shape, paddle.shape(x)), x.shape) self.assertEqual( paddle.jit.dy2static.choose_shape_attr_or_api( x.shape, paddle.shape(x), 3), x.shape[3])
def forward(self, src_word, trg_word): src_max_len = paddle.shape(src_word)[-1] trg_max_len = paddle.shape(trg_word)[-1] base_attn_bias = paddle.cast( src_word == self.bos_id, dtype=paddle.get_default_dtype()).unsqueeze([1, 2]) * -1e9 src_slf_attn_bias = base_attn_bias src_slf_attn_bias.stop_gradient = True trg_slf_attn_bias = paddle.tensor.triu( (paddle.ones( (trg_max_len, trg_max_len), dtype=paddle.get_default_dtype()) * -np.inf), 1) trg_slf_attn_bias.stop_gradient = True trg_src_attn_bias = paddle.tile(base_attn_bias, [1, 1, trg_max_len, 1]) src_pos = paddle.cast( src_word != self.bos_id, dtype="int64") * paddle.arange( start=0, end=src_max_len) trg_pos = paddle.cast( trg_word != self.bos_id, dtype="int64") * paddle.arange( start=0, end=trg_max_len) src_emb = self.src_word_embedding(src_word) src_pos_emb = self.src_pos_embedding(src_pos) src_emb = src_emb + src_pos_emb enc_input = F.dropout( src_emb, p=self.dropout, training=self.training) if self.dropout else src_emb with paddle.static.amp.fp16_guard(): if self.waitk >= src_max_len or self.waitk == -1: # Full sentence enc_outputs = [ self.encoder( enc_input, src_mask=src_slf_attn_bias) ] else: # Wait-k policy enc_outputs = [] for i in range(self.waitk, src_max_len + 1): enc_output = self.encoder( enc_input[:, :i, :], src_mask=src_slf_attn_bias[:, :, :, :i]) enc_outputs.append(enc_output) trg_emb = self.trg_word_embedding(trg_word) trg_pos_emb = self.trg_pos_embedding(trg_pos) trg_emb = trg_emb + trg_pos_emb dec_input = F.dropout( trg_emb, p=self.dropout, training=self.training) if self.dropout else trg_emb dec_output = self.decoder( dec_input, enc_outputs, tgt_mask=trg_slf_attn_bias, memory_mask=trg_src_attn_bias) predict = self.linear(dec_output) return predict
def test_shape_with_var(self): with program_guard(Program(), Program()): x = paddle.static.data(shape=[-1, 1, 3], name='x') fake_var = paddle.randn([2, 3]) target_shape = [ -1, paddle.shape(fake_var)[0], paddle.shape(fake_var)[1] ] out = paddle.expand(x, shape=target_shape) self.assertListEqual(list(out.shape), [-1, -1, -1])
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 test_negative_attr_shape(self): x = paddle.zeros([7]) self.assertEqual( paddle.jit.dy2static.choose_shape_attr_or_api([-1], paddle.shape(x), 0), paddle.shape(x)[0]) self.assertEqual( paddle.jit.dy2static.choose_shape_attr_or_api([-1], paddle.shape(x)), paddle.shape(x))
def forward(self, x): y = paddle.rand(shape=[1, 3, 4, 4]) w1 = paddle.shape(y)[0] w2 = paddle.shape(x)[0] while w2 != w1: x = F.avg_pool2d(x, kernel_size=3, padding=1, stride=2) w2 = paddle.shape(x)[0] return x + y
def forward(self, input_ids, position_ids=None, attention_mask=None, use_cache=False, cache=None): self.checkpoints = [] if position_ids is None: past_length = 0 if cache is not None: past_length = paddle.shape(cache[0].k)[-2] position_ids = paddle.arange(past_length, paddle.shape(input_ids)[-1] + past_length, dtype='int64') position_ids = position_ids.unsqueeze(0) position_ids = paddle.fluid.layers.expand_as( position_ids, input_ids) embedding_output = self.embeddings(input_ids=input_ids, position_ids=position_ids) if _global_parallel_strategy == "pp": auto.shard_tensor(input_ids, dist_attr={ "process_mesh": PP_MESH_LIST[0], "dims_mapping": [-1 for i in range(len(input_ids.shape))] }) if _global_parallel_strategy == "dp_pp": auto.shard_tensor( input_ids, dist_attr={ "process_mesh": DPPP_MESH_LIST[0], "dims_mapping": [0] + [-1 for i in range(len(input_ids.shape) - 1)] }) if _global_parallel_strategy == "dp_mp_pp": auto.shard_tensor( input_ids, dist_attr={ "process_mesh": DPMPPP_MESH_LIST[0], "dims_mapping": [0] + [-1 for i in range(len(input_ids.shape) - 1)] }) encoder_outputs = self.decoder(embedding_output, memory=None, tgt_mask=attention_mask, use_cache=use_cache, cache=cache) self.checkpoints.extend(self.decoder.checkpoints) return encoder_outputs
def dmr_fcn_attention(item_eb, item_his_eb, context_his_eb, mask, mode='SUM'): mask = paddle.equal(mask, paddle.ones_like(mask)) item_eb_tile = paddle.tile(item_eb, [1, paddle.shape(mask)[1]]) # B, T*E item_eb_tile = paddle.reshape( item_eb_tile, [-1, paddle.shape(mask)[1], item_eb.shape[-1]]) # B, T, E if context_his_eb is None: query = item_eb_tile else: query = paddle.concat([item_eb_tile, context_his_eb], axis=-1) query = self.query_layer2(query) query = self.query_prelu2(query) dmr_all = paddle.concat( [ query, item_his_eb, query - item_his_eb, query * item_his_eb ], axis=-1) att_layer_1 = self.att_layer1_layer2(dmr_all) att_layer_1 = F.sigmoid(att_layer_1) att_layer_2 = self.att_layer2_layer2(att_layer_1) att_layer_2 = F.sigmoid(att_layer_2) att_layer_3 = self.att_layer3_layer2(att_layer_2) # B, T, 1 att_layer_3 = paddle.reshape( att_layer_3, [-1, 1, paddle.shape(item_his_eb)[1]]) # B,1,T scores = att_layer_3 scores = scores.reshape([-1, 1, self.history_length]) ## # Mask key_masks = paddle.unsqueeze(mask, 1) # B,1,T paddings = paddle.ones_like(scores) * (-2**32 + 1) paddings_no_softmax = paddle.zeros_like(scores) scores = paddle.where(key_masks, scores, paddings) # [B, 1, T] scores_no_softmax = paddle.where(key_masks, scores, paddings_no_softmax) scores = F.softmax(scores) if mode == 'SUM': output = paddle.matmul(scores, item_his_eb) # [B, 1, H] output = paddle.sum(output, axis=1) # B,E else: scores = paddle.reshape(scores, [-1, paddle.shape(item_his_eb)[1]]) output = item_his_eb * paddle.unsqueeze(scores, -1) output = paddle.reshape(output, paddle.shape(item_his_eb)) return output, scores, scores_no_softmax
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 drop_path(x, drop_prob=0., training=False): if drop_prob == 0. or not training: return x keep_prob = 1 - drop_prob B = paddle.shape(x)[0] ndim = len(paddle.shape(x)) shape = (B,) + (1,) * (ndim - 1) # work with diff dim tensors, not just 2D ConvNets random_tensor = keep_prob + paddle.rand(shape, dtype=x.dtype) random_tensor = random_tensor.floor() # binarize output = x / keep_prob * random_tensor return output