Exemplo n.º 1
0
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))
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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