forked from taki0112/SPADE-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·280 lines (199 loc) · 9.39 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import tensorflow as tf
from tensorflow.contrib import slim
from scipy import misc
import os, random
import numpy as np
from glob import glob
from tqdm import tqdm
from ast import literal_eval
import cv2
class Image_data:
def __init__(self, img_height, img_width, channels, segmap_ch, dataset_path, augment_flag):
self.img_height = img_height
self.img_width = img_width
self.channels = channels
self.segmap_channel = segmap_ch
self.augment_flag = augment_flag
self.dataset_path = dataset_path
self.img_dataset_path = os.path.join(dataset_path, 'image')
self.segmap_dataset_path = os.path.join(dataset_path, 'segmap')
self.segmap_test_dataset_path = os.path.join(dataset_path, 'segmap_test')
self.image = []
self.color_value_dict = {}
self.segmap = []
self.segmap_test = []
self.set_x = set()
def image_processing(self, filename, segmap):
x = tf.read_file(filename)
x_decode = tf.image.decode_jpeg(x, channels=self.channels, dct_method='INTEGER_ACCURATE')
img = tf.image.resize_images(x_decode, [self.img_height, self.img_width])
img = tf.cast(img, tf.float32) / 127.5 - 1
segmap_x = tf.read_file(segmap)
segmap_decode = tf.image.decode_jpeg(segmap_x, channels=self.segmap_channel, dct_method='INTEGER_ACCURATE')
segmap_img = tf.image.resize_images(segmap_decode, [self.img_height, self.img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
if self.augment_flag :
augment_height_size = self.img_height + (30 if self.img_height == 256 else int(self.img_height * 0.1))
augment_width_size = self.img_width + (30 if self.img_width == 256 else int(self.img_width * 0.1))
p = random.random()
if p > 0.5:
img, segmap_img = augmentation(img, segmap_img, augment_height_size, augment_width_size)
label_map = convert_from_color_segmentation(self.color_value_dict, segmap_img, tensor_type=True)
segmap_onehot = tf.one_hot(label_map, len(self.color_value_dict))
return img, segmap_img, segmap_onehot
def test_image_processing(self, segmap):
segmap_x = tf.read_file(segmap)
segmap_decode = tf.image.decode_jpeg(segmap_x, channels=self.segmap_channel, dct_method='INTEGER_ACCURATE')
segmap_img = tf.image.resize_images(segmap_decode, [self.img_height, self.img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
label_map = convert_from_color_segmentation(self.color_value_dict, segmap_img, tensor_type=True)
segmap_onehot = tf.one_hot(label_map, len(self.color_value_dict))
return segmap_img, segmap_onehot
def preprocess(self):
self.image = glob(self.img_dataset_path + '/*.*')
self.segmap = glob(self.segmap_dataset_path + '/*.*')
self.segmap_test = glob(self.segmap_test_dataset_path + '/*.*')
segmap_label_path = os.path.join(self.dataset_path, 'segmap_label.txt')
if os.path.exists(segmap_label_path) :
print("segmap_label exists ! ")
with open(segmap_label_path, 'r') as f:
self.color_value_dict = literal_eval(f.read())
else :
print("segmap_label no exists ! ")
x_img_list = []
label = 0
for img in tqdm(self.segmap) :
if self.segmap_channel == 1 :
x = cv2.imread(img, flags=cv2.IMREAD_GRAYSCALE)
else :
x = cv2.imread(img, flags=cv2.IMREAD_COLOR)
x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
x = cv2.resize(x, dsize=(self.img_width, self.img_height), interpolation=cv2.INTER_NEAREST)
if self.segmap_channel == 1 :
x = np.expand_dims(x, axis=-1)
h, w, c = x.shape
x_img_list.append(x)
for i in range(h) :
for j in range(w) :
if tuple(x[i, j, :]) not in self.color_value_dict.keys() :
self.color_value_dict[tuple(x[i, j, :])] = label
label += 1
with open(segmap_label_path, 'w') as f :
f.write(str(self.color_value_dict))
print()
def load_segmap(dataset_path, image_path, img_width, img_height, img_channel):
segmap_label_path = os.path.join(dataset_path, 'segmap_label.txt')
with open(segmap_label_path, 'r') as f:
color_value_dict = literal_eval(f.read())
if img_channel == 1:
segmap_img = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE)
else :
segmap_img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR)
segmap_img = cv2.cvtColor(segmap_img, cv2.COLOR_BGR2RGB)
segmap_img = cv2.resize(segmap_img, dsize=(img_width, img_height), interpolation=cv2.INTER_NEAREST)
if img_channel == 1:
segmap_img = np.expand_dims(segmap_img, axis=-1)
label_map = convert_from_color_segmentation(color_value_dict, segmap_img, tensor_type=False)
segmap_onehot = get_one_hot(label_map, len(color_value_dict))
segmap_onehot = np.expand_dims(segmap_onehot, axis=0)
"""
segmap_x = tf.read_file(image_path)
segmap_decode = tf.image.decode_jpeg(segmap_x, channels=img_channel, dct_method='INTEGER_ACCURATE')
segmap_img = tf.image.resize_images(segmap_decode, [img_height, img_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
label_map = convert_from_color_segmentation(color_value_dict, segmap_img, tensor_type=True)
segmap_onehot = tf.one_hot(label_map, len(color_value_dict))
segmap_onehot = tf.expand_dims(segmap_onehot, axis=0)
"""
return segmap_onehot
def load_style_image(image_path, img_width, img_height, img_channel):
if img_channel == 1 :
img = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE)
else :
img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, dsize=(img_width, img_height))
if img_channel == 1 :
img = np.expand_dims(img, axis=0)
img = np.expand_dims(img, axis=-1)
else :
img = np.expand_dims(img, axis=0)
img = preprocessing(img)
return img
def preprocessing(x):
x = x/127.5 - 1 # -1 ~ 1
return x
def augmentation(image, segmap, augment_height, augment_width):
seed = random.randint(0, 2 ** 31 - 1)
ori_image_shape = tf.shape(image)
image = tf.image.random_flip_left_right(image, seed=seed)
image = tf.image.resize_images(image, [augment_height, augment_width])
image = tf.random_crop(image, ori_image_shape, seed=seed)
ori_segmap_shape = tf.shape(segmap)
segmap = tf.image.random_flip_left_right(segmap, seed=seed)
segmap = tf.image.resize_images(segmap, [augment_height, augment_width], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
segmap = tf.random_crop(segmap, ori_segmap_shape, seed=seed)
return image, segmap
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def inverse_transform(images):
return (images+1.) / 2
def imsave(images, size, path):
return misc.imsave(path, merge(images, size))
def merge(images, size):
h, w = images.shape[1], images.shape[2]
c = images.shape[3]
img = np.zeros((h * size[0], w * size[1], c))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[h*j:h*(j+1), w*i:w*(i+1), :] = image
return img
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def check_folder(log_dir):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return log_dir
def str2bool(x):
return x.lower() in ('true')
def get_one_hot(targets, nb_classes):
x = np.eye(nb_classes)[targets]
return x
def convert_from_color_segmentation(color_value_dict, arr_3d, tensor_type=False):
if tensor_type :
arr_2d = tf.zeros(shape=[tf.shape(arr_3d)[0], tf.shape(arr_3d)[1]], dtype=tf.uint8)
for c, i in color_value_dict.items() :
color_array = tf.reshape(np.asarray(c, dtype=np.uint8), shape=[1, 1, -1])
condition = tf.reduce_all(tf.equal(arr_3d, color_array), axis=-1)
arr_2d = tf.where(condition, tf.cast(tf.fill(tf.shape(arr_2d), i), tf.uint8), arr_2d)
return arr_2d
else :
arr_2d = np.zeros((np.shape(arr_3d)[0], np.shape(arr_3d)[1]), dtype=np.uint8)
for c, i in color_value_dict.items():
color_array = np.asarray(c, np.float32).reshape([1, 1, -1])
m = np.all(arr_3d == color_array, axis=-1)
arr_2d[m] = i
return arr_2d
def pytorch_xavier_weight_factor(gain=0.02, uniform=False) :
if uniform :
factor = gain * gain
mode = 'FAN_AVG'
else :
factor = (gain * gain) / 1.3
mode = 'FAN_AVG'
return factor, mode, uniform
def pytorch_kaiming_weight_factor(a=0.0, activation_function='relu', uniform=False) :
if activation_function == 'relu' :
gain = np.sqrt(2.0)
elif activation_function == 'leaky_relu' :
gain = np.sqrt(2.0 / (1 + a ** 2))
elif activation_function == 'tanh' :
gain = 5.0 / 3
else :
gain = 1.0
if uniform :
factor = gain * gain
mode = 'FAN_IN'
else :
factor = (gain * gain) / 1.3
mode = 'FAN_IN'
return factor, mode, uniform