def check_node(): # type: () -> None parser = argparse.ArgumentParser('check-node') parser.add_argument('node_pb', type=argparse.FileType('rb')) args = parser.parse_args() node = NodeProto() node.ParseFromString(args.node_pb.read()) checker.check_node(node)
def check_node(cls, node, version=0): version = version or cls.VERSION if version == 0: raise ValueError("version can not be 0.") ctx = checker.C.CheckerContext() ctx.ir_version = onnx.IR_VERSION ctx.opset_imports = {cls.DOMAIN: version} checker.check_node(node, ctx=ctx)
def check_node() -> None: parser = argparse.ArgumentParser('check-node') parser.add_argument('node_pb', type=argparse.FileType('rb')) args = parser.parse_args() node = NodeProto() node.ParseFromString(args.node_pb.read()) checker.check_node(node)
def create_node(func_name, cand, input_names, output_names, parameters): converter_name = 'convert_{}'.format(func_name) if hasattr(functions, converter_name): converter = getattr(functions, converter_name) nodes = converter(cand, input_names, output_names, parameters) else: raise ValueError('{} is not supported.'.format(func_name)) for node in nodes: checker.check_node(node) return nodes
def test_check_node_input_marked_optional(self): # type: () -> None # GivenTensorFill's input is marked optional, hence it is used in this test. node = helper.make_node("GivenTensorFill", [], ["Y"], name="test") checker.check_node(node) # Explicitly pass the empty string as optional node = helper.make_node("GivenTensorFill", [""], ["Y"], name="test") # Input of RELU is not optional node = helper.make_node("Relu", [""], ["Y"], name="test") self.assertRaises(checker.ValidationError, checker.check_node, node)
def caffe2_op_to_node_def(op_def, env): node_def = onnx_pb2.NodeProto() # NB: This must happen BEFORE we start freshening inplace outputs node_def.input.extend(map(env.rename, op_def.input)) node_def.op_type = get_node_op_type(op_def) # Determine what was inplace updates input_set = set(op_def.input) output_set = set(op_def.output) schema = onnx.defs.get_schema(node_def.op_type) # ints does not support extend() consumes = [] for i, x in enumerate(op_def.input): is_consumed, output_idx = schema.consumed(i) if is_consumed == onnx.defs.OpSchema.UseType.CONSUME_ENFORCED: consumes.append(1) elif is_consumed == onnx.defs.OpSchema.UseType.CONSUME_ALLOWED: if x in output_set: consumes.append(1) else: consumes.append(0) else: if x in output_set: raise RuntimeError( "schema says consume not allowed, but caffe2 used inplace syntax" ) consumes.append(0) if any(consumes): consumes_attr = onnx_pb2.AttributeProto() consumes_attr.name = "consumed_inputs" consumes_attr.ints.extend(consumes) else: consumes_attr = None def fresh_or_rename(out): if out in input_set: return env.fresh(out) else: return env.rename(out) node_def.output.extend(map(fresh_or_rename, op_def.output)) # TODO: refactor frontend to allow special handling for individual ops if node_def.op_type == 'Concat': assert len(node_def.output) == 2 del node_def.output[1] node_def.name = op_def.name attrs = get_onnx_attrs(node_def.op_type, op_def) if consumes_attr: attrs.append(consumes_attr) node_def.attribute.extend(attrs) checker.check_node(node_def) return node_def
def test_check_node_input_marked_optional(self): # Constant fill's input is marked optional node = helper.make_node("ConstantFill", [], ["Y"], name="test") checker.check_node(node) # Explicitly pass the empty string as optional node = helper.make_node("ConstantFill", [""], ["Y"], name="test") # Input of RELU is not optional node = helper.make_node("Relu", [""], ["Y"], name="test") self.assertRaises(checker.ValidationError, checker.check_node, node)
def test_check_node_input_marked_optional(self): # type: () -> None # Constant fill's input is marked optional node = helper.make_node( "ConstantFill", [], ["Y"], name="test") checker.check_node(node) # Explicitly pass the empty string as optional node = helper.make_node( "ConstantFill", [""], ["Y"], name="test") # Input of RELU is not optional node = helper.make_node( "Relu", [""], ["Y"], name="test") self.assertRaises(checker.ValidationError, checker.check_node, node)
def eval_onnx_node(self, no_check_set): """Run a Caffe2 program using their ONNX backend. Prior to running the backend, use the Paddle scope to construct ONNX ops and prepare the inputs and output values based on ONNX compatibility. """ # Convert inputs and outputs to ONNX tensors. # Use the Paddle fetch_list to prepare the outputs. inputs = [ paddle_variable_to_onnx_tensor(v, self.block) for v in self.feed_map ] fetch_target_names = [ fetch_target.name for fetch_target in self.fetch_list \ if fetch_target.name not in no_check_set ] outputs = [ paddle_variable_to_onnx_tensor(v, self.block) for v in fetch_target_names ] # Construct the ONNX model using paddle-onnx. onnx_node = ops.node_maker[self.op_type](operator=self.op, block=self.block) node_list = list(onnx_node) if isinstance(onnx_node, tuple) else [onnx_node] for node in node_list: check_node(node) #onnx_graph = make_graph(node_list, self.op_type, inputs, vars) onnx_graph = make_graph(node_list, self.op_type, inputs, outputs) onnx_model = make_model(onnx_graph, producer_name='unittest') # Expand input dictionary if there are tensor arrays input_map = {} for v in self.inputs: if isinstance(self.inputs[v], list): input_map.update(self.inputs[v]) else: input_map[v] = self.inputs[v] # Run the Caffe2Backend with the ONNX model. rep = Caffe2Backend.prepare(onnx_model, device='CPU') in_vals = [input_map[input.name] for input in inputs] outs = rep.run(in_vals) return outs
def test_check_removed_experimental_op(self): # type: () -> None node = helper.make_node("ConstantFill", [], ["Y"], name="test", shape=[1, 2]) checker.check_node(node)
def test_check_node(self): # type: () -> None node = helper.make_node("Relu", ["X"], ["Y"], name="test") checker.check_node(node)
def check_onnx_with_onnxruntime(self, no_check_set=[], is_onnxruntime=False, is_interp=False): """Run a Caffe2 program using their ONNX backend. Prior to running the backend, use the Paddle scope to construct ONNX ops and prepare the inputs and output values based on ONNX compatibility. """ # Convert inputs and outputs to ONNX tensors. # Use the Paddle fetch_list to prepare the outputs. inputs = [ paddle_variable_to_onnx_tensor(v, self.block) for v in self.feed_map ] fetch_target_names = [ fetch_target.name for fetch_target in self.fetch_list \ if fetch_target.name not in no_check_set ] outputs = [ paddle_variable_to_onnx_tensor(v, self.block) for v in fetch_target_names ] # Construct the ONNX model using paddle-onnx. onnx_node = ops.node_maker[self.op_type](operator=self.op, block=self.block) #onnx_node, vars = ops.node_maker[self.op_type](operator=self.op, # block=self.block) node_list = list(onnx_node) if isinstance(onnx_node, tuple) else [onnx_node] for node in node_list: check_node(node) onnx_graph = make_graph(node_list, self.op_type, inputs, outputs) onnx_model = make_model(onnx_graph, producer_name='unittest') if is_onnxruntime: with open("tests/onnx_op/onnxruntime_test.onnx", 'wb') as f: f.write(onnx_model.SerializeToString()) elif is_interp: with open("tests/onnx_op/interp_test.onnx", 'wb') as f: f.write(onnx_model.SerializeToString()) else: with open("tests/nms_test.onnx", 'wb') as f: f.write(onnx_model.SerializeToString()) checker.check_model(onnx_model) # Expand input dictionary if there are tensor arrays input_map = {} for v in self.inputs: if isinstance(self.inputs[v], list): input_map.update(self.inputs[v]) else: input_map[v] = self.inputs[v] in_vals = [input_map[input.name] for input in inputs] with open("tests/inputs_test.pkl", "wb") as f: pickle.dump(input_map, f) ret = os.system("python tests/onnx_runtime.py %s %s" % (is_onnxruntime, is_interp)) #if is_slice: # ret = os.system("python tests/slice_onnx_runtime.py") #elif is_nearest_interp: # ret = os.system("python tests/nearest_onnx_runtime.py") #else: # ret = os.system("python tests/onnx_runtime.py") with open("tests/outputs_test.pkl", "rb") as f: outputs_val = pickle.load(f) f.close() return outputs_val
def test_elu(self): self.assertTrue(defs.has('Elu')) node_def = helper.make_node( 'Elu', ['X'], ['Y'], alpha=1.0) checker.check_node(node_def)
def _conv_json_to_onnx(self, export_path): tflite_model = self._model tflite_tensors = self._model.subgraphs.tensors operator_codes = self._model.operator_codes # graph name onnx_graph_name = self._model_name # graph input& output input_node_indices = tflite_model.subgraphs.inputs output_node_indices = tflite_model.subgraphs.outputs onnx_graph_inputs = [ self._create_value_info(tflite_tensors[index]) for index in input_node_indices ] onnx_graph_outputs = [ self._create_value_info(tflite_tensors[index]) for index in output_node_indices ] # onnx_graph_outputs = [helper.make_tensor_value_info(name=tflite_tensors[output_node_indices[0]].name, # elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[TFLITE_TENSOR_TYPE_TO_NP_TYPE[tflite_tensors[output_node_indices[0]].tensor_type]], # shape=(1,20,4)), # helper.make_tensor_value_info(name=tflite_tensors[output_node_indices[1]].name, # elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[TFLITE_TENSOR_TYPE_TO_NP_TYPE[tflite_tensors[output_node_indices[1]].tensor_type]], # shape=(20,1)), # helper.make_tensor_value_info(name=tflite_tensors[output_node_indices[2]].name, # elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[TFLITE_TENSOR_TYPE_TO_NP_TYPE[tflite_tensors[output_node_indices[2]].tensor_type]], # shape=(20,1)), # helper.make_tensor_value_info(name=tflite_tensors[output_node_indices[3]].name, # elem_type=mapping.NP_TYPE_TO_TENSOR_TYPE[TFLITE_TENSOR_TYPE_TO_NP_TYPE[tflite_tensors[output_node_indices[3]].tensor_type]], # shape=(1,))] nodes = [] for (i, o) in enumerate(tflite_model.subgraphs.operators): # # todo: op & version adaptive op_type_name = operator_codes[o.opcode_index].builtin_code # inputs tflite_input_tensors = [tflite_tensors[idx] for idx in o.inputs] tflite_output_tensors = [tflite_tensors[idx] for idx in o.outputs] tflite_input_buffers = [ tflite_model.np_buffers[tensor.buffer] for tensor in tflite_input_tensors ] tflite_output_buffers = [ tflite_model.np_buffers[tensor.buffer] for tensor in tflite_output_tensors ] ops = tflite_ops.clsmembers if hasattr(ops[op_type_name], "TFLITE_OP" ) and ops[op_type_name].TFLITE_OP == op_type_name: nodes.append(ops[op_type_name].create_onnx_node( operator=o, operator_idx=i, inputs=tflite_input_tensors, outputs=tflite_output_tensors, input_buffers=tflite_input_buffers, output_buffers=tflite_output_buffers, data_format=self.data_format)) else: raise ValueError("OP Not Suppoted") # onnx node onnx_nodes = [] for n in nodes: for on in n.onnx_nodes: checker.check_node(on) onnx_nodes.append(on) # print(getattr(n, "version_1", None)) # graph value_info output_value_info_list = [] graph_value_info = [] output_scalar_names = [] register_names = [input.name for input in onnx_graph_inputs] #register_names.extend([output.name for output in onnx_graph_outputs]) for index in output_node_indices: if (tflite_tensors[index].shape != []): register_names.append(tflite_tensors[index].name) else: output_scalar_names.append(tflite_tensors[index].name) for n in nodes: for value_info in n.onnx_value_infos: if value_info.name in register_names: continue register_names.append(value_info.name) checker.check_value_info(value_info) graph_value_info.append(value_info) if value_info.name in output_scalar_names: output_value_info_list.append(value_info) # remake output value info for index in output_node_indices: if (tflite_tensors[index].shape != []): output_value_info_list.append( self._create_value_info(tflite_tensors[index])) onnx_graph_outputs = output_value_info_list onnx_tensors = self._create_onnx_tensor(nodes) graph = self._create_onnx_graph(nodes=onnx_nodes, name=onnx_graph_name, inputs=onnx_graph_inputs, outputs=onnx_graph_outputs, initializer=onnx_tensors, doc_string=tflite_model.description, value_info=graph_value_info) onnx_model = self._create_onnx_model(graph) inferred_model = shape_inference.infer_shapes(onnx_model) save(inferred_model, path.join(export_path, '{0}.onnx'.format(self.model_name)))
def test_elu(self): # type: () -> None self.assertTrue(defs.has('Elu')) node_def = helper.make_node( 'Elu', ['X'], ['Y'], alpha=1.0) checker.check_node(node_def)
def test_check_node(self): # type: () -> None node = helper.make_node( "Relu", ["X"], ["Y"], name="test") checker.check_node(node)