def mmdnn_to_annette(args): """Convert MMDNN graph .pb-File to annette json format and stores to Annette graph std path. Args: network name (str): network name Returns: :obj:`annette.AnnetteGraph`: AnnetteGraph object """ graphfile = get_database( 'graphs', 'mmdnn', args.network+'.pb') if(os.path.exists(graphfile)): print("Graph found") elif(os.path.exists(args.network)): print("Graph-file detected") # extract network name args.network = os.path.split(args.network)[1].split('.pb')[0] graphfile = get_database( 'graphs', 'mmdnn', args.network+'.pb') else: logging.error("File not found") weightfile = None mmdnn_graph = MMGraph(graphfile, weightfile) annette_graph = mmdnn_graph.convert_to_annette(args.network) json_file = get_database( 'graphs', 'annette', annette_graph.model_spec["name"]+'.json') annette_graph.to_json(json_file) return annette_graph
def estimate(args): """Estimate example function Args: network name (str): network name Returns: float: estimated time in ms """ model = AnnetteGraph( args.network, get_database('graphs', 'annette', args.network + '.json')) if args.mapping != "none": opt = Mapping_model.from_json( get_database('models', 'mapping', args.mapping + '.json')) opt.run_optimization(model) # LOAD MODELS mod = Layer_model.from_json( get_database('models', 'layer', args.layer + '.json')) # APPLY ESTIMATION res = mod.estimate_model(model) write_result(args.network, res, args.mapping, args.layer, get_database('results')) return res[0], res[2]
def test_read_ncs2_report(network="benchmark_average_counters_report"): report_file = get_database('benchmarks', 'tmp', network + '.csv') ncs2.read_report(report_file) annette_report = ncs2.r2a(report_file) annette_report.to_pickle( get_database('benchmarks', 'tmp', 'annette_bench1.pkl')) assert True
def test_ONNXGraph_to_annette(network="cf_resnet50", inputs=None): network_file = get_database('graphs', 'onnx', network + '.onnx') onnx_network = graph.ONNXGraph(network_file) annette_graph = onnx_network.onnx_to_annette(network, inputs) json_file = get_database('graphs', 'annette', annette_graph.model_spec["name"] + '.json') annette_graph.to_json(json_file) assert True return 0
def test_matcher(network='annette_bench3', shape=None): gen = generator.Graph_generator(network) gen.add_configfile("config_v6.csv") gen.generate_graph_from_config(401) test_net = get_database('graphs', 'tf', network + '.pb') #ncs2.optimize_network(test_net, source_fw = "tf", network = network, image = shape , input_node = "data", save_folder = get_database('benchmarks','tmp')) #test_net = get_database('benchmarks','tmp',network+'.xml') #not running network #ncs2.run_network(test_net, report_dir = get_database('benchmarks','tmp')) test_report = get_database('benchmarks', 'tmp', 'benchmark_average_counters_report.csv') ncs2.read_report(test_report) print(gen.graph.model_spec)
def tf_export_to_pb(self, output_node, save_path=None): # Collect default graph information g = tf.get_default_graph() with tf.Session() as sess: # Initialize the variables sess.run(tf.global_variables_initializer()) g = g.as_graph_def(add_shapes=True) # Convert variables to constants until the "fully_conn_1/Softmax" node frozen_graph_def = tf.graph_util.convert_variables_to_constants( sess, g, output_node) print("load graph") graph_nodes = [n for n in frozen_graph_def.node] names = [] for t in graph_nodes: if not ("Variable" in t.name or "BiasAdd" in t.name): names.append(t.name.replace("/", "_").replace("-", "_")) print(names) # Write the intermediate representation of the graph to .pb file if save_path: net_file = save_path else: net_file = get_database('graphs', 'tf', self.graph.model_spec['name'] + ".pb") print(net_file) with open(os.path.join(net_file), 'wb') as f: graph_string = (frozen_graph_def.SerializeToString()) f.write(graph_string)
def load_estimator(self, est_model=None, est_dict=None): if est_model != None: self.est_model = pickle.load(open(get_database(est_model), 'rb')) self.desc['est_model'] = est_model else: return False self.est_dict = est_dict self.desc['est_dict'] = est_dict return True
def load_model(self, est_model=None, conv_dict=None): if est_model != None: self.est_model = pickle.load(open(get_database(est_model), 'rb')) self.desc['est_model'] = est_model else: print("No Model to be loaded") pass self.conv_dict = conv_dict self.desc['conv_dict'] = conv_dict print("Conversion Dict: ") print(self.conv_dict)
def generate_tf_model(graph): """generates Tensorflow 2 graph out of ANNETTE graph description and stores to benchmark/graphs/tf2/ directory Args: graph :obj:`annette.graph.AnnetteGraph`: annette graph description to generate the tf2 graph from """ # generate tensorflow model and export to out_file # with __dict__ we can see the content of the class logging.debug(graph.__dict__) # model_spec contains some info about the model for key, value in graph.model_spec.items(): logging.debug(key) logging.debug(value) network_name = graph.model_spec['name'] filename = get_database('benchmark', 'graphs', 'tf2', network_name + '.pb') logging.debug("Stored to: %s" % filename)
def add_configfile(self, configfile): self.config = pd.read_csv( get_database('benchmarks', 'config', configfile)) print(self.config)
def __init__(self, network): #load graphstruct json_file = get_database('graphs', 'annette', network + '.json') self.graph = AnnetteGraph(network, json_file) print(self.graph)