-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
178 lines (150 loc) · 8.06 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
# Author: Wentao Yuan ([email protected]) 05/31/2018
# revised by Hyeontae Son
import datetime
import importlib
import os
import sys
import time
import numpy as np
import tensorflow as tf
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, 'models'))
from utils.data_util import Dataset
from utils.visu_util import plot_pcd_three_views
from utils.args import trainArguments
from termcolor import colored
def train(config):
data_config = config['dataset']
train_config = config['train_setting']
lr_config = train_config['learning_rate']
# Data
is_training_pl = tf.placeholder(tf.bool, shape=(), name='is_training')
inputs_pl = tf.placeholder(tf.float32, (1, None, 3), 'inputs')
npts_pl = tf.placeholder(tf.int32, (train_config['batch_size'],), 'num_points')
gt_pl = tf.placeholder(tf.float32, (train_config['batch_size'], data_config['num_gt_points'], 3), 'ground_truths')
train_set = Dataset(data_config, train_config, is_training=True)
valid_set = Dataset(data_config, train_config, is_training=False)
num_train = train_set.get_num_data()
num_valid = valid_set.get_num_data()
# Model
model_module = importlib.import_module(config['model']['decoder']['type'])
model = model_module.model(config, inputs_pl, npts_pl, gt_pl, is_training_pl)
# Optimizer
optimizer = importlib.import_module('optimizer').optimizer(lr_config, model.global_step, model.target_loss)
# TF Config
config_proto = tf.ConfigProto()
config_proto.gpu_options.allow_growth = True
config_proto.allow_soft_placement = True
sess = tf.Session(config=config_proto)
saver = tf.train.Saver()
train_summary = tf.summary.merge_all('train_summary')
valid_summary = tf.summary.merge_all('valid_summary')
# restart training
if config['restore']:
saver.restore(sess, tf.train.latest_checkpoint(config['log_dir']))
writer = tf.summary.FileWriter(config['log_dir'])
# calc the last best valid loss
num_eval_steps = num_valid // train_config['batch_size']
total_eval_loss = 0
sess.run(tf.local_variables_initializer())
for i in range(num_eval_steps):
ids, inputs, npts, gt = valid_set.fetch(sess)
gt = gt[:, :data_config['num_gt_points'], :]
feed_dict = {inputs_pl: inputs, npts_pl: npts, gt_pl: gt, is_training_pl: False}
evaluation_loss = sess.run(model.evaluation_loss, feed_dict=feed_dict)
total_eval_loss += evaluation_loss
best_valid_loss = total_eval_loss / num_eval_steps
# train from scratch
else:
sess.run(tf.global_variables_initializer())
if os.path.exists(config['log_dir']):
delete_key = input(colored('%s exists. Delete? [y (or enter)/N]'
% config['log_dir'], 'white', 'on_red'))
if delete_key == 'y' or delete_key == "":
os.system('rm -rf %s/*' % config['log_dir'])
os.makedirs(os.path.join(config['log_dir'], 'plots'))
else:
os.makedirs(os.path.join(config['log_dir'], 'plots'))
# save configuration in log directory
os.system('cp %s %s' % (config['config_path'], config['log_dir']))
os.system('cp train.py %s' % config['log_dir'])
writer = tf.summary.FileWriter(config['log_dir'], sess.graph)
best_valid_loss = 1e5 # initialize with enough big num
print(colored("Training will begin.. ", 'grey', 'on_green'))
print(colored("Batch_size: " + str(train_config['batch_size']), 'grey', 'on_green'))
print(colored("Batch norm use?: " + str(config['model']['use_bn']), 'red', 'on_green'))
print(colored("Decoder arch: " + config['model']['decoder']['type'], 'grey', 'on_green'))
print(colored("Last best_validation_loss: " + str(best_valid_loss), 'grey', 'on_green'))
# Training
total_time = 0
train_start = time.time()
init_step = sess.run(model.global_step)
for step in range(init_step + 1, train_config['max_step'] + 1):
epoch = step * train_config['batch_size'] // num_train + 1
ids, inputs, npts, gt = train_set.fetch(sess)
gt = gt[:, :data_config['num_gt_points'], :]
start = time.time()
feed_dict = {inputs_pl: inputs, npts_pl: npts, gt_pl: gt, is_training_pl: True}
_, target_loss, summary = sess.run([optimizer.train, model.target_loss, train_summary], feed_dict=feed_dict)
total_time += time.time() - start
writer.add_summary(summary, step)
# logging
if step % train_config['steps_per_print'] == 0:
print('epoch %d step %d target_loss %.8f - time per batch %.4f' %
(epoch, step, target_loss, total_time / train_config['steps_per_print']))
total_time = 0
# eval on validation set
if step % train_config['steps_per_eval'] == 0:
print(colored('Testing...', 'grey', 'on_green'))
num_eval_steps = num_valid // train_config['batch_size']
total_eval_loss = 0
total_time = 0
sess.run(tf.local_variables_initializer())
for i in range(num_eval_steps):
start = time.time()
ids, inputs, npts, gt = valid_set.fetch(sess)
gt = gt[:, :data_config['num_gt_points'], :]
feed_dict = {inputs_pl: inputs, npts_pl: npts, gt_pl: gt, is_training_pl: False}
evaluation_loss, _ = sess.run([model.evaluation_loss, model.update], feed_dict=feed_dict)
total_eval_loss += evaluation_loss
total_time += time.time() - start
summary = sess.run(valid_summary, feed_dict={is_training_pl: False})
writer.add_summary(summary, step)
temp_valid_loss = total_eval_loss / num_eval_steps
print(colored('epoch %d step %d eval_loss %.8f - time per batch %.4f' %
(epoch, step, temp_valid_loss, total_time / num_eval_steps),
'grey', 'on_green'))
if temp_valid_loss <= best_valid_loss: # save best model for validation set
best_valid_loss = temp_valid_loss
saver.save(sess, os.path.join(config['log_dir'], 'model'), step)
print(colored('Model saved at %s' % config['log_dir'], 'white', 'on_blue'))
total_time = 0
# visualize
if step % config['visualizing']['steps_per_visu'] == 0:
print('visualizing!')
vis_ids, vis_inputs, vis_npts, vis_gt = valid_set.fetch(sess)
if data_config['type'] == 'topnet':
# for replace the character "/" to "_"
vis_ids = vis_ids.astype('U')
vis_ids = np.char.split(vis_ids, sep='/', maxsplit=1)
vis_ids = np.char.join(['_'] * train_config['batch_size'], vis_ids)
vis_feed_dict = {inputs_pl:vis_inputs, npts_pl:vis_npts, gt_pl:vis_gt, is_training_pl:False}
all_pcds = sess.run(model.visualize_ops, feed_dict=vis_feed_dict)
is_from_decoder = \
np.arange(0, config['model']['decoder']['num_decoder_points'] + config['model']['upsampling_ratio'] * train_config['num_input_points'])\
>= config['model']['upsampling_ratio'] * train_config['num_input_points']
for i in range(0, train_config['batch_size'], config['visualizing']['visu_freq']):
plot_path = os.path.join(config['log_dir'], 'plots',
'epoch_%d_step_%d_%s.png' % (epoch, step, vis_ids[i]))
pcds = [x[i] for x in all_pcds]
if config['visualizing']['visu_split']:
plot_pcd_three_views(plot_path, pcds, model.visualize_titles, is_from_decoder)
else:
plot_pcd_three_views(plot_path, pcds, model.visualize_titles, None)
print(colored("Training ended!", 'grey', 'on_green'))
print('Total training time', datetime.timedelta(seconds=time.time() - train_start))
sess.close()
if __name__ == '__main__':
config = trainArguments().to_config()
train(config)