-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
183 lines (142 loc) · 7.07 KB
/
train.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
"""
ECBM 4040
Group YYZA
Group Members: Manqi Yang (my2577), Shiyun Yang (sy2797), Yizhi Zhang (yz3376)
"""
import tensorflow as tf
import numpy as np
from skimage.io import imsave,imshow,imread
from PIL import Image as pil_image
from Encoder import Vgg19
from Decoder import Decoder
from wct import wct_tf
from argparse import ArgumentParser
class Train_Model:
"""
Train Model Class:
this class is used for decoder training
"""
def __init__(self, target_layer=None, pretrained_path=None, max_iterator=None, checkpoint_path=None,
tfrecord_path=None, batch_size=None):
"""
Train Model Initialization:
Inputs:
target_layer: indicate which decoder to train (decoder1 -> relu1, ..., decoder5 -> relu5)
pretrained_path: path to pre-trained VGG19 weights (vgg19.npy)
max_iterator: number of iterations for training
checkpoint_path: path to save genertated model
tfrecord_path: path to tfrecord file of training data
batch_size: batch size for training
"""
self.pretrained_path = pretrained_path
self.target_layer = target_layer
self.encoder = Vgg19(self.pretrained_path)
self.max_iterator = max_iterator
self.checkpoint_path = checkpoint_path
self.tfrecord_path = tfrecord_path
self.batch_size = batch_size
def encoder_decoder(self,inputs):
"""
encoder_decoder function:
Outputs:
encoded: feature map obtained by processing the original image with the encoder
decoded: reconstructed image obtained by processing the original image with both the encoder and decoder
decoded_encoded: feature map obtained by processing the reconstructed image with the encoder
"""
encoded = self.encoder.encoder(inputs,self.target_layer)
model=Decoder()
decoded,_ = model.decoder(encoded,self.target_layer)
decoded_encoded= self.encoder.encoder(decoded,self.target_layer)
return encoded,decoded,decoded_encoded
def train(self):
"""
Train function:
trains the decoder specified
"""
# save the pixel losses and feature losses during training so we can plot
p_loss_list = []
f_loss_list = []
inputs = tf.placeholder('float',[None,224,224,3])
outputs = tf.placeholder('float',[None,224,224,3])
encoded,decoded,decoded_encoded = self.encoder_decoder(inputs)
# compute pixel loss and feature loss:
# pixel loss = square L2 norm of Io-Ii, where Io is the reconstructed image and Ii is the original image
# feature loss = square L2 norm of phi(Io)-phi(Ii),
# where phi(Io) is the feature map of the reconstructed image computed by VGG encoder
# and phi(Ii) is the feature map of the original image
pixel_loss = tf.losses.mean_squared_error(decoded,outputs)
feature_loss = tf.losses.mean_squared_error(decoded_encoded,encoded)
# total loss is computed as the sum of pixel loss and feature loss
loss = pixel_loss+ feature_loss
opt= tf.train.AdamOptimizer(0.0001).minimize(loss)
# we use TensorFlow Record (binary format) for our training data, this gives better efficiency than processing data directly
tfrecords_filename = self.tfrecord_path
filename_queue = tf.train.string_input_producer([tfrecords_filename],num_epochs=100)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
feature2 = {'image_raw': tf.FixedLenFeature([], tf.string)}
features = tf.parse_single_example(serialized_example, features=feature2)
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image,[224,224,3])
images = tf.train.shuffle_batch([image], batch_size=self.batch_size, capacity=30, min_after_dequeue=10)
# GPU configuration
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config = config)as sess :
tf.global_variables_initializer().run()
tf.local_variables_initializer().run()
# we use TensorFlow's RandomShuffleQueue to prepare training inputs
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
saver = tf.train.Saver()
# training iterations
for i in range (self.max_iterator):
batch_x=sess.run(images)
feed_dict = {inputs:batch_x, outputs : batch_x}
_,p_loss,f_loss,reconstruct_imgs=sess.run([opt,pixel_loss,feature_loss,decoded],feed_dict=feed_dict)
print('step %d | pixel_loss is %f | feature_loss is %f |'%(i,p_loss,f_loss))
p_loss_list.append(p_loss)
f_loss_list.append(f_loss)
if i % 5 ==0:
result_img = np.clip(reconstruct_imgs[0],0,255).astype(np.uint8)
imsave('result.jpg',result_img)
saver.save(sess,self.checkpoint_path)
coord.request_stop()
coord.join(threads)
# write pixel losses and feature losses to files
fp = open("loss/p_loss.csv",'w')
fp.writelines(str(p_loss_list))
fp.close()
fp = open("loss/f_loss.csv",'w')
fp.writelines(str(f_loss_list))
fp.close()
parser = ArgumentParser()
parser.add_argument('--target_layer', type=str,
dest='target_layer', help='target_layer(such as relu5)',
metavar='target_layer', required=True)
parser.add_argument('--pretrained_path',type=str,
dest='pretrained_path',help='the pretrained vgg19 path',
metavar='Pretrained',required = True)
parser.add_argument('--max_iterator',type=int,
dest='max_iterator',help='the max iterator',
metavar='MAX',required = True)
parser.add_argument('--checkpoint_path',type=str,
dest='checkpoint_path',help='checkpoint path',
metavar='CheckPoint',required = True)
parser.add_argument('--tfrecord_path',type=str,
dest='tfrecord_path',help='tfrecord path',
metavar='Tfrecord',required = True)
parser.add_argument('--batch_size',type=int,
dest='batch_size',help='batch_size',
metavar='Batch_size',required = True)
def main():
opts = parser.parse_args()
model = Train_Model(target_layer = opts.target_layer,
pretrained_path = opts.pretrained_path,
max_iterator = opts.max_iterator,
checkpoint_path = opts.checkpoint_path,
tfrecord_path = opts.tfrecord_path,
batch_size = opts.batch_size)
model.train()
if __name__=='__main__' :
main()