-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.py
More file actions
225 lines (163 loc) · 6.15 KB
/
utils.py
File metadata and controls
225 lines (163 loc) · 6.15 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import os
import glob
import scipy.misc
import scipy.ndimage
import scipy.signal
import numpy as np
import tensorflow as tf
try:
xrange
except:
xrange = range
w_l,h_l,c_l = 0,0,0
FLAGS = tf.app.flags.FLAGS
i=1
def fillzero(input,w,h):
if w > h:
pad = int((w - h) /2)
zeros = [[[0.]*3 for y in range(pad)] for x in range(w)]
input = np.hstack((zeros,input,zeros))
if h > w:
pad = int((h - w) /2)
zeros = [[[0.]*3 for y in range(h)] for x in range(pad)]
input = np.vstack((zeros,input,zeros))
return input
def preprocess(path,scale):
with tf.Session() as sess:
image = imread(path)
w,h,_ = np.shape(image)
if not FLAGS.is_train and max(w,h) > 400:
r = 400./max(w,h)
image = scipy.misc.imresize(image,size =(int(r*w),int(r*h)))
print("image",np.shape(image))
label_ = modcrop(image, scale)
input_ = scipy.ndimage.interpolation.zoom(label_, [(1./scale), (1./scale), 1.], prefilter=True,order=1)
input_ = scipy.ndimage.interpolation.zoom(input_, [(scale/1.), (scale/1.), 1.], prefilter=True,order=1)
label_ =label_ / 255.
input_ = input_ / 255.
if not FLAGS.is_train:
image_path = os.path.join(os.getcwd(), FLAGS.sample_dir)
path = os.path.join(image_path, "ORIGNAL.png")
imsave(input_, path)
path = os.path.join(image_path, "GROUNDTRUTH.png")
imsave(label_, path)
global w_l,h_l,c_l
w_l,h_l,c_l = np.shape(input_)
input_ = fillzero(input_, w_l, h_l)
w_l,h_l,c_l = np.shape(label_)
label_ = fillzero(label_, w_l, h_l)
else :
global i
print('read the image '+str(i)+' :', input_.shape)
i+=1
return input_, label_
def prepare_data(dataset,config):
if FLAGS.is_train:
filenames = os.listdir(dataset)
data_dir = os.path.join(os.getcwd(), dataset)
types = ["*.bmp","*.jpg","*.png","gif"]
data = []
for type in types:
data.extend(glob.glob(os.path.join(data_dir, type)))
else:
data = os.path.join(os.sep, (os.path.join(os.getcwd(), dataset)),config.testimg)
return data
def imread(path):
return scipy.misc.imread(path, mode='RGB').astype(np.float)
def modcrop(image, scale=3):
if len(image.shape) == 3:
h, w, _ = image.shape
h = h - np.mod(h, scale)
w = w - np.mod(w, scale)
image = image[0:h, 0:w, :]
else:
h, w = image.shape
h = h - np.mod(h, scale)
w = w - np.mod(w, scale)
image = image[0:h, 0:w]
return image
def input_setup(config):
"""
Read image files and make their sub-images.
"""
if config.is_train:
data = prepare_data(dataset="Train",config = config)
else:
data = prepare_data(dataset="Test",config = config)
sub_input_sequence = []
sub_label_sequence = []
padding = abs(config.image_size - config.label_size) / 2
if config.is_train :
for i in xrange(len(data)):
input_, label_ = preprocess(data[i],config.scale)
if len(input_.shape) == 3:
h, w, _ = input_.shape
else:
h, w = input_.shape
for x in range(0, h-config.image_size+1, config.stride):
for y in range(0, w-config.image_size+1, config.stride):
sub_input = input_[x:x+config.image_size, y:y+config.image_size] # [32 x 32]
sub_label = label_[x+int(padding):x+int(padding)+config.label_size, y+int(padding):y+int(padding)+config.label_size] # [22 x 22]
sub_input = sub_input.reshape([config.image_size, config.image_size, 3])
sub_label = sub_label.reshape([config.label_size, config.label_size, 3])
sub_input_sequence.append(sub_input)
sub_label_sequence.append(sub_label)
else:
input_, label_ = preprocess(data,config.scale)
if len(input_.shape) == 3:
h, w, _ = input_.shape
else:
h, w = input_.shape
nx = ny = 0
for x in range(0, h-config.image_size+1, config.stride):
nx += 1; ny = 0
for y in range(0, w-config.image_size+1, config.stride):
ny += 1
if x>2 and y > 2:
sub_input = input_[x:x+config.image_size, y:y+config.image_size] # [32 x 32]
sub_label = label_[x+int(padding):x+int(padding)+config.label_size, y+int(padding):y+int(padding)+config.label_size] # [22 x 22]
else:
sub_input = input_[x:x+config.image_size, y:y+config.image_size] # [33 x 33]
sub_label = label_[x+int(padding):x+int(padding)+config.label_size, y+int(padding):y+int(padding)+config.label_size] # [22 x 22]
sub_input = sub_input.reshape([config.image_size, config.image_size, 3])
sub_label = sub_label.reshape([config.label_size, config.label_size, 3])
sub_input_sequence.append(sub_input)
sub_label_sequence.append(sub_label)
arrdata = np.asarray(sub_input_sequence) # [?, 32, 32, 3]
arrlabel = np.asarray(sub_label_sequence) # [?, 22, 22, 3]
if not config.is_train:
return nx, ny ,arrdata,arrlabel
else:
return arrdata,arrlabel
def imsave(image, path):
scipy.misc.imsave(path, image)
def crop(images):
pw, ph, _ = np.shape(images)
global w_l, h_l, c_l
if w_l > h_l:
depad = int((ph - h_l)/2)+2
images = images[:,depad:-depad]
elif w_l < h_l:
depad = int((pw - w_l)/2)+2
images = images[depad:-depad,:]
else:
images = images[:,:,:]
return images
def merge(images, size):
h, w = images.shape[1], images.shape[2]
img = np.zeros(((h-8)*size[0], (w-8)*size[1], 3))
for idx, image in enumerate(images):
i = idx % size[0]
j = idx // size[1]
img[j*(h-8):j*(h-8)+h-8, i*(w-8):i*(w-8)+w-8, :] = image[4:-4,4:-4]
return img
'''
def merge(images, size):
h, w = images.shape[1], images.shape[2]
img = np.zeros(((h)*size[0], (w)*size[1], 3))
for idx, image in enumerate(images):
i = idx % size[0]
j = idx // size[1]
img[j*(h):j*(h)+h, i*(w):i*(w)+w, :] = image[:,:]
return img
'''