antinucleon commented on issue #5427:
URL: https://github.com/apache/incubator-tvm/issues/5427#issuecomment-619423150


   Before the parser is fixed, in case someone needs a parser in a hurry, here 
is my emergency use parser: 
   
   ```
   import re
   import random
   
   import tvm
   from tvm import auto_scheduler
   from tvm import relay
   
   class ReRelayParser(object):
       def __init__(self, expr_text, param_dict):
           self.memo = {}
           self.expr_text = expr_text
           self.param_dict = param_dict
   
       def _extract_args(self):
           ARG_LIST = re.compile(r'fn\s\((.*?)\)\s->')
           ARGS = 
re.compile(r'\%([a-zA-Z0-9_]+):\sTensor\[\(([0-9,\s]+)\),\s([a-z0-9]+)\]')
           arg_list = ARG_LIST.findall(self.expr_text)[0]
           args = ARGS.findall(arg_list)
           ret = []
           for name, ss, dtype in args:
               cmd = "{name} = relay.var('{name}', shape=[{shape}], 
dtype='{dtype}')".format(name=name, shape=ss, dtype=dtype)
               ret.append(cmd)
               self.memo["%" + name] = name
           return ret
   
       def _extract_body(self):
           ret = []
           STMT = 
re.compile(r'(\%\d+)\s=\s([a-zA-Z0-9._]+)\(([a-zA-Z\%0-9,\s_=\[\]"]+)\)')
           LAST_STMT = 
re.compile(r'([a-zA-Z0-9._]+)\(([a-zA-Z\%0-9,\s_=\[\]"]+)\)')
           
           def random_name(length=8):
               name = ""
               for i in range(length):
                   name += chr(random.randint(97, 122))
               return name
           
           def process_args(args):
               VAR_ARGS = re.compile(r'(%[0-9a-zA-z_]+)')
               var_arg = VAR_ARGS.findall(args)
               var_len = len(var_arg)
               tmp = args.split(",")
               return var_arg, ",".join(tmp[var_len:])
           
           tmp = STMT.findall(self.expr_text)
           for ss in tmp:
               output = ss[0]
               op = ss[1]
               args = ss[2]
               self.memo[output] = random_name()
               var_args, attrs = process_args(args)
               cmd = "{name} = relay.{op}({var_args}, {attrs})".format(
                       name=self.memo[output],
                       op=op,
                       var_args=", ".join([self.memo[x] for x in var_args]),
                       attrs=attrs
                   )
               ret.append(cmd)
           op, args = LAST_STMT.findall(self.expr_text)[-1]
           var_args, attrs = process_args(args)
           cmd = "output_expr = relay.{op}({var_args}, {attrs})".format(
               op=op,
               var_args=", ".join([self.memo[x] for x in var_args]),
               attrs=attrs
           )
           ret.append(cmd)
           return ret
   
       def _convert_parmas(self):
           ret = {}
           for key in self.param_dict.keys():
               new_key = "v" + key
               ret[new_key] = self.param_dict[key]
           return ret
   
       def convert(self):
           cmds = []
           cmds.extend(self._extract_args())
           cmds.extend(self._extract_body())
           new_params = self._convert_parmas()
           l = {}
           exec("\n".join(cmds), None, l)
           output_expr = l["output_expr"]
           func = relay.Function(relay.analysis.free_vars(output_expr), 
output_expr)
           # mod = tvm.ir.IRModule.from_expr(output_expr)
           return func, new_params
   
   
   if __name__ == "__main__":
       from PIL import Image
       from matplotlib import pyplot as plt
       import numpy as np
       from tvm.contrib.download import download_testdata
   
       image_url = 
'https://github.com/dmlc/mxnet.js/blob/master/data/cat.png?raw=true'
       image_path = download_testdata(image_url, 'cat.png', module='data')
       resized_image = Image.open(image_path).resize((224, 224))
       #plt.imshow(resized_image)
       #plt.show()
       image_data = np.asarray(resized_image).astype("float32")
   
       # Add a dimension to the image so that we have NHWC format layout
       image_data = np.expand_dims(image_data, axis=0)
   
       synset_url = ''.join(['https://gist.githubusercontent.com/zhreshold/',
                           '4d0b62f3d01426887599d4f7ede23ee5/raw/',
                           '596b27d23537e5a1b5751d2b0481ef172f58b539/',
                           'imagenet1000_clsid_to_human.txt'])
       synset_name = 'imagenet1000_clsid_to_human.txt'
       synset_path = download_testdata(synset_url, synset_name, module='data')
       with open(synset_path) as f:
           synset = eval(f.read())
   
       # Preprocess image as described here:
       # 
https://github.com/tensorflow/models/blob/edb6ed22a801665946c63d650ab9a0b23d98e1b1/research/slim/preprocessing/inception_preprocessing.py#L243
       #image_data[:, :, :, 0] = 2.0 / 255.0 * image_data[:, :, :, 0] - 1
       #image_data[:, :, :, 1] = 2.0 / 255.0 * image_data[:, :, :, 1] - 1
       #image_data[:, :, :, 2] = 2.0 / 255.0 * image_data[:, :, :, 2] - 1
       print('input', image_data.shape)
       input_tensor = "float_image_input"
       input_shape = (1, 224, 224, 3)
       input_dtype = "float32"
   
       #############################################################
       with open("tmp.txt") as fi:
           netdef = fi.read()
       
       with open("tmp.params", "rb") as fi:
           net_params = relay.load_param_dict(fi.read())
   
       mod, params = ReRelayParser(netdef, net_params).convert()
   
       target = "llvm -mcpu=core-avx2"
       with relay.build_config(opt_level=3):
           c_graph, c_lib, c_params = relay.build(mod, target, params=params)
       
       from tvm.contrib import graph_runtime as runtime
   
       # Create a runtime executor module
       module1 = runtime.create(c_graph, c_lib, tvm.cpu())
   
       # Feed input data
       module1.set_input(input_tensor, tvm.nd.array(image_data))
   
       # Feed related params
       module1.set_input(**c_params)
   
       # Run
       module1.run()
   
       # Get output
       tvm_output1 = module1.get_output(0).asnumpy()
       top1 = np.argmax(tvm_output1)
       print('Prediction top-1:', top1, synset[top1])
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to