forked from taki0112/MUNIT-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
80 lines (62 loc) · 2.43 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
import tensorflow as tf
from tensorflow.contrib import slim
from scipy import misc
import os, random
import numpy as np
# https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/
# https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/
class ImageData:
def __init__(self, img_h, img_w, channels, augment_flag=False):
self.img_h = img_h
self.img_w = img_w
self.channels = channels
self.augment_flag = augment_flag
def image_processing(self, filename):
x = tf.read_file(filename)
x_decode = tf.image.decode_jpeg(x, channels=self.channels)
img = tf.image.resize_images(x_decode, [self.img_h, self.img_w])
img = tf.cast(img, tf.float32) / 127.5 - 1
if self.augment_flag :
augment_size_h = self.img_h + (30 if self.img_h == 256 else 15)
augment_size_w = self.img_w + (30 if self.img_w == 256 else 15)
p = random.random()
if p > 0.5:
img = augmentation(img, augment_size_h, augment_size_w)
return img
def load_test_data(image_path, size_h=256, size_w=256):
img = misc.imread(image_path, mode='RGB')
img = misc.imresize(img, [size_h, size_w])
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, aug_img_h, aug_img_w):
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, [aug_img_h, aug_img_w])
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
def imsave(images, size, path):
return misc.imsave(path, merge(images, size))
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 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