2
0

app.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. ''' Expermiental Python Server backend test '''
  3. import os
  4. import netron
  5. root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
  6. third_party_dir = os.path.join(root_dir, 'third_party')
  7. def _test_onnx():
  8. file = os.path.join(third_party_dir, 'test', 'onnx', 'candy.onnx')
  9. import onnx # pylint: disable=import-outside-toplevel
  10. model = onnx.load(file)
  11. netron.serve('x.onnx', model, browse=True, verbosity='quiet')
  12. def _test_onnx_list():
  13. folder = os.path.join(third_party_dir, 'test', 'onnx')
  14. for item in os.listdir(folder):
  15. file = os.path.join(folder, item)
  16. if file.endswith('.onnx') and \
  17. item != 'super_resolution.onnx' and \
  18. item != 'arcface-resnet100.onnx':
  19. print(item)
  20. import onnx # pylint: disable=import-outside-toplevel
  21. model = onnx.load(file)
  22. address = netron.serve('x.onnx', model, verbosity='quiet')
  23. netron.stop(address)
  24. def _test_torchscript():
  25. import torch # pylint: disable=import-outside-toplevel disable
  26. import torchvision # pylint: disable=import-outside-toplevel
  27. model = torchvision.models.resnet34(weights=torchvision.models.ResNet34_Weights.DEFAULT)
  28. args = torch.zeros([1, 3, 224, 224]) # pylint: disable=no-member
  29. graph = torch.jit._get_trace_graph(model, args) # pylint: disable=protected-access
  30. # graph = torch.onnx._optimize_trace(graph, torch.onnx.OperatorExportTypes.ONNX)
  31. # https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/ir/ir.h
  32. netron.serve('x.pt', graph, browse=True, verbosity='quiet')
  33. # _test_onnx()
  34. # _test_torchscript()
  35. _test_onnx_list()