2
0

onnx_script.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ''' ONNX metadata script '''
  2. import collections
  3. import io
  4. import json
  5. import os
  6. import re
  7. import sys
  8. categories = {
  9. 'Constant': 'Constant',
  10. 'Conv': 'Layer',
  11. 'ConvInteger': 'Layer',
  12. 'ConvTranspose': 'Layer',
  13. 'FC': 'Layer',
  14. 'RNN': 'Layer',
  15. 'LSTM': 'Layer',
  16. 'GRU': 'Layer',
  17. 'Gemm': 'Layer',
  18. 'FusedConv': 'Layer',
  19. 'Dropout': 'Dropout',
  20. 'Elu': 'Activation',
  21. 'HardSigmoid': 'Activation',
  22. 'LeakyRelu': 'Activation',
  23. 'PRelu': 'Activation',
  24. 'ThresholdedRelu': 'Activation',
  25. 'Relu': 'Activation',
  26. 'Selu': 'Activation',
  27. 'Sigmoid': 'Activation',
  28. 'Tanh': 'Activation',
  29. 'LogSoftmax': 'Activation',
  30. 'Softmax': 'Activation',
  31. 'Softplus': 'Activation',
  32. 'Softsign': 'Activation',
  33. 'Clip': 'Activation',
  34. 'BatchNormalization': 'Normalization',
  35. 'InstanceNormalization': 'Normalization',
  36. 'LpNormalization': 'Normalization',
  37. 'LRN': 'Normalization',
  38. 'Flatten': 'Shape',
  39. 'Reshape': 'Shape',
  40. 'Tile': 'Shape',
  41. 'Xor': 'Logic',
  42. 'Not': 'Logic',
  43. 'Or': 'Logic',
  44. 'Less': 'Logic',
  45. 'And': 'Logic',
  46. 'Greater': 'Logic',
  47. 'Equal': 'Logic',
  48. 'AveragePool': 'Pool',
  49. 'GlobalAveragePool': 'Pool',
  50. 'GlobalLpPool': 'Pool',
  51. 'GlobalMaxPool': 'Pool',
  52. 'LpPool': 'Pool',
  53. 'MaxPool': 'Pool',
  54. 'MaxRoiPool': 'Pool',
  55. 'Concat': 'Tensor',
  56. 'Slice': 'Tensor',
  57. 'Split': 'Tensor',
  58. 'Pad': 'Tensor',
  59. 'ImageScaler': 'Data',
  60. 'Crop': 'Data',
  61. 'Upsample': 'Data',
  62. 'Transpose': 'Transform',
  63. 'Gather': 'Transform',
  64. 'Unsqueeze': 'Transform',
  65. 'Squeeze': 'Transform',
  66. }
  67. attribute_type_table = {
  68. 'undefined': None,
  69. 'float': 'float32', 'int': 'int64', 'string': 'string',
  70. 'tensor': 'tensor', 'graph': 'graph',
  71. 'floats': 'float32[]', 'ints': 'int64[]', 'strings': 'string[]',
  72. 'tensors': 'tensor[]', 'graphs': 'graph[]',
  73. }
  74. def _get_attr_type(attribute_type, attribute_name, op_type, op_domain):
  75. key = op_domain + ':' + op_type + ':' + attribute_name
  76. if key in (':Cast:to', ':EyeLike:dtype', ':RandomNormal:dtype'):
  77. return 'DataType'
  78. value = str(attribute_type)
  79. value = value[value.rfind('.')+1:].lower()
  80. if value in attribute_type_table:
  81. return attribute_type_table[value]
  82. return None
  83. def _get_attr_default_value(attr_value):
  84. if not str(attr_value):
  85. return None
  86. if attr_value.HasField('i'):
  87. return attr_value.i
  88. if attr_value.HasField('s'):
  89. return attr_value.s.decode('utf8')
  90. if attr_value.HasField('f'):
  91. return attr_value.f
  92. return None
  93. def _generate_json_support_level_name(support_level):
  94. value = str(support_level)
  95. return value[value.rfind('.')+1:].lower()
  96. def _format_description(description):
  97. def replace_line(match):
  98. link = match.group(1)
  99. url = match.group(2)
  100. if not url.startswith("http://") and not url.startswith("https://"):
  101. url = "https://github.com/onnx/onnx/blob/master/docs/" + url
  102. return "[" + link + "](" + url + ")"
  103. description = re.sub("\\[(.+)\\]\\(([^ ]+?)( \"(.+)\")?\\)", replace_line, description)
  104. return description
  105. def _metadata():
  106. json_root = []
  107. import onnx.backend.test.case # pylint: disable=import-outside-toplevel
  108. import onnx.defs # pylint: disable=import-outside-toplevel
  109. snippets = onnx.backend.test.case.collect_snippets()
  110. all_schemas_with_history = onnx.defs.get_all_schemas_with_history()
  111. for schema in all_schemas_with_history:
  112. json_schema = {}
  113. json_schema['name'] = schema.name
  114. json_schema['module'] = schema.domain if schema.domain else 'ai.onnx'
  115. json_schema['version'] = schema.since_version
  116. json_schema['support_level'] = _generate_json_support_level_name(schema.support_level)
  117. if schema.doc:
  118. json_schema['description'] = _format_description(schema.doc.lstrip())
  119. if schema.attributes:
  120. json_schema['attributes'] = []
  121. for _ in collections.OrderedDict(schema.attributes.items()).values():
  122. json_attribute = {}
  123. json_attribute['name'] = _.name
  124. attribute_type = _get_attr_type(_.type, _.name, schema.name, schema.domain)
  125. if attribute_type:
  126. json_attribute['type'] = attribute_type
  127. elif 'type' in json_attribute:
  128. del json_attribute['type']
  129. json_attribute['required'] = _.required
  130. default_value = _get_attr_default_value(_.default_value)
  131. if default_value:
  132. json_attribute['default'] = default_value
  133. json_attribute['description'] = _format_description(_.description)
  134. json_schema['attributes'].append(json_attribute)
  135. if schema.inputs:
  136. json_schema['inputs'] = []
  137. for _ in schema.inputs:
  138. json_input = {}
  139. json_input['name'] = _.name
  140. json_input['type'] = _.typeStr
  141. if _.option == onnx.defs.OpSchema.FormalParameterOption.Optional:
  142. json_input['option'] = 'optional'
  143. elif _.option == onnx.defs.OpSchema.FormalParameterOption.Variadic:
  144. json_input['list'] = True
  145. json_input['description'] = _format_description(_.description)
  146. json_schema['inputs'].append(json_input)
  147. json_schema['min_input'] = schema.min_input
  148. json_schema['max_input'] = schema.max_input
  149. if schema.outputs:
  150. json_schema['outputs'] = []
  151. for _ in schema.outputs:
  152. json_output = {}
  153. json_output['name'] = _.name
  154. json_output['type'] = _.typeStr
  155. if _.option == onnx.defs.OpSchema.FormalParameterOption.Optional:
  156. json_output['option'] = 'optional'
  157. elif _.option == onnx.defs.OpSchema.FormalParameterOption.Variadic:
  158. json_output['list'] = True
  159. json_output['description'] = _format_description(_.description)
  160. json_schema['outputs'].append(json_output)
  161. json_schema['min_output'] = schema.min_output
  162. json_schema['max_output'] = schema.max_output
  163. def format_range(value):
  164. return '∞' if value == 2147483647 else str(value)
  165. if schema.min_input != schema.max_input:
  166. json_schema['inputs_range'] = format_range(schema.min_input) + ' - ' \
  167. + format_range(schema.max_input)
  168. if schema.min_output != schema.max_output:
  169. json_schema['outputs_range'] = format_range(schema.min_output) + ' - ' \
  170. + format_range(schema.max_output)
  171. if schema.type_constraints:
  172. json_schema['type_constraints'] = []
  173. for type_constraint in schema.type_constraints:
  174. json_schema['type_constraints'].append({
  175. 'description': type_constraint.description,
  176. 'type_param_str': type_constraint.type_param_str,
  177. 'allowed_type_strs': type_constraint.allowed_type_strs
  178. })
  179. if schema.name in snippets:
  180. def update_code(code):
  181. lines = code.splitlines()
  182. while len(lines) > 0 and re.search("\\s*#", lines[-1]):
  183. lines.pop()
  184. if len(lines) > 0 and len(lines[-1]) == 0:
  185. lines.pop()
  186. return '\n'.join(lines)
  187. json_schema['examples'] = []
  188. for summary, code in sorted(snippets[schema.name]):
  189. json_schema['examples'].append({
  190. 'summary': summary,
  191. 'code': update_code(code)
  192. })
  193. if schema.name in categories:
  194. json_schema['category'] = categories[schema.name]
  195. json_root.append(json_schema)
  196. json_root = sorted(json_root, key=lambda item: item['name'] + ':' + \
  197. str(item['version'] if 'version' in item else 0).zfill(4))
  198. json_file = os.path.join(os.path.dirname(__file__), '../source/onnx-metadata.json')
  199. with io.open(json_file, 'r', encoding='utf-8') as file:
  200. content = file.read()
  201. items = json.loads(content)
  202. items = list(filter(lambda item: item['module'] == "com.microsoft", items))
  203. json_root = json_root + items
  204. json_root = json.dumps(json_root, indent=2)
  205. with io.open(json_file, 'w', encoding='utf-8', newline='') as file:
  206. for line in json_root.splitlines():
  207. file.write(line.rstrip() + '\n')
  208. def _infer():
  209. import onnx # pylint: disable=import-outside-toplevel
  210. import onnx.shape_inference # pylint: disable=import-outside-toplevel
  211. file = sys.argv[2]
  212. base = os.path.splitext(file)[0]
  213. onnx_model = onnx.load(base + '.onnx')
  214. onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
  215. onnx.save(onnx_model, base + '.shape.onnx')
  216. def main(): # pylint: disable=missing-function-docstring
  217. command_table = { 'metadata': _metadata, 'infer': _infer }
  218. command = sys.argv[1]
  219. command_table[command]()
  220. if __name__ == '__main__':
  221. main()