-
Notifications
You must be signed in to change notification settings - Fork 2
/
freeze_graph.py
170 lines (137 loc) · 6.39 KB
/
freeze_graph.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
#!/usr/bin/env python3
from argparse import ArgumentParser
import glob
from importlib import import_module
import json
import os
import numpy as np
import tensorflow as tf
from tensorflow.contrib import slim
import utils
parser = ArgumentParser(description='Evaluate a semantic segmentation network.')
parser.add_argument(
'--experiment_root', required=True, type=utils.writeable_directory,
help='Location used to load checkpoints and store images.')
parser.add_argument(
'--dataset_config', type=str, default=None,
help='Path to the json file containing the dataset config. When left blank,'
'the first dataset used during training is used.')
parser.add_argument(
'--output_graph', type=str, default=None,
help='Target file to store the graph protobuf to. If left blank it will be '
'named based on the checkpoint name.')
parser.add_argument(
'--checkpoint_iteration', type=int, default=-1,
help='Iteration from which the checkpoint will be loaded. Defaults to -1, '
'which results in the last checkpoint being used.')
parser.add_argument(
'--fixed_input_height', default=None, type=utils.nonnegative_int,
help='A fixed value for the input height. If specified this will bake it '
'into the frozen model, possibly increasing the speed.')
parser.add_argument(
'--fixed_input_width', default=None, type=utils.nonnegative_int,
help='A fixed value for the input width. If specified this will bake it '
'into the frozen model, possibly increasing the speed.')
parser.add_argument(
'--fixed_batch_size', default=None, type=utils.nonnegative_int,
help='A fixed value for the batch size. If specified this will bake it into'
' the frozen model, possibly increasing the speed.')
# TODO(pandoro) generalize to use all output logits instead of a single one.
# This will make the freezing less redundant. Simple make a "default" additional
# probability and color coding which is a duplicate of one of the dataset, but
# also support all datasets.
def main():
args = parser.parse_args()
# Parse original info from the experiment root and add new ones.
args_file = os.path.join(args.experiment_root, 'args.json')
if not os.path.isfile(args_file):
raise IOError('`args.json` not found in {}'.format(args_file))
print('Loading args from {}.'.format(args_file))
with open(args_file, 'r') as f:
args_resumed = json.load(f)
for key, value in args_resumed.items():
if key not in args.__dict__:
args.__dict__[key] = value
# In case no dataset config was specified, we need to fix the argument here
# since there will be a list of configs.
if args.dataset_config is None:
args.dataset_config = args_resumed['dataset_config'][0]
# Load the config for the dataset.
with open(args.dataset_config, 'r') as f:
dataset_config = json.load(f)
# Compute the label to color map
id_to_rgb = tf.constant(np.asarray(
dataset_config['rgb_colors'] + [(0, 0, 0)], dtype=np.uint8)[:,::-1],
name='id_to_rgb')
id_to_class_names = tf.constant(np.asarray(
dataset_config['class_names'] + ['void']), name='id_to_class_names')
# Setup the input
image_input = tf.placeholder(
tf.uint8, shape=(
args.fixed_batch_size, args.fixed_input_height,
args.fixed_input_width, 3),
name='input')
image_input = (tf.to_float(image_input) - 128.0) / 128.0
# Determine the checkpoint location.
if args.checkpoint_iteration == -1:
# The default TF way to do this fails when moving folders.
checkpoint = os.path.join(
args.experiment_root,
'checkpoint-{}'.format(args.train_iterations))
else:
checkpoint = os.path.join(
args.experiment_root,
'checkpoint-{}'.format(args.checkpoint_iteration))
iteration = int(checkpoint.split('-')[-1])
print('Restoring from checkpoint: {}'.format(checkpoint))
# Check if the checkpoint contains a specifically named output_conv. This is
# needed for models trained with the older single dataset code.
reader = tf.train.NewCheckpointReader(checkpoint)
var_to_shape_map = reader.get_variable_to_shape_map()
output_conv_name = 'output_conv_{}'.format(
dataset_config.get('dataset_name'))
output_conv_name_found = False
for k in var_to_shape_map.keys():
output_conv_name_found = output_conv_name in k
if output_conv_name_found:
break
if not output_conv_name_found:
print('Warning: An output for the specific dataset could not be found '
'in the checkpoint. This likely means it\'s an old checkpoint. '
'Revertig to the old default output name. This could cause '
'issues if the dataset class count and output classes of the '
'network do not match.')
output_conv_name = 'output_conv'
# Setup the network for simple forward passing.
model = import_module('networks.' + args.model_type)
with tf.name_scope('model'):
net = model.network(image_input, is_training=False,
**args.model_params)
logits = slim.conv2d(net, len(dataset_config['class_names']),
[3,3], scope=output_conv_name, activation_fn=None,
weights_initializer=slim.variance_scaling_initializer(),
biases_initializer=tf.zeros_initializer())
predictions = tf.nn.softmax(logits, name='class_probabilities')
# Add a color decoder to create nice color-coded images for this
# dataset.
colored_predictions = tf.gather(
id_to_rgb, tf.argmax(predictions, -1), name='class_colors')
with tf.Session() as sess:
checkpoint_loader = tf.train.Saver()
checkpoint_loader.restore(sess, checkpoint)
# Possibly fix the output graph.
if args.output_graph is None:
output_graph = checkpoint + '_frozen.pb'
else:
output_graph = args.output_graph
# Freeze all variables.
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
sess.graph_def,
['class_probabilities', 'class_colors', 'id_to_rgb',
'id_to_class_names'])
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(frozen_graph_def.SerializeToString())
print('Frozen graph saved to: {}'.format(output_graph))
if __name__ == '__main__':
main()