def inference(self, img): img_info = {'id': 0} if isinstance(img, str): img_info['file_name'] = os.path.basename(img) img = cv2.imread(img) else: img_info['file_name'] = None height, width = img.shape[:2] img_info['height'] = height img_info['width'] = width img_info['raw_img'] = img img, ratio = preproc(img, self.test_size, self.rgb_means, self.std) img_info['ratio'] = ratio img = torch.from_numpy(img).unsqueeze(0).cuda() with torch.no_grad(): t0 = time.time() outputs = self.model(img) if self.decoder is not None: outputs = self.decoder(outputs, dtype=outputs.type()) outputs = postprocess( outputs, self.num_classes, self.confthre, self.nmsthre ) logger.info('Infer time: {:.4f}s'.format(time.time()-t0)) return outputs, img_info
def yolox_inference(img, model, test_size): print("yolox-infarenceファンクション") bboxes = [] bbclasses = [] scores = [] preproc = ValTransform(legacy = False) tensor_img, _ = preproc(img, None, test_size) tensor_img = torch.from_numpy(tensor_img).unsqueeze(0) tensor_img = tensor_img.float() tensor_img = tensor_img#.cuda() with torch.no_grad(): outputs = model(tensor_img) outputs = postprocess( outputs, num_classes, confthre, nmsthre, class_agnostic=True ) if outputs[0] is None: return [], [], [] outputs = outputs[0].cpu() bboxes = outputs[:, 0:4] bboxes /= min(test_size[0] / img.shape[0], test_size[1] / img.shape[1]) bbclasses = outputs[:, 6] scores = outputs[:, 4] * outputs[:, 5] return bboxes, bbclasses, scores
def inference(self, img, timer): img_info = {"id": 0} if isinstance(img, str): img_info["file_name"] = osp.basename(img) img = cv2.imread(img) else: img_info["file_name"] = None height, width = img.shape[:2] img_info["height"] = height img_info["width"] = width img_info["raw_img"] = img img, ratio = preproc(img, self.test_size, self.rgb_means, self.std) img_info["ratio"] = ratio img = torch.from_numpy(img).unsqueeze(0).float().to(self.device) if self.fp16: img = img.half() # to FP16 with torch.no_grad(): timer.tic() outputs = self.model(img) if self.decoder is not None: outputs = self.decoder(outputs, dtype=outputs.type()) outputs = postprocess(outputs, self.num_classes, self.confthre, self.nmsthre) #logger.info("Infer time: {:.4f}s".format(time.time() - t0)) return outputs, img_info
def forward(self, input_tensors): preds = self.model(input_tensors) if self.decoder is not None: preds = self.decoder(preds, dtype=preds.type()) preds = postprocess( preds, self.num_classes, self.confthre, self.nmsthre, class_agnostic=True ) return preds
def inference(self, img, visual=True, conf=0.5, logger_=True): img_info = {"id": 0} if isinstance(img, str): img_info["file_name"] = os.path.basename(img) img = cv2.imread(img) else: img_info["file_name"] = None img_info['img'] = img height, width = img.shape[:2] img_info["height"], img_info["width"], img_info[ "img"] = height, width, img ratio = min(self.test_size[0] / img.shape[0], self.test_size[1] / img.shape[1]) img_info["ratio"] = ratio img, _ = self.preproc(img, None, self.test_size) img = torch.from_numpy(img).unsqueeze(0) img = img.float() if self.device == torch.device('cuda'): img = img.cuda() # img = img.half() # to FP16 with torch.no_grad(): t0 = time.time() outputs = self.model(img) outputs = postprocess(outputs, self.exp.num_classes, self.exp.test_conf, self.exp.nmsthre)[0].cpu().numpy() img_info['boxes'] = outputs[:, 0:4] / ratio img_info['scores'] = outputs[:, 4] * outputs[:, 5] img_info['class_ids'] = outputs[:, 6] img_info['box_nums'] = outputs.shape[0] if visual: img_info['visual'] = vis2(img_info['img'], img_info['boxes'], img_info['scores'], img_info['class_ids'], conf, COCO_CLASSES) if logger_: logger.info("Infer time: {:.4f}s".format(time.time() - t0)) return outputs, img_info
def inference(self, img): img_info = {"id": 0} if isinstance(img, str): img_info["file_name"] = os.path.basename(img) img = cv2.imread(img) else: img_info["file_name"] = None height, width = img.shape[:2] img_info["height"] = height img_info["width"] = width img_info["raw_img"] = img ratio = min(self.test_size[0] / img.shape[0], self.test_size[1] / img.shape[1]) img_info["ratio"] = ratio img, _ = self.preproc(img, None, self.test_size) img = torch.from_numpy(img).unsqueeze(0) img = img.float() if self.device == "gpu": img = img.cuda() if self.fp16: img = img.half() # to FP16 with torch.no_grad(): t0 = time.time() outputs = self.model(img) if self.decoder is not None: outputs = self.decoder(outputs, dtype=outputs.type()) outputs = postprocess(outputs, self.num_classes, self.confthre, self.nmsthre, class_agnostic=True) logger.info("Infer time: {:.4f}s".format(time.time() - t0)) return outputs, img_info
def evaluate(self, model, distributed=False, half=False, trt_file=None, decoder=None, test_size=None): """ VOC average precision (AP) Evaluation. Iterate inference on the test dataset and the results are evaluated by COCO API. NOTE: This function will change training mode to False, please save states if needed. Args: model : model to evaluate. Returns: ap50_95 (float) : COCO style AP of IoU=50:95 ap50 (float) : VOC 2007 metric AP of IoU=50 summary (sr): summary info of evaluation. """ # TODO half to amp_test tensor_type = torch.cuda.HalfTensor if half else torch.cuda.FloatTensor model = model.eval() if half: model = model.half() ids = [] data_list = {} progress_bar = tqdm if is_main_process() else iter inference_time = 0 nms_time = 0 n_samples = len(self.dataloader) - 1 if trt_file is not None: from torch2trt import TRTModule model_trt = TRTModule() model_trt.load_state_dict(torch.load(trt_file)) x = torch.ones(1, 3, test_size[0], test_size[1]).cuda() model(x) model = model_trt for cur_iter, (imgs, _, info_imgs, ids) in enumerate(progress_bar(self.dataloader)): with torch.no_grad(): imgs = imgs.type(tensor_type) # skip the the last iters since batchsize might be not enough for batch inference is_time_record = cur_iter < len(self.dataloader) - 1 if is_time_record: start = time.time() outputs = model(imgs) if decoder is not None: outputs = decoder(outputs, dtype=outputs.type()) if is_time_record: infer_end = time_synchronized() inference_time += infer_end - start outputs = postprocess(outputs, self.num_classes, self.confthre, self.nmsthre) if is_time_record: nms_end = time_synchronized() nms_time += nms_end - infer_end data_list.update( self.convert_to_voc_format(outputs, info_imgs, ids)) statistics = torch.cuda.FloatTensor( [inference_time, nms_time, n_samples]) if distributed: data_list = gather(data_list, dst=0) data_list = ChainMap(*data_list) torch.distributed.reduce(statistics, dst=0) eval_results = self.evaluate_prediction(data_list, statistics) synchronize() return eval_results