def prepare_emit_ir(graph: nx.MultiDiGraph, data_type: str, output_dir: str, output_model_name: str, mean_data: [list, None] = None, input_names: list = [], meta_info: dict = dict()): for sub_graph in [graph] + collect_sub_graphs(graph): create_const_nodes(sub_graph, start_data_nodes_are_not_allowed=(sub_graph == graph)) op_order, data_order = determined_sort(get_sorted_outputs(sub_graph)) mapping = {v: u for u, v in enumerate(op_order)} mapping.update({v: u for u, v in enumerate(data_order, start=len(sub_graph))}) relabel_nodes_inplace_safe(sub_graph, mapping) port_renumber(sub_graph) convert_data_type.convert(sub_graph, data_type) tensor_names.propagate_op_name_to_tensor(graph) weights = np.array([]) bin_file = os.path.join(output_dir, '{}.bin'.format(output_model_name)) if(data_type == "FP16"): weights = serialize_constants(weights,graph,data_type=np.float16) elif(data_type == "FP32"): weights = serialize_constants(weights,graph,data_type=np.float32) mean_offset = None mean_size = None if mean_data: mean_offset, mean_size = serialize_mean_image(bin_file, mean_data=mean_data) xml_string = generate_ie_ir(graph=graph, file_name=os.path.join(output_dir, '{}.xml'.format(output_model_name)), input_names=input_names, mean_offset=mean_offset, mean_size=mean_size, meta_info=meta_info) # tensor_names.output_tensor_names_map(graph, os.path.join(output_dir, '{}.mapping'.format(output_model_name))) return weights, xml_string
def test_convert_blob_to_int32_with_force_precision_error(self): graph = build_graph( nodes_attributes, [('data_node', 'op_node', { 'bin': 1 })], { 'data_node': { 'force_precision': 'int32', 'value': np.array([4.0, 3.0, 2.0, 1.1], dtype=np.float64) } }) with self.assertRaisesRegex(Error, '.*results in rounding.*'): convert(graph, "FP32")
def test_convert_blob_to_fp16_from_fp64_overflow(self): graph = build_graph( nodes_attributes, [('data_node', 'op_node', { 'bin': 1 })], { 'data_node': { 'value': np.array([4.0, 3.0, 2.0, 1e10], dtype=np.float64) } }) convert(graph, "FP16") result_value = graph.node['data_node']['value'] self.assertTrue(result_value.dtype == np.float16) self.assertListEqual(list(result_value), [4, 3, 2, np.inf])
def test_convert_blob_to_int32_with_force_precision(self): graph = build_graph( nodes_attributes, [('data_node', 'op_node', { 'bin': 1 })], { 'data_node': { 'force_precision': 'int32', 'value': np.array([4.0, 3.0, 2.0, 1.0], dtype=np.float64) } }) convert(graph, "FP32") result_value = graph.node['data_node']['value'] self.assertTrue(result_value.dtype == np.int32) self.assertListEqual(list(result_value), [4, 3, 2, 1])
def prepare_emit_ir(graph: Graph, data_type: str, output_dir: str, output_model_name: str, mean_data: [list, None] = None, input_names: list = [], meta_info: dict = dict()): graph.strict_mode = False for sub_graph in [graph] + collect_sub_graphs(graph): op_order, data_order = determined_sort(get_sorted_outputs(sub_graph)) mapping = {v: u for u, v in enumerate(op_order)} mapping.update( {v: u for u, v in enumerate(data_order, start=len(sub_graph))}) relabel_nodes_inplace_safe(sub_graph, mapping) port_renumber(sub_graph) convert_data_type.convert(sub_graph, data_type) tensor_names.propagate_op_name_to_tensor(graph) bin_file = os.path.join(output_dir, '{}.bin'.format(output_model_name)) serialize_constants(graph, bin_file) mean_offset = None mean_size = None if mean_data: mean_offset, mean_size = serialize_mean_image(bin_file, mean_data=mean_data) generate_ie_ir(graph=graph, file_name=os.path.join(output_dir, '{}.xml'.format(output_model_name)), input_names=input_names, mean_offset=mean_offset, mean_size=mean_size, meta_info=meta_info) tensor_names.output_tensor_names_map( graph, os.path.join(output_dir, '{}.mapping'.format(output_model_name)))