From bbc973bc6657e2f4787d1869b2fc78bad2813e26 Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 16:00:50 -0800 Subject: [PATCH 1/6] Decrease default workspace size from 8GB to 4GB --- tftrt/examples/image-classification/image_classification.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tftrt/examples/image-classification/image_classification.py b/tftrt/examples/image-classification/image_classification.py index d7891364f..998ef3e3a 100644 --- a/tftrt/examples/image-classification/image_classification.py +++ b/tftrt/examples/image-classification/image_classification.py @@ -431,7 +431,7 @@ def get_frozen_graph( use_synthetic=False, cache=False, default_models_dir='./data', - max_workspace_size=(2<<32)-1000): + max_workspace_size=(1<<32)): """Retreives a frozen GraphDef from model definitions in classification.py and applies TF-TRT model: str, the model name (see NETS table in classification.py) @@ -547,7 +547,7 @@ def get_frozen_graph( parser.add_argument('--num_calib_inputs', type=int, default=500, help='Number of inputs (e.g. images) used for calibration ' '(last batch is skipped in case it is not full)') - parser.add_argument('--max_workspace_size', type=int, default=(2<<32)-1000, + parser.add_argument('--max_workspace_size', type=int, default=(1<<32), help='workspace size in bytes') parser.add_argument('--cache', action='store_true', help='If set, graphs will be saved to disk after conversion. If a converted graph is present on disk, it will be loaded instead of building the graph again.') From 4d2069447acb5888b212534b1e8274d8d24376cd Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 17:02:15 -0800 Subject: [PATCH 2/6] Print graph size for both TF and TRT graphs --- .../image_classification.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tftrt/examples/image-classification/image_classification.py b/tftrt/examples/image-classification/image_classification.py index 998ef3e3a..a02a0f059 100644 --- a/tftrt/examples/image-classification/image_classification.py +++ b/tftrt/examples/image-classification/image_classification.py @@ -442,6 +442,7 @@ def get_frozen_graph( """ num_nodes = {} times = {} + graph_sizes = {} # Load from pb file if frozen graph was already created and cached if cache: @@ -456,11 +457,13 @@ def get_frozen_graph( times['loading_frozen_graph'] = time.time() - start_time num_nodes['loaded_frozen_graph'] = len(frozen_graph.node) num_nodes['trt_only'] = len([1 for n in frozen_graph.node if str(n.op)=='TRTEngineOp']) - return frozen_graph, num_nodes, times + graph_sizes['loaded_frozen_graph'] = len(frozen_graph.SerializeToString()) + return frozen_graph, num_nodes, times, graph_sizes # Build graph and load weights frozen_graph = build_classification_graph(model, model_dir, default_models_dir) num_nodes['native_tf'] = len(frozen_graph.node) + graph_sizes['native_tf'] = len(frozen_graph.SerializeToString()) # Convert to TensorRT graph if use_trt: @@ -477,9 +480,11 @@ def get_frozen_graph( times['trt_conversion'] = time.time() - start_time num_nodes['tftrt_total'] = len(frozen_graph.node) num_nodes['trt_only'] = len([1 for n in frozen_graph.node if str(n.op)=='TRTEngineOp']) + graph_sizes['trt'] = len(frozen_graph.SerializeToString()) if precision == 'int8': calib_graph = frozen_graph + graph_sizes['calib'] = len(calib_graph.SerializeToString()) # INT8 calibration step print('Calibrating INT8...') start_time = time.time() @@ -490,6 +495,8 @@ def get_frozen_graph( start_time = time.time() frozen_graph = trt.calib_graph_to_infer_graph(calib_graph) times['trt_int8_conversion'] = time.time() - start_time + # This is already set but overwriting it here to ensure the right size + graph_sizes['trt'] = len(frozen_graph.SerializeToString()) del calib_graph print('INT8 graph created.') @@ -506,7 +513,7 @@ def get_frozen_graph( f.write(frozen_graph.SerializeToString()) times['saving_frozen_graph'] = time.time() - start_time - return frozen_graph, num_nodes, times + return frozen_graph, num_nodes, times, graph_sizes if __name__ == '__main__': parser = argparse.ArgumentParser(description='Evaluate model') @@ -577,7 +584,7 @@ def get_files(data_dir, filename_pattern): calib_files = get_files(args.calib_data_dir, 'train*') # Retreive graph using NETS table in graph.py - frozen_graph, num_nodes, times = get_frozen_graph( + frozen_graph, num_nodes, times, graph_sizes = get_frozen_graph( model=args.model, model_dir=args.model_dir, use_trt=args.use_trt, @@ -592,16 +599,15 @@ def get_files(data_dir, filename_pattern): default_models_dir=args.default_models_dir, max_workspace_size=args.max_workspace_size) - def print_dict(input_dict, str=''): + def print_dict(input_dict, str='', scale=None): for k, v in sorted(input_dict.items()): headline = '{}({}): '.format(str, k) if str else '{}: '.format(k) + v = v * scale if scale else v print('{}{}'.format(headline, '%.1f'%v if type(v)==float else v)) - serialized_graph = frozen_graph.SerializeToString() - print('frozen graph size: {}'.format(len(serialized_graph))) - print_dict(vars(args)) print_dict(num_nodes, str='num_nodes') + print_dict(graph_sizes, str='graph_size(MB)', scale=1./(1<<20)) print_dict(times, str='time(s)') # Evaluate model From 7fe06a3ef03370e760dfbd900d1d5c5b2e872b12 Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 17:14:13 -0800 Subject: [PATCH 3/6] Fix third_party submodules --- tftrt/examples/object_detection/third_party/cocoapi | 1 - tftrt/examples/object_detection/third_party/models | 1 - 2 files changed, 2 deletions(-) delete mode 160000 tftrt/examples/object_detection/third_party/cocoapi delete mode 160000 tftrt/examples/object_detection/third_party/models diff --git a/tftrt/examples/object_detection/third_party/cocoapi b/tftrt/examples/object_detection/third_party/cocoapi deleted file mode 160000 index ed842bffd..000000000 --- a/tftrt/examples/object_detection/third_party/cocoapi +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ed842bffd41f6ff38707c4f0968d2cfd91088688 diff --git a/tftrt/examples/object_detection/third_party/models b/tftrt/examples/object_detection/third_party/models deleted file mode 160000 index 402b561b0..000000000 --- a/tftrt/examples/object_detection/third_party/models +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 402b561b03857151f684ee00b3d997e5e6be9778 From 1af92026db37289f964928c2d4adaad02f1382cf Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 17:25:10 -0800 Subject: [PATCH 4/6] Revert change in .gitmodules --- .gitmodules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 36fd3ef55..2688d24bc 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "third_party/models"] - path = tftrt/examples/third_party/models + path = tftrt/examples/object_detection/third_party/models url = https://github.com/tensorflow/models [submodule "third_party/cocoapi"] - path = tftrt/examples/third_party/cocoapi + path = tftrt/examples/object_detection/third_party/cocoapi url = https://github.com/cocodataset/cocoapi From b1754def74c3743621da1b57a556a205078f6c6e Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 17:36:43 -0800 Subject: [PATCH 5/6] Update submodules --- .gitmodules | 6 ++++++ tftrt/examples/third_party/cocoapi | 1 + tftrt/examples/third_party/models | 1 + 3 files changed, 8 insertions(+) create mode 160000 tftrt/examples/third_party/cocoapi create mode 160000 tftrt/examples/third_party/models diff --git a/.gitmodules b/.gitmodules index 2688d24bc..6c8f43414 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,9 @@ [submodule "third_party/cocoapi"] path = tftrt/examples/object_detection/third_party/cocoapi url = https://github.com/cocodataset/cocoapi +[submodule "tftrt/examples/third_party/models"] + path = tftrt/examples/third_party/models + url = https://github.com/tensorflow/models.git +[submodule "tftrt/examples/third_party/cocoapi"] + path = tftrt/examples/third_party/cocoapi + url = https://github.com/cocodataset/cocoapi.git diff --git a/tftrt/examples/third_party/cocoapi b/tftrt/examples/third_party/cocoapi new file mode 160000 index 000000000..ed842bffd --- /dev/null +++ b/tftrt/examples/third_party/cocoapi @@ -0,0 +1 @@ +Subproject commit ed842bffd41f6ff38707c4f0968d2cfd91088688 diff --git a/tftrt/examples/third_party/models b/tftrt/examples/third_party/models new file mode 160000 index 000000000..402b561b0 --- /dev/null +++ b/tftrt/examples/third_party/models @@ -0,0 +1 @@ +Subproject commit 402b561b03857151f684ee00b3d997e5e6be9778 From 24945f61756077a42ae8c380c71b390dc82cd14e Mon Sep 17 00:00:00 2001 From: Pooya Davoodi Date: Fri, 21 Dec 2018 17:47:43 -0800 Subject: [PATCH 6/6] Update submodule --- .gitmodules | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.gitmodules b/.gitmodules index 6c8f43414..9aa63ca53 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,3 @@ -[submodule "third_party/models"] - path = tftrt/examples/object_detection/third_party/models - url = https://github.com/tensorflow/models -[submodule "third_party/cocoapi"] - path = tftrt/examples/object_detection/third_party/cocoapi - url = https://github.com/cocodataset/cocoapi [submodule "tftrt/examples/third_party/models"] path = tftrt/examples/third_party/models url = https://github.com/tensorflow/models.git