parse the ONNX and to create layers Args: model (ModelProto): the loaded ONNX model device (string): CPU or CUDA Returns: a SingaRep instance to stores the layers and weights
(cls, model, device='CPU', **kwargs)
| 1909 | |
| 1910 | @classmethod |
| 1911 | def prepare(cls, model, device='CPU', **kwargs): |
| 1912 | """ |
| 1913 | parse the ONNX and to create layers |
| 1914 | Args: |
| 1915 | model (ModelProto): the loaded ONNX model |
| 1916 | device (string): CPU or CUDA |
| 1917 | Returns: |
| 1918 | a SingaRep instance to stores the layers and weights |
| 1919 | """ |
| 1920 | super(SingaBackend, cls).prepare(model, device, **kwargs) |
| 1921 | # optimize and infer the shape of the model |
| 1922 | try: |
| 1923 | model = onnx.utils.polish_model(model) |
| 1924 | except IndexError as err: |
| 1925 | model = shape_inference.infer_shapes(model) |
| 1926 | |
| 1927 | # check the opset version and ir version |
| 1928 | # SINGA supports opset version(11), ir version(1.6.0 -> 6) |
| 1929 | opset_version = None |
| 1930 | for imp in model.opset_import: |
| 1931 | if not imp.HasField("domain") or imp.domain == "": |
| 1932 | opset_version = imp.version |
| 1933 | if imp.version > cls._opset_version: |
| 1934 | warnings.warn( |
| 1935 | "The imported opertor set verion {} is larger than the supported version {}." |
| 1936 | .format(imp.version, cls._opset_version)) |
| 1937 | else: |
| 1938 | warnings.warn("Unrecognized operator set {}".format(imp.domain)) |
| 1939 | |
| 1940 | if model.ir_version > cls._ir_version: |
| 1941 | warnings.warn( |
| 1942 | "The imported ir verion {} is larger than the supported version {}." |
| 1943 | .format(cls._ir_version, imp.version)) |
| 1944 | |
| 1945 | graph = model.graph |
| 1946 | params, inputs, outputs, layers = cls._onnx_model_to_singa_ops( |
| 1947 | graph, device, opset_version) |
| 1948 | return SingaRep(params, inputs, outputs, layers, device) |
| 1949 | |
| 1950 | |
| 1951 | class SingaRep(BackendRep): |