def test(model, path): ##### put ur data_path of TestDataSet/Kvasir here ##### data_path = path ##### ##### model.eval() image_root = '{}/images/'.format(data_path) gt_root = '{}/masks/'.format(data_path) test_loader = test_dataset(image_root, gt_root, 352) b = 0.0 for i in range(100): image, gt, name = test_loader.load_data() gt = np.asarray(gt, np.float32) gt /= (gt.max() + 1e-8) image = image.cuda() res = model(image) res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) input = res target = np.array(gt) N = gt.shape smooth = 1 input_flat = np.reshape(input, (-1)) target_flat = np.reshape(target, (-1)) intersection = (input_flat * target_flat) loss = (2 * intersection.sum() + smooth) / (input.sum() + target.sum() + smooth) a = '{:.4f}'.format(loss) a = float(a) b = b + a return b / 100
for _data_name in [ 'CVC-300', 'CVC-ClinicDB', 'Kvasir', 'CVC-ColonDB', 'ETIS-LaribPolypDB' ]: data_path = './data/TestDataset/{}/'.format(_data_name) save_path = './results/PraNet/{}/'.format(_data_name) opt = parser.parse_args() model = PraNet() model.load_state_dict(torch.load(opt.pth_path)) model.cuda() model.eval() os.makedirs(save_path, exist_ok=True) image_root = '{}/images/'.format(data_path) gt_root = '{}/masks/'.format(data_path) test_loader = test_dataset(image_root, gt_root, opt.testsize) for i in range(test_loader.size): image, gt, name = test_loader.load_data() gt = np.asarray(gt, np.float32) gt /= (gt.max() + 1e-8) image = image.cuda() res5, res4, res3, res2 = model(image) res = res2 res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8)
parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--pth_path', type=str, default='./snapshots/PraNet-ori.pth') opt = parser.parse_args() for _data_name in ['CVC-300', 'CVC-ClinicDB', 'CVC-ColonDB', 'ETIS-LaribPolypDB', 'Kvasir']: data_path = './data/TestDataset/{}/'.format(_data_name) save_path = './results/PraNet/{}/'.format(_data_name) model = PraNet() model.load(opt.pth_path) model.eval() os.makedirs(save_path, exist_ok=True) image_root = '{}/images/'.format(data_path) gt_root = '{}/masks/'.format(data_path) test_loader = test_dataset(image_root, gt_root, opt.testsize) \ .set_attrs(batch_size=1, shuffle=False) for image, gt, name in test_loader: gt /= (gt.max() + 1e-08) (res5, res4, res3, res2) = model(image) res = res2 c, h, w = gt.shape upsample = nn.upsample(res, size=(h, w), mode='bilinear') res = res.sigmoid().data.squeeze() res = ((res - res.min()) / ((res.max() - res.min()) + 1e-08)) print('> {} - {}'.format(_data_name, name)) imageio.imwrite((save_path + name[0]), res)