You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # pnnx
  2. python wrapper of pnnx, only support python 3.7+ now.
  3. Install from pip
  4. ==================
  5. pnnx is available as wheel packages for macOS, Windows and Linux distributions, you can install with pip:
  6. ```
  7. pip install pnnx
  8. ```
  9. # Build & Install from source
  10. ## Prerequisites
  11. **On Unix (Linux, OS X)**
  12. * A compiler with C++14 support
  13. * CMake >= 3.4
  14. **On Mac**
  15. * A compiler with C++14 support
  16. * CMake >= 3.4
  17. **On Windows**
  18. * Visual Studio 2015 or higher
  19. * CMake >= 3.4
  20. ## Build & install
  21. 1. clone ncnn.
  22. ```bash
  23. git clone https://github.com/Tencent/ncnn.git
  24. ```
  25. 2. install pytorch
  26. install pytorch according to https://pytorch.org/ . Anaconda is strongly recommended for example:
  27. ```bash
  28. conda install pytorch
  29. ```
  30. 3. install
  31. ```bash
  32. cd /pathto/ncnntools/pnnx/python
  33. python setup.py install
  34. ```
  35. > **Note:**
  36. > If torchvision and pnnx2onnx are needed, you can set the following environment variables before 'python setup.py install' to enable them. e.g. on ubuntu:
  37. >
  38. > ```
  39. > export TORCHVISION_INSTALL_DIR="/project/torchvision"
  40. > export PROTOBUF_INCLUDE_DIR="/project/protobuf/include"
  41. > export PROTOBUF_LIBRARIES="/project/protobuf/lib64/libprotobuf.a"
  42. > export PROTOBUF_PROTOC_EXECUTABLE="/project/protobuf/bin/protoc"
  43. > ```
  44. >
  45. > To do these, you must install Torchvision and Protobuf first.
  46. ## Tests
  47. ```bash
  48. cd /pathto/ncnn/tools/pnnx/python
  49. pytest tests
  50. ```
  51. ## Usage
  52. 1. export model to pnnx
  53. ```python
  54. import torch
  55. import torchvision.models as models
  56. import pnnx
  57. net = models.resnet18(pretrained=True)
  58. x = torch.rand(1, 3, 224, 224)
  59. # You could try disabling checking when torch tracing raises error
  60. # opt_net = pnnx.export(net, "resnet18.pt", x, check_trace=False)
  61. opt_net = pnnx.export(net, "resnet18.pt", x)
  62. ```
  63. 2. convert existing model to pnnx
  64. ```python
  65. import torch
  66. import pnnx
  67. x = torch.rand(1, 3, 224, 224)
  68. opt_net = pnnx.convert("resnet18.pt", x)
  69. ```
  70. ## API Reference
  71. 1. pnnx.export
  72. `model` (torch.nn.Model): model to be exported.
  73. `ptpath` (str): the torchscript name.
  74. `inputs` (torch.Tensor of list of torch.Tensor) expected inputs of the model.
  75. `inputs2` (torch.Tensor of list of torch.Tensor) alternative inputs of the model. Usually, it is used with input_shapes to resolve dynamic shape.
  76. `input_shapes` (Optional, list of int or list of list with int type inside) shapes of model inputs.
  77. It is used to resolve tensor shapes in model graph. for example, [1,3,224,224] for the model with only
  78. 1 input, [[1,3,224,224],[1,3,224,224]] for the model that have 2 inputs.
  79. `input_types` (Optional, str or list of str) types of model inputs, it should have the same length with `input_shapes`.
  80. for example, "f32" for the model with only 1 input, ["f32", "f32"] for the model that have 2 inputs.
  81. | typename | torch type |
  82. |:--------:|:--------------------------------|
  83. | f32 | torch.float32 or torch.float |
  84. | f64 | torch.float64 or torch.double |
  85. | f16 | torch.float16 or torch.half |
  86. | u8 | torch.uint8 |
  87. | i8 | torch.int8 |
  88. | i16 | torch.int16 or torch.short |
  89. | i32 | torch.int32 or torch.int |
  90. | i64 | torch.int64 or torch.long |
  91. | c32 | torch.complex32 |
  92. | c64 | torch.complex64 |
  93. | c128 | torch.complex128 |
  94. `input_shapes2` (Optional, list of int or list of list with int type inside) shapes of alternative model inputs,
  95. the format is identical to `input_shapes`. Usually, it is used with input_shapes to resolve dynamic shape (-1)
  96. in model graph.
  97. `input_types2` (Optional, str or list of str) types of alternative model inputs.
  98. `device` (Optional, str, default="cpu") device type for the input in TorchScript model, cpu or gpu.
  99. `customop` (Optional, str or list of str) list of Torch extensions (dynamic library) for custom operators.
  100. For example, "/home/nihui/.cache/torch_extensions/fused/fused.so" or
  101. ["/home/nihui/.cache/torch_extensions/fused/fused.so",...].
  102. `moduleop` (Optional, str or list of str) list of modules to keep as one big operator.
  103. for example, "models.common.Focus" or ["models.common.Focus","models.yolo.Detect"].
  104. `optlevel` (Optional, int, default=2) graph optimization level
  105. | option | optimization level |
  106. |:--------:|:----------------------------------|
  107. | 0 | do not apply optimization |
  108. | 1 | do not apply optimization |
  109. | 2 | optimization more for inference |
  110. `pnnxparam` (Optional, str, default="*.pnnx.param", * is the model name): PNNX graph definition file.
  111. `pnnxbin` (Optional, str, default="*.pnnx.bin"): PNNX model weight.
  112. `pnnxpy` (Optional, str, default="*_pnnx.py"): PyTorch script for inference, including model construction
  113. and weight initialization code.
  114. `pnnxonnx` (Optional, str, default="*.pnnx.onnx"): PNNX model in onnx format.
  115. `ncnnparam` (Optional, str, default="*.ncnn.param"): ncnn graph definition.
  116. `ncnnbin` (Optional, str, default="*.ncnn.bin"): ncnn model weight.
  117. `ncnnpy` (Optional, str, default="*_ncnn.py"): pyncnn script for inference.
  118. 2. pnnx.convert
  119. `ptpath` (str): torchscript model to be converted.
  120. Other parameters are consistent with `pnnx.export`