def eval(model, save_path, test_path, device,threshold): model.eval() save_path = os.path.join(save_path, "submit") os.makedirs(save_path, exist_ok=True) img_path_root = os.path.join(test_path, "MSRA-TD500", "test") # 预测所有测试图片 img_paths = [os.path.join(img_path_root, x) for x in os.listdir(img_path_root)] for img_path in tqdm(img_paths, desc='test models'): if not img_path.endswith('.JPG') and not img_path.endswith('.jpg'): continue img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) org_img = img.copy() h, w = img.shape[:2] img = scale_image(img) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale, org_img) np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # 开始计算 recall precision f1 f_score_new = get_msra_result(save_path, img_path_root) return f_score_new
def predict(self, img: str, long_size: int = 2240): ''' 对传入的图像进行预测,支持图像地址,opecv 读取图片,偏慢 :param img: 图像地址 :param is_numpy: :return: ''' assert os.path.exists(img), 'file is not exists' img = cv2.imread(img) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) h, w = img.shape[:2] scale = long_size / max(h, w) img = cv2.resize(img, None, fx=scale, fy=scale) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = tensor.unsqueeze_(0) tensor = tensor.to(self.device) with torch.no_grad(): torch.cuda.synchronize() start = time.time() preds = self.net(tensor) preds, boxes_list = pse_decode(preds[0], self.scale) scale = (preds.shape[1] / w, preds.shape[0] / h) # print(scale) # preds, boxes_list = decode(preds,num_pred=-1) if len(boxes_list): boxes_list = boxes_list / scale torch.cuda.synchronize() t = time.time() - start return preds, boxes_list, t
def detect(self, image, img_show): # get model output preds = self.model.forward(image) preds, boxes, contours = pse_decode(preds[0], self.scale, self.threshold) output = {'image': image, 'tr': preds, 'bbox': boxes} return contours, output
def eval(model, workspace, test_path, device): model.eval() # torch.cuda.empty_cache() # speed up evaluating after training finished img_path = os.path.join(test_path, 'Images/Test/') save_path = os.path.join(workspace, 'output') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) vis_ctw1500 = os.path.join(workspace, 'vis_ctw1500') if os.path.exists(vis_ctw1500): shutil.rmtree(vis_ctw1500, ignore_errors=True) if not os.path.exists(vis_ctw1500): os.makedirs(vis_ctw1500) long_size = 1280 # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] for idx, img_path in enumerate(tqdm(img_paths, desc='test models')): img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) org_img = img.copy() h, w = img.shape[:2] img = scale_aligned_short(img) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale, org_img) if config.visualization: for bbox in boxes_list: bbox = np.array(bbox, np.int) cv2.drawContours(org_img, [bbox.reshape(int(bbox.shape[0] / 2), 2)], -1, (0, 255, 0), 2) org_img = cv2.resize(org_img, (640, 640)) debug(idx, img_path, [[org_img]], vis_ctw1500) image_name = img_path.split('/')[-1].split('.')[0] write_result_as_txt(image_name, boxes_list, save_path) # recall precision f1 gt_path = os.path.join(test_path, 'gt/Test') fid_path = os.path.join(workspace, 'res_tt.txt') precision, recall, hmean = evl_totaltext(save_path, gt_path, fid_path) # f_score_new = getresult(save_path,config.gt_name) return precision, recall, hmean
def eval(model, save_path, test_path, device): model.eval() # torch.cuda.empty_cache() # speed up evaluating after training finished img_path = os.path.join(test_path, 'img') gt_path = os.path.join(test_path, 'gt') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] for img_path in tqdm(img_paths, desc='test models'): img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) h, w = img.shape[:2] # if (w < h): # if (h > MAX_LEN): # scale = 1.0 * MAX_LEN / h # w = w * scale # h = MAX_LEN # elif (h <= w): # if (w > MAX_LEN): # scale = 1.0 * MAX_LEN / w # h = scale * h # w = MAX_LEN # # w = int(w // 32 * 32) # h = int(h // 32 * 32) # # img = cv2.resize(img, (h, w), interpolation=cv2.INTER_AREA) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale) scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) if len(boxes_list): boxes_list = boxes_list / scale np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # 开始计算 recall precision f1 result_dict = cal_recall_precison_f1(gt_path, save_path) return result_dict['recall'], result_dict['precision'], result_dict[ 'hmean']
def eval(model, config, device, thre, long_size=1280): model.eval() img_path = os.path.join(config.testroot, 'test_img') save_path = os.path.join(config.workspace, 'output_eval') # if os.path.exists(save_path): # shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) long_size = 2240 # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] for img_path in tqdm(img_paths, desc='test models'): img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) h, w = img.shape[:2] scale = long_size / max(h, w) img = cv2.resize(img, None, fx=scale, fy=scale) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale) scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) if len(boxes_list): boxes_list = boxes_list / scale np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # 开始计算 recall precision f1 methodHmean, methodPrecision, methodRecall = getresult( save_path, config.gt_name) print("precision: {} , recall: {}, f1: {}".format(methodPrecision, methodRecall, methodHmean))
def valid_one_epoch(config, psenet, pseloss, optimizer, schedular, writer, epoch): # pbar = tqdm(total=100) image_names, labels = load_imagename_labels(config.DATASET.LABEL_VAL_ROOT) len_images = len(image_names) long_edge = 2240 save_path = '/home/simple/mydemo/ocr_project/pytorch_psenet_project/pytorch_psenet/results' if os.path.exists(save_path) == False: os.mkdir(save_path) gt_path = config.DATASET.LABEL_VAL_ROOT for i in tqdm.tqdm(range(0, len_images)): image_name = image_names[i] + '.jpg' image = cv2.imread( os.path.join(config.DATASET.IMAGE_VAL_ROOT, image_name)) image = cv2.cvtColor(image, code=cv2.COLOR_BGR2RGB) image_show = image.copy() # cv2.imshow('ttt',image_show) h, w, c = image.shape ratio = long_edge / max(h, w) image = cv2.resize(image, dsize=None, fx=ratio, fy=ratio) # image=torch.Tensor(image,dtype=torch.float32) image = torchvision.transforms.ToTensor()(image).unsqueeze(0).to( torch.device('cuda:' + config.CUDA.GPU)) save_name = os.path.join(save_path, 'res_' + image_names[i] + '.txt') # put the batch dataset to GPU # images = images.to(torch.device('cuda:' + config.CUDA.GPU)) # text_kernel_masks = text_kernel_masks.to(torch.device('cuda:' + config.CUDA.GPU)) # train_masks = train_masks.to(torch.device('cuda:' + config.CUDA.GPU)) # the model forward with torch.no_grad(): output = psenet(image, True) preds, boxes_list = pse_decode(output[0], config.VALID.SCALE) scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) if len(boxes_list): boxes_list = boxes_list / scale np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') result_dict = cal_recall_precison_f1(gt_path, save_path) print(result_dict) return result_dict['recall'], result_dict['precision'], result_dict[ 'hmean']
def eval(model, save_path, test_path, device): model.eval() img_path = os.path.join(test_path, 'val', 'image') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) long_size = 2240 # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] for img_path in tqdm(img_paths, desc='test models'): img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) org_img = img.copy() h, w = img.shape[:2] #if max(h, w) > long_size: # scale = long_size / max(h, w) # img = cv2.resize(img, None, fx=scale, fy=scale) img = scale_image(img) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale, org_img, config.is_test) scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) if len(boxes_list): boxes_list = boxes_list / scale np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # 开始计算 recall precision f1 f_score_new = getresult(save_path, config.gt_name) return f_score_new
def eval(model, save_path, test_path, device): model.eval() # torch.cuda.empty_cache() # speed up evaluating after training finished img_path = os.path.join(test_path, 'img') gt_path = os.path.join(test_path, 'gt') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) long_size = 2240 # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] # img_paths = img_paths[:2] for img_path in tqdm(img_paths, desc='test models'): img_name = os.path.basename(img_path).split('.')[0] save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) h, w = img.shape[:2] # if max(h, w) > long_size: scale = long_size / max(h, w) img = cv2.resize(img, None, fx=scale, fy=scale) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale) scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) if len(boxes_list): boxes_list = boxes_list / scale np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # 开始计算 recall precision f # print('222', save_path) result_dict = cal_recall_precison_f1(gt_path, save_path) # print('111', result_dict) return result_dict['recall'], result_dict['precision'], result_dict[ 'hmean']
def merge_eval(model, save_path, test_path, device, base_path=r'', use_sub=True): def get_filename_map(json_file): merge_images_ids_to_corner = { image_info["id"]: [] for image_info in json_file["images"] } merge_filename_to_info = { image_info["filename"]: image_info for image_info in json_file["images"] } for sub_image_info in json_file["sub_images"]: merge_images_ids_to_corner[sub_image_info["id"]].append( sub_image_info["corner"]) merge_filename_to_corner = { image_info["filename"]: merge_images_ids_to_corner[image_info["id"]] for image_info in json_file["images"] } return merge_filename_to_corner, merge_filename_to_info from utils.metric.metrics import GetScore import json model.eval() metric_cls = GetScore() get_polygon = GetPolygon() raw_metrics = [] # torch.cuda.empty_cache() # speed up evaluating after training finished with open(test_path) as f: gt_file = json.load(f) if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) # 预测所有测试图片 imgs_name = [x['filename'] for x in gt_file["images"]] filename_to_corner, filename_to_info = get_filename_map(gt_file) for img_name in tqdm(imgs_name, desc="test models"): img_path = os.path.join(base_path + 'img/', img_name) assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) h, w = img.shape[:2] s = 0 if use_sub: merge_pred = torch.zeros([config.n, h, w], requires_grad=False) for corner in filename_to_corner[img_name]: sub_im = img[corner[1]:corner[3], corner[0]:corner[2]] # h, w = sub_im.shape[:2] # scale = long_size / max(h, w) # sub_im = cv2.resize(sub_im, None, fx=scale, fy=scale) # 将图片由(w,h)变为(1,img_channel,h,w) cv2.rectangle(img, (corner[0], corner[1]), (corner[2], corner[3]), (255, s, 0), 5) s += 20 tensor = transforms.ToTensor()(sub_im) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) merge_pred[:, corner[1]:corner[3], corner[0]:corner[2]] = preds[0] preds = merge_pred else: # long_size = 3456 # scale = long_size / max(h, w) # img = cv2.resize(img, None, fx=scale, fy=scale) # 将图片由(w,h)变为(1,img_channel,h,w) tensor = transforms.ToTensor()(img) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds = preds[0] preds = pse_decode(preds) m = (preds * 100).astype(np.uint8) m = cv2.resize(m, (1280, 720)) cv2.imshow("figure", img) cv2.waitKey(0) cv2.destroyWindow() preds[preds > 0] = 1 pred_polygon = get_polygon(pred_map=preds, dest_shape=[h, w]) raw_metrics.append( metric_cls(pred_polys=pred_polygon, ignore_tags=np.zeros(len( filename_to_info[img_name]["polygon"], ), dtype=np.bool), polys=filename_to_info[img_name]["polygon"])) metrics = metric_cls.gather_measure(raw_metrics) return metrics['recall'].avg, metrics['precision'].avg, metrics[ 'fmeasure'].avg
def eval(model, workspace, test_path, kern_size_, device): model.eval() img_path = os.path.join(test_path, 'Images/Test/') gt_path = os.path.join(test_path, 'gt/Test/') save_path = os.path.join(workspace, 'output') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) vis_ctw1500 = os.path.join(workspace, 'vis_ctw1500') if os.path.exists(vis_ctw1500): shutil.rmtree(vis_ctw1500, ignore_errors=True) if not os.path.exists(vis_ctw1500): os.makedirs(vis_ctw1500) img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] gt_paths = [ os.path.join(gt_path, 'poly_gt_' + x.split('.')[0] + '.mat') for x in os.listdir(img_path) ] for idx, img_path in enumerate(tqdm(img_paths, desc='test models')): img_name = os.path.basename(img_path).split('.')[0] # 读取gt gt_path_one = gt_paths[idx] gt_box = get_bboxes(gt_path_one) assert os.path.exists(img_path), 'file is not exists' img = cv2.imread(img_path) org_img = img.copy() img = scale_aligned_short(img) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds_1 = torch.sigmoid(preds[0]) preds_1 = preds_1.detach().cpu().numpy() preds, boxes_list = pse_decode(preds[0], config.scale, org_img, kern_size_) if config.visualization: for bbox in boxes_list: bbox = np.array(bbox, np.int) cv2.drawContours(org_img, [bbox.reshape(int(bbox.shape[0] / 2), 2)], -1, (0, 255, 0), 2) for bbox in gt_box: bbox = np.array(bbox, np.int) cv2.drawContours(org_img, [bbox.reshape(int(bbox.shape[0] / 2), 2)], -1, (0, 0, 255), 2) org_img = cv2.resize(org_img, (640, 640)) image_list = [] image_list.append(org_img) for i in range(7): score = (preds_1[i] * preds_1[-1]).copy().astype(np.float32) score = cv2.resize(score, (640, 640)) score = np.expand_dims(score, -1) score = np.concatenate((score, score, score), -1) image_list.append(score * 255) debug(idx, img_path, [image_list], vis_ctw1500) image_name = img_path.split('/')[-1].split('.')[0] write_result_as_txt(image_name, boxes_list, save_path) # recall precision f1 gt_path = os.path.join(test_path, 'gt/Test') fid_path = os.path.join(workspace, 'res_tt.txt') shutil.rmtree(fid_path, ignore_errors=True) precision, recall, hmean = evl_totaltext(save_path, gt_path, fid_path) # f_score_new = getresult(save_path,config.gt_name) return precision, recall, hmean
def eval(model, workspace, test_path, is_test, device): model.eval() # torch.cuda.empty_cache() # speed up evaluating after training finished if is_test: img_path = os.path.join(test_path, 'test') save_path = os.path.join(workspace, 'output_test') else: img_path = os.path.join(test_path, 'val', 'image') save_path = os.path.join(workspace, 'output') gt_path = os.path.join(test_path, 'gt/Test/') if os.path.exists(save_path): shutil.rmtree(save_path, ignore_errors=True) if not os.path.exists(save_path): os.makedirs(save_path) vis_icd17 = os.path.join(workspace, 'vis_icd17') if os.path.exists(vis_icd17): shutil.rmtree(vis_icd17, ignore_errors=True) if not os.path.exists(vis_icd17): os.makedirs(vis_icd17) short_size = 1600 # 预测所有测试图片 img_paths = [os.path.join(img_path, x) for x in os.listdir(img_path)] gt_paths = [ os.path.join(gt_path, 'poly_gt_' + x.split('.')[0] + '.mat') for x in os.listdir(img_path) ] for idx, img_path_one in enumerate(tqdm(img_paths, desc='test models')): img_name = os.path.basename(img_path_one).split('.')[0] if is_test: save_name = os.path.join( save_path, 'res_' + img_name.split('ts_')[-1] + '.txt') else: save_name = os.path.join(save_path, 'res_' + img_name + '.txt') assert os.path.exists(img_path_one), 'file is not exists' img = readImg(img_path_one) org_img = img.copy() h, w = img.shape[:2] img = scale_image(img, short_size) tensor = transforms.ToTensor()(img) tensor = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(tensor) tensor = tensor.unsqueeze_(0) tensor = tensor.to(device) with torch.no_grad(): preds = model(tensor) preds, boxes_list = pse_decode(preds[0], config.scale, org_img, is_test) # scale = (preds.shape[1] * 1.0 / w, preds.shape[0] * 1.0 / h) # if len(boxes_list): # boxes_list = boxes_list / scale if is_test: np.savetxt(save_name, boxes_list.reshape(-1, 9), delimiter=',', fmt='%d') else: np.savetxt(save_name, boxes_list.reshape(-1, 8), delimiter=',', fmt='%d') # recall precision f1 if not is_test: f_score_new = getresult(save_path, config.gt_name) return f_score_new