-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
210 lines (183 loc) · 8.25 KB
/
utils.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
from PIL import Image
import numpy as np
RESHAPE = (256,256)
def is_an_image_file(filename):
IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.npy']
for ext in IMAGE_EXTENSIONS:
if ext in filename:
return True
return False
def list_image_files(directory):
files = os.listdir(directory)
return [os.path.join(directory, f) for f in files if is_an_image_file(f)]
def load_image(path):
img = Image.open(path)
return img
def load_image_with_C(path):
img = np.load(path)
return img
def preprocess_image(cv_img):
# cv_img = cv_img.resize(RESHAPE)
img = np.array(cv_img)
img = (img - 127.5) / 127.5
# img = (img ) / 255
return img
def deprocess_image(img):
img = img * 127.5 + 127.5
return img.astype('uint8')
def save_image(np_arr, path):
img = np_arr * 127.5 + 127.5
im = Image.fromarray(img)
im.save(path)
def load_images(path, n_images, istrain = True):
sub = True
images_A, images_B, images_C = [], [], []
images_A_paths, images_B_paths, images_C_paths = [], [], []
for root, dirs, files in os.walk(path):
if sub:
for classes in dirs:
path_class = os.path.join(root,classes)
if istrain:
A_paths, B_paths, C_paths = os.path.join(path_class, 'A_train'), os.path.join(path_class, 'B_train'), os.path.join(path_class, 'C_train')
else:
A_paths, B_paths, C_paths = os.path.join(path_class, 'A_test'), os.path.join(path_class, 'B_test'), os.path.join(path_class, 'C_test')
all_A_paths, all_B_paths, all_C_paths = list_image_files(A_paths), list_image_files(B_paths), list_image_files(C_paths)
for path_A, path_B, path_C, i in zip(all_A_paths, all_B_paths, all_C_paths, range(len(all_A_paths))):
img_A, img_B, img_C = load_image(path_A), load_image(path_B), load_image_with_C(path_C)
images_A.append(preprocess_image(img_A))
images_B.append(preprocess_image(img_B))
images_C.append((img_C))
images_A_paths.append(path_A)
images_B_paths.append(path_B)
images_C_paths.append(path_C)
if i+1 >= n_images : break
sub = False
return {
'A': np.array(images_A),
'A_paths': np.array(images_A_paths),
'B': np.array(images_B),
'B_paths': np.array(images_B_paths),
'C': np.array(images_C),
'C_paths': np.array(images_C_paths)
}
def load_images_with_C(path, n_images, istrain = True):
sub = True
images_A, images_C = [], []
images_A_paths, images_C_paths = [], []
for root, dirs, files in os.walk(path):
if sub:
for classes in dirs:
path_class = os.path.join(root,classes)
if istrain:
A_paths, C_paths = os.path.join(path_class, 'A_train'), os.path.join(path_class, 'C_train')
else:
A_paths, C_paths = os.path.join(path_class, 'A_test'), os.path.join(path_class, 'C_test')
all_A_paths, all_C_paths = list_image_files(A_paths), list_image_files(C_paths)
for path_A, path_C, i in zip(all_A_paths, all_C_paths, range(len(all_A_paths))):
img_A, img_C = load_image(path_A), load_image_with_C(path_C)
images_A.append(preprocess_image(img_A))
images_C.append((img_C))
images_A_paths.append(path_A)
images_C_paths.append(path_C)
if i+1 >= n_images : break
sub = False
return {
'A': np.array(images_A),
'A_paths': np.array(images_A_paths),
'C': np.array(images_C),
'C_paths': np.array(images_C_paths)
}
def load__class_images(path, n_images, istrain = False):
if istrain:
A_paths, B_paths = os.path.join(path, 'A_train'), os.path.join(path, 'B_train')
else:
A_paths, B_paths = os.path.join(path, 'A_test'), os.path.join(path, 'B_test')
all_A_paths, all_B_paths = list_image_files(A_paths), list_image_files(B_paths)
images_A, images_B = [], []
images_A_paths, images_B_paths = [], []
for path_A, path_B in zip(all_A_paths, all_B_paths):
img_A, img_B = load_image(path_A), load_image(path_B)
images_A.append(preprocess_image(img_A))
images_B.append(preprocess_image(img_B))
images_A_paths.append(path_A)
images_B_paths.append(path_B)
if len(images_A) > n_images - 1: break
return {
'A': np.array(images_A),
'A_paths': np.array(images_A_paths),
'B': np.array(images_B),
'B_paths': np.array(images_B_paths)
}
def load__class_images_with_C(path, n_images, istrain = False):
if istrain:
A_paths, B_paths, C_paths = os.path.join(path, 'A_train'), os.path.join(path, 'B_train'), os.path.join(path, 'C_train')
else:
A_paths, B_paths, C_paths = os.path.join(path, 'A_test'), os.path.join(path, 'B_test'), os.path.join(path, 'C_test')
all_A_paths, all_B_paths, all_C_paths = list_image_files(A_paths), list_image_files(B_paths), list_image_files(C_paths)
images_A, images_B, images_C = [], [], []
images_A_paths, images_B_paths, images_C_paths = [], [], []
for path_A, path_B, path_C in zip(all_A_paths, all_B_paths, all_C_paths):
img_A, img_B, img_C = load_image(path_A), load_image(path_B), load_image_with_C(path_C)
images_A.append(preprocess_image(img_A))
images_B.append(preprocess_image(img_B))
images_C.append((img_C))
images_A_paths.append(path_A)
images_B_paths.append(path_B)
images_C_paths.append(path_C)
if len(images_A) > n_images - 1: break
return {
'A': np.array(images_A),
'A_paths': np.array(images_A_paths),
'B': np.array(images_B),
'B_paths': np.array(images_B_paths),
'C': np.array(images_C),
'C_paths': np.array(images_C_paths)
}
def load__attitude_images(path, n_images):
A_paths, B_paths = os.path.join(path, 'A_test_attitude'), os.path.join(path, 'B_test_attitude')
all_A_paths, all_B_paths = list_image_files(A_paths), list_image_files(B_paths)
images_A, images_B = [], []
images_A_paths, images_B_paths = [], []
for path_A, path_B in zip(all_A_paths, all_B_paths):
img_A, img_B = load_image(path_A), load_image(path_B)
images_A.append(preprocess_image(img_A))
images_B.append(preprocess_image(img_B))
images_A_paths.append(path_A)
images_B_paths.append(path_B)
if len(images_A) > n_images - 1: break
return {
'A': np.array(images_A),
'A_paths': np.array(images_A_paths),
'B': np.array(images_B),
'B_paths': np.array(images_B_paths)
}
def load_jitter(path, n_images):
path = os.path.join(path, 'jitter')
image_list = list_image_files(path)
jitter_all = []
for path in image_list:
jitter = np.load(path)
jitter_all.append(jitter)
if len(jitter_all) > n_images - 1:break
return jitter_all
if __name__ == '__main__':
path = '..\dataset\image_deform\\'
n_images = 10
sub = True
images_A, images_B = [], []
images_A_paths, images_B_paths = [], []
for root, dirs, files in os.walk(path):
if sub:
for classes in dirs:
path_class = os.path.join(root,classes)
A_paths, B_paths = os.path.join(path_class, 'A'), os.path.join(path_class, 'B')
all_A_paths, all_B_paths = list_image_files(A_paths), list_image_files(B_paths)
for path_A, path_B, i in zip(all_A_paths, all_B_paths, range(len(all_A_paths))):
img_A, img_B = load_image(path_A), load_image(path_B)
images_A.append(preprocess_image(img_A))
images_B.append(preprocess_image(img_B))
images_A_paths.append(path_A)
images_B_paths.append(path_B)
if i+1 >= n_images : break
sub = False