-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.py
152 lines (109 loc) · 4.56 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
import numpy as np
import os
import cv2
import tensorflow as tf
import tensorflow.contrib.slim as slim
import random
from glob import glob
class Image_data:
def __init__(self, img_height, img_width, channels, dataset_path, label_list, augment_flag):
self.img_height = img_height
self.img_width = img_width
self.channels = channels
self.augment_flag = augment_flag
self.label_list = label_list
self.dataset_path = dataset_path
self.image = []
self.label = []
def image_processing(self, filename, label):
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
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))
img = tf.cond(pred=tf.greater_equal(tf.random_uniform(shape=[], minval=0.0, maxval=1.0), 0.5),
true_fn=lambda : augmentation(img, augment_height_size, augment_width_size),
false_fn=lambda : img)
return img, label
def preprocess(self):
# self.label_list = ['tiger', 'cat', 'dog', 'lion']
for idx, label in enumerate(self.label_list) :
image_list = glob(os.path.join(self.dataset_path, label) + '/*.png') + glob(os.path.join(self.dataset_path, label) + '/*.jpg')
label_one_hot = [[idx]] * len(image_list) # [ [0], [0], ... , [0] ]
self.image.extend(image_list)
self.label.extend(label_one_hot)
def load_test_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 = img/127.5 - 1
return img
def augmentation(image, 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)
return image
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def inverse_transform(images):
return ((images+1.) / 2) * 255.0
def imsave(images, size, path):
images = merge(images, size)
images = cv2.cvtColor(images.astype('uint8'), cv2.COLOR_RGB2BGR)
return cv2.imwrite(path, images)
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[1]
j = idx // size[1]
img[h*j:h*(j+1), w*i:w*(i+1), :] = image
return img
def return_images(images, size) :
x = merge(images, size)
return x
def check_folder(log_dir):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return log_dir
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def str2bool(x):
return x.lower() in ('true')
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