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