-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
177 lines (134 loc) · 6.37 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# coding=utf-8
'''
@Author: wjm
@Date: 2019-10-23 14:57:22
LastEditTime: 2021-01-19 20:57:29
@Description: file content
'''
import torch.utils.data as data
import torch, random, os
import numpy as np
from os import listdir
from os.path import join
from PIL import Image, ImageOps
from random import randrange
import torch.nn.functional as F
from torchvision.transforms import Compose, ToTensor
def is_image_file(filename):
return any(filename.endswith(extension) for extension in
['.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 'tif', 'TIF'])
def load_img(filepath):
img = Image.open(filepath)
# img = Image.open(filepath)
return img
def transform():
return Compose([
ToTensor(),
])
def rescale_img(img_in, scale):
size_in = img_in.size
new_size_in = tuple([int(x * scale) for x in size_in])
img_in = img_in.resize(new_size_in, resample=Image.BICUBIC)
return img_in
def get_patch(ms_image, lms_image, pan_image, bms_image, patch_size, scale, ix=-1, iy=-1):
(ih, iw) = lms_image.size
(th, tw) = (scale * ih, scale * iw)
patch_mult = scale # if len(scale) > 1 else 1
tp = patch_mult * patch_size
ip = tp // scale
if ix == -1:
ix = random.randrange(0, iw - ip + 1)
if iy == -1:
iy = random.randrange(0, ih - ip + 1)
(tx, ty) = (scale * ix, scale * iy)
lms_image = lms_image.crop((iy, ix, iy + ip, ix + ip))
ms_image = ms_image.crop((ty, tx, ty + tp, tx + tp))
pan_image = pan_image.crop((ty, tx, ty + tp, tx + tp))
bms_image = bms_image.crop((ty, tx, ty + tp, tx + tp))
info_patch = {
'ix': ix, 'iy': iy, 'ip': ip, 'tx': tx, 'ty': ty, 'tp': tp}
return ms_image, lms_image, pan_image, bms_image, info_patch
def augment(ms_image, lms_image, pan_image, bms_image, flip_h=True, rot=True):
info_aug = {'flip_h': False, 'flip_v': False, 'trans': False}
if random.random() < 0.5 and flip_h:
ms_image = ImageOps.flip(ms_image)
lms_image = ImageOps.flip(lms_image)
pan_image = ImageOps.flip(pan_image)
# bms_image = ImageOps.flip(bms_image)
info_aug['flip_h'] = True
if rot:
if random.random() < 0.5:
ms_image = ImageOps.mirror(ms_image)
lms_image = ImageOps.mirror(lms_image)
pan_image = ImageOps.mirror(pan_image)
# bms_image = ImageOps.mirror(bms_image)
info_aug['flip_v'] = True
if random.random() < 0.5:
ms_image = ms_image.rotate(180)
lms_image = lms_image.rotate(180)
pan_image = pan_image.rotate(180)
# bms_image = pan_image.rotate(180)
info_aug['trans'] = True
return ms_image, lms_image, pan_image, info_aug
class Data(data.Dataset):
def __init__(self, data_dir_ms, data_dir_pan, transform=transform(), upscale = 4):
super(Data, self).__init__()
self.ms_image_filenames = [join(data_dir_ms, x) for x in listdir(data_dir_ms) if is_image_file(x)]
self.pan_image_filenames = [join(data_dir_pan, x) for x in listdir(data_dir_pan) if is_image_file(x)]
self.upscale_factor = upscale
self.transform = transform
def __getitem__(self, index):
ms_image = load_img(self.ms_image_filenames[index])
pan_image = load_img(self.pan_image_filenames[index])
_, file = os.path.split(self.ms_image_filenames[index])
ms_image = ms_image.crop((0, 0, ms_image.size[0] // self.upscale_factor * self.upscale_factor,
ms_image.size[1] // self.upscale_factor * self.upscale_factor))
lms_image = ms_image.resize(
(int(ms_image.size[0] / self.upscale_factor), int(ms_image.size[1] / self.upscale_factor)), Image.BICUBIC)
pan_image = pan_image.crop((0, 0, pan_image.size[0] // self.upscale_factor * self.upscale_factor,
pan_image.size[1] // self.upscale_factor * self.upscale_factor))
if self.transform:
ms_image = self.transform(ms_image)
lms_image = self.transform(lms_image)
pan_image = self.transform(pan_image)
# bms_image = self.transform(bms_image)
return lms_image, pan_image, ms_image
def __len__(self):
return len(self.ms_image_filenames)
class Data_test(data.Dataset):
def __init__(self, data_dir_ms, data_dir_pan, transform=transform(), upscale = 4):
super(Data_test, self).__init__()
self.ms_image_filenames = [join(data_dir_ms, x) for x in listdir(data_dir_ms) if is_image_file(x)]
self.pan_image_filenames = [join(data_dir_pan, x) for x in listdir(data_dir_pan) if is_image_file(x)]
self.upscale_factor = upscale
self.transform = transform
# self.data_augmentation = cfg['data']['data_augmentation']
# self.normalize = cfg['data']['normalize']
# self.cfg = cfg
def __getitem__(self, index):
ms_image = load_img(self.ms_image_filenames[index])
pan_image = load_img(self.pan_image_filenames[index])
_, file = os.path.split(self.ms_image_filenames[index])
ms_image = ms_image.crop((0, 0, ms_image.size[0] // self.upscale_factor * self.upscale_factor,
ms_image.size[1] // self.upscale_factor * self.upscale_factor))
lms_image = ms_image.resize(
(int(ms_image.size[0] / self.upscale_factor), int(ms_image.size[1] / self.upscale_factor)), Image.BICUBIC)
pan_image = pan_image.crop((0, 0, pan_image.size[0] // self.upscale_factor * self.upscale_factor,
pan_image.size[1] // self.upscale_factor * self.upscale_factor))
# bms_image = rescale_img(lms_image, self.upscale_factor)
# if self.data_augmentation:
# ms_image, lms_image, pan_image, _ = augment(ms_image, lms_image, pan_image)
if self.transform:
ms_image = self.transform(ms_image)
lms_image = self.transform(lms_image)
pan_image = self.transform(pan_image)
# bms_image = self.transform(bms_image)
# if self.normalize:
# ms_image = ms_image * 2 - 1
# lms_image = lms_image * 2 - 1
# pan_image = pan_image * 2 - 1
# bms_image = bms_image * 2 - 1
return lms_image, pan_image,ms_image
def __len__(self):
return len(self.ms_image_filenames)