def FasterRcnn_eval(dataset_path, ckpt_path, ann_file): """FasterRcnn evaluation.""" ds = create_fasterrcnn_dataset(dataset_path, batch_size=config.test_batch_size, is_training=False) net = Faster_Rcnn_Resnet50(config) param_dict = load_checkpoint(ckpt_path) load_param_into_net(net, param_dict) net.set_train(False) eval_iter = 0 total = ds.get_dataset_size() outputs = [] dataset_coco = COCO(ann_file) print("\n========================================\n") print("total images num: ", total) print("Processing, please wait a moment.") max_num = 128 for data in ds.create_dict_iterator(num_epochs=1): eval_iter = eval_iter + 1 img_data = data['image'] img_metas = data['image_shape'] gt_bboxes = data['box'] gt_labels = data['label'] gt_num = data['valid_num'] start = time.time() # run net output = net(img_data, img_metas, gt_bboxes, gt_labels, gt_num) end = time.time() print("Iter {} cost time {}".format(eval_iter, end - start)) # output all_bbox = output[0] all_label = output[1] all_mask = output[2] for j in range(config.test_batch_size): all_bbox_squee = np.squeeze(all_bbox.asnumpy()[j, :, :]) all_label_squee = np.squeeze(all_label.asnumpy()[j, :, :]) all_mask_squee = np.squeeze(all_mask.asnumpy()[j, :, :]) all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :] all_labels_tmp_mask = all_label_squee[all_mask_squee] if all_bboxes_tmp_mask.shape[0] > max_num: inds = np.argsort(-all_bboxes_tmp_mask[:, -1]) inds = inds[:max_num] all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds] all_labels_tmp_mask = all_labels_tmp_mask[inds] outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes) outputs.append(outputs_tmp) eval_types = ["bbox"] result_files = results2json(dataset_coco, outputs, "./results.pkl") coco_eval(result_files, eval_types, dataset_coco, single_result=True)
print("Create Mindrecord. It may take some time.") data_to_mindrecord_byte_image("other", True, prefix) print("Create Mindrecord Done, at {}".format(mindrecord_dir)) else: print("image_dir or anno_path not exits.") while not os.path.exists(mindrecord_file + ".db"): time.sleep(5) print("CHECKING MINDRECORD FILES DONE!") loss_scale = float(config.loss_scale) # When create MindDataset, using the fitst mindrecord file, such as FasterRcnn.mindrecord0. dataset = create_fasterrcnn_dataset(mindrecord_file, batch_size=config.batch_size, device_num=device_num, rank_id=rank) dataset_size = dataset.get_dataset_size() print("Create dataset done!") net = Faster_Rcnn_Resnet50(config=config) net = net.set_train() load_path = args_opt.pre_trained if load_path != "": param_dict = load_checkpoint(load_path) key_mapping = { 'down_sample_layer.1.beta': 'bn_down_sample.beta',
pre_trained = '/ckpt_path' print("CHECKING MINDRECORD FILES ...") if not os.path.exists(mindrecord_file): if not os.path.isdir(mindrecord_dir): os.makedirs(mindrecord_dir) if os.path.isdir(config.coco_root): print("Create Mindrecord. It may take some time.") data_to_mindrecord_byte_image("coco", False, prefix, file_num=1) print("Create Mindrecord Done, at {}".format(mindrecord_dir)) else: print("coco_root not exits.") print('Start generate adversarial samples.') # build network and dataset ds = create_fasterrcnn_dataset(mindrecord_file, batch_size=config.test_batch_size, \ repeat_num=1, is_training=False) net = Faster_Rcnn_Resnet50(config) param_dict = load_checkpoint(pre_trained) load_param_into_net(net, param_dict) net = net.set_train(False) # build attacker model = ModelToBeAttacked(net) attack = GeneticAttack(model, model_type='detection', max_steps=50, reserve_ratio=0.3, mutation_rate=0.05, per_bounds=0.5, step_size=0.25, temp=0.1) # generate adversarial samples sample_num = 5 ori_imagess = [] adv_imgs = [] ori_meta = []