forked from MiguelMonteiro/permutohedral_lattice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plnet.py
191 lines (158 loc) · 5.75 KB
/
plnet.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
import tensorflow as tf
import numpy as np
import scipy.io
import os
import lattice_filter_op_loader
import cv2
import matplotlib.pyplot as plt
from copy import deepcopy
from scipy.misc import imresize
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.layers.core import Activation, Dropout, Flatten, Lambda
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD, Adam, Nadam
from keras.utils import np_utils, plot_model
from keras import objectives, layers
from keras import backend as K
from os import path
module = tf.load_op_library(path.join(path.dirname(path.abspath(__file__)), 'lattice_filter.so'))
input_path = 'datasets/A'
output_path = 'datasets/B'
m = 256
n = 256
sketch_dim = (m,n,3)
img_dim = (m,n,3)
num_images = 16
num_epochs = 2
batch_size = 4
file_names = []
def load_file_names(path):
return os.listdir(path)
def sub_plot(x,y,z):
fig = plt.figure()
a = fig.add_subplot(1,3,1)
imgplot = plt.imshow(x, cmap='gray')
a.set_title('Sketch')
plt.axis("off")
a = fig.add_subplot(1,3,2)
imgplot = plt.imshow(z)
a.set_title('Prediction')
plt.axis("off")
a = fig.add_subplot(1,3,3)
imgplot = plt.imshow(y)
a.set_title('Ground Truth')
plt.axis("off")
plt.show()
def imshow(x, gray=False):
plt.imshow(x, cmap='gray' if gray else None)
plt.show()
def get_batch(idx, X = True, Y = True):
global file_names
X_train = np.zeros((batch_size, m, n, 3), dtype='float32')
Y_train = np.zeros((batch_size, m, n, 3), dtype='float32')
F_train = None
x_path = input_path
y_path = output_path
if len(file_names) == 0:
file_names = load_file_names(x_path)
if X:
# Load Sketches
for i in range(batch_size):
file = os.path.join(x_path, file_names[i+batch_size*idx])
img = cv2.imread(file)
img = imresize(img, sketch_dim)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype('float32')
X_train[i] = img / 255.
if Y:
# Load Ground-truth Images
for i in range(batch_size):
file = os.path.join(y_path, file_names[i+batch_size*idx])
img = cv2.imread(file)
img = imresize(img, img_dim)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype('float32')
Y_train[i] = img / 255.
X_train = np.reshape(X_train, (batch_size, m, n, 3))
return X_train, Y_train
def pixel_loss(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_true - y_pred))) + 0.00001*total_variation_loss(y_pred)
def total_variation_loss(y_pred):
if K.image_data_format() == 'channels_first':
a = K.square(y_pred[:, :, :m - 1, :n - 1] - y_pred[:, :, 1:, :n - 1])
b = K.square(y_pred[:, :, :m - 1, :n - 1] - y_pred[:, :, :m - 1, 1:])
else:
a = K.square(y_pred[:, :m - 1, :n - 1, :] - y_pred[:, 1:, :n - 1, :])
b = K.square(y_pred[:, :m - 1, :n - 1, :] - y_pred[:, :m - 1, 1:, :])
return K.sum(K.pow(a + b, 1.25))
def generator_model(input_img):
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
# x = Conv2D(9, (3, 3), activation=None, padding='same')(x)
# lattice = Lambda(lambda x:module.lattice_filter(x[:,:,:,0:3], x[:,:,:,3:], bilateral=True, theta_alpha=8, theta_beta=0.125))
# output = lattice(x)
y = Conv2D(6, (3, 3), activation=None, padding='same')(x)
x = Conv2D(3, (3, 3), activation=None, padding='same')(x)
lattice = Lambda(lambda x:module.lattice_filter(x[0], x[1], bilateral=True, theta_alpha=8, theta_beta=0.125))
output = lattice([x,y])
return output
def full_model(summary = True):
input_img = Input(shape=(m, n, 3))
generator = generator_model(input_img)
model = Model(input=input_img, output=[generator], name='architect')
model.summary()
return model
def train_faces(weights=None):
model = full_model()
optim = Adam(lr=1e-4,beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(loss=[pixel_loss], loss_weights=[1], optimizer=optim)
if weights is not None:
model.load_weights(weights)
print('start train')
for epoch in range(num_epochs):
num_batches = num_images // batch_size
print('epoch:',epoch)
for batch in range(num_batches):
X,Y = get_batch(batch)
loss = model.train_on_batch(X, [Y])
print("Loss in Epoch # ",epoch,"| Batch #", batch, ":", loss)
model.save_weights("plnet_weights_%d" % epoch)
def predict(batch, i, weights):
model = full_model()
model.load_weights(weights)
X, T = get_batch(batch, Y = True)
Y= model.predict(X[:i])
x = X[i].reshape(m,n)
y = Y[i]
sub_plot(x, T[i], y)
def sketchback(image, weights):
model = full_model()
model.load_weights(weights)
sketch = cv2.imread(image)
sketch = imresize(sketch, sketch_dim)
sketch = sketch / 255.
sketch = sketch.reshape(1,m,n,3)
result = model.predict(sketch)
imshow(result[0])
fig = plt.figure()
a = fig.add_subplot(1,2,1)
imgplot = plt.imshow(sketch[0].reshape(m,n), cmap='gray')
a.set_title('Sketch')
plt.axis("off")
a = fig.add_subplot(1,2,2)
imgplot = plt.imshow(result[0])
a.set_title('Prediction')
plt.axis("off")
plt.show()
def test(image, weights, save_name):
model = full_model()
model.load_weights(weights)
sketch = cv2.imread(image)
sketch = imresize(sketch, sketch_dim)
sketch = sketch / 255.
sketch = sketch.reshape(1,m,n,3)
result = model.predict(sketch)
cv2.imwrite(save_name, result[0])
if __name__ == "__main__":
train_faces()