def run(FLAGS, cfg, place): # Model main_arch = cfg.architecture model = create(cfg.architecture) # Init Model load_weight(model, cfg.weights) # Data Reader dataset = cfg.EvalDataset eval_loader, _ = create('EvalReader')(dataset, cfg['worker_num'], place) # Run Eval outs_res = [] start_time = time.time() sample_num = 0 for iter_id, data in enumerate(eval_loader): # forward model.eval() outs = model(data, cfg['EvalReader']['inputs_def']['fields'], 'infer') outs_res.append(outs) # log sample_num += len(data) if iter_id % 100 == 0: logger.info("Eval iter: {}".format(iter_id)) cost_time = time.time() - start_time logger.info('Total sample number: {}, averge FPS: {}'.format( sample_num, sample_num / cost_time)) eval_type = ['bbox'] if getattr(cfg, 'MaskHead', None): eval_type.append('mask') # Metric # TODO: support other metric from ppdet.utils.coco_eval import get_category_info anno_file = dataset.get_anno() with_background = cfg.with_background use_default_label = dataset.use_default_label clsid2catid, catid2name = get_category_info(anno_file, with_background, use_default_label) infer_res = get_infer_results(outs_res, eval_type, clsid2catid) eval_results(infer_res, cfg.metric, anno_file)
def run(FLAGS, cfg, place): # Model main_arch = cfg.architecture model = create(cfg.architecture) # data dataset = cfg.TestDataset test_images = get_test_images(FLAGS.infer_dir, FLAGS.infer_img) dataset.set_images(test_images) test_loader = create('TestReader')(dataset, cfg['worker_num']) extra_key = ['im_shape', 'scale_factor', 'im_id'] # TODO: support other metrics imid2path = dataset.get_imid2path() anno_file = dataset.get_anno() with_background = cfg.with_background use_default_label = dataset.use_default_label if cfg.metric == 'COCO': from ppdet.utils.coco_eval import get_category_info if cfg.metric == 'VOC': from ppdet.utils.voc_eval import get_category_info clsid2catid, catid2name = get_category_info(anno_file, with_background, use_default_label) # Init Model load_weight(model, cfg.weights) # Run Infer for iter_id, data in enumerate(test_loader): # forward model.eval() outs = model(data) for key in extra_key: outs[key] = data[key] for key, value in outs.items(): outs[key] = value.numpy() if 'mask' in outs and 'bbox' in outs: mask_resolution = model.mask_post_process.mask_resolution from ppdet.py_op.post_process import mask_post_process outs['mask'] = mask_post_process(outs, outs['im_shape'], outs['scale_factor'], mask_resolution) eval_type = [] if 'bbox' in outs: eval_type.append('bbox') if 'mask' in outs: eval_type.append('mask') batch_res = get_infer_results([outs], eval_type, clsid2catid) logger.info('Infer iter {}'.format(iter_id)) bbox_res = None mask_res = None bbox_num = outs['bbox_num'] start = 0 for i, im_id in enumerate(outs['im_id']): image_path = imid2path[int(im_id)] image = Image.open(image_path).convert('RGB') end = start + bbox_num[i] # use VisualDL to log original image if FLAGS.use_vdl: original_image_np = np.array(image) vdl_writer.add_image( "original/frame_{}".format(vdl_image_frame), original_image_np, vdl_image_step) if 'bbox' in batch_res: bbox_res = batch_res['bbox'][start:end] if 'mask' in batch_res: mask_res = batch_res['mask'][start:end] image = visualize_results(image, bbox_res, mask_res, int(outs['im_id']), catid2name, FLAGS.draw_threshold) # use VisualDL to log image with bbox if FLAGS.use_vdl: infer_image_np = np.array(image) vdl_writer.add_image("bbox/frame_{}".format(vdl_image_frame), infer_image_np, vdl_image_step) vdl_image_step += 1 if vdl_image_step % 10 == 0: vdl_image_step = 0 vdl_image_frame += 1 # save image with detection save_name = get_save_image_name(FLAGS.output_dir, image_path) logger.info("Detection bbox results save in {}".format(save_name)) image.save(save_name, quality=95) start = end
def run(FLAGS, cfg, place): # Model main_arch = cfg.architecture model = create(cfg.architecture) # data dataset = cfg.TestDataset test_images = get_test_images(FLAGS.infer_dir, FLAGS.infer_img) dataset.set_images(test_images) test_loader, _ = create('TestReader')(dataset, cfg['worker_num'], place) # TODO: support other metrics imid2path = dataset.get_imid2path() from ppdet.utils.coco_eval import get_category_info anno_file = dataset.get_anno() with_background = cfg.with_background use_default_label = dataset.use_default_label clsid2catid, catid2name = get_category_info(anno_file, with_background, use_default_label) # Init Model load_weight(model, cfg.weights) # Run Infer for iter_id, data in enumerate(test_loader): # forward model.eval() outs = model(data, cfg.TestReader['inputs_def']['fields'], 'infer') batch_res = get_infer_results([outs], outs.keys(), clsid2catid) logger.info('Infer iter {}'.format(iter_id)) bbox_res = None mask_res = None im_ids = outs['im_id'] bbox_num = outs['bbox_num'] start = 0 for i, im_id in enumerate(im_ids): im_id = im_ids[i] image_path = imid2path[int(im_id)] image = Image.open(image_path).convert('RGB') end = start + bbox_num[i] # use VisualDL to log original image if FLAGS.use_vdl: original_image_np = np.array(image) vdl_writer.add_image( "original/frame_{}".format(vdl_image_frame), original_image_np, vdl_image_step) if 'bbox' in batch_res: bbox_res = batch_res['bbox'][start:end] if 'mask' in batch_res: mask_res = batch_res['mask'][start:end] image = visualize_results(image, bbox_res, mask_res, int(im_id), catid2name, FLAGS.draw_threshold) # use VisualDL to log image with bbox if FLAGS.use_vdl: infer_image_np = np.array(image) vdl_writer.add_image("bbox/frame_{}".format(vdl_image_frame), infer_image_np, vdl_image_step) vdl_image_step += 1 if vdl_image_step % 10 == 0: vdl_image_step = 0 vdl_image_frame += 1 # save image with detection save_name = get_save_image_name(FLAGS.output_dir, image_path) logger.info("Detection bbox results save in {}".format(save_name)) image.save(save_name, quality=95) start = end
def run(FLAGS, cfg, place): # Model main_arch = cfg.architecture model = create(cfg.architecture) # Init Model load_weight(model, cfg.weights) # Data Reader dataset = cfg.EvalDataset eval_loader = create('EvalReader')(dataset, cfg['worker_num']) extra_key = ['im_shape', 'scale_factor', 'im_id'] if cfg.metric == 'VOC': extra_key += ['gt_bbox', 'gt_class', 'difficult'] # Run Eval outs_res = [] sample_num = 0 start_time = time.time() for iter_id, data in enumerate(eval_loader): # forward model.eval() outs = model(data, mode='infer') for key in extra_key: outs[key] = data[key] for key, value in outs.items(): outs[key] = value.numpy() if 'mask' in outs and 'bbox' in outs: mask_resolution = model.mask_post_process.mask_resolution from ppdet.py_op.post_process import mask_post_process outs['mask'] = mask_post_process(outs, outs['im_shape'], outs['scale_factor'], mask_resolution) outs_res.append(outs) # log sample_num += outs['im_id'].shape[0] if iter_id % 100 == 0: logger.info("Eval iter: {}".format(iter_id)) cost_time = time.time() - start_time logger.info('Total sample number: {}, averge FPS: {}'.format( sample_num, sample_num / cost_time)) eval_type = [] if 'bbox' in outs: eval_type.append('bbox') if 'mask' in outs: eval_type.append('mask') # Metric # TODO: support other metric with_background = cfg.with_background use_default_label = dataset.use_default_label if cfg.metric == 'COCO': from ppdet.utils.coco_eval import get_category_info clsid2catid, catid2name = get_category_info(dataset.get_anno(), with_background, use_default_label) infer_res = get_infer_results(outs_res, eval_type, clsid2catid) elif cfg.metric == 'VOC': from ppdet.utils.voc_eval import get_category_info clsid2catid, catid2name = get_category_info(dataset.get_label_list(), with_background, use_default_label) infer_res = outs_res eval_results(infer_res, cfg.metric, dataset)