|
| 1 | +# coding: utf-8 |
| 2 | +""" |
| 3 | +Profiles the conversion of a Keras model. |
| 4 | +""" |
| 5 | +import sys |
| 6 | +import cProfile |
| 7 | +from pstats import SortKey, Stats |
| 8 | +import io |
| 9 | +import argparse |
| 10 | +import tensorflow as tf |
| 11 | +from tensorflow.keras.applications import MobileNet, EfficientNetB2 |
| 12 | +from tf2onnx import tfonnx |
| 13 | +try: |
| 14 | + from pyinstrument import Profiler |
| 15 | +except ImportError: |
| 16 | + Profiler = None |
| 17 | + |
| 18 | + |
| 19 | +def spy_model(name): |
| 20 | + "Creates the model." |
| 21 | + with tf.compat.v1.Session(graph=tf.Graph()) as session: |
| 22 | + if name == "MobileNet": |
| 23 | + model = MobileNet() |
| 24 | + elif name == "EfficientNetB2": |
| 25 | + model = EfficientNetB2() |
| 26 | + else: |
| 27 | + raise ValueError("Unknown model name %r." % name) |
| 28 | + |
| 29 | + graph_def = tf.compat.v1.graph_util.convert_variables_to_constants( |
| 30 | + sess=session, |
| 31 | + input_graph_def=session.graph_def, |
| 32 | + output_node_names=[model.output.op.name]) |
| 33 | + |
| 34 | + return graph_def, model |
| 35 | + |
| 36 | + |
| 37 | +def spy_convert(graph_def, model): |
| 38 | + "Converts the model." |
| 39 | + with tf.Graph().as_default() as graph: |
| 40 | + tf.import_graph_def(graph_def=graph_def, name='') |
| 41 | + |
| 42 | + def spy_convert_in(): |
| 43 | + return tfonnx.process_tf_graph( |
| 44 | + tf_graph=graph, input_names=[model.input.name], |
| 45 | + output_names=[model.output.name]) |
| 46 | + |
| 47 | + spy_convert_in() |
| 48 | + |
| 49 | + |
| 50 | +def create(name): |
| 51 | + "Creates the model." |
| 52 | + graph_def, model = spy_model(name) |
| 53 | + return graph_def, model |
| 54 | + |
| 55 | + |
| 56 | +def convert(graph_def, model): |
| 57 | + "Converts the model." |
| 58 | + spy_convert(graph_def, model) |
| 59 | + |
| 60 | + |
| 61 | +def profile(profiler="none", name="MobileNet", show_all=False): |
| 62 | + """ |
| 63 | + Profiles the conversion of a model. |
| 64 | +
|
| 65 | + :param profiler: one among none, spy, pyinstrument, cProfile |
| 66 | + :param name: model to profile, MobileNet, EfficientNetB2 |
| 67 | + :param showall: used by pyinstrument to show all functions |
| 68 | + """ |
| 69 | + print("create(%r, %r)" % (profiler, name)) |
| 70 | + graph_def, model = create(name) |
| 71 | + print("profile(%r, %r)" % (profiler, name)) |
| 72 | + if profiler == 'none': |
| 73 | + convert(graph_def, model) |
| 74 | + elif profiler == "spy": |
| 75 | + # py-spy record -r 10 -o profile.svg -- python conversion_time.py spy |
| 76 | + convert(graph_def, model) |
| 77 | + elif profiler == "pyinstrument": |
| 78 | + if Profiler is None: |
| 79 | + raise ImportError("pyinstrument is not installed") |
| 80 | + profiler = Profiler(interval=0.0001) |
| 81 | + profiler.start() |
| 82 | + convert(graph_def, model) |
| 83 | + profiler.stop() |
| 84 | + print(profiler.output_text(unicode=False, color=False, show_all=show_all)) |
| 85 | + elif profiler == "cProfile": |
| 86 | + pr = cProfile.Profile() |
| 87 | + pr.enable() |
| 88 | + convert(graph_def, model) |
| 89 | + pr.disable() |
| 90 | + s = io.StringIO() |
| 91 | + sortby = SortKey.CUMULATIVE |
| 92 | + ps = Stats(pr, stream=s).sort_stats(sortby) |
| 93 | + ps.print_stats() |
| 94 | + print(s.getvalue()) |
| 95 | + else: |
| 96 | + raise ValueError("Unknown profiler %r." % profiler) |
| 97 | + |
| 98 | + |
| 99 | +def main(args): |
| 100 | + parser = argparse.ArgumentParser(description='Process some integers.') |
| 101 | + parser.add_argument('--profiler', default='none', |
| 102 | + choices=['none', 'spy', 'pyinstrument', 'cProfile'], |
| 103 | + help='a profiler') |
| 104 | + parser.add_argument('--name', default="MobileNet", |
| 105 | + choices=['MobileNet', 'EfficientNetB2'], |
| 106 | + help="a model") |
| 107 | + parser.add_argument('--showall', type=bool, default=False, |
| 108 | + help="used by pyinstrument to show all functions") |
| 109 | + res = parser.parse_args(args) |
| 110 | + profile(res.profiler, res.name, res.showall) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + print('Begin profiling with', sys.argv[1:]) |
| 115 | + main(sys.argv[1:]) |
| 116 | + print('Profile complete.') |
0 commit comments