cuda.md 3.2 KB
Newer Older
Z
Zhaolong Xing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
---
layout: post
title: Lite基于CUDA的模型预测
---


Lite支持在x86_64,arm64架构上(如:TX2)进行CUDA的编译运行。

## 编译

**NOTE:** 如果是在TX2等NVIDIA嵌入式硬件上编译,请使用最新的[Jetpack](https://developer.nvidia.com/embedded/jetpack) 安装依赖库。


一: 下载代码

```
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
```

二:编译

```
# 进入代码目录
cd Paddle-Lite

# 运行编译脚本
# 编译结束会在本目录下生成 build_cuda 目录
# 编译过程中如果提示找不到CUDA,CUDNN,请在环境变量设置CUDA_TOOLKIT_ROOT_DIR, CUDNN_ROOT
# CUDA_TOOLKIT_ROOT_DIR,CUDNN_ROOT分别表示CUDA,CUDNN的根目录
./lite/tools/build.sh cuda
31 32
# 如果使用python接口,需要打开build_python选项
./lite/tools/build.sh --build_python=ON cuda
Z
Zhaolong Xing 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
```

编译结束会在 `build_cuda/inference_lite_lib/python/lib/` 目录下生成 `lite_core.so`

## 运行

以下以Yolov3模型为例,介绍如何在Nvidia GPU硬件上运行模型。

一: 下载darknet_yolov3模型,模型信息请参考[这里](https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/yolov3)


```
# 下载模型
wget https://paddle-inference-dist.cdn.bcebos.com/PaddleLite/yolov3_infer.tar.gz
47
tar -zxf yolov3_infer.tar.gz
Z
Zhaolong Xing 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
# 下载图片样例
wget https://paddle-inference-dist.cdn.bcebos.com/PaddleLite/kite.jpg
```

二: 运行   

**NOTE:**此处示例使用的是python接口,后续会开放C++接口以及示例。

``` python
#-*- coding: utf-8 -*-
from __future__ import print_function
import sys
import numpy as np
import cv2
sys.path.append('build_cuda/inference_lite_lib/python/lib')
from lite_core import *

def read_img(im_path, resize_h, resize_w):
  im = cv2.imread(im_path).astype('float32')
  im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
  h, w, _ = im.shape
  im_scale_x = resize_h / float(w)
  im_scale_y = resize_w / float(h)
  out_img = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=cv2.INTER_CUBIC)
  mean = np.array([0.485, 0.456, 0.406]).reshape((1, 1, -1))
  std = np.array([0.229, 0.224, 0.225]).reshape((1, 1, -1))
  out_img = (out_img / 255.0 - mean) / std
  out_img = out_img.transpose((2, 0, 1))
  return out_img

# 配置config
a = CxxConfig()
a.set_model_file('./yolov3_infer/__model__') # 指定模型文件路径 
a.set_param_file('./yolov3_infer/__params__') # 指定参数文件路径
place_cuda = Place(TargetType.CUDA)
a.set_valid_places([place_cuda])

# 创建predictor
predictor = create_paddle_predictor(a)

# 设置输入
input_tensor = predictor.get_input(0);
height, width = 608, 608
input_tensor.resize([1, 3, height, width])
data = read_img('./kite.jpg', height, width).flatten()
input_tensor.set_float_data(data, TargetType.CUDA)

in2 = predictor.get_input(1);
in2.resize([1, 2])
in2.set_int32_data([height, width], TargetType.CUDA)

# 运行
predictor.run()

# 获取输出
output_tensor = predictor.get_output(0);

print (output_tensor.shape())
# [100L, 6L]
print (output_tensor.target())
# TargetType.Host
print (output_tensor.float_data()[:6])
# [0.0, 0.9862784743309021, 98.51927185058594, 471.2381286621094, 120.73092651367188, 578.33251953125]

```

**NOTE:** 对CUDA的支持还在持续开发中。