def predict_image(self, images): """ 单图检测 """ self.dataset.set_images(images) loader = create('TestReader')(self.dataset, 0) anno_file = self.dataset.get_anno() clsid2catid, catid2name = get_categories(self.cfg.metric, anno_file=anno_file) # Run Infer self.status['mode'] = 'test' self.model.eval() info = {} for step_id, data in enumerate(loader): self.status['step_id'] = step_id # forward outs = self.model(data) for key in ['im_shape', 'scale_factor', 'im_id']: outs[key] = data[key] for key, value in outs.items(): if hasattr(value, 'numpy'): outs[key] = value.numpy() batch_res = get_infer_results(outs, clsid2catid) bbox_res = batch_res['bbox'] """ 返回信息 """ info = {'classes': list(catid2name.values()), 'bbox_res': bbox_res} return info
def predict(self, images, draw_threshold=0.5, output_dir='output', save_txt=False): self.dataset.set_images(images) loader = create('TestReader')(self.dataset, 0) imid2path = self.dataset.get_imid2path() anno_file = self.dataset.get_anno() clsid2catid, catid2name = get_categories(self.cfg.metric, anno_file) # Run Infer self.status['mode'] = 'test' self.model.eval() for step_id, data in enumerate(loader): self.status['step_id'] = step_id # forward outs = self.model(data) for key in ['im_shape', 'scale_factor', 'im_id']: outs[key] = data[key] for key, value in outs.items(): outs[key] = value.numpy() batch_res = get_infer_results(outs, clsid2catid) 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') self.status['original_image'] = np.array(image.copy()) end = start + bbox_num[i] bbox_res = batch_res['bbox'][start:end] \ if 'bbox' in batch_res else None mask_res = batch_res['mask'][start:end] \ if 'mask' in batch_res else None segm_res = batch_res['segm'][start:end] \ if 'segm' in batch_res else None image = visualize_results(image, bbox_res, mask_res, segm_res, int(outs['im_id']), catid2name, draw_threshold) self.status['result_image'] = np.array(image.copy()) if self._compose_callback: self._compose_callback.on_step_end(self.status) # save image with detection save_name = self._get_save_image_name(output_dir, image_path) logger.info( "Detection bbox results save in {}".format(save_name)) image.save(save_name, quality=95) if save_txt: save_path = os.path.splitext(save_name)[0] + '.txt' save_result(save_path, bbox_res, catid2name, draw_threshold) start = end
def predict(self, images, draw_threshold=0.5, output_dir='output'): self.dataset.set_images(images) loader = create('TestReader')(self.dataset, 0) imid2path = self.dataset.get_imid2path() anno_file = self.dataset.get_anno() with_background = self.cfg.with_background clsid2catid, catid2name = get_categories(self.cfg.metric, anno_file, with_background) # Run Infer for step_id, data in enumerate(loader): self.status['step_id'] = step_id # forward self.model.eval() outs = self.model(data) for key in ['im_shape', 'scale_factor', 'im_id']: outs[key] = data[key] for key, value in outs.items(): outs[key] = value.numpy() # FIXME: for more elegent coding if 'mask' in outs and 'bbox' in outs: mask_resolution = self.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) batch_res = get_infer_results(outs, clsid2catid) 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] bbox_res = batch_res['bbox'][start:end] \ if 'bbox' in batch_res else None mask_res = batch_res['mask'][start:end] \ if 'mask' in batch_res else None segm_res = batch_res['segm'][start:end] \ if 'segm' in batch_res else None image = visualize_results(image, bbox_res, mask_res, segm_res, int(outs['im_id']), catid2name, draw_threshold) # save image with detection save_name = self._get_save_image_name(output_dir, image_path) logger.info("Detection bbox results save in {}".format( save_name)) image.save(save_name, quality=95) start = end
def on_train_end(self, status): self.loader.dataset = self.dataset results = self._eval_with_loader(self.loader) results = self.dataset.anno_cropper.aggregate_chips_detections(results) # sniper proposals = [] clsid2catid = {v: k for k, v in self.dataset.catid2clsid.items()} for outs in results: batch_res = get_infer_results(outs, clsid2catid) start = 0 for i, im_id in enumerate(outs['im_id']): bbox_num = outs['bbox_num'] end = start + bbox_num[i] bbox_res = batch_res['bbox'][start:end] \ if 'bbox' in batch_res else None if bbox_res: proposals += bbox_res logger.info("save proposals in {}".format(self.cfg.proposals_path)) with open(self.cfg.proposals_path, 'w') as f: json.dump(proposals, f)
def predict(self, images, draw_threshold=0.5, output_dir='output', save_txt=False): self.dataset.set_images(images) loader = create('TestReader')(self.dataset, 0) imid2path = self.dataset.get_imid2path() anno_file = self.dataset.get_anno() clsid2catid, catid2name = get_categories(self.cfg.metric, anno_file=anno_file) # Run Infer self.status['mode'] = 'test' self.model.eval() if self.cfg.get('print_flops', False): self._flops(loader) results = [] for step_id, data in enumerate(loader): self.status['step_id'] = step_id # forward outs = self.model(data) for key in ['im_shape', 'scale_factor', 'im_id']: outs[key] = data[key] for key, value in outs.items(): if hasattr(value, 'numpy'): outs[key] = value.numpy() results.append(outs) # sniper if type(self.dataset) == SniperCOCODataSet: results = self.dataset.anno_cropper.aggregate_chips_detections( results) for outs in results: batch_res = get_infer_results(outs, clsid2catid) 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') image = ImageOps.exif_transpose(image) self.status['original_image'] = np.array(image.copy()) end = start + bbox_num[i] bbox_res = batch_res['bbox'][start:end] \ if 'bbox' in batch_res else None mask_res = batch_res['mask'][start:end] \ if 'mask' in batch_res else None segm_res = batch_res['segm'][start:end] \ if 'segm' in batch_res else None keypoint_res = batch_res['keypoint'][start:end] \ if 'keypoint' in batch_res else None image = visualize_results(image, bbox_res, mask_res, segm_res, keypoint_res, int(im_id), catid2name, draw_threshold) self.status['result_image'] = np.array(image.copy()) if self._compose_callback: self._compose_callback.on_step_end(self.status) # save image with detection save_name = self._get_save_image_name(output_dir, image_path) logger.info( "Detection bbox results save in {}".format(save_name)) image.save(save_name, quality=95) if save_txt: save_path = os.path.splitext(save_name)[0] + '.txt' results = {} results["im_id"] = im_id if bbox_res: results["bbox_res"] = bbox_res if keypoint_res: results["keypoint_res"] = keypoint_res save_result(save_path, results, catid2name, draw_threshold) start = end
def predict(self, images, draw_threshold=0.5, output_dir='output', save_results=False): self.dataset.set_images(images) loader = create('TestReader')(self.dataset, 0) def setup_metrics_for_loader(): # mem metrics = copy.deepcopy(self._metrics) mode = self.mode save_prediction_only = self.cfg[ 'save_prediction_only'] if 'save_prediction_only' in self.cfg else None output_eval = self.cfg[ 'output_eval'] if 'output_eval' in self.cfg else None # modify self.mode = '_test' self.cfg['save_prediction_only'] = True self.cfg['output_eval'] = output_dir self._init_metrics() # restore self.mode = mode self.cfg.pop('save_prediction_only') if save_prediction_only is not None: self.cfg['save_prediction_only'] = save_prediction_only self.cfg.pop('output_eval') if output_eval is not None: self.cfg['output_eval'] = output_eval _metrics = copy.deepcopy(self._metrics) self._metrics = metrics return _metrics if save_results: metrics = setup_metrics_for_loader() else: metrics = [] imid2path = self.dataset.get_imid2path() anno_file = self.dataset.get_anno() clsid2catid, catid2name = get_categories( self.cfg.metric, anno_file=anno_file) # Run Infer self.status['mode'] = 'test' self.model.eval() if self.cfg.get('print_flops', False): flops_loader = create('TestReader')(self.dataset, 0) self._flops(flops_loader) results = [] for step_id, data in enumerate(tqdm(loader)): self.status['step_id'] = step_id # forward outs = self.model(data) for _m in metrics: _m.update(data, outs) for key in ['im_shape', 'scale_factor', 'im_id']: if isinstance(data, typing.Sequence): outs[key] = data[0][key] else: outs[key] = data[key] for key, value in outs.items(): if hasattr(value, 'numpy'): outs[key] = value.numpy() results.append(outs) # sniper if type(self.dataset) == SniperCOCODataSet: results = self.dataset.anno_cropper.aggregate_chips_detections( results) for _m in metrics: _m.accumulate() _m.reset() for outs in results: batch_res = get_infer_results(outs, clsid2catid) 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') image = ImageOps.exif_transpose(image) self.status['original_image'] = np.array(image.copy()) end = start + bbox_num[i] bbox_res = batch_res['bbox'][start:end] \ if 'bbox' in batch_res else None mask_res = batch_res['mask'][start:end] \ if 'mask' in batch_res else None segm_res = batch_res['segm'][start:end] \ if 'segm' in batch_res else None keypoint_res = batch_res['keypoint'][start:end] \ if 'keypoint' in batch_res else None image = visualize_results( image, bbox_res, mask_res, segm_res, keypoint_res, int(im_id), catid2name, draw_threshold) self.status['result_image'] = np.array(image.copy()) if self._compose_callback: self._compose_callback.on_step_end(self.status) # save image with detection save_name = self._get_save_image_name(output_dir, image_path) logger.info("Detection bbox results save in {}".format( save_name)) image.save(save_name, quality=95) start = end