def expand6(inputs): shape = paddle.to_tensor(np.array([2, 1, 3, 7, 7]).astype("int32")) return paddle.expand(inputs, shape=shape)
def predictEnsembleThree(model, model_1, model_crop, model_path, model_path_1, model_path_crop, transforms, transforms_crop, image_list, image_dir=None, save_dir='output', aug_pred=False, scales=1.0, flip_horizontal=True, flip_vertical=False, is_slide=False, stride=None, crop_size=None): """ predict and visualize the image_list. Args: model (nn.Layer): Used to predict for input image. model_path (str): The path of pretrained model. transforms (transform.Compose): Preprocess for input image. image_list (list): A list of image path to be predicted. image_dir (str, optional): The root directory of the images predicted. Default: None. save_dir (str, optional): The directory to save the visualized results. Default: 'output'. aug_pred (bool, optional): Whether to use mulit-scales and flip augment for predition. Default: False. scales (list|float, optional): Scales for augment. It is valid when `aug_pred` is True. Default: 1.0. flip_horizontal (bool, optional): Whether to use flip horizontally augment. It is valid when `aug_pred` is True. Default: True. flip_vertical (bool, optional): Whether to use flip vertically augment. It is valid when `aug_pred` is True. Default: False. is_slide (bool, optional): Whether to predict by sliding window. Default: False. stride (tuple|list, optional): The stride of sliding window, the first is width and the second is height. It should be provided when `is_slide` is True. crop_size (tuple|list, optional): The crop size of sliding window, the first is width and the second is height. It should be provided when `is_slide` is True. """ utils.utils.load_entire_model(model, model_path) model.eval() utils.utils.load_entire_model(model_1, model_path_1) model_1.eval() utils.utils.load_entire_model(model_crop, model_path_crop) model_crop.eval() nranks = paddle.distributed.get_world_size() local_rank = paddle.distributed.get_rank() if nranks > 1: img_lists = partition_list(image_list, nranks) else: img_lists = [image_list] added_saved_dir = os.path.join(save_dir, 'added_prediction') pred_saved_dir = os.path.join(save_dir, 'pseudo_color_prediction') logger.info("Start to predict...") progbar_pred = progbar.Progbar(target=len(img_lists[0]), verbose=1) with paddle.no_grad(): for i, im_path in enumerate(img_lists[local_rank]): im_origin = cv2.imread(im_path) ori_shape = im_origin.shape[:2] im, _ = transforms(im_origin) im = im[np.newaxis, ...] im = paddle.to_tensor(im) ims, _ = transforms_crop(im_origin) im1 = ims[:, 540:540 + 720, 320:320 + 1280] im2 = ims[:, 540:540 + 720, 960:960 + 1280] im3 = ims[:, 540:540 + 720, 1600:1600 + 1280] im1 = im1[np.newaxis, ...] im1 = paddle.to_tensor(im1) im2 = im2[np.newaxis, ...] im2 = paddle.to_tensor(im2) im3 = im3[np.newaxis, ...] im3 = paddle.to_tensor(im3) ims_ = [im1, im2, im3] if aug_pred: pred = infer_ensemble.aug_inference( model, model_1, im, ori_shape=ori_shape, transforms=transforms.transforms, scales=scales, flip_horizontal=flip_horizontal, flip_vertical=flip_vertical, is_slide=is_slide, stride=stride, crop_size=crop_size) else: pred = infer_ensemble.inference( model, model_1, im, ori_shape=ori_shape, transforms=transforms.transforms, is_slide=is_slide, stride=stride, crop_size=crop_size) preds = [] for ii in range(3): im_ = ims_[ii] if aug_pred: pred_crop = infer_crop.aug_inference( model, im_, ori_shape=ori_shape, transforms=transforms.transforms, scales=scales, flip_horizontal=flip_horizontal, flip_vertical=flip_vertical, is_slide=is_slide, stride=stride, crop_size=crop_size) else: pred_crop = infer_crop.inference( model, im_, ori_shape=ori_shape, transforms=transforms.transforms, is_slide=is_slide, stride=stride, crop_size=crop_size) preds.append(pred_crop) left_ensem = (preds[0][:, :, :, 640:1280] + preds[1][:, :, :, 0:640]) / 2 right_ensem = (preds[1][:, :, :, 640:1280] + preds[2][:, :, :, 0:640]) / 2 pred_ensem = paddle.concat([ preds[0][:, :, :, 0:640], left_ensem, right_ensem, preds[2][:, :, :, 640:1280] ], axis=3) logit = F.interpolate(pred_ensem, (432, 768), mode='bilinear') pred_logit = pred.clone() pred_logit[:, :, 324:756, 576:1344] = logit pred = pred + pred_logit pred = F.interpolate(pred, ori_shape, mode='bilinear') pred = paddle.argmax(pred, axis=1, keepdim=True, dtype='int32') pred = paddle.squeeze(pred) pred = pred.numpy().astype('uint8') # get the saved name if image_dir is not None: im_file = im_path.replace(image_dir, '') else: im_file = os.path.basename(im_path) if im_file[0] == '/': im_file = im_file[1:] # save added image added_image = utils.visualize.visualize(im_path, pred, weight=0.6) added_image_path = os.path.join(added_saved_dir, im_file) mkdir(added_image_path) cv2.imwrite(added_image_path, added_image) # save pseudo color prediction pred_mask = utils.visualize.get_pseudo_color_map(pred) pred_saved_path = os.path.join(pred_saved_dir, im_file.rsplit(".")[0] + ".png") mkdir(pred_saved_path) pred_mask.save(pred_saved_path) # pred_im = utils.visualize(im_path, pred, weight=0.0) # pred_saved_path = os.path.join(pred_saved_dir, im_file) # mkdir(pred_saved_path) # cv2.imwrite(pred_saved_path, pred_im) progbar_pred.update(i + 1)
def test_api_dygraph(self): paddle.disable_static(self.place) x = paddle.to_tensor(self.x_np) out = paddle.frac(x) out_ref = ref_frac(self.x_np) self.assertTrue(np.allclose(out_ref, out.numpy()))
def setUp(self): self.p = paddle.distribution.Dirichlet(paddle.to_tensor(self.conc1)) self.q = paddle.distribution.Dirichlet(paddle.to_tensor(self.conc2))
[-1, max_col_nums, max_col_tokens, hidden_size]) span_enc2 = paddle.sum(paddle.multiply( span_tokens_enc_origin, span_tokens_weight.unsqueeze([-1])), axis=2) span_enc = paddle.concat([span_enc1, span_enc2], axis=-1) if proj_fn is not None: span_enc = proj_fn(span_enc) return span_enc if __name__ == "__main__": """run some simple test cases""" inputs = { 'src_ids': paddle.to_tensor( np.array([0, 1, 2, 3, 4, 5], dtype=np.int64).reshape([1, 6])), 'sent_ids': paddle.to_tensor( np.array([0, 1, 1, 1, 1, 1], dtype=np.int64).reshape([1, 6])), 'question_tokens_index': paddle.to_tensor(list(range(1, 5)), dtype='int64').reshape([1, 4]), 'column_index': paddle.to_tensor([1, 4], dtype='int64').reshape([1, 2]), 'column_mask': paddle.to_tensor([1, 1], dtype='float32').reshape([1, 2]), 'column_tokens_index': paddle.to_tensor([1, 2, 3, 4, 5, 0], dtype='int64').reshape([1, 2, 3]), 'column_tokens_mask': paddle.to_tensor([1, 1, 1, 1, 1, 0], dtype='float32').reshape([1, 2, 3]), 'table_index':
def prepare_data(self): dataset = MAG240MDataset(self.data_dir) log.info(dataset.num_authors) log.info(dataset.num_institutions) author_path = f'{dataset.dir}/author_feat.npy' path = f'{dataset.dir}/institution_feat.npy' t = time.perf_counter() if not osp.exists(path): log.info('get institution_feat...') author_feat = np.memmap(author_path, dtype=np.float16, shape=(dataset.num_authors, self.num_features), mode='r') # author edge_index = dataset.edge_index('author', 'institution') edge_index = edge_index.T log.info(edge_index.shape) institution_graph = BiGraph(edge_index, dst_num_nodes=dataset.num_institutions) institution_graph.tensor() log.info('finish institution graph') institution_x = np.memmap(path, dtype=np.float16, mode='w+', shape=(dataset.num_institutions, self.num_features)) dim_chunk_size = 64 degree = paddle.zeros(shape=[dataset.num_institutions, 1], dtype='float32') temp_one = paddle.ones(shape=[edge_index.shape[0], 1], dtype='float32') degree = scatter(degree, overwrite=False, index=institution_graph.edges[:, 1], updates=temp_one) log.info('finish degree') for i in tqdm(range(0, self.num_features, dim_chunk_size)): j = min(i + dim_chunk_size, self.num_features) inputs = get_col_slice(author_feat, start_row_idx=0, end_row_idx=dataset.num_authors, start_col_idx=i, end_col_idx=j) inputs = paddle.to_tensor(inputs, dtype='float32') outputs = institution_graph.send_recv(inputs) outputs = outputs / degree outputs = outputs.astype('float16').numpy() del inputs save_col_slice(x_src=outputs, x_dst=institution_x, start_row_idx=0, end_row_idx=dataset.num_institutions, start_col_idx=i, end_col_idx=j) del outputs institution_x.flush() del institution_x log.info(f'Done! [{time.perf_counter() - t:.2f}s]')
pass @param.place(config.DEVICES) @param.param_cls((param.TEST_CASE_NAME, 'p', 'q'), [('test-unregister', DummyDistribution(), DummyDistribution)]) class TestDispatch(unittest.TestCase): def test_dispatch_with_unregister(self): with self.assertRaises(NotImplementedError): paddle.distribution.kl_divergence(self.p, self.q) @param.place(config.DEVICES) @param.param_cls( (param.TEST_CASE_NAME, 'p', 'q'), [('test-diff-dist', mock.Exponential(paddle.rand((100, 200, 100)) + 1.0), mock.Exponential(paddle.rand((100, 200, 100)) + 2.0)), ('test-same-dist', mock.Exponential( paddle.to_tensor(1.0)), mock.Exponential(paddle.to_tensor(1.0)))]) class TestKLExpfamilyExpFamily(unittest.TestCase): def test_kl_expfamily_expfamily(self): np.testing.assert_allclose(paddle.distribution.kl_divergence( self.p, self.q), kl._kl_expfamily_expfamily(self.p, self.q), rtol=config.RTOL.get(config.DEFAULT_DTYPE), atol=config.ATOL.get(config.DEFAULT_DTYPE)) if __name__ == '__main__': unittest.main()
def set_input(self, input): """Unpack input data from the dataloader and perform necessary pre-processing steps. Parameters: input (dict): include the data itself and its metadata information. The option 'direction' can be used to swap domain A and domain B. """ self.real_A = paddle.to_tensor(input['image_A']) self.real_B = paddle.to_tensor(input['image_B']) self.c_m = paddle.to_tensor(input['consis_mask']) self.P_A = paddle.to_tensor(input['P_A']) self.P_B = paddle.to_tensor(input['P_B']) self.mask_A_aug = paddle.to_tensor(input['mask_A_aug']) self.mask_B_aug = paddle.to_tensor(input['mask_B_aug']) self.c_m_t = paddle.transpose(self.c_m, perm=[0, 2, 1]) if self.is_train: self.mask_A = paddle.to_tensor(input['mask_A']) self.mask_B = paddle.to_tensor(input['mask_B']) self.c_m_idt_a = paddle.to_tensor(input['consis_mask_idt_A']) self.c_m_idt_b = paddle.to_tensor(input['consis_mask_idt_B'])
def backward_G(self): """Calculate the loss for generators G_A and G_B""" lambda_idt = self.cfg.lambda_identity lambda_A = self.cfg.lambda_A lambda_B = self.cfg.lambda_B lambda_vgg = 5e-3 # Identity loss if lambda_idt > 0: self.idt_A, _ = self.nets['netG'](self.real_A, self.real_A, self.P_A, self.P_A, self.c_m_idt_a, self.mask_A_aug, self.mask_B_aug) # G_A(A) self.loss_idt_A = self.criterionIdt( self.idt_A, self.real_A) * lambda_A * lambda_idt self.idt_B, _ = self.nets['netG'](self.real_B, self.real_B, self.P_B, self.P_B, self.c_m_idt_b, self.mask_A_aug, self.mask_B_aug) # G_A(A) self.loss_idt_B = self.criterionIdt( self.idt_B, self.real_B) * lambda_B * lambda_idt # visual self.visual_items['idt_A'] = self.idt_A self.visual_items['idt_B'] = self.idt_B else: self.loss_idt_A = 0 self.loss_idt_B = 0 # GAN loss D_A(G_A(A)) self.loss_G_A = self.criterionGAN(self.nets['netD_A'](self.fake_A), True) # GAN loss D_B(G_B(B)) self.loss_G_B = self.criterionGAN(self.nets['netD_B'](self.fake_B), True) # Forward cycle loss || G_B(G_A(A)) - A|| self.loss_cycle_A = self.criterionCycle(self.rec_A, self.real_A) * lambda_A # Backward cycle loss || G_A(G_B(B)) - B|| self.loss_cycle_B = self.criterionCycle(self.rec_B, self.real_B) * lambda_B self.losses['G_A_adv_loss'] = self.loss_G_A self.losses['G_B_adv_loss'] = self.loss_G_B mask_A_lip = self.mask_A_aug[:, 0].unsqueeze(1) mask_B_lip = self.mask_B_aug[:, 0].unsqueeze(1) mask_A_lip_np = mask_A_lip.numpy().squeeze() mask_B_lip_np = mask_B_lip.numpy().squeeze() mask_A_lip_np, mask_B_lip_np, index_A_lip, index_B_lip = mask_preprocess( mask_A_lip_np, mask_B_lip_np) real_A = paddle.nn.clip((self.real_A + 1.0) / 2.0, 0.0, 1.0) * 255.0 real_A_np = real_A.numpy().squeeze() real_B = paddle.nn.clip((self.real_B + 1.0) / 2.0, 0.0, 1.0) * 255.0 real_B_np = real_B.numpy().squeeze() fake_A = paddle.nn.clip((self.fake_A + 1.0) / 2.0, 0.0, 1.0) * 255.0 fake_A_np = fake_A.numpy().squeeze() fake_B = paddle.nn.clip((self.fake_B + 1.0) / 2.0, 0.0, 1.0) * 255.0 fake_B_np = fake_B.numpy().squeeze() fake_match_lip_A = hisMatch(fake_A_np, real_B_np, mask_A_lip_np, mask_B_lip_np, index_A_lip) fake_match_lip_B = hisMatch(fake_B_np, real_A_np, mask_B_lip_np, mask_A_lip_np, index_B_lip) fake_match_lip_A = paddle.to_tensor(fake_match_lip_A) fake_match_lip_A.stop_gradient = True fake_match_lip_A = fake_match_lip_A.unsqueeze(0) fake_match_lip_B = paddle.to_tensor(fake_match_lip_B) fake_match_lip_B.stop_gradient = True fake_match_lip_B = fake_match_lip_B.unsqueeze(0) fake_A_lip_masked = fake_A * mask_A_lip fake_B_lip_masked = fake_B * mask_B_lip g_A_lip_loss_his = self.criterionL1(fake_A_lip_masked, fake_match_lip_A) g_B_lip_loss_his = self.criterionL1(fake_B_lip_masked, fake_match_lip_B) #skin mask_A_skin = self.mask_A_aug[:, 1].unsqueeze(1) mask_B_skin = self.mask_B_aug[:, 1].unsqueeze(1) mask_A_skin_np = mask_A_skin.numpy().squeeze() mask_B_skin_np = mask_B_skin.numpy().squeeze() mask_A_skin_np, mask_B_skin_np, index_A_skin, index_B_skin = mask_preprocess( mask_A_skin_np, mask_B_skin_np) fake_match_skin_A = hisMatch(fake_A_np, real_B_np, mask_A_skin_np, mask_B_skin_np, index_A_skin) fake_match_skin_B = hisMatch(fake_B_np, real_A_np, mask_B_skin_np, mask_A_skin_np, index_B_skin) fake_match_skin_A = paddle.to_tensor(fake_match_skin_A) fake_match_skin_A.stop_gradient = True fake_match_skin_A = fake_match_skin_A.unsqueeze(0) fake_match_skin_B = paddle.to_tensor(fake_match_skin_B) fake_match_skin_B.stop_gradient = True fake_match_skin_B = fake_match_skin_B.unsqueeze(0) fake_A_skin_masked = fake_A * mask_A_skin fake_B_skin_masked = fake_B * mask_B_skin g_A_skin_loss_his = self.criterionL1(fake_A_skin_masked, fake_match_skin_A) g_B_skin_loss_his = self.criterionL1(fake_B_skin_masked, fake_match_skin_B) #eye mask_A_eye = self.mask_A_aug[:, 2].unsqueeze(1) mask_B_eye = self.mask_B_aug[:, 2].unsqueeze(1) mask_A_eye_np = mask_A_eye.numpy().squeeze() mask_B_eye_np = mask_B_eye.numpy().squeeze() mask_A_eye_np, mask_B_eye_np, index_A_eye, index_B_eye = mask_preprocess( mask_A_eye_np, mask_B_eye_np) fake_match_eye_A = hisMatch(fake_A_np, real_B_np, mask_A_eye_np, mask_B_eye_np, index_A_eye) fake_match_eye_B = hisMatch(fake_B_np, real_A_np, mask_B_eye_np, mask_A_eye_np, index_B_eye) fake_match_eye_A = paddle.to_tensor(fake_match_eye_A) fake_match_eye_A.stop_gradient = True fake_match_eye_A = fake_match_eye_A.unsqueeze(0) fake_match_eye_B = paddle.to_tensor(fake_match_eye_B) fake_match_eye_B.stop_gradient = True fake_match_eye_B = fake_match_eye_B.unsqueeze(0) fake_A_eye_masked = fake_A * mask_A_eye fake_B_eye_masked = fake_B * mask_B_eye g_A_eye_loss_his = self.criterionL1(fake_A_eye_masked, fake_match_eye_A) g_B_eye_loss_his = self.criterionL1(fake_B_eye_masked, fake_match_eye_B) self.loss_G_A_his = (g_A_eye_loss_his + g_A_lip_loss_his + g_A_skin_loss_his * 0.1) * 0.1 self.loss_G_B_his = (g_B_eye_loss_his + g_B_lip_loss_his + g_B_skin_loss_his * 0.1) * 0.1 self.losses['G_A_his_loss'] = self.loss_G_A_his self.losses['G_B_his_loss'] = self.loss_G_B_his #vgg loss vgg_s = self.vgg(self.real_A) vgg_s.stop_gradient = True vgg_fake_A = self.vgg(self.fake_A) self.loss_A_vgg = self.criterionL2(vgg_fake_A, vgg_s) * lambda_A * lambda_vgg vgg_r = self.vgg(self.real_B) vgg_r.stop_gradient = True vgg_fake_B = self.vgg(self.fake_B) self.loss_B_vgg = self.criterionL2(vgg_fake_B, vgg_r) * lambda_B * lambda_vgg self.loss_rec = (self.loss_cycle_A * 0.2 + self.loss_cycle_B * 0.2 + self.loss_A_vgg + self.loss_B_vgg) * 0.5 self.loss_idt = (self.loss_idt_A + self.loss_idt_B) * 0.1 self.losses['G_A_vgg_loss'] = self.loss_A_vgg self.losses['G_B_vgg_loss'] = self.loss_B_vgg self.losses['G_rec_loss'] = self.loss_rec self.losses['G_idt_loss'] = self.loss_idt # bg consistency loss mask_A_consis = paddle.cast( (self.mask_A == 0), dtype='float32') + paddle.cast( (self.mask_A == 10), dtype='float32') + paddle.cast( (self.mask_A == 8), dtype='float32') mask_A_consis = paddle.unsqueeze(paddle.clip(mask_A_consis, 0, 1), 1) self.loss_G_bg_consis = self.criterionL1( self.real_A * mask_A_consis, self.fake_A * mask_A_consis) * 0.1 # combined loss and calculate gradients self.loss_G = self.loss_G_A + self.loss_G_B + self.loss_rec + self.loss_idt + self.loss_G_A_his + self.loss_G_B_his + self.loss_G_bg_consis self.loss_G.backward()
def get_loss(self, head_outs, gt_meta): cls_scores, bbox_preds = head_outs num_level_anchors = [ featmap.shape[-2] * featmap.shape[-1] for featmap in cls_scores ] num_imgs = gt_meta['im_id'].shape[0] featmap_sizes = [[featmap.shape[-2], featmap.shape[-1]] for featmap in cls_scores] decode_bbox_preds = [] center_and_strides = [] for featmap_size, stride, bbox_pred in zip(featmap_sizes, self.fpn_stride, bbox_preds): # center in origin image yy, xx = self.get_single_level_center_point(featmap_size, stride, self.cell_offset) strides = paddle.full((len(xx), ), stride) center_and_stride = paddle.stack([xx, yy, strides, strides], -1).tile([num_imgs, 1, 1]) center_and_strides.append(center_and_stride) center_in_feature = center_and_stride.reshape( [-1, 4])[:, :-2] / stride bbox_pred = bbox_pred.transpose([0, 2, 3, 1]).reshape( [num_imgs, -1, 4 * (self.reg_max + 1)]) pred_distances = self.distribution_project(bbox_pred) decode_bbox_pred_wo_stride = distance2bbox( center_in_feature, pred_distances).reshape([num_imgs, -1, 4]) decode_bbox_preds.append(decode_bbox_pred_wo_stride * stride) flatten_cls_preds = [ cls_pred.transpose([0, 2, 3, 1]).reshape( [num_imgs, -1, self.cls_out_channels]) for cls_pred in cls_scores ] flatten_cls_preds = paddle.concat(flatten_cls_preds, axis=1) flatten_bboxes = paddle.concat(decode_bbox_preds, axis=1) flatten_center_and_strides = paddle.concat(center_and_strides, axis=1) gt_boxes, gt_labels = gt_meta['gt_bbox'], gt_meta['gt_class'] pos_num_l, label_l, label_weight_l, bbox_target_l = [], [], [], [] for flatten_cls_pred, flatten_center_and_stride, flatten_bbox,gt_box,gt_label \ in zip(flatten_cls_preds.detach(), flatten_center_and_strides.detach(), \ flatten_bboxes.detach(),gt_boxes,gt_labels): pos_num, label, label_weight, bbox_target = self._get_target_single( flatten_cls_pred, flatten_center_and_stride, flatten_bbox, gt_box, gt_label) pos_num_l.append(pos_num) label_l.append(label) label_weight_l.append(label_weight) bbox_target_l.append(bbox_target) labels = paddle.to_tensor(np.stack(label_l, axis=0)) label_weights = paddle.to_tensor(np.stack(label_weight_l, axis=0)) bbox_targets = paddle.to_tensor(np.stack(bbox_target_l, axis=0)) center_and_strides_list = self._images_to_levels( flatten_center_and_strides, num_level_anchors) labels_list = self._images_to_levels(labels, num_level_anchors) label_weights_list = self._images_to_levels(label_weights, num_level_anchors) bbox_targets_list = self._images_to_levels(bbox_targets, num_level_anchors) num_total_pos = sum(pos_num_l) try: num_total_pos = paddle.distributed.all_reduce(num_total_pos.clone( )) / paddle.distributed.get_world_size() except: num_total_pos = max(num_total_pos, 1) loss_bbox_list, loss_dfl_list, loss_vfl_list, avg_factor = [], [], [], [] for cls_score, bbox_pred, center_and_strides, labels, label_weights, bbox_targets, stride in zip( cls_scores, bbox_preds, center_and_strides_list, labels_list, label_weights_list, bbox_targets_list, self.fpn_stride): center_and_strides = center_and_strides.reshape([-1, 4]) cls_score = cls_score.transpose([0, 2, 3, 1]).reshape( [-1, self.cls_out_channels]) bbox_pred = bbox_pred.transpose([0, 2, 3, 1]).reshape( [-1, 4 * (self.reg_max + 1)]) bbox_targets = bbox_targets.reshape([-1, 4]) labels = labels.reshape([-1]) bg_class_ind = self.num_classes pos_inds = paddle.nonzero( paddle.logical_and((labels >= 0), (labels < bg_class_ind)), as_tuple=False).squeeze(1) # vfl vfl_score = np.zeros(cls_score.shape) if len(pos_inds) > 0: pos_bbox_targets = paddle.gather(bbox_targets, pos_inds, axis=0) pos_bbox_pred = paddle.gather(bbox_pred, pos_inds, axis=0) pos_centers = paddle.gather( center_and_strides[:, :-2], pos_inds, axis=0) / stride weight_targets = F.sigmoid(cls_score.detach()) weight_targets = paddle.gather( weight_targets.max(axis=1, keepdim=True), pos_inds, axis=0) pos_bbox_pred_corners = self.distribution_project(pos_bbox_pred) pos_decode_bbox_pred = distance2bbox(pos_centers, pos_bbox_pred_corners) pos_decode_bbox_targets = pos_bbox_targets / stride bbox_iou = bbox_overlaps( pos_decode_bbox_pred.detach().numpy(), pos_decode_bbox_targets.detach().numpy(), is_aligned=True) # vfl pos_labels = paddle.gather(labels, pos_inds, axis=0) vfl_score[pos_inds.numpy(), pos_labels] = bbox_iou pred_corners = pos_bbox_pred.reshape([-1, self.reg_max + 1]) target_corners = bbox2distance(pos_centers, pos_decode_bbox_targets, self.reg_max).reshape([-1]) # regression loss loss_bbox = paddle.sum( self.loss_bbox(pos_decode_bbox_pred, pos_decode_bbox_targets) * weight_targets) # dfl loss loss_dfl = self.loss_dfl( pred_corners, target_corners, weight=weight_targets.expand([-1, 4]).reshape([-1]), avg_factor=4.0) else: loss_bbox = bbox_pred.sum() * 0 loss_dfl = bbox_pred.sum() * 0 weight_targets = paddle.to_tensor([0], dtype='float32') # vfl loss num_pos_avg_per_gpu = num_total_pos vfl_score = paddle.to_tensor(vfl_score) loss_vfl = self.loss_vfl( cls_score, vfl_score, avg_factor=num_pos_avg_per_gpu) loss_bbox_list.append(loss_bbox) loss_dfl_list.append(loss_dfl) loss_vfl_list.append(loss_vfl) avg_factor.append(weight_targets.sum()) avg_factor = sum(avg_factor) try: avg_factor = paddle.distributed.all_reduce(avg_factor.clone()) avg_factor = paddle.clip( avg_factor / paddle.distributed.get_world_size(), min=1) except: avg_factor = max(avg_factor.item(), 1) if avg_factor <= 0: loss_vfl = paddle.to_tensor(0, dtype='float32', stop_gradient=False) loss_bbox = paddle.to_tensor( 0, dtype='float32', stop_gradient=False) loss_dfl = paddle.to_tensor(0, dtype='float32', stop_gradient=False) else: losses_bbox = list(map(lambda x: x / avg_factor, loss_bbox_list)) losses_dfl = list(map(lambda x: x / avg_factor, loss_dfl_list)) loss_vfl = sum(loss_vfl_list) loss_bbox = sum(losses_bbox) loss_dfl = sum(losses_dfl) loss_states = dict( loss_vfl=loss_vfl, loss_bbox=loss_bbox, loss_dfl=loss_dfl) return loss_states
fluid.core.load_custom_op('relu2_op.so') OpProtoHolder.instance().update_op_proto() op_proto = OpProtoHolder.instance().get_op_proto("relu2") print(op_proto) def relu2(x, name=None): # relu2的type和在OP中定义的type相同 helper = LayerHelper("relu2", **locals()) # 创建输出Variable out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op(type="relu2", inputs={"X0": [x]}, outputs={"Out": [out]}) return out paddle.disable_static() paddle.set_device('cpu') x = np.random.uniform(-1, 1, [4, 32]).astype('float32') t = paddle.to_tensor(x) t.stop_gradient = False out = relu2(t) out.stop_gradient = False print(out.numpy()) print("start grad") out.backward() print(out.grad) print(t.grad)
model.eval() #训练模式 #展示预测图片 infer_path = '/home/aistudio/alexandrite_6.jpg' img = Image.open(infer_path) plt.imshow(img) #根据数组绘制图像 plt.show() #显示图像 #对预测图片进行预处理 infer_imgs = [] infer_imgs.append(load_image(infer_path)) infer_imgs = np.array(infer_imgs) label_dic = train_parameters['label_dict'] for i in range(len(infer_imgs)): data = infer_imgs[i] dy_x_data = np.array(data).astype('float32') dy_x_data = dy_x_data[np.newaxis, :, :, :] img = paddle.to_tensor(dy_x_data) out = model(img) lab = np.argmax(out.numpy()) #argmax():返回最大数的索引 print("第{}个样本,被预测为:{},真实标签为:{}".format( i + 1, label_dic[str(lab)], infer_path.split('/')[-1].split("_")[0])) print("结束") # In[ ]:
def slice4(inputs): x0 = paddle.to_tensor([2]) - paddle.to_tensor([1]) x1 = paddle.to_tensor([3]) + paddle.to_tensor([1]) return inputs[:, x0:, 1:x1, :]
def forward(self, x, y): out = paddle.to_tensor([True, True, True]) z = self.func(x, y, out=out) return paddle.cast(z, "int32")
cost = np.ones((N, G, A), np.float32) cost[0, :, :] = np.array([[2, 99, 102, 3, 108], [4, 100, 103, 2, 109], [9, 100, 103, 1, 109]]).astype(np.float32) cost[1, :, :] = np.array([[2, 99, 102, 1, 108], [5, 100, 103, 2, 109], [7, 100, 103, 3, 109]]).astype(np.float32) # [N, G] 表示每个gt分配给了几个预测框。最少1个。 dynamic_ks = np.array([[2, 1, 1], [1, 1, 1]]).astype(np.int32) # [N, G] 是否是gt。 is_gt = np.array([[1, 1, 0], [1, 1, 1]]).astype(np.float32) is_in_boxes_or_center = np.array([[3, 100, 103, 2, 109], [3, 100, 103, 2, 109]]).astype(np.float32) cost = paddle.to_tensor(cost) dynamic_ks = paddle.to_tensor(dynamic_ks) is_gt = paddle.to_tensor(is_gt) max_dynamic_ks = dynamic_ks.max(-1) # [N, ] 每张图片所有gt的dynamic_ks的最大值 max_k = max_dynamic_ks.max() # [1, ] 所有图片所有gt的dynamic_ks的最大值 # 下三角全是1的矩阵 topk_mask = paddle.ones((max_k, max_k), 'float32') # [max_k, max_k] topk_mask = paddle.tril(topk_mask, diagonal=0) # [max_k, max_k] fill_value = paddle.gather(topk_mask, dynamic_ks.reshape( (-1, )) - 1) # [N*G, max_k] 填入matching_matrix fill_value *= is_gt.reshape((-1, 1)) # [N*G, max_k] 还要处理假gt,假gt处全部填0 fill_value = fill_value.reshape((-1, )) # [N*G*max_k, ] 填入matching_matrix # 不放心的话,再次将假gt的cost增大
def error_groups_2(): with paddle.fluid.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle(paddle.to_tensor(x), -1)
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ''' 输入数据形状是 [N, K]时的batchnorm示例 ''' import numpy as np import paddle from paddle.nn import BatchNorm1D # 创建数据 data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype('float32') # 使用BatchNorm1D计算归一化的输出 # 输入数据维度[N, K],num_features等于K bn = BatchNorm1D(num_features=3) x = paddle.to_tensor(data) y = bn(x) print('output of BatchNorm1D Layer: \n {}'.format(y.numpy())) # 使用Numpy计算均值、方差和归一化的输出 # 这里对第0个特征进行验证 a = np.array([1, 4, 7]) a_mean = a.mean() a_std = a.std() b = (a - a_mean) / a_std print('std {}, mean {}, \n output {}'.format(a_mean, a_std, b))
def error_data_format(): with paddle.fluid.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle(paddle.to_tensor(x), 3, "WOW")
def test_Negative(): paddle.disable_static(paddle.XPUPlace(0)) img = paddle.to_tensor(x) out = paddle.flatten(img, start_axis=-2, stop_axis=-1) return out.numpy().shape
def error_input_layer(): with paddle.fluid.dygraph.guard(): x = np.random.random([9, 4, 4]).astype("float64") cs = paddle.nn.ChannelShuffle(3) cs(paddle.to_tensor(x))
def setUp(self): self.p = paddle.distribution.Beta(paddle.to_tensor(self.a1), paddle.to_tensor(self.b1)) self.q = paddle.distribution.Beta(paddle.to_tensor(self.a2), paddle.to_tensor(self.b2))
model.set_dict(state_dict) print("Loaded parameters from %s" % params_path) else: raise ValueError( "Please set --params_path with correct pretrained model file") # conver_example function's input must be dict corpus_list = [{idx: text} for idx, text in id2corpus.items()] corpus_ds = MapDataset(corpus_list) corpus_data_loader = create_dataloader(corpus_ds, mode='predict', batch_size=batch_size, batchify_fn=batchify_fn, trans_fn=trans_func) all_embeddings = [] model.eval() with paddle.no_grad(): for batch_data in corpus_data_loader: input_ids, token_type_ids = batch_data input_ids = paddle.to_tensor(input_ids) token_type_ids = paddle.to_tensor(token_type_ids) text_embeddings = model(input_ids, token_type_ids) all_embeddings.append(text_embeddings) text_embedding = all_embeddings[0] print(text_embedding.shape) print(text_embedding.numpy())
train(model) # 读取一张本地的样例图片,转变成模型输入的格式 def load_image(img_path): # 从img_path中读取图像,并转为灰度图 im = Image.open(img_path).convert('L') im = im.resize((28, 28), Image.ANTIALIAS) im = np.array(im).reshape(1, 1, 28, 28).astype(np.float32) # 图像归一化 im = 1.0 - im / 255. return im # 定义预测过程 model = MNIST(num_classes=10) params_file_path = 'mnist.pdparams' img_path = 'work/example_0.png' # 加载模型参数 param_dict = paddle.load(params_file_path) model.load_dict(param_dict) # 灌入数据 model.eval() tensor_img = load_image(img_path) #模型反馈10个分类标签的对应概率 results = model(paddle.to_tensor(tensor_img)) #取概率最大的标签作为预测输出 lab = np.argsort(results.numpy()) print("本次预测的数字是: ", lab[0][-1])
def forward(ctx, x1): y1 = paddle.tanh(x1) ctx.save_for_backward(y1) tensor_1 = paddle.to_tensor([1, 2], dtype='float32') return y1, 5, None, "helloworld", tensor_1
def do_predict(): no_entity_label = "O" ignore_label = -1 tokenizer = ErnieTokenizer.from_pretrained("ernie-1.0") label_map = load_dict(args.tag_path) id2label = {val: key for key, val in label_map.items()} model = ErnieForTokenClassification.from_pretrained( "ernie-1.0", num_classes=len(label_map)) print("============start predict==========") if not args.init_ckpt or not os.path.isfile(args.init_ckpt): raise Exception("init checkpoints {} not exist".format(args.init_ckpt)) else: state_dict = paddle.load(args.init_ckpt) model.set_dict(state_dict) print("Loaded parameters from %s" % args.init_ckpt) # load data from predict file sentences = read_by_lines(args.predict_data) # origin data format sentences = [json.loads(sent) for sent in sentences] encoded_inputs_list = [] for sent in sentences: sent = sent["text"] input_ids, token_type_ids, seq_len = convert_example( [list(sent), []], tokenizer, max_seq_len=args.max_seq_len, is_test=True) encoded_inputs_list.append((input_ids, token_type_ids, seq_len)) batchify_fn = lambda samples, fn=Tuple( Pad(axis=0, pad_val=tokenizer.vocab[tokenizer.pad_token]), # input_ids Pad(axis=0, pad_val=tokenizer.vocab[tokenizer.pad_token] ), # token_type_ids Stack() # sequence lens ): fn(samples) # Seperates data into some batches. batch_encoded_inputs = [ encoded_inputs_list[i:i + args.batch_size] for i in range(0, len(encoded_inputs_list), args.batch_size) ] results = [] model.eval() for batch in batch_encoded_inputs: input_ids, token_type_ids, seq_lens = batchify_fn(batch) input_ids = paddle.to_tensor(input_ids) token_type_ids = paddle.to_tensor(token_type_ids) logits = model(input_ids, token_type_ids) probs = F.softmax(logits, axis=-1) probs_ids = paddle.argmax(probs, -1).numpy() probs = probs.numpy() for p_list, p_ids, seq_len in zip(probs.tolist(), probs_ids.tolist(), seq_lens.tolist()): prob_one = [ p_list[index][pid] for index, pid in enumerate(p_ids[1:seq_len - 1]) ] label_one = [id2label[pid] for pid in p_ids[1:seq_len - 1]] results.append({"probs": prob_one, "labels": label_one}) assert len(results) == len(sentences) for sent, ret in zip(sentences, results): sent["pred"] = ret sentences = [json.dumps(sent, ensure_ascii=False) for sent in sentences] write_by_lines(args.predict_save_path, sentences) print("save data {} to {}".format(len(sentences), args.predict_save_path))
def predict(self, data, max_seq_len=128, batch_size=1, use_gpu=False): """ Predicts the data labels. Args: data (obj:`List(str)`): The processed data whose each element is the raw text. max_seq_len (:obj:`int`, `optional`, defaults to :int:`None`): If set to a number, will limit the total sequence returned so that it has a maximum length. batch_size(obj:`int`, defaults to 1): The number of batch. use_gpu(obj:`bool`, defaults to `False`): Whether to use gpu to run or not. Returns: results(obj:`list`): All the predictions labels. """ # TODO(zhangxuefei): add task token_classification task predict. if self.task not in ['sequence_classification']: raise RuntimeError( "The predict method is for sequence_classification task, but got task %s." % self.task) paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu') tokenizer = self.get_tokenizer() examples = [] for text in data: if len(text) == 1: encoded_inputs = tokenizer.encode(text[0], text_pair=None, max_seq_len=max_seq_len) elif len(text) == 2: encoded_inputs = tokenizer.encode(text[0], text_pair=text[1], max_seq_len=max_seq_len) else: raise RuntimeError( 'The input text must have one or two sequence, but got %d. Please check your inputs.' % len(text)) examples.append( (encoded_inputs['input_ids'], encoded_inputs['segment_ids'])) def _batchify_fn(batch): input_ids = [entry[0] for entry in batch] segment_ids = [entry[1] for entry in batch] return input_ids, segment_ids # Seperates data into some batches. batches = [] one_batch = [] for example in examples: one_batch.append(example) if len(one_batch) == batch_size: batches.append(one_batch) one_batch = [] if one_batch: # The last batch whose size is less than the config batch_size setting. batches.append(one_batch) results = [] self.eval() for batch in batches: input_ids, segment_ids = _batchify_fn(batch) input_ids = paddle.to_tensor(input_ids) segment_ids = paddle.to_tensor(segment_ids) # TODO(zhangxuefei): add task token_classification postprocess after prediction. if self.task == 'sequence_classification': probs = self(input_ids, segment_ids) idx = paddle.argmax(probs, axis=1).numpy() idx = idx.tolist() labels = [self.label_map[i] for i in idx] results.extend(labels) return results
def test_dygraph_error(self): paddle.disable_static(self.place) x = paddle.to_tensor(self.x_np, dtype='int16') self.assertRaises(TypeError, paddle.frac, x)
def predict(self, image, img_metas): image = paddle.to_tensor(image, place=self.place) preds = self.model(image, img_metas) preds = [pred.numpy() for pred in preds] return preds
def example_to_tensorlist_without_batch(example, float_type=paddle.float32): example_list = [None] * 13 pillar_x = example['voxels'][:, :, 0][np.newaxis, np.newaxis, :, :] # (N,1,K,T) pillar_y = example['voxels'][:, :, 1][np.newaxis, np.newaxis, :, :] pillar_z = example['voxels'][:, :, 2][np.newaxis, np.newaxis, :, :] pillar_i = example['voxels'][:, :, 3][np.newaxis, np.newaxis, :, :] num_points_per_pillar = example['num_points'][np.newaxis, :] # (N,K,) coors = example['coordinates'][np.newaxis, :] # (N,K,3) anchors = example['anchors'] # (B,num_anchors,7) image_ids = [int(elem['image_idx']) for elem in example['metadata']] image_ids = np.array(image_ids, dtype=np.int32) voxel_mask = example['voxel_mask'][np.newaxis, :] # (N,K) # ################################################################ # Find distance of x, y, z from pillar center coors_x = example['coordinates'][:, 2][np.newaxis, :] # (N,K) coors_y = example['coordinates'][:, 1][np.newaxis, :] x_sub = coors_x[:, np.newaxis, :, np.newaxis] * 0.16 + 0.08 # Pillars的中心的位置坐标 (N,1,K,1) y_sub = coors_y[:, np.newaxis, :, np.newaxis] * 0.16 - 39.6 x_sub_shaped = x_sub.repeat(pillar_x.shape[3], -1) y_sub_shaped = y_sub.repeat(pillar_x.shape[3], -1) # (N,1,K,T) num_points_for_a_pillar = pillar_x.shape[3] # (T) mask = get_paddings_indicator_np(num_points_per_pillar, num_points_for_a_pillar, axis=0) # (N,T,K) mask = mask.transpose(0, 2, 1) # (N,K,T) mask = mask[:, np.newaxis, :, :] # (N,1,K,T) mask = mask.astype(pillar_x.dtype) example_list[0] = paddle.to_tensor(pillar_x, dtype=float_type) example_list[1] = paddle.to_tensor(pillar_y, dtype=float_type) example_list[2] = paddle.to_tensor(pillar_z, dtype=float_type) example_list[3] = paddle.to_tensor(pillar_i, dtype=float_type) example_list[4] = paddle.to_tensor(num_points_per_pillar, dtype=float_type) example_list[5] = paddle.to_tensor(x_sub_shaped, dtype=float_type) example_list[6] = paddle.to_tensor(y_sub_shaped, dtype=float_type) example_list[7] = paddle.to_tensor(mask, dtype=float_type) example_list[8] = paddle.to_tensor(coors, dtype=paddle.int32) example_list[9] = paddle.to_tensor(voxel_mask, dtype=paddle.bool) example_list[10] = paddle.to_tensor(anchors, dtype=float_type) example_list[11] = paddle.to_tensor(image_ids, dtype=paddle.int32) if 'anchors_mask' in example.keys(): example_list[12] = paddle.to_tensor(example['anchors_mask'], dtype=paddle.bool) print(example_list[12]) else: example_list[12] = None return example_list
def assign_value(inputs): x = paddle.to_tensor(np.array([3]).astype("float32")) return inputs + x