当前位置:   article > 正文

vitis-ai-gpu FPGA实现部分_freeze fpga

freeze fpga

来源:目前我们的板子是1.3版本 2020.12

a. 几个代码教程:https://github.com/Xilinx/Vitis-In-Depth-Tutorial/tree/master/Machine_Learning 其中example2的代码主要供我们参考

b.  安装系统:https://github.com/Xilinx/Vitis-AI/blob/master/demo/VART/README.md

c. 安装docker(其中碰到的nvidia-container-runtime问题,参考另外一篇博客):https://github.com/Xilinx/Vitis-AI     https://github.com/Xilinx/Vitis-AI/blob/master/docs/install_docker/README.md

d. compile 部分:https://forums.xilinx.com/t5/Vitis-HLS-Alveo-ML%E4%BB%A5%E5%8F%8A%E5%85%B6%E4%BB%96%E8%BD%AF%E4%BB%B6%E5%BA%94%E7%94%A8/vitis-ai-1-3%E7%BC%96%E8%AF%91%E6%8A%A5%E9%94%99/td-p/1187929 目前需要用另外一个pb转换工具

e. 用户指导手册:大量api 很重要!https://japan.xilinx.com/html_docs/vitis_ai/1_3/compiling_model.html#hnl1605502495146__section_ehb_d2y_ykb

最重要的是:我们最终对应处理需要的都是网络inference的代码 去freeze 不要搞错了

启动docker:sudo ./docker_run.sh xilinx/vitis-ai-gpu:latest 重启:sudo systemctl restart docker

1. 首先 要将tensorflow model转换为对应的pb格式(checkpoint格式对应也可以 但是需要修改源码 比较繁琐 因为推荐用save_model的方式将原本的文件转换过来)如下图所示:

saved_model_cli show --dir ./save_model --all 这个命令可以看输入输出tensor的名称

  1. def compress():
  2. """Compresses an image."""
  3. img = tf.placeholder(tf.float32, [None, None, None, 3])
  4. with tf.name_scope('intra'):
  5. # Transform and compress the image, then remove batch dimension.
  6. y = analysis_transform(img, args.conv_filters, args.num_filters)
  7. with tf.Session() as sess:
  8. # Load the latest model checkpoint, get the compressed string and the tensor
  9. # shapes.
  10. latest = tf.train.latest_checkpoint(checkpoint_dir=args.checkpoint_dir)
  11. tf.train.Saver().restore(sess, save_path=latest)
  12. print('save model ing')
  13. tf.saved_model.simple_save(sess,"./save_model_e", inputs={"inputs":img}, outputs={"outputs":y})

对应inputs和outputs即为我们保存的节点,后续freeze针对的输出就是这个outputs节点。

2. freeze部分

第一次启动docker的话 执行这一步可能会比较慢 耐心等待 ~QAQ~  

freeze代码如下:这里的save_model_e即为对应上面保存的pb文件所在位置 output_node_names需要指定对应你所需要的输出。

  1. #!/bin/bash
  2. # remove previous results
  3. rm -rf ./freeze_e
  4. mkdir ./freeze_e
  5. #conda activate decent_q3
  6. # freeze trained graph
  7. echo "#####################################"
  8. echo "FREEZE GRAPH"
  9. echo "#####################################"
  10. freeze_graph --input_saved_model_dir=./save_model_e/ \
  11. --output_graph=./freeze_e/frozen_graph.pb \
  12. --output_node_names=intra/enc_4/BiasAdd
  13. echo "#####################################"
  14. echo "FREEZE GRAPH COMPLETED"
  15. echo "#####################################"

3. quantize 部分

根据上面freeze的图片,输入一部分的数据集进行预处理来实现量化 input_fn对应一个生成数据集的函数 这个数据集是为了校准数据的 数据集代码如下图所示:注意 如果网络原本的输入都是/255归一化的数据,那这里也要提供归一化的数据。

  1. import os
  2. import numpy as np
  3. import cv2
  4. from os import listdir
  5. from os.path import join
  6. calib_image_dir_name = "./kodak"
  7. height = 512
  8. width = 768
  9. channels = 3
  10. def calib_input(iter):
  11. image = []
  12. imgs = np.zeros([8, height, width, channels])
  13. # read image
  14. image_index = 0
  15. pngfilenames = [x for x in listdir(calib_image_dir_name)]
  16. for pngfilename in pngfilenames:
  17. print(pngfilename)
  18. in_png_file = join(calib_image_dir_name, pngfilename)
  19. image = cv2.imread(in_png_file, cv2.IMREAD_COLOR) / 255.
  20. image = np.reshape(image,[height, width, channels])
  21. image = np.float32(image)
  22. imgs[image_index, :, :, :] = image[:, :, :]
  23. image_index += 1
  24. return {"Placeholder": imgs}

然后,进入我们的量化部分 input_nodes就是对应我们数据集的名字和size 这个数据集是为了模型的校准 具体的效果还要调研 默认为8bit量化 对应method是1 

output_nodes就是对应原来pb中对应输出的名字

  1. #!/bin/bash
  2. # activate DECENT_Q Python3.6 virtual environment
  3. #conda activate decent_q3
  4. # generate calibraion images and list file
  5. # remove existing files
  6. rm -rf ./quantize_e
  7. mkdir ./quantize_e
  8. # run quantization
  9. echo "#####################################"
  10. echo "QUANTIZE"
  11. echo "#####################################"
  12. vai_q_tensorflow quantize \
  13. --input_frozen_graph ./freeze_e/frozen_graph.pb \
  14. --input_nodes Placeholder \
  15. --input_shapes ?,512,768,3 \
  16. --output_nodes intra/enc_4/BiasAdd \
  17. --input_fn graph_input_fn_e_list.calib_input \
  18. --method 1 \
  19. --weight_bit 8 \
  20. --activation_bit 8 \
  21. --gpu 0 \
  22. --calib_iter 10 \
  23. --output_dir ./quantize_e
  24. echo "#####################################"
  25. echo "QUANTIZATION COMPLETED"
  26. echo "#####################################"

4. 最后一个阶段 编译阶段 代码如下: 其实代码很简单 就是把上面量化的文件转换为能在FPGA开发板上跑的东西 .xmodel格式。

重要的问题https://forums.xilinx.com/t5/Vitis-HLS-Alveo-ML%E4%BB%A5%E5%8F%8A%E5%85%B6%E4%BB%96%E8%BD%AF%E4%BB%B6%E5%BA%94%E7%94%A8/vitis-ai-1-3%E7%BC%96%E8%AF%91%E6%8A%A5%E9%94%99/td-p/1187929 

以前vitis-1.2版本使用deploy_model.pb格式的,但是现在2020.12更新了1.3版本,所以现在统一都用quantize_eval_model.pb的文件。

而且 提到了一种情况,有时候没有输入的size信息,需要手动去指定,需要--options 指定shape才可以。

  1. #!/bin/bash
  2. # delete previous results
  3. rm -rf ./compile_e
  4. mkdir ./compile_e
  5. #conda activate decent_q3
  6. # --frozen_pb=./quantize_e/deploy_model.pb \
  7. # Compile
  8. echo "#####################################"
  9. echo "COMPILE WITH DNNC"
  10. echo "#####################################"
  11. vai_c_tensorflow \
  12. --frozen_pb=./quantize_e/quantize_eval_model.pb \
  13. --arch=/opt/vitis_ai/compiler/arch/DPUCZDX8G/ZCU104/arch.json \
  14. --output_dir=compile_e \
  15. --net_name=analysis_transform \
  16. --options '{"input_shape": "10,512,768,3"}'
  17. echo "#####################################"
  18. echo "COMPILATION COMPLETED"
  19. echo "#####################################"

对于最后一个阶段json文件的选择 则根据以下的表格来确定 具体操作可以看guide部分。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/250455
推荐阅读
相关标签
  

闽ICP备14008679号