def get_all_backend_handlers(opset_dict): """ Get a dict of all backend handler classes. e.g. {'domain': {'Abs': Abs handler class}, ...}, }. :param opset_dict: A dict of opset. e.g. {'domain': version, ...} :return: Dict. """ handlers = {} for handler in BackendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] handler.VERSION = version since_version = 1 if defs.has(handler.ONNX_OP, domain=handler.DOMAIN): try: since_version = defs.get_schema( handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version).since_version except RuntimeError: warnings.warn( "Fail to get since_version of {} in domain `{}` " "with max_inclusive_version={}. Set to 1.".format( handler.ONNX_OP, handler.DOMAIN, version)) else: warnings.warn("Unknown op {} in domain `{}`.".format( handler.ONNX_OP, handler.DOMAIN or "ai.onnx")) handler.SINCE_VERSION = since_version handlers.setdefault(domain, {})[handler.ONNX_OP] = handler return handlers
def get_all_backend_handlers(opset_dict): """ Get a dict of all backend handler classes. e.g. {'domain': {'Abs': Abs handler class}, ...}, }. :param opset_dict: A dict of opset. e.g. {'domain': version, ...} :return: Dict. """ handlers = {} for handler in BackendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] if domain in opset_dict else 1 handler.VERSION = version since_version = 1 if defs.has(handler.ONNX_OP, domain=handler.DOMAIN): try: since_version = defs.get_schema( # @IgnoreException handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version).since_version except RuntimeError: common.logger.debug("Fail to get since_version of %s in domain `%s` " "with max_inclusive_version=%s. Set to 1.", handler.ONNX_OP, handler.DOMAIN, version) else: common.logger.debug("Unknown op %s in domain `%s`.", handler.ONNX_OP, handler.DOMAIN or "ai.onnx") handler.SINCE_VERSION = since_version handlers.setdefault(domain, {})[handler.ONNX_OP] = handler return handlers
def get_all_frontend_handlers(opset_dict): """ Get a dict of all frontend handler classes. e.g. {'domain': {'Abs': Abs handler class}, ...}, }. :param opset_dict: A dict of opset. e.g. {'domain': version, ...} :return: Dict. """ handlers = {} for handler in FrontendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] handler.VERSION = version since_version = 1 if handler.ONNX_OP and defs.has(handler.ONNX_OP, domain=handler.DOMAIN): since_version = defs.get_schema( handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version).since_version else: warnings.warn("Unknown op {} in domain `{}`. " "Can't check specification by ONNX. " "Please set should_check flag to False " "when call make_node method in handler.".format( handler.ONNX_OP or "Undefined", handler.DOMAIN or "ai.onnx")) handler.SINCE_VERSION = since_version for tf_op in handler.TF_OP: handlers.setdefault(domain, {})[tf_op] = handler return handlers
def test_node_no_arg(self): # type: () -> None self.assertTrue(defs.has("Relu")) node_def = helper.make_node("Relu", ["X"], ["Y"], name="test") self.assertEqual(node_def.op_type, "Relu") self.assertEqual(node_def.name, "test") self.assertEqual(list(node_def.input), ["X"]) self.assertEqual(list(node_def.output), ["Y"])
def get_all_backend_handlers(opset_dict): """Get a dict of all backend handler classes. e.g. {'domain': {'Abs': Abs handler class}, ...}, }. :param opset_dict: A dict of opset. e.g. {'domain': version, ...} :return: Dict. """ handlers = {} for handler in BackendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] if domain in opset_dict else 1 handler.VERSION = version since_version = 1 if defs.has(handler.ONNX_OP, domain=handler.DOMAIN): try: since_version = defs.get_schema( handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version, ).since_version except: logger.warning( f"Fail to get since_version of {handler.ONNX_OP} " f"in domain `{handler.DOMAIN}` " f"with max_inclusive_version={version}. Set to 1.") else: logger.warning( f"Unknown op {handler.ONNX_OP} in domain `{handler.DOMAIN}`.") handler.SINCE_VERSION = since_version handlers.setdefault(domain, {})[handler.ONNX_OP] = handler return handlers
def check_node(node): """Checks if a node is legal. Inputs: node: a NodeProto object. Returns: None An exception is thrown if it does not pass the test. """ # General checks. if not isinstance(node, NodeProto): raise RuntimeError('You cannot pass an object that is not NodeProto.') if not node.op_type: raise NameError('NodeProto does not have a proper op_type set.') if not node.input and not node.output: raise ValueError('NodeProto has zero input and zero output.') if not defs.has(node.op_type): raise NameError('Node op_type {} not recognized by onnx.'.format( node.op_type)) if not defs.get_schema(node.op_type).verify(node.SerializeToString()): raise ValueError( 'NodeProto of type {} did not pass defs schema check.'.format( str(node.op_type))) for attr in node.attribute: if attr.HasField('t'): check_tensor(attr.t) elif attr.HasField('tensors'): for tensor in attr.tensors: check_tensor(tensor)
def get_all_backend_handlers(opset_dict): handlers = {} for handler in BackendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] handler.VERSION = version since_version = 1 if defs.has(handler.ONNX_OP, domain=handler.DOMAIN): try: since_version = defs.get_schema( handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version, ).since_version except RuntimeError: print( "Fail to get since_version of {} in domain `{}` " "with max_inclusive_version={}. Set to 1.".format( handler.ONNX_OP, handler.DOMAIN, version ) ) else: print( "Unknown op {} in domain `{}`.".format( handler.ONNX_OP, handler.DOMAIN or "ai.onnx" ) ) handler.SINCE_VERSION = since_version handlers.setdefault(domain, {})[handler.ONNX_OP] = handler return handlers
def get_all_backend_handlers(opset_dict): """ Get a dict of all backend handler classes. e.g. {'domain': {'Abs': Abs handler class}, ...}, }. :param opset_dict: A dict of opset. e.g. {'domain': version, ...} :return: Dict. """ handlers = {} for handler in BackendHandler.__subclasses__(): handler.check_cls() domain = handler.DOMAIN version = opset_dict[domain] if domain in opset_dict else 1 handler.VERSION = version since_version = 1 if defs.has(handler.ONNX_OP, domain=handler.DOMAIN): try: since_version = defs.get_schema( handler.ONNX_OP, domain=handler.DOMAIN, max_inclusive_version=version).since_version except RuntimeError: # ONNX throws RuntimeError up to 1.8 common_logging(handler, version) except defs.SchemaError: # ONNX changed to defs.SchemaError since 1.9 common_logging(handler, version) else: common.logger.debug("Unknown op {} in domain `{}`.".format( handler.ONNX_OP, handler.DOMAIN or "ai.onnx")) handler.SINCE_VERSION = since_version handlers.setdefault(domain, {})[handler.ONNX_OP] = handler return handlers
def test_node_no_arg(self): # type: () -> None self.assertTrue(defs.has("Relu")) node_def = helper.make_node( "Relu", ["X"], ["Y"], name="test") self.assertEqual(node_def.op_type, "Relu") self.assertEqual(node_def.name, "test") self.assertEqual(list(node_def.input), ["X"]) self.assertEqual(list(node_def.output), ["Y"])
def test_node_with_arg(self): # type: () -> None self.assertTrue(defs.has("Relu")) # Note: Relu actually does not need an arg, but let's # test it. node_def = helper.make_node("Relu", ["X"], ["Y"], arg_value=1) self.assertEqual(node_def.op_type, "Relu") self.assertEqual(list(node_def.input), ["X"]) self.assertEqual(list(node_def.output), ["Y"]) self.assertEqual(len(node_def.attribute), 1) self.assertEqual(node_def.attribute[0], helper.make_attribute("arg_value", 1))
def test_node_with_arg(self): # type: () -> None self.assertTrue(defs.has("Relu")) # Note: Relu actually does not need an arg, but let's # test it. node_def = helper.make_node( "Relu", ["X"], ["Y"], arg_value=1) self.assertEqual(node_def.op_type, "Relu") self.assertEqual(list(node_def.input), ["X"]) self.assertEqual(list(node_def.output), ["Y"]) self.assertEqual(len(node_def.attribute), 1) self.assertEqual( node_def.attribute[0], helper.make_attribute("arg_value", 1))
def test_relu(self): # type: () -> None self.assertTrue(defs.has('Relu')) helper.make_node( 'Relu', ['X'], ['Y'])
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 test_relu(self): self.assertTrue(defs.has('Relu')) node_def = helper.make_node('Relu', ['X'], ['Y'])
# -*- coding: utf-8 -*- # @Time : 2021/3/28 20:15 # @Author : zxf import onnx from onnx import defs from onnx_tf.backend import prepare print(defs.has("DynamicSlice")) model_onnx = onnx.load('./model/model_simple.onnx') # Check the model # onnx.checker.check_model(model_onnx) tf_rep = prepare(model_onnx, strict=False) # Export model as .pb file tf_rep.export_graph('./model/model_simple.pb')
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)