forked from saeid-h/bts-fully-tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bts_eval.py
161 lines (128 loc) · 6.8 KB
/
bts_eval.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
# This file is a part of BTS.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from __future__ import absolute_import, division, print_function
import os
import argparse
import time
import cv2
import sys
from collections import namedtuple
bts_parameters = namedtuple('parameters', 'encoder, '
'height, width, '
'max_depth, '
'batch_size, '
'dataset, '
'num_epochs, '
'use_tpu, ')
import tensorflow as tf
from tensorflow.keras import callbacks
from bts_dataloader import BtsReader, BtsDataloader
from bts import si_log_loss_wrapper, bts_model
from custom_eval_metrics import metrics_list_factory
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
def convert_arg_line_to_args(arg_line):
for arg in arg_line.split():
if not arg.strip():
continue
yield arg
parser = argparse.ArgumentParser(description='BTS TensorFlow implementation.', fromfile_prefix_chars='@')
parser.convert_arg_line_to_args = convert_arg_line_to_args
parser.add_argument('--model_name', type=str, help='model name', default='bts_v0_0_1')
parser.add_argument('--encoder', type=str, help='type of encoder, desenet121_bts or densenet161_bts', default='densenet161_bts')
parser.add_argument('--data_path', type=str, help='path to the data', required=False)
parser.add_argument('--gt_path', type=str, help='path to the groundtruth data', required=False)
parser.add_argument('--tfrecord_path', type=str, help='path to the combined TFRecord dataset in zip format', required=False)
parser.add_argument('--tfrecord_shards', type=int, help='number of shards of the combined TFRecord dataset to read', default=1)
parser.add_argument('--filenames_file', type=str, help='path to the filenames text file', required=True)
parser.add_argument('--input_height', type=int, help='input height', default=480)
parser.add_argument('--input_width', type=int, help='input width', default=640)
parser.add_argument('--max_depth', type=float, help='maximum depth in estimation', default=80)
parser.add_argument('--weights_path', type=str, help='path to a specific checkpoint to load', default='', required=True)
parser.add_argument('--dataset', type=str, help='dataset to train on, make3d or nyudepthv2', default='nyu')
parser.add_argument('--eigen_crop', help='if set, crops according to Eigen NIPS14', action='store_true')
parser.add_argument('--garg_crop', help='if set, crops according to Garg ECCV16', action='store_true')
parser.add_argument('--min_depth_eval', type=float, help='minimum depth for evaluation', default=1e-3)
parser.add_argument('--max_depth_eval', type=float, help='maximum depth for evaluation', default=80)
parser.add_argument('--do_kb_crop', help='if set, crop input images as kitti benchmark images', action='store_true')
parser.add_argument('--batch_size', type=int, help='batch size per training replica', default=1)
parser.add_argument('--num_gpus', type=int, help='number of GPUs to use for evaluation', default=1)
if sys.argv.__len__() > 1 and sys.argv[1][0] != '-':
args = parser.parse_args(['@' + sys.argv[1]] + sys.argv[2:])
else:
args = parser.parse_args()
def get_num_lines(file_path):
f = open(file_path, 'r')
lines = f.readlines()
f.close()
return len(lines)
def test(strategy, params):
model_file = os.path.join(args.weights_path, args.model_name, 'final_model')
tensorboard_log_dir = os.path.join(args.weights_path, args.model_name, 'tensorboard/')
reader = BtsReader(params)
processor = BtsDataloader(params, do_kb_crop=args.do_kb_crop)
if args.tfrecord_path is None or args.tfrecord_path == '':
loader = reader.read_from_image_files(args.data_path, args.gt_path, args.filenames_file, 'test')
else:
loader = reader.read_from_tf_record(args.tfrecord_path, args.filenames_file, 'test', args.tfrecord_shards)
loader = processor.process_dataset(loader, 'test')
with strategy.scope():
model, _ = bts_model(params, 'test')
loss = si_log_loss_wrapper(params.dataset)
model.compile(optimizer='adam', loss=loss, metrics=metrics_list_factory(args))
print('Loading checkpoint at {}'.format(model_file))
model.load_weights(model_file, by_name=False).expect_partial()
print('Checkpoint successfully loaded')
model_callbacks = [callbacks.ProgbarLogger(count_mode='steps')]
model.summary()
num_test_samples = get_num_lines(args.filenames_file)
with open(args.filenames_file) as f:
lines = f.readlines()
print('Now testing {} images.'.format(num_test_samples))
metrics = model.evaluate(loader, verbose=1, callbacks=model_callbacks)
print("{:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}".format('silog', 'abs_rel', 'log10', 'rms', 'sq_rel', 'log_rms', 'd1', 'd2', 'd3'))
print("{0[1]:7.4f}, {0[2]:7.4f}, {0[3]:7.3f}, {0[4]:7.3f}, {0[5]:7.3f}, {0[6]:7.3f}, {0[7]:7.3f}, {0[8]:7.3f}, {0[9]:7.3f}".format(metrics))
def main():
# Find TPU cluster if available
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
tpu = None
# TPUStrategy for distributed training
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.TPUStrategy(tpu)
# MirroredStrategy for distributed training on multiple GPUs
elif args.num_gpus > 1:
gpus = tf.config.experimental.list_physical_devices('GPU')
if len(gpus) < args.num_gpus:
raise ValueError('Insufficient resources for distributed training: {} GPUs available out of {} GPUs requested.'.format(len(gpus), args.num_gpus))
gpu_name_list = [gpu.name for gpu in gpus]
strategy = tf.distribute.MirroredStrategy(devices=gpu_name_list[:num_gpus])
# Default strategy that works on CPU and single GPU
else:
strategy = tf.distribute.get_strategy()
params = bts_parameters(
encoder=args.encoder,
height=args.input_height,
width=args.input_width,
batch_size=args.batch_size * strategy.num_replicas_in_sync,
dataset=args.dataset,
max_depth=args.max_depth,
num_epochs=None,
use_tpu=False if tpu is None else True)
test(strategy, params)
if __name__ == '__main__':
main()