def ssd_eval(dataset_path, ckpt_path, anno_json): """SSD evaluation.""" batch_size = 1 ds = create_ssd_dataset(dataset_path, batch_size=batch_size, repeat_num=1, is_training=False, use_multiprocessing=False) if config.model == "ssd300": net = SSD300(ssd_mobilenet_v2(), config, is_training=False) elif config.model == "ssd_vgg16": net = ssd_vgg16(config=config) elif config.model == "ssd_mobilenet_v1_fpn": net = ssd_mobilenet_v1_fpn(config=config) elif config.model == "ssd_resnet50_fpn": net = ssd_resnet50_fpn(config=config) else: raise ValueError(f'config.model: {config.model} is not supported') net = SsdInferWithDecoder(net, Tensor(default_boxes), config) print("Load Checkpoint!") param_dict = load_checkpoint(ckpt_path) net.init_parameters_data() load_param_into_net(net, param_dict) net.set_train(False) total = ds.get_dataset_size() * batch_size print("\n========================================\n") print("total images num: ", total) print("Processing, please wait a moment.") eval_param_dict = {"net": net, "dataset": ds, "anno_json": anno_json} mAP = apply_eval(eval_param_dict) print("\n========================================\n") print(f"mAP: {mAP}")
def ssd_model_build(args_opt): if config.model == "ssd300": backbone = ssd_mobilenet_v2() ssd = SSD300(backbone=backbone, config=config) init_net_param(ssd) if args_opt.freeze_layer == "backbone": for param in backbone.feature_1.trainable_params(): param.requires_grad = False elif config.model == "ssd_mobilenet_v1_fpn": ssd = ssd_mobilenet_v1_fpn(config=config) init_net_param(ssd) if config.feature_extractor_base_param != "": param_dict = load_checkpoint(config.feature_extractor_base_param) for x in list(param_dict.keys()): param_dict["network.feature_extractor.mobilenet_v1." + x] = param_dict[x] del param_dict[x] load_param_into_net(ssd.feature_extractor.mobilenet_v1.network, param_dict) elif config.model == "ssd_resnet50_fpn": ssd = ssd_resnet50_fpn(config=config) init_net_param(ssd) if config.feature_extractor_base_param != "": param_dict = load_checkpoint(config.feature_extractor_base_param) for x in list(param_dict.keys()): param_dict["network.feature_extractor.resnet." + x] = param_dict[x] del param_dict[x] load_param_into_net(ssd.feature_extractor.resnet, param_dict) else: raise ValueError(f'config.model: {config.model} is not supported') return ssd
def ssd_eval(dataset_path, ckpt_path, anno_json): """SSD evaluation.""" batch_size = 1 ds = create_ssd_dataset(dataset_path, batch_size=batch_size, repeat_num=1, is_training=False, use_multiprocessing=False) if config.model == "ssd300": net = SSD300(ssd_mobilenet_v2(), config, is_training=False) elif config.model == "ssd_vgg16": net = ssd_vgg16(config=config) elif config.model == "ssd_mobilenet_v1_fpn": net = ssd_mobilenet_v1_fpn(config=config) elif config.model == "ssd_resnet50_fpn": net = ssd_resnet50_fpn(config=config) else: raise ValueError(f'config.model: {config.model} is not supported') net = SsdInferWithDecoder(net, Tensor(default_boxes), config) print("Load Checkpoint!") param_dict = load_checkpoint(ckpt_path) net.init_parameters_data() load_param_into_net(net, param_dict) net.set_train(False) i = batch_size total = ds.get_dataset_size() * batch_size start = time.time() pred_data = [] print("\n========================================\n") print("total images num: ", total) print("Processing, please wait a moment.") for data in ds.create_dict_iterator(output_numpy=True, num_epochs=1): img_id = data['img_id'] img_np = data['image'] image_shape = data['image_shape'] output = net(Tensor(img_np)) for batch_idx in range(img_np.shape[0]): pred_data.append({ "boxes": output[0].asnumpy()[batch_idx], "box_scores": output[1].asnumpy()[batch_idx], "img_id": int(np.squeeze(img_id[batch_idx])), "image_shape": image_shape[batch_idx] }) percent = round(i / total * 100., 2) print(f' {str(percent)} [{i}/{total}]', end='\r') i += batch_size cost_time = int((time.time() - start) * 1000) print(f' 100% [{total}/{total}] cost {cost_time} ms') mAP = metrics(pred_data, anno_json) print("\n========================================\n") print(f"mAP: {mAP}")
parser.add_argument('--file_format', type=str, choices=["AIR", "MINDIR"], default='AIR', help='file format') parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", help="device target") args = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) if args.device_target == "Ascend": context.set_context(device_id=args.device_id) if __name__ == '__main__': if config.model == "ssd300": net = SSD300(ssd_mobilenet_v2(), config, is_training=False) elif config.model == "ssd_vgg16": net = ssd_vgg16(config=config) elif config.model == "ssd_mobilenet_v1_fpn": net = ssd_mobilenet_v1_fpn(config=config) elif config.model == "ssd_resnet50_fpn": net = ssd_resnet50_fpn(config=config) else: raise ValueError(f'config.model: {config.model} is not supported') net = SsdInferWithDecoder(net, Tensor(default_boxes), config) param_dict = load_checkpoint(args.ckpt_file) net.init_parameters_data() load_param_into_net(net, param_dict) net.set_train(False) input_shp = [args.batch_size, 3] + config.img_shape input_array = Tensor(np.random.uniform(-1.0, 1.0, size=input_shp), mindspore.float32) export(net, input_array, file_name=args.file_name, file_format=args.file_format)