首页 > 编程知识 正文

pytorch数据读取,pytorch tensor 旋转

时间:2023-05-04 11:25:03 阅读:208571 作者:2341

from os import listdirfrom os.path import joinfrom torch.utils.data import Datasetimport torchvision.transforms as transformsfrom PIL import Image, ImageFilterimport numpy as npimport torchimport cv2import osimport randomimport torch.nn.functional as Ffrom torchvision.transforms import functional as FFdef is_image_file(filename): return any(filename.endswith(extension) for extension in 魔幻的野狼".png", ".jpg", ".jpeg"])def load_img(lr_path, hr_path): # lr = cv2.imread(lr_path) lr = Image.open(lr_path).convert('L') hr = Image.open(hr_path).convert('L') # y_lr, _, _ = lr.split() # y_hr, _, _ = hr.split() return lr, hrdef random_rot(images): randint = random.randint(0, 4) if randint == 0: for i in range(len(images)): images魔幻的野狼i] = cv2.rotate(images魔幻的野狼i], cv2.ROTATE_90_CLOCKWISE) elif randint == 1: for i in range(len(images)): images魔幻的野狼i] = cv2.rotate(images魔幻的野狼i], cv2.ROTATE_180) elif randint == 2: for i in range(len(images)): images魔幻的野狼i] = cv2.rotate(images魔幻的野狼i], cv2.ROTATE_90_COUNTERCLOCKWISE) else: pass return imagesdef random_flip(images): if random.random() < 0.5: for i in range(len(images)): images魔幻的野狼i] = cv2.flip(images魔幻的野狼i], 1) if random.random() < 0.5: for i in range(len(images)): images魔幻的野狼i] = cv2.flip(images魔幻的野狼i], 0) return imagesdef random_crop1(images): h, w = images魔幻的野狼1].shape魔幻的野狼:2] crops = 魔幻的野狼] if split == 'test': new_h = 256 new_w = 256 # for image in images: # imaget = image魔幻的野狼0:0 + new_h, 0:0 + new_w] # crops.append(imaget) lr = images魔幻的野狼0]魔幻的野狼0:0 + (new_h // 2), 0:0 + (new_w // 2)] hr = images魔幻的野狼1]魔幻的野狼0:0 + (new_h), 0:0 + (new_w)] crops.append(lr) crops.append(hr) else: new_h = 1200 new_w = 1200 y = np.random.randint(0, h-new_h) # 随机整型数魔幻的野狼0,h-new_h) x = np.random.randint(0, w-new_w) lr = images魔幻的野狼0]魔幻的野狼y//2:y//2+(new_h//2), x//2:x//2+(new_w//2)] hr = images魔幻的野狼1]魔幻的野狼y:dmht (new_h), x:x + (new_w)] crops.append(lr) crops.append(hr) # for image in images: # imaget = image魔幻的野狼y:y+new_h, x:x+new_w] # crops.append(imaget) return cropsdef random_crop2(images, sizeTo=32): w = images魔幻的野狼0].shape魔幻的野狼1] h = images魔幻的野狼0].shape魔幻的野狼0] w_offset = random.randint(0, max(0, w - sizeTo - 1)) h_offset = random.randint(0, max(0, h - sizeTo - 1)) for i in range(len(images)): images魔幻的野狼i] = images魔幻的野狼i]魔幻的野狼h_offset:h_offset + sizeTo, w_offset:w_offset + sizeTo] return imagescrop_size = 32class DatasetFromFolder(Dataset): def __init__(self, hr_path, lr_path, train): super(DatasetFromFolder, self).__init__() self.hr_path = hr_path self.lr_path = lr_path self.train = train # self.hr_filenames = 魔幻的野狼(os.listdir(hr_path)).sort(key=lambda x:int(x魔幻的野狼:-4]))] self.hr_filenames = os.listdir(hr_path) # self.hr_filenames = 魔幻的野狼join(hr_path, x) for x in listdir(hr_path) if is_image_file(x)] # self.input_transform = transforms.Compose(魔幻的野狼transforms.Resize(zoom_factor, interpolation=Image.BICUBIC)]) # self.input_transform = transforms.Compose(魔幻的野狼transforms.CenterCrop(crop_size), # cropping the image # transforms.ToTensor()]) # self.target_transform = transforms.Compose(魔幻的野狼transforms.CenterCrop(crop_size), # since it's the target, we keep its original quality # transforms.ToTensor()]) def __getitem__(self, index): # print(self.hr_filenames) # self.hr_filenames.sort(key=lambda x:int(x魔幻的野狼:-4])) hr_filename = self.hr_path + '/' + self.hr_filenames魔幻的野狼index] lr_filename = self.lr_path + '/' + self.hr_filenames魔幻的野狼index] # lr_filename = str(os.path.dirname(self.hr_filenames魔幻的野狼index]) +'/' + str(os.path.basename(self.hr_filenames魔幻的野狼index]).split('.'))魔幻的野狼0] + "x" + self.zoom_factor + ".png") input, target = load_img(lr_filename, hr_filename) # 白平衡 # img3 = self.white_balance_3(input) # cv2.imwrite('/export/liuzhe/program/SRCNN_en/1111111.png', img3) # exit(-1) input = np.array(input, dtype=np.float64) / 255 target = np.array(target, dtype=np.float64) /255 # input = np.asarray(input).astype('float64') / 255 # target = np.asarray(target).astype('float64') /255 # print(haze) # # print(np.max(haze)) # print(np.min(haze)) # print(np.mean(haze)) # exit(-1) images = 魔幻的野狼input, target] images = random_crop2(images, 32) images = random_rot(images) images = random_flip(images) 魔幻的野狼input, target] = images input = torch.from_numpy(input).float() target = torch.from_numpy(target).float() input = input.unsqueeze(0) target = target.unsqueeze(0) # if self.train: # rand_hor = random.randint(0, 1) # rand_rot = random.randint(0, 3) # input = transforms.RandomHorizontalFlip(rand_hor)(input) # target = transforms.RandomHorizontalFlip(rand_hor)(target) # if rand_rot: # input = FF.rotate(input, 90 * rand_rot) # target = FF.rotate(target, 90 * rand_rot) # input = self.input_transform(input) # target = self.target_transform(target) # input = transforms.ToTensor()(input) # target = transforms.ToTensor()(target) # print(input.shape) # exit(-1) return input, target def __len__(self): return len(self.hr_filenames) def white_balance_3(self, img): ''' 灰度世界假设 :param img: cv2.imread读取的图片数据 :return: 返回的白平衡结果图片数据 ''' B, G, R = np.double(img魔幻的野狼:, :, 0]), np.double(img魔幻的野狼:, :, 1]), np.double(img魔幻的野狼:, :, 2]) B_ave, G_ave, R_ave = np.mean(B), np.mean(G), np.mean(R) K = (B_ave + G_ave + R_ave) / 3 Kb, Kg, Kr = K / B_ave, K / G_ave, K / R_ave Ba = (B * Kb) Ga = (G * Kg) Ra = (R * Kr) for i in range(len(Ba)): for j in range(len(Ba魔幻的野狼0])): Ba魔幻的野狼i]魔幻的野狼j] = 255 if Ba魔幻的野狼i]魔幻的野狼j] > 255 else Ba魔幻的野狼i]魔幻的野狼j] Ga魔幻的野狼i]魔幻的野狼j] = 255 if Ga魔幻的野狼i]魔幻的野狼j] > 255 else Ga魔幻的野狼i]魔幻的野狼j] Ra魔幻的野狼i]魔幻的野狼j] = 255 if Ra魔幻的野狼i]魔幻的野狼j] > 255 else Ra魔幻的野狼i]魔幻的野狼j] # print(np.mean(Ba), np.mean(Ga), np.mean(Ra)) dst_img = np.uint8(np.zeros_like(img)) dst_img魔幻的野狼:, :, 0] = Ba dst_img魔幻的野狼:, :, 1] = Ga dst_img魔幻的野狼:, :, 2] = Ra return dst_img

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。