def val(val_loader, model, epoch): model.eval() val_psnr = 0 for iteration, batch in enumerate(val_loader, 1): input, HR = Variable(batch[0], volatile=True), Variable(batch[1]) if opt.cuda: input = input.cuda() HR = HR.cuda() SR = model(input) val_psnr += PSNR(SR.cpu().data[0].numpy(), HR.cpu().data[0].numpy()) val_psnr = val_psnr / len(val_loader) print("PSNR = {:.3f}".format(val_psnr)) if opt.show: writer.add_scalar('Val/PSNR', val_psnr, epoch)
def main(): input_path = '/media/hdisk/liqiang/hyperSR/test/' + opt.datasetName + '/' + str( opt.upscale_factor) + '/' out_path = '/media/hdisk/liqiang/hyperSR/result/' + opt.datasetName + '/' + str( opt.upscale_factor) + '/' + opt.method + '/' if not os.path.exists(out_path): os.makedirs(out_path) PSNRs = [] SSIMs = [] SAMs = [] if not os.path.exists(out_path): os.makedirs(out_path) if opt.cuda: print("=> use gpu id: '{}'".format(opt.gpus)) os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpus if not torch.cuda.is_available(): raise Exception( "No GPU found or Wrong gpu id, please run without --cuda") model = SFCSR(opt) if opt.cuda: model = nn.DataParallel(model).cuda() checkpoint = torch.load(opt.model_name) model.load_state_dict(checkpoint['model']) model.eval() images_name = [x for x in listdir(input_path) if is_image_file(x)] T = 0 for index in range(len(images_name)): mat = scio.loadmat(input_path + images_name[index]) hyperLR = mat['LR'].astype(np.float32).transpose(2, 0, 1) HR = mat['HR'].astype(np.float32).transpose(2, 0, 1) input = Variable(torch.from_numpy(hyperLR).float(), volatile=True).contiguous().view( 1, -1, hyperLR.shape[1], hyperLR.shape[2]) SR = np.array(HR).astype(np.float32) if opt.cuda: input = input.cuda() localFeats = [] start = time.time() for i in range(input.shape[1]): if i == 0: x = input[:, 0:3, :, :] y = input[:, 0, :, :] elif i == input.shape[1] - 1: x = input[:, i - 2:i + 1, :, :] y = input[:, i, :, :] else: x = input[:, i - 1:i + 2, :, :] y = input[:, i, :, :] output, localFeats = model(x, y, localFeats, i) SR[i, :, :] = output.cpu().data[0].numpy() end = time.time() print(end - start) T = T + (end - start) SR[SR < 0] = 0 SR[SR > 1.] = 1. psnr = PSNR(SR, HR) ssim = SSIM(SR, HR) sam = SAM(SR, HR) PSNRs.append(psnr) SSIMs.append(ssim) SAMs.append(sam) SR = SR.transpose(1, 2, 0) HR = HR.transpose(1, 2, 0) scio.savemat(out_path + images_name[index], {'HR': HR, 'SR': SR}) print( "===The {}-th picture=====PSNR:{:.3f}=====SSIM:{:.4f}=====SAM:{:.3f}====Name:{}" .format(index + 1, psnr, ssim, sam, images_name[index])) print("=====averPSNR:{:.3f}=====averSSIM:{:.4f}=====averSAM:{:.3f}".format( np.mean(PSNRs), np.mean(SSIMs), np.mean(SAMs))) print T / len(images_name)
def val(val_loader, model, epoch): model.eval() val_psnr = 0 val_ssim = 0 val_sam = 0 for iteration, batch in enumerate(val_loader, 1): input, bicu, HR = Variable(batch[0], volatile=True), Variable( batch[1], volatile=True), Variable(batch[2]) SR = np.zeros( (HR.shape[1], HR.shape[2], HR.shape[3])).astype(np.float32) first_state = None if opt.cuda: input = input.cuda() bicu = bicu.cuda() neigbor_l = [] neigbor_r = [] for i in range(input.shape[1]): if i == 0: neigbor_l.append(input[:, 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 4, :, :].data.unsqueeze(1)) elif i == 1: neigbor_l.append(input[:, 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, 0, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 4, :, :].data.unsqueeze(1)) elif i == input.shape[1] - 2: neigbor_l.append(input[:, i - 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 2, :, :].data.unsqueeze(1)) elif i == input.shape[1] - 1: neigbor_l.append(input[:, i - 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 4, :, :].data.unsqueeze(1)) else: neigbor_l.append(input[:, i - 2, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 2, :, :].data.unsqueeze(1)) x = input[:, i, :, :] single_bicu = bicu[:, i, :, :] left = Variable(torch.cat(neigbor_l, 1)) right = Variable(torch.cat(neigbor_r, 1)) output, first_state = model(i, x, single_bicu, left, right, first_state) SR[i, :, :] = output.cpu().data[0].numpy() val_psnr += PSNR(SR, HR.data[0].numpy()) val_ssim += SSIM(SR, HR.data[0].numpy()) val_sam += SAM(SR, HR.data[0].numpy()) print("PSNR = {:.3f} SSIM = {:.4F} SAM = {:.3f}".format( val_psnr / len(val_loader), val_ssim / len(val_loader), val_sam / len(val_loader))) if opt.show: writer.add_scalar('Val/PSNR', val_psnr, epoch)
def main(): input_path = '/media/hdisk/liqiang/hyperSR/test/' + opt.datasetName + '/' + str( opt.upscale_factor) + '/' out_path = '/media/hdisk/liqiang/hyperSR/result/' + opt.datasetName + '/' + str( opt.upscale_factor) + '/' + opt.method + '/' if not os.path.exists(out_path): os.makedirs(out_path) PSNRs = [] SSIMs = [] SAMs = [] if not os.path.exists(out_path): os.makedirs(out_path) if opt.cuda: print("=> use gpu id: '{}'".format(opt.gpus)) os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpus if not torch.cuda.is_available(): raise Exception( "No GPU found or Wrong gpu id, please run without --cuda") model = pre_Net(opt) if opt.cuda: model = nn.DataParallel(model).cuda() checkpoint = torch.load(opt.model_name) model.load_state_dict(checkpoint['model']) model.eval() images_name = [x for x in listdir(input_path) if is_image_file(x)] for index in range(len(images_name)): mat = scio.loadmat(input_path + images_name[index]) LR = mat['LR'].astype(np.float32).transpose(2, 0, 1) HR = mat['HR'].astype(np.float32).transpose(2, 0, 1) bic = np.zeros(HR.shape, dtype=np.float32) for i in range(bic.shape[0]): bic[i, :, :] = imresize(LR[i, :, :], (bic.shape[1], bic.shape[2]), 'bicubic', mode='F') bicu = Variable(torch.from_numpy(bic).float(), volatile=True).contiguous().view( 1, -1, bic.shape[1], bic.shape[2]) input = Variable(torch.from_numpy(LR).float(), volatile=True).contiguous().view( 1, -1, LR.shape[1], LR.shape[2]) SR = np.array(HR).astype(np.float32) first_state = None if opt.cuda: input = input.cuda() bicu = bicu.cuda() neigbor_l = [] neigbor_r = [] for i in range(input.shape[1]): if i == 0: neigbor_l.append(input[:, 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 4, :, :].data.unsqueeze(1)) elif i == 1: neigbor_l.append(input[:, 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, 0, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, 4, :, :].data.unsqueeze(1)) elif i == input.shape[1] - 2: neigbor_l.append(input[:, i - 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 2, :, :].data.unsqueeze(1)) elif i == input.shape[1] - 1: neigbor_l.append(input[:, i - 3, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 2, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i - 4, :, :].data.unsqueeze(1)) else: neigbor_l.append(input[:, i - 2, :, :].data.unsqueeze(1)) neigbor_l.append(input[:, i - 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 1, :, :].data.unsqueeze(1)) neigbor_r.append(input[:, i + 2, :, :].data.unsqueeze(1)) x = input[:, i, :, :] single_bicu = bicu[:, i, :, :] left = Variable(torch.cat(neigbor_l, 1)) right = Variable(torch.cat(neigbor_r, 1)) output, first_state = model(i, x, single_bicu, left, right, first_state) SR[i, :, :] = output.cpu().data[0].numpy() SR[SR < 0] = 0 SR[SR > 1.] = 1. psnr = PSNR(SR, HR) ssim = SSIM(SR, HR) sam = SAM(SR, HR) PSNRs.append(psnr) SSIMs.append(ssim) SAMs.append(sam) SR = SR.transpose(1, 2, 0) HR = HR.transpose(1, 2, 0) scio.savemat(out_path + images_name[index], {'HR': HR, 'SR': SR}) print( "===The {}-th picture=====PSNR:{:.3f}=====SSIM:{:.4f}=====SAM:{:.3f}====Name:{}" .format(index + 1, psnr, ssim, sam, images_name[index])) print("=====averPSNR:{:.3f}=====averSSIM:{:.4f}=====averSAM:{:.3f}".format( np.mean(PSNRs), np.mean(SSIMs), np.mean(SAMs)))
def main(): input_path = '/mnt/liguanlin/DataSets/hypserdatasets/CAVE/test/2/' out_path = '/mnt/liguanlin/DataSets/hypserdatasets/CAVE/result/2/' PSNRs = [] SSIMs = [] SAMs = [] if not os.path.exists(out_path): os.makedirs(out_path) if opt.cuda: print("=> use gpu id: '{}'".format(opt.gpus)) os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpus if not torch.cuda.is_available(): raise Exception( "No GPU found or Wrong gpu id, please run without --cuda") model = MCNet(opt) model.eval() if opt.cuda: model = nn.DataParallel(model).cuda() checkpoint = torch.load(opt.model_name) model.load_state_dict(checkpoint["model"]) images_name = [x for x in listdir(input_path) if is_image_file(x)] for index in range(len(images_name)): mat = scio.loadmat(input_path + images_name[index]) hyperLR = mat['LR'].transpose(2, 0, 1).astype(np.float32) input = Variable(torch.from_numpy(hyperLR).float(), volatile=True).contiguous().view( 1, -1, hyperLR.shape[1], hyperLR.shape[2]) if opt.cuda: input = input.cuda() with torch.no_grad(): #加上这一句,否则会出现gpu内存不足的提示 output = model(input) HR = mat['HR'].transpose(2, 0, 1).astype(np.float32) SR = output.cpu().data[0].numpy().astype(np.float32) SR[SR < 0] = 0 SR[SR > 1.] = 1. print('SR.shape:', SR.shape) print('HR.shape:', HR.shape) psnr = PSNR(SR, HR) ssim = SSIM(SR, HR) sam = SAM(SR, HR) PSNRs.append(psnr) SSIMs.append(ssim) SAMs.append(sam) SR = SR.transpose(1, 2, 0) HR = HR.transpose(1, 2, 0) scio.savemat(out_path + images_name[index], {'HR': HR, 'SR': SR}) print( "===The {}-th picture=====PSNR:{:.3f}=====SSIM:{:.4f}=====SAM:{:.3f}====Name:{}" .format( index + 1, psnr, ssim, sam, images_name[index], )) print("=====averPSNR:{:.3f}=====averSSIM:{:.4f}=====averSAM:{:.3f}".format( np.mean(PSNRs), np.mean(SSIMs), np.mean(SAMs)))