图像数据读取
input_list = sorted([os.path.join("/root/userfolder/cl/TTSR/dataset/CUFED/train/input/input", name) for name in
os.listdir("/root/userfolder/cl/TTSR/dataset/CUFED/train/input/input")])
x_ = sorted(os.listdir("/root/userfolder/cl/TTSR/dataset/CUFED/train/input/input"))# os.listdir 只能一个变量
# print(len(x_))
# for i in range(len(x_)):
# print(x_[i].split('.')[-2])
for idx in range(len(input_list)):
HR = imread(input_list[idx])
name = x_[idx].split('.')[-2]
h, w = HR.shape[:2]
### LR and LR_sr
LR_4 = np.array(Image.fromarray(HR).resize((w // 4, h // 4), Image.BICUBIC))
im_4 = Image.fromarray(LR_4)
im_4.save(name + 'x4.png')
input_list = sorted(glob.glob(os.path.join('/home/cl/ttsr/dataset/CUFED/test/CUFED5/'+'*_0.png'))) #glob必须具体到某个文件类型,不能是文件夹
cnt = 0
# print(len(x_))
# for i in range(len(x_)):
# print(x_[i].split('.')[-2])
for idx in range(len(input_list)):
HR = imread(input_list[idx])
import torch
from PIL import Image
from imageio import imread
import numpy as np
import glob
import os
import random
from torchvision import models
from imageio import imread
input_list = sorted(glob.glob('/home/cl/DRN/HR/'+'*.png'))#一定要精确到文件
x_ = sorted(os.listdir("/home/cl/DRN/HR"))
for idx in range(len(input_list)):
HR = imread(input_list[idx])
name = x_[idx].split('.')[-2]
h, w = HR.shape[:2]
### LR and LR_sr
LR_4 = np.array(Image.fromarray(HR).resize((w // 4, h // 4), Image.BICUBIC))
im_4 = Image.fromarray(LR_4)
im_4.save(name + 'x4.png')
im_HR = Image.fromarray(HR)
im_HR.save(str(cnt)+ '.png')
cnt= cnt+1
input_list = sorted(glob.glob(os.path.join('/home/cl/ttsr/dataset/CUFED/test/CUFED5/'+'*_0.png')))
cnt = 0
# print(len(x_))
# for i in range(len(x_)):
# print(x_[i].split('.')[-2])
for idx in range(len(input_list)):
HR = imread(input_list[idx])
h, w = HR.shape[:2]
h, w = h // 4 * 4, w // 4 * 4
HR = HR[:h, :w, :]
im_HR = Image.fromarray(HR)
im_HR.save(str(cnt)+ '.png')
cnt= cnt+1
class TestSet(Dataset): def __init__(self, args, ref_level='1', transform=transforms.Compose([ToTensor()])): self.input_list = sorted(glob.glob(os.path.join(args.dataset_dir, 'test/CUFED5', '*_0.png'))) self.ref_list = sorted(glob.glob(os.path.join(args.dataset_dir, 'test/CUFED5', '*_' + ref_level + '.png'))) # self.lr_sr_list = sorted(glob.glob(os.path.join(args.dataset_dir, 'test/lr_sr', '*.png'))) self.lr_sr_list = sorted(os.listdir('./dataset/CUFED/test/lr_sr')) self.lr_sr_list.sort(key=lambda x: int(x[:-4])) self.transform = transform def __len__(self): return len(self.input_list) def __getitem__(self, idx): ### HR #print('idx',idx) HR = imread(self.input_list[idx]) #print(self.input_list[idx]) h, w = HR.shape[:2] h, w = h//4*4, w//4*4 HR = HR[:h, :w, :] ### crop to the multiple of 4 # print('w h',w,h) # ### LR and LR_sr LR = np.array(Image.fromarray(HR).resize((w//4, h//4), Image.BICUBIC)) #LR_sr = np.array(Image.fromarray(LR).resize((w, h), Image.BICUBIC)) # print('LR_sr_shanfgxia sep',LR_sr.shape) LR_sr = imread('./dataset/CUFED/test/lr_sr/'+self.lr_sr_list[idx]) # print('LR_sr_duqu ', LR_sr.shape) ### Ref and Ref_sr Ref = imread(self.ref_list[idx]) h2, w2 = Ref.shape[:2] h2, w2 = h2//4*4, w2//4*4 Ref = Ref[:h2, :w2, :] Ref_sr = np.array(Image.fromarray(Ref).resize((w2//4, h2//4), Image.BICUBIC)) Ref_sr = np.array(Image.fromarray(Ref_sr).resize((w2, h2), Image.BICUBIC)) ### change type LR = LR.astype(np.float32) LR_sr = LR_sr.astype(np.float32) HR = HR.astype(np.float32) Ref = Ref.astype(np.float32) Ref_sr = Ref_sr.astype(np.float32) ### rgb range to [-1, 1] LR = LR / 127.5 - 1. LR_sr = LR_sr / 127.5 - 1. HR = HR / 127.5 - 1. Ref = Ref / 127.5 - 1. Ref_sr = Ref_sr / 127.5 - 1. sample = {'LR': LR, 'LR_sr': LR_sr, 'HR': HR, 'Ref': Ref, 'Ref_sr': Ref_sr} if self.transform: sample = self.transform(sample) return sample