-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsizeHALF.py
170 lines (134 loc) · 6.17 KB
/
sparsizeHALF.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
import vgg
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sys import stderr
import time
FIRST_HALF_RELU_LAYERS = ('relu1_1', 'relu1_2', 'relu2_1', 'relu2_2')
SECOND_HALF_RELU_LAYERS = ('relu3_1', 'relu3_2', 'relu3_3', 'relu3_4','relu4_1','relu4_2', 'relu4_3', 'relu4_4', 'relu5_1', 'relu5_2', 'relu5_3', 'relu5_4')
def sparsizeHALF(network, img, regularisation_coeff, iterations, learning_rate, beta1,
beta2, epsilon, pooling, print_iterations=None, checkpoint_iterations=None):
"""
Stylize images.
This function yields tuples (iteration, image); `iteration` is None
if this is the final image (the last iteration). Other tuples are yielded
every `checkpoint_iterations` iterations.
:rtype: iterator[tuple[int|None,image]]
"""
shape = (1,) + img.shape
img_conteneur = np.zeros(shape, dtype='float32')
img_conteneur[0, :, :, :] = img
vgg_weights, vgg_mean_pixel = vgg.load_net(network)
loss_curve = []
sparsness_curve = []
# make sparse encoded image using backpropogation
with tf.Graph().as_default():
init_image = tf.constant(img_conteneur)
net_init = vgg.net_preloaded(vgg_weights, init_image, pooling)
layer_nonzero_init = []
layer_size = []
firs_half_layer_init =[]
for layer in FIRST_HALF_RELU_LAYERS:
encoding = net_init[layer]
firs_half_layer_init.append(encoding)
for layer in SECOND_HALF_RELU_LAYERS:
init_encoding = net_init[layer]
layer_nonzero_init.append(tf.count_nonzero(init_encoding))
layer_size.append(tf.size(init_encoding))
nonzero_init = tf.reduce_sum(layer_nonzero_init)
size = tf.reduce_sum(layer_size)
init_sparsness = 1. - tf.to_float(nonzero_init) / tf.to_float(size)
image = tf.Variable(tf.random_normal(shape) * 0.256)
net = vgg.net_preloaded(vgg_weights, image, pooling)
layer_l1_norm = []
layer_nonzero = []
layer_size = []
first_half_layer =[]
for layer in FIRST_HALF_RELU_LAYERS:
encoding = net[layer]
first_half_layer.append(encoding)
first_layer_similarity = tf.zeros([1])
for i in range(len(FIRST_HALF_RELU_LAYERS)):
first_layer_similarity += tf.nn.l2_loss(firs_half_layer_init[i] - first_half_layer[i])
for layer in SECOND_HALF_RELU_LAYERS:
encoding = net[layer]
layer_l1_norm.append(tf.reduce_sum(encoding))
layer_nonzero.append(tf.count_nonzero(encoding))
layer_size.append(tf.size(encoding))
reg_term = tf.reduce_sum(layer_l1_norm)
nonzero = tf.reduce_sum(layer_nonzero)
sparsness = 1. - tf.to_float(nonzero) / tf.to_float(size)
lbda = tf.constant(regularisation_coeff)
loss = tf.nn.l2_loss(image - init_image) + first_layer_similarity + lbda * reg_term
# optimizer setup
train_step = tf.train.AdamOptimizer(learning_rate, beta1, beta2, epsilon).minimize(loss)
def print_progress():
stderr.write(' loss: %g\n' % loss.eval())
stderr.write(' initial sparsness: %g\n' % init_sparsness.eval())
stderr.write(' final sparsness: %g\n' % sparsness.eval())
# optimization
best_loss = float('inf')
best = None
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
stderr.write('Optimization started...\n')
if (print_iterations and print_iterations != 0):
print_progress()
iteration_times = []
start = time.time()
for i in range(iterations):
iteration_start = time.time()
if i > 0:
elapsed = time.time() - start
# take average of last couple steps to get time per iteration
remaining = np.mean(iteration_times[-10:]) * (iterations - i)
stderr.write('Iteration %4d/%4d (%s elapsed, %s remaining)\n' % (
i + 1,
iterations,
hms(elapsed),
hms(remaining)
))
loss_curve.append(sess.run(loss))
sparsness_curve.append(sess.run(sparsness))
else:
stderr.write('Iteration %4d/%4d\n' % (i + 1, iterations))
train_step.run()
last_step = (i == iterations - 1)
if last_step or (print_iterations and i % print_iterations == 0):
print_progress()
if (checkpoint_iterations and i % checkpoint_iterations == 0) or last_step:
this_loss = loss.eval()
if this_loss < best_loss:
best_loss = this_loss
best = image.eval()
img_out = best.reshape(shape[1:])
yield (
(None if last_step else i),
img_out
)
iteration_end = time.time()
iteration_times.append(iteration_end - iteration_start)
fig1 = plt.figure('loss')
plt.xlabel('Iterations')
plt.ylabel('Fonction de perte')
plt.title('Fonction de perte au fil des itérations')
plt.step(np.arange(1, iterations), loss_curve, 'r')
fig2 = plt.figure('sparsness')
plt.xlabel('Iterations')
plt.title("Parcimonie de l'encodage de la nouvelle image")
plt.ylabel("Pourcentage de zéro dans l'encodage de la nouvelle image")
plt.step(np.arange(1, iterations), sparsness_curve, 'b')
# fig1.savefig('body1_transformation/' + layer + '_' + str(regularisation_coeff) + '_loss_plot.png')
# fig2.savefig('body1_transformation/' + layer + '_' + str(regularisation_coeff) + '_sparsness_plot.png')
plt.show()
def hms(seconds):
seconds = int(seconds)
hours = (seconds // (60 * 60))
minutes = (seconds // 60) % 60
seconds = seconds % 60
if hours > 0:
return '%d hr %d min' % (hours, minutes)
elif minutes > 0:
return '%d min %d sec' % (minutes, seconds)
else:
return '%d sec' % seconds