def _seq_classification_task(self, lstm_output): """calc seq class loss""" conv_out = self.conv_encoder(lstm_output) conv_out = F.relu(conv_out) # Shape: (batch_size, num_class, num_tokens, ) logits = self.output_layer(conv_out).transpose(perm=(0, 2, 1)) return logits
def forward(self, fpn_feats, spatial_scale, mode): cls_logits_list = [] bboxes_reg_list = [] centerness_list = [] assert len(fpn_feats) == len( self.fpn_stride ), "The size of fpn_feats is not equal to size of fpn_stride" fcos_cls_feats, fcos_reg_feats = self.fcos_feat(fpn_feats) for scale_reg, fpn_stride, fcos_cls_feat, fcos_reg_feat in zip( self.scales_regs, self.fpn_stride, fcos_cls_feats, fcos_reg_feats): cls_logits = self.fcos_head_cls[0](fcos_cls_feat) bbox_reg = self.fcos_head_reg[0](fcos_reg_feat) bbox_reg = scale_reg(bbox_reg) if self.centerness_on_reg: centerness = self.fcos_head_centerness[0](fcos_reg_feat) else: centerness = self.fcos_head_centerness[0](fcos_cls_feat) if self.norm_reg_targets: bbox_reg = F.relu(bbox_reg) if mode == 'infer': bbox_reg = bbox_reg * fpn_stride else: bbox_reg = paddle.exp(bbox_reg) cls_logits_list.append(cls_logits) bboxes_reg_list.append(bbox_reg) centerness_list.append(centerness) return cls_logits_list, bboxes_reg_list, centerness_list
def forward(self, src, src_mask=None, src_key_padding_mask=None): """Pass the input through the endocder layer. Args: src: the sequnce to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). """ src2 = self.self_attn(src, src, src, attn_mask=src_mask, key_padding_mask=src_key_padding_mask)[0] src = src + self.dropout1(src2) src = self.norm1(src) src = src.transpose([1, 2, 0]) src = paddle.unsqueeze(src, 2) src2 = self.conv2(F.relu(self.conv1(src))) src2 = paddle.squeeze(src2, 2) src2 = src2.transpose([2, 0, 1]) src = paddle.squeeze(src, 2) src = src.transpose([2, 0, 1]) src = src + self.dropout2(src2) src = self.norm2(src) return src
def forward(self, x): """ Args: x: batch * len * dim Output: matched_seq: batch * len * dim """ # Project vectors batch_size,seq_len,hidden_dim = x.shape[0],x.shape[1],x.shape[2] if self.linear: x_proj = self.linear(x.reshape(shape=[batch_size*seq_len,hidden_dim])).reshape(shape=x.shape) x_proj = F.relu(x_proj) else: x_proj = x # Compute scores scores = x_proj.bmm(paddle.transpose(x_proj,perm=[0,2,1])) if not self.diag: x_len = x.shape[1] for i in range(x_len): scores[:, i, i] = 0 # Normalize with softmax alpha = F.softmax(scores, axis=2) # Take weighted average matched_seq = alpha.bmm(x) return matched_seq
def forward(self, inputs): if self.data_format == "NHWC": inputs = paddle.tensor.transpose(inputs, [0, 2, 3, 1]) inputs.stop_gradient = True y = self.conv(inputs) y = self.pool2d_max(y) stage_index = np.cumsum(self.depth) features = [] stage = 2 for idx, block in enumerate(self.block_list): y = block(y) if idx + 1 in stage_index: if self.get_feats_before_relu: features.append(y) y = F.relu(y) if not self.get_feats_before_relu: features.append(y) stage += 1 y = self.pool2d_avg(y) features.append(y) y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels]) y = self.out(y) if self.return_feats: return {"feats": features, "logits": y} else: return y
def forward(self, inputs): out = self.conv(inputs) out = self.norm(out) if self.act == 'relu': out = F.relu(out) return out
def forward(self, inputs): xyz = paddle.to_tensor(inputs[0]) cls_label = Categorical(inputs[1], self.num_classes) # Set Abstraction layers B, C, N = xyz.shape if self.normal_channel: l0_points = xyz l0_xyz = xyz[:, :3, :] else: l0_points = xyz l0_xyz = xyz l1_xyz, l1_points = self.sa1(l0_xyz, l0_points) l2_xyz, l2_points = self.sa2(l1_xyz, l1_points) l3_xyz, l3_points = self.sa3(l2_xyz, l2_points) # Feature Propagation layers l2_points = self.fp3(l2_xyz, l3_xyz, l2_points, l3_points) l1_points = self.fp2(l1_xyz, l2_xyz, l1_points, l2_points) cls_label_one_hot = paddle.tile( cls_label.reshape([B, self.num_classes, 1]), [1, 1, N]) l0_points = self.fp1( l0_xyz, l1_xyz, paddle.concat([cls_label_one_hot, l0_xyz, l0_points], 1), l1_points) # FC layers feat = F.relu(self.bn1(self.conv1(l0_points))) x = self.drop1(feat) x = self.conv2(x) x = x.transpose([0, 2, 1]) return x
def forward(self, inputs): y = self._conv(inputs) y = self._batch_norm(y) if self.act == 'relu': y = F.relu(y) return y
def forward(self, g): x = g.node_feat["feat"] edge_feat = g.edge_feat["feat"] ### computing input node embedding h_list = [self.atom_encoder(x)] for layer in range(self.num_layers): h = self.convs[layer](g, h_list[layer], edge_feat) h = self.batch_norms[layer](h) if layer == self.num_layers - 1: #remove relu for the last layer h = F.dropout(h, self.drop_ratio, training=self.training) else: h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) if self.residual: h += h_list[layer] h_list.append(h) ### Different implementations of Jk-concat if self.JK == "last": node_representation = h_list[-1] elif self.JK == "sum": node_representation = 0 for layer in range(self.num_layers): node_representation += h_list[layer] return node_representation
def forward(self, g): x = g.node_feat["feat"] edge_feat = g.edge_feat["feat"] h_list = [self.atom_encoder(x)] ### virtual node embeddings for graphs virtualnode_embedding = self.virtualnode_embedding.expand( [g.num_graph, self.virtualnode_embedding.shape[-1]]) for layer in range(self.num_layers): ### add message from virtual nodes to graph nodes h_list[layer] = h_list[layer] + paddle.gather( virtualnode_embedding, g.graph_node_id) ### Message passing among graph nodes h = self.convs[layer](g, h_list[layer], edge_feat) h = self.batch_norms[layer](h) if layer == self.num_layers - 1: #remove relu for the last layer h = F.dropout(h, self.drop_ratio, training=self.training) else: h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) if self.residual: h = h + h_list[layer] h_list.append(h) ### update the virtual nodes if layer < self.num_layers - 1: ### add message from graph nodes to virtual nodes virtualnode_embedding_temp = self.pool( g, h_list[layer]) + virtualnode_embedding ### transform virtual nodes using MLP if self.residual: virtualnode_embedding = virtualnode_embedding + F.dropout( self.mlp_virtualnode_list[layer] (virtualnode_embedding_temp), self.drop_ratio, training=self.training) else: virtualnode_embedding = F.dropout( self.mlp_virtualnode_list[layer]( virtualnode_embedding_temp), self.drop_ratio, training=self.training) ### Different implementations of Jk-concat if self.JK == "last": node_representation = h_list[-1] elif self.JK == "sum": node_representation = 0 for layer in range(self.num_layers): node_representation += h_list[layer] return node_representation
def forward(self, xyz, points): """ Input: xyz: input points position data, [B, C, N] points: input points data, [B, D, N] Return: new_xyz: sampled points position data, [B, C, S] new_points_concat: sample points feature data, [B, D', S] """ xyz = xyz.transpose([0, 2, 1]) if points is not None: points = points.transpose([0, 2, 1]) # new_xyz: sampled points position data, [B, npoint, C] # new_points: sampled points data, [B, npoint, nsample, C+D] if self.group_all: new_xyz, new_points = sample_and_group_all(xyz, points) else: new_xyz, new_points = sample_and_group(self.npoint, self.radius, self.nsample, xyz, points) new_points = new_points.transpose([0, 3, 2, 1]) for i, conv in enumerate(self.mlp_convs): bn = self.mlp_bns[i] new_points = F.relu(bn(conv(new_points))) new_points = paddle.max(new_points, 2) new_xyz = new_xyz.transpose([0, 2, 1]) return new_xyz, new_points
def forward(self, inputs): outputs = self.avg_pool(inputs) outputs = self.conv1(outputs) outputs = F.relu(outputs) outputs = self.conv2(outputs) outputs = F.hardsigmoid(outputs, slope=0.2, offset=0.5) return inputs * outputs
def forward(self, x): x.stop_gradient = False x = x.transpose([0, 3, 2, 1]) x = self.bn0(x) x = x.transpose([0, 3, 2, 1]) x = self.conv_block1(x, pool_size=(2, 2), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block2(x, pool_size=(2, 2), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block3(x, pool_size=(2, 2), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block4(x, pool_size=(2, 2), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block5(x, pool_size=(2, 2), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block6(x, pool_size=(1, 1), pool_type='avg') x = F.dropout(x, p=0.2, training=self.training) x = x.mean(axis=3) x = x.max(axis=2) + x.mean(axis=2) x = F.dropout(x, p=0.5, training=self.training) x = F.relu(self.fc1(x)) if self.extract_embedding: output = F.dropout(x, p=0.5, training=self.training) else: output = F.sigmoid(self.fc_audioset(x)) return output
def forward(self, x): short = x x = self.conv1(x) if self.avd and self.avd_first and (self.stride > 1 or self.is_first): x = self.avg_pool2d_1(x) x = self.conv2(x) if self.avd and self.avd_first == False and (self.stride > 1 or self.is_first): x = self.avg_pool2d_2(x) x = self.conv3(x) if self.stride != 1 or self.inplanes != self.planes * 4: if self.avg_down: short = self.avg_pool2d_3(short) short = self.conv4(short) short = self._batch_norm(short) y = paddle.add(x=short, y=x) y = F.relu(y) return y
def forward(self, x): x = self.conv_1(x) x = self.bn_1(x) x = F.relu(x) x = self.conv_2(x) x = self.bn_2(x) x = F.relu(x) x = self.conv_3(x) x = self.bn_3(x) x = F.relu(x) x = self.conv_4(x) x = self.bn_4(x) x = F.relu(x) x = self.conv_5(x) x = self.tanh(x) return x
def forward(self, inputs): outputs = self.avg_pool(inputs) outputs = self.conv1(outputs) outputs = F.relu(outputs) outputs = self.conv2(outputs) outputs = F.hardsigmoid(outputs, slope=0.2, offset=0.5) return paddle.multiply(x=inputs, y=outputs)
def forward(self, fpn_feats, is_training): assert len(fpn_feats) == len( self.fpn_stride ), "The size of fpn_feats is not equal to size of fpn_stride" cls_logits_list = [] bboxes_reg_list = [] centerness_list = [] for scale_reg, fpn_stride, fpn_feat in zip(self.scales_regs, self.fpn_stride, fpn_feats): fcos_cls_feat, fcos_reg_feat = self.fcos_feat(fpn_feat) cls_logits = self.fcos_head_cls(fcos_cls_feat) bbox_reg = scale_reg(self.fcos_head_reg(fcos_reg_feat)) if self.centerness_on_reg: centerness = self.fcos_head_centerness(fcos_reg_feat) else: centerness = self.fcos_head_centerness(fcos_cls_feat) if self.norm_reg_targets: bbox_reg = F.relu(bbox_reg) if not is_training: bbox_reg = bbox_reg * fpn_stride else: bbox_reg = paddle.exp(bbox_reg) cls_logits_list.append(cls_logits) bboxes_reg_list.append(bbox_reg) centerness_list.append(centerness) if not is_training: locations_list = [] for fpn_stride, feature in zip(self.fpn_stride, fpn_feats): location = self._compute_locations_by_level(fpn_stride, feature) locations_list.append(location) return locations_list, cls_logits_list, bboxes_reg_list, centerness_list else: return cls_logits_list, bboxes_reg_list, centerness_list
def forward(self, inputs: paddle.Tensor): outputs = self.avg_pool(inputs) outputs = self.conv1(outputs) outputs = F.relu(outputs) outputs = self.conv2(outputs) outputs = F.hard_sigmoid(outputs) return paddle.multiply(x=inputs, y=outputs, axis=0)
def forward(self, inputs): outputs = self.avg_pool(inputs) outputs = self.conv1(outputs) outputs = F.relu(outputs) outputs = self.conv2(outputs) outputs = hardsigmoid(outputs) return paddle.multiply(x=inputs, y=outputs)
def forward(self, x: paddle.Tensor) -> paddle.Tensor: outs = [] residual_func_idx = 0 for i in range(self._actual_ch): residual = x[i] residual_shape = paddle.shape(residual)[-2:] for j in range(len(self._in_channels)): if j > i: y = self.residual_func_list[residual_func_idx](x[j]) residual_func_idx += 1 y = F.interpolate(y, residual_shape, mode='bilinear', align_corners=self.align_corners) residual = residual + y elif j < i: y = x[j] for k in range(i - j): y = self.residual_func_list[residual_func_idx](y) residual_func_idx += 1 residual = residual + y residual = F.relu(residual) outs.append(residual) return outs
def forward(self, x): bottom_up_features = self.bottom_up(x) bottom_up_features = bottom_up_features[self.output_b - 2:] bottom_up_features = bottom_up_features[::-1] results = [] prev_features = self.lateral_convs[0](bottom_up_features[0]) results.append(self.output_convs[0](prev_features)) for l_id, (features, lateral_conv, output_conv) in enumerate( zip(bottom_up_features[1:], self.lateral_convs[1:], self.output_convs[1:])): top_down_features = F.interpolate(prev_features, scale_factor=2, mode="bilinear", align_corners=False) lateral_features = lateral_conv(features) prev_features = lateral_features + top_down_features results.append(output_conv(prev_features)) if (self.output_e == 6): p6 = F.max_pool2d(results[0], kernel_size=1, stride=2, padding=0) results.insert(0, p6) elif (self.output_e == 7): p6 = self.p6(results[0]) results.insert(0, p6) p7 = self.p7(F.relu(results[0])) results.insert(0, p7) return results
def _get_output_single(self, input, idx): ins_kernel_feat = input # CoordConv x_range = paddle.linspace(-1, 1, paddle.shape(ins_kernel_feat)[-1], dtype='float32') y_range = paddle.linspace(-1, 1, paddle.shape(ins_kernel_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(ins_kernel_feat)[0], 1, -1, -1]) x = paddle.expand(x, shape=[paddle.shape(ins_kernel_feat)[0], 1, -1, -1]) coord_feat = paddle.concat([x, y], axis=1) ins_kernel_feat = paddle.concat([ins_kernel_feat, coord_feat], axis=1) # kernel branch kernel_feat = ins_kernel_feat seg_num_grid = self.seg_num_grids[idx] kernel_feat = F.interpolate(kernel_feat, size=[seg_num_grid, seg_num_grid], mode='bilinear', align_corners=False, align_mode=0) cate_feat = kernel_feat[:, :-2, :, :] for kernel_layer in self.kernel_pred_convs: kernel_feat = F.relu(kernel_layer(kernel_feat)) if self.drop_block and self.training: kernel_feat = self.drop_block_fun(kernel_feat) kernel_pred = self.solo_kernel(kernel_feat) # cate branch for cate_layer in self.cate_pred_convs: cate_feat = F.relu(cate_layer(cate_feat)) if self.drop_block and self.training: cate_feat = self.drop_block_fun(cate_feat) cate_pred = self.solo_cate(cate_feat) if not self.training: cate_pred = self._points_nms(F.sigmoid(cate_pred), kernel_size=2) cate_pred = paddle.transpose(cate_pred, [0, 2, 3, 1]) return cate_pred, kernel_pred
def forward(self, features, im_info, boxes=None): # prediction pred_cls_score_list = [] pred_bbox_offsets_list = [] for x in features: t = F.relu(self.rpn_conv(x)) pred_cls_score_list.append(self.rpn_cls_score(t)) pred_bbox_offsets_list.append(self.rpn_bbox_offsets(t)) # get anchors all_anchors_list = [] # stride: 64,32,16,8,4 p6->p2 base_stride = 4 off_stride = 2**(len(features) - 1) # 16 for fm in features: layer_anchors = self.anchors_generator(fm, base_stride, off_stride) off_stride = off_stride // 2 all_anchors_list.append(layer_anchors) # sample from the predictions rpn_rois = find_top_rpn_proposals(self.training, pred_bbox_offsets_list, pred_cls_score_list, all_anchors_list, im_info) rpn_rois = rpn_rois.cast('float32') if self.training: rpn_labels, rpn_bbox_targets = fpn_anchor_target( boxes, im_info, all_anchors_list) #rpn_labels = rpn_labels.astype(np.int32) pred_cls_score, pred_bbox_offsets = fpn_rpn_reshape( pred_cls_score_list, pred_bbox_offsets_list) # rpn loss valid_masks = rpn_labels >= 0 # objectness_loss = softmax_loss( # torch.gather(pred_cls_score,torch.nonzero(valid_masks)), # torch.gather(rpn_labels,torch.nonzero(valid_masks))) objectness_loss = F.binary_cross_entropy( F.softmax( torch.gather(pred_cls_score, torch.nonzero(valid_masks))), torch.gather( torch.eye(2), torch.gather(rpn_labels, torch.nonzero(valid_masks)))) pos_masks = rpn_labels > 0 # localization_loss = smooth_l1_loss( # pred_bbox_offsets[pos_masks], # rpn_bbox_targets[pos_masks], # config.rpn_smooth_l1_beta) localization_loss = \ F.smooth_l1_loss(torch.gather(pred_bbox_offsets, torch.nonzero(pos_masks)), torch.gather(rpn_bbox_targets, torch.nonzero(pos_masks)),delta=config.rcnn_smooth_l1_beta) normalizer = 1 / valid_masks.cast('float32').sum() loss_rpn_cls = objectness_loss.sum() * normalizer loss_rpn_loc = localization_loss.sum() * normalizer loss_dict = {} loss_dict['loss_rpn_cls'] = loss_rpn_cls loss_dict['loss_rpn_loc'] = loss_rpn_loc return rpn_rois, loss_dict else: return rpn_rois
def forward(self, inputs): x = self._conv1(inputs) x = self._conv2(x) x = self._conv3(x) x = F.relu(x) x = self._conv4(x) x = F.relu(x) x = self._conv5(x) x = paddle.flatten(x, start_axis=1, stop_axis=-1) x = self._drop1(x) x = self._fc6(x) x = F.relu(x) x = self._drop2(x) x = self._fc7(x) x = F.relu(x) x = self._fc8(x) return x
def forward(self, x): x = self._conv(x) x = self._batch_norm(x) if self.act == "relu": x = F.relu(x) elif self.act == "relu6": x = F.relu6(x) return x
def forward(self, x): hidden = self.fc1(x) hidden = F.relu(hidden) if self.dropout_rate: hidden = F.dropout( hidden, p=self.dropout_rate, mode="downscale_in_infer") out = self.fc2(hidden) return out
def forward(self, inputs): children = inputs out = self.conv(paddle.concat(inputs, axis=1)) if self.residual: out = paddle.add(x=out, y=children[0]) out = F.relu(out) return out
def forward(self, x): out = self.conv1(x) out = F.relu(out) out = self.conv2(out) out = F.relu(out) out = self.conv3(out) if self.shortcut is not None: shortcut = self.shortcut(x) else: shortcut = x out += shortcut out = F.relu(out) return out
def forward(self, x): conv0 = self.conv0_ssh(x) conv1 = self.conv1_ssh(conv0) conv2 = self.conv2_ssh(conv1) conv3 = self.conv3_ssh(conv2) conv4 = self.conv4_ssh(conv3) concat = paddle.concat([conv0, conv2, conv4], axis=1) return F.relu(concat)
def forward(self, x): x = self.conv1(x) x = F.relu(x) x = self.pool1(x) x = self.conv2(x) x = F.relu(x) x = self.pool2(x) x = self.conv3(x) x = F.relu(x) x = self.flatten(x) x = self.linear1(x) x = F.relu(x) x = self.linear2(x) return x