-
Notifications
You must be signed in to change notification settings - Fork 6
/
tune.py
149 lines (118 loc) · 5.85 KB
/
tune.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
import tensorflow as tf
from tensorboard.plugins.hparams import api as hp
from tqdm import tqdm
from path import Path
from data.parallel_dataset import Dataset, DataLoader
from model.model import DepthAwareNet
from model.loss import L2DepthLoss, L2NormRMSE
from solver.optimizer import OptimizerFactory
from solver.trainer import Trainer
import argparse
tf.keras.backend.clear_session()
parser = argparse.ArgumentParser(description='Select between small or big data',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--data-size', type=str, choices=['big', 'small', 'real'], default='small')
parser.add_argument('-b', '--batch-size', type=int, default=32)
parser.add_argument('-j', '--jobs', type=int, default=8)
IMAGE_SHAPE = (720, 1280)
HP_STRIDES = hp.HParam('strides', hp.Discrete([4, 8, 12, 16]))
HP_KSIZE = hp.HParam('kernel_size', hp.Discrete([3, 5, 7]))
HP_NCONV = hp.HParam('num_conv', hp.Discrete([4, 5, 6]))
HP_USE_MSE = hp.HParam('use_mse', hp.Discrete([True, False]))
with tf.summary.create_file_writer('/media/data/teamAI/minh/logs/hparam_tuning_2601/').as_default():
_ = hp.hparams_config(hparams=[HP_STRIDES, HP_KSIZE, HP_NCONV, HP_USE_MSE],
metrics=[hp.Metric('train_err', display_name='Train Distance Error (m)'),
hp.Metric('val_err', display_name='Validation Distance Error (m)'),
hp.Metric('num_params', display_name='# Params')])
args = parser.parse_args()
def train_val_model(hparams, sess: int):
input_shape = (IMAGE_SHAPE[0] // hparams[HP_STRIDES],
IMAGE_SHAPE[1] // hparams[HP_STRIDES])
################################
# Define data and dataloader #
################################
if args.data_size == 'big':
# train_path = "../data/train_big.csv"
# val_path = "../data/val_big.csv"
# img_directory = "../data/big/"
train_path = "/home/ivsr/CV_Group/phuc/airsim/train.csv"
val_path = "/home/ivsr/CV_Group/phuc/airsim/val.csv"
img_directory = "/home/ivsr/CV_Group/phuc/airsim/data"
else:
# train_path = "../data/train_small.csv"
# val_path = "../data/val_small.csv"
# img_directory = "../data/small/"
train_path = "/home/ivsr/CV_Group/minh/train588_50.csv"
val_path = "/home/ivsr/CV_Group/minh/val588_50.csv"
img_directory = "/home/ivsr/CV_Group/minh/50imperpose/full"
train_dataset = Dataset(train_path, img_directory, input_shape)
val_dataset = Dataset(val_path, img_directory, input_shape)
train_loader = DataLoader(train_dataset, input_shape=input_shape, batch_size=args.batch_size,
num_parallel_calls=args.jobs)
val_loader = DataLoader(val_dataset, input_shape=input_shape, batch_size=args.batch_size,
num_parallel_calls=args.jobs)
################
# Define model #
################
net = DepthAwareNet(num_ext_conv=hparams[HP_NCONV]-4, ksize=hparams[HP_KSIZE])
net.build(input_shape=(None, input_shape[0], input_shape[1], 1))
net.summary()
#######################
# Define loss function#
#######################
if hparams[HP_USE_MSE] is True:
dist_loss_fn = tf.keras.losses.MeanSquaredError()
depth_loss_fn = tf.keras.losses.MeanSquaredError()
else:
dist_loss_fn = L2NormRMSE()
depth_loss_fn = L2DepthLoss()
#######################
# Define optimizer#
#######################
factory = OptimizerFactory(lr=1e-3, use_scheduler=False)
optimizer = factory.get_optimizer()
#trainer and train
trainer = Trainer(train_loader, val_loader=val_loader,
model=net, distance_loss_fn=dist_loss_fn, depth_loss_fn=depth_loss_fn,
optimizer=optimizer,
log_path=Path('../ivsr_logs')/'tuning2501'/str(sess)+'.txt',
savepath='../ivsr_weights/tuning_2501',
use_mse=hparams[HP_USE_MSE])
train_loss, val_loss = trainer.train(13, save_checkpoint=False, early_stop=True)
return input_shape, train_loss, val_loss, net.count_params()
def run(run_dir, hparams, sess: int):
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
input_shape, train_loss, val_loss, num_params = train_val_model(hparams, sess)
#tf.summary.text('input_size', f'{}input_shape, step=1)
tf.summary.scalar('train_err', val_loss, step=1)
tf.summary.scalar('val_err', train_loss, step=1)
tf.summary.scalar('num_params', num_params, step=1)
session_num = 0
for use_mse in HP_USE_MSE.domain.values:
for ksize in HP_KSIZE.domain.values:
for stride in HP_STRIDES.domain.values:
if stride < 12:
for num_conv in HP_NCONV.domain.values:
hparams = {
HP_STRIDES: stride,
HP_NCONV: num_conv,
HP_KSIZE: ksize,
HP_USE_MSE: use_mse,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
run('/media/data/teamAI/minh/logs/hparam_tuning_2601/' + run_name, hparams, session_num)
session_num += 1
hparams = {
HP_STRIDES: stride,
HP_NCONV: 4,
HP_KSIZE: ksize,
HP_USE_MSE: use_mse,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
run('/media/data/teamAI/minh/logs/hparam_tuning_2601/' + run_name, hparams, session_num)
session_num += 1