-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.py
executable file
·73 lines (55 loc) · 2.23 KB
/
ops.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
# -*- coding: utf-8 -*-
import os
import numpy as np
import scipy.misc
import tensorflow as tf
import matplotlib.pyplot as plt
def mkdir(dirpath):
if not os.path.isdir(dirpath):
os.mkdir(dirpath)
return
def plot_batch(train_imgs, train_labels, batch_size):
for ind in range(batch_size):
f, ax = plt.subplots(1,2, figsize=(12,5))
ax[0].imshow(np.array(np.array(train_imgs[ind][:,:,[2,1,0]]+1)*127.5, dtype=np.uint8))
ax[1].imshow(np.array(np.array(train_labels[ind][:,:,[2,1,0]]+1)*127.5, dtype=np.uint8))
plt.show()
def imread(path, is_grayscale = False):
if (is_grayscale):
return scipy.misc.imread(path, flatten = True).astype(np.float)
return scipy.misc.imread(path).astype(np.float)
def load_image(image_path):
input_img = imread(image_path)
w = int(input_img.shape[1])
w2 = int(w/2)
img_A = input_img[:, 0:w2]
img_B = input_img[:, w2:w]
return img_A, img_B
def unsclae_img(img):
return np.asarray(np.clip((img + 1.)*127.5, 0., 255.), dtype=np.uint8)
def show_image(img):
plt.imshow(np.asarray(np.clip((img + 1.)*127.5, 0., 255.), dtype=np.uint8))
plt.show()
def save_image(img, filedir, i, j=0, score=0):
plt.imsave(filedir + '/epoch-{}-{}-{:.6}.jpg'.format(i, j, score), np.asarray(np.clip((img + 1.)*127.5, 0., 255.), dtype=np.uint8))
def img_preprocess(img, label, fine_size, load_size, is_test=False):
if is_test:
img = scipy.misc.imresize(img, [fine_size, fine_size])
label = scipy.misc.imresize(label, [fine_size, fine_size])
else:
img = scipy.misc.imresize(img, [load_size, load_size])
label = scipy.misc.imresize(label, [load_size, load_size])
h1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
w1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
img = img[h1: h1 + fine_size, w1: w1 + fine_size]
label = label[h1: h1 + fine_size, w1: w1 + fine_size]
if np.random.random() > 0.5:
img = np.fliplr(img)
label = np.fliplr(label)
img = img_shift(img)
label = img_shift(label)
return img, label
def img_shift(img):
return img / 127.5 - 1.
def concat(x, y):
return tf.concat([x, y],3)