-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathinspect_model_architecture.py
76 lines (62 loc) · 2.78 KB
/
inspect_model_architecture.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
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Export a dummy-quantized tflite model corresponding to the given model."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import app
from absl import flags
import tensorflow as tf
import efficientnet_builder
from edgetpu import efficientnet_edgetpu_builder
flags.DEFINE_string('model_name', 'efficientnet-b0', 'Model name to inspect.')
flags.DEFINE_integer('image_res', 224, 'The size of the input image')
flags.DEFINE_string('output_tflite', '/tmp/model.tflite',
'Location of the generated tflite model')
# FLAGS should not be used before main.
FLAGS = flags.FLAGS
def main(unused_argv):
tf.logging.set_verbosity(tf.logging.ERROR)
image_res = FLAGS.image_res
model_name = FLAGS.model_name
model_builder_fn = None
if model_name.startswith('efficientnet-edgetpu'):
model_builder_fn = efficientnet_edgetpu_builder
elif model_name.startswith('efficientnet'):
model_builder_fn = efficientnet_builder
else:
raise ValueError(
'Model must be either efficientnet-b* or efficientnet-edgetpu*')
with tf.Graph().as_default(), tf.Session() as sess:
images = tf.placeholder(
tf.float32, shape=(1, image_res, image_res, 3), name='input')
output, _ = model_builder_fn.build_model(
images, FLAGS.model_name, training=False)
tf.global_variables_initializer().run()
updates = []
for var in tf.trainable_variables():
noise = tf.random.normal(shape=var.shape, stddev=0.001)
updates.append(var.assign_add(noise))
sess.run(updates)
converter = tf.lite.TFLiteConverter.from_session(sess, [images], [output]) # pytype: disable=attribute-error
converter.inference_type = tf.lite.constants.QUANTIZED_UINT8
converter.quantized_input_stats = {'input': (0, 1.)}
converter.default_ranges_stats = (-10, 10)
tflite_model = converter.convert()
tf.gfile.Open(FLAGS.output_tflite, 'wb').write(tflite_model)
print('Model %s, image size %d' % (model_name, image_res))
print('TfLite model stored at %s' % FLAGS.output_tflite)
if __name__ == '__main__':
app.run(main)