TNN Model Converter教程

1.安装docekr 

首先安装依赖
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
信任 Docker 的 GPG 公钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
对于 amd64 架构的计算机,添加软件仓库
 
sudo add-apt-repository \
 "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
最后安装
sudo apt-get update
sudo apt-get install docker-ce
把当前用户加到docker用户组中
添加docker用户组
 
sudo groupadd docker
 
把自己加到docker用户组中
#myusername   是你自己的用户名
sudo gpasswd -a myusername docker
 
重启docker后台服务
 
sudo service docker restart
切换当前用户到新group
 
newgrp - docker
确定docker可以非sudo运行
 
docker ps

2.拉取构建好的 docker 镜像

docker pull turandotkay/tnn-convert

通过docker images 来查看是否构建成功

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
turandotkay/tnn-convert   latest              28c93a738b08        15 minutes ago      2.81GB

我们发现pull 下来的 docker 镜像的 REPOSIOTY 的名称太长了,我们可以通过下面的命令进行重命名:

docker tag turandotkay/tnn-convert:latest tnn-convert:latest
docker rmi turandotkay/tnn-convert:latest

此时再次执行 docker images 命令,会得到下面的类似的输出:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tnn-convert         latest              28c93a738b08        16 minutes ago      2.81GB

构建 docker 镜像(如果上面已经拉取了 image,这一步,可直接跳过)

cd <path-to-tnn>/
docker build -t tnn-convert:latest .

docker 会根据 Dockerfile 文件进行构建,这需要等待一会。等构建完成之后,你可以通过下面的命令进行验证是否构建完成。

docker images

在输出的列表中会有下面类似的输出,这表明docker 的镜像已经构建好了。

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tnn-convert         latest              9fb83110d2c9        26 minutes ago      2.79GB

convert2tnn 工具进行转换

首先验证下 docker 镜像能够正常使用,首先我们通过下面的命令来看下 convert2tnn 的帮助信息:

docker run  -it tnn-convert:latest  python3 ./converter.py -h

如果docker 镜像是正确的话,你会得到下面的输出:


usage: convert [-h] {onnx2tnn,caffe2tnn,tf2tnn} ...

convert ONNX/Tensorflow/Caffe model to TNN model

positional arguments:
  {onnx2tnn,caffe2tnn,tf2tnn}
    onnx2tnn            convert onnx model to tnn model
    caffe2tnn           convert caffe model to tnn model
    tf2tnn              convert tensorflow model to tnn model
    tflite2tnn          convert tensorflow-lite model to tnn model

optional arguments:
  -h, --help            show this help message and exit

从上面的帮助信息中,我们可以得知,目前 convert2tnn 提供了 3 种模型格式的转换支持。假设我们这里想将 TensorFlow 模型转换成 TNN 模型,我们输入下面的命令继续获得帮助信息:

docker run  -it tnn-convert:latest  python3 ./converter.py tf2tnn -h

得到的输出信息如下:

usage: convert tf2tnn [-h] -tp TF_PATH -in input_info [input_info ...] -on output_name [output_name ...] [-o OUTPUT_DIR] [-v v1.0] [-optimize] [-half] [-align] [-input_file INPUT_FILE_PATH]
                      [-ref_file REFER_FILE_PATH]

optional arguments:
  -h, --help            show this help message and exit
  -tp TF_PATH           the path for tensorflow graphdef file
  -in input_info [input_info ...]
                        specify the input name and shape of the model. e.g., -in input1_name:1,128,128,3 input2_name:1,256,256,3
  -on output_name [output_name ...]
                        the tensorflow model's output name. e.g. -on output_name1 output_name2
  -o OUTPUT_DIR         the output tnn directory
  -v v1.0               the version for model
  -optimize             If the model has fixed input shape, use this option to optimize the model for speed. On the other hand, if the model has dynamic input shape, dont use this option. It may cause warong result
  -half                 save the model using half
  -align                align the onnx model with tnn model
  -input_file INPUT_FILE_PATH
                        the input file path which contains the input data for the inference model.
  -ref_file REFER_FILE_PATH
                        the reference file path which contains the reference data to compare the results.

 执行如下命令

 

猜你喜欢

转载自blog.csdn.net/wzhrsh/article/details/116230688