def validation(model, data_path): model.eval() image_root = f'{data_path}/images/' gt_root = f'{data_path}/masks/' validation_loader = ValidationDataset(image_root, gt_root, 352) b = 0.0 for i in range(validation_loader.size): image, gt, name = validation_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=True) 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 / validation_loader.size
pth_path = config['Paths']['effnetv2pd_pth_path'] result_path = config['Paths']['effnetv2pd_result_path'] model.load_state_dict(torch.load(pth_path)) model.cuda() model.eval() for _data_name in [ 'CVC-300', 'CVC-ClinicDB', 'Kvasir', 'CVC-ColonDB', 'ETIS-LaribPolypDB' ]: data_path = f'{test_path}/{_data_name}' save_path = f'{result_path}/{_data_name}/' os.makedirs(save_path, exist_ok=True) image_root = '{}/images/'.format(data_path) gt_root = '{}/masks/'.format(data_path) test_loader = ValidationDataset(image_root, gt_root, test_size) 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() if model_name == 'pranet' or model_name == 'prahardnet': _, _, _, res = model(image) elif model_name == 'hardmseg' or model_name == 'resnetpd' or model_name == 'effnetv2pd': res = model(image) elif model_name == 'hardcpd' or model_name == 'resnetcpd' or model_name == 'effnetv2cpd': _, res = model(image) res = F.upsample(res, size=gt.shape,