forked from jiuyuan/InfiniTensor
Add examples: inference of Paddle models (#192)
* Add paddle model and infer with InfiniTensor * Remove unused import --------- Co-authored-by: kilinchange <44265800+kilinchange@users.noreply.github.com> 【Hackathon No.106】Add paddle model and infer with InfiniTensor
This commit is contained in:
parent
a3929c25f8
commit
9a9587556c
|
@ -0,0 +1,80 @@
|
||||||
|
|
||||||
|
import paddle
|
||||||
|
import paddle.vision.transforms as T
|
||||||
|
from paddle.vision.datasets import Cifar10
|
||||||
|
from pyinfinitensor.onnx import OnnxStub, backend
|
||||||
|
import onnx
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
def run_cifar_train_and_infer():
|
||||||
|
|
||||||
|
paddle.device.set_device("gpu")
|
||||||
|
|
||||||
|
transform = T.Compose(
|
||||||
|
[
|
||||||
|
T.Resize(224),
|
||||||
|
T.ToTensor(),
|
||||||
|
T.Normalize(
|
||||||
|
mean=[0.5, 0.5, 0.5],
|
||||||
|
std=[0.5, 0.5, 0.5],
|
||||||
|
to_rgb=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 下载数据集并初始化 DataSet
|
||||||
|
train_dataset = paddle.vision.datasets.Cifar10(mode='train', transform=transform)
|
||||||
|
test_dataset = paddle.vision.datasets.Cifar10(mode='test', transform=transform)
|
||||||
|
|
||||||
|
# 模型组网并初始化网络
|
||||||
|
densenet = paddle.vision.models.DenseNet(num_classes=10)
|
||||||
|
model = paddle.Model(densenet)
|
||||||
|
|
||||||
|
# 模型训练的配置准备,准备损失函数,优化器和评价指标
|
||||||
|
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
|
||||||
|
paddle.nn.CrossEntropyLoss(),
|
||||||
|
paddle.metric.Accuracy())
|
||||||
|
|
||||||
|
# 模型训练
|
||||||
|
model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)
|
||||||
|
# 模型评估
|
||||||
|
model.evaluate(test_dataset, batch_size=64, verbose=1)
|
||||||
|
|
||||||
|
# export to ONNX
|
||||||
|
save_path = 'onnx.save/densenet' # 需要保存的路径
|
||||||
|
x_spec = paddle.static.InputSpec([1, 3, 224, 224], 'float32', 'x') # 为模型指定输入的形状和数据类型,支持持 Tensor 或 InputSpec ,InputSpec 支持动态的 shape。
|
||||||
|
paddle.onnx.export(densenet, save_path, input_spec=[x_spec], opset_version=11)
|
||||||
|
|
||||||
|
# 加载onnx模型并放到Infinitensor中
|
||||||
|
model_path = save_path + ".onnx"
|
||||||
|
onnx_model = onnx.load(model_path)
|
||||||
|
gofusion_model = OnnxStub(onnx_model, backend.cuda_runtime())
|
||||||
|
model = gofusion_model
|
||||||
|
model.init()
|
||||||
|
|
||||||
|
# 启动推理
|
||||||
|
cifar10_test = Cifar10(
|
||||||
|
mode="test",
|
||||||
|
transform=transform, # apply transform to every image
|
||||||
|
backend="cv2", # use OpenCV as image transform backend
|
||||||
|
)
|
||||||
|
batch_size = 1
|
||||||
|
total_size = 0
|
||||||
|
total_acc = 0.0
|
||||||
|
for data in itertools.islice(iter(cifar10_test), 10000):
|
||||||
|
images, labels = data
|
||||||
|
next(model.inputs.items().__iter__())[1].copyin_float(images.reshape([3*224*224]).tolist())
|
||||||
|
model.run()
|
||||||
|
outputs = next(model.outputs.items().__iter__())[1].copyout_float()
|
||||||
|
outputs = paddle.to_tensor(outputs)
|
||||||
|
outputs = paddle.reshape(outputs, (1, 10))
|
||||||
|
labels = paddle.to_tensor(labels)
|
||||||
|
labels = paddle.reshape(labels, (1,1))
|
||||||
|
acc = paddle.metric.accuracy(outputs, labels)
|
||||||
|
total_acc += acc
|
||||||
|
total_size += batch_size
|
||||||
|
print("test acc: {}".format(total_acc.numpy() / total_size))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_cifar_train_and_infer()
|
|
@ -0,0 +1,80 @@
|
||||||
|
import paddle
|
||||||
|
import paddle.vision.transforms as T
|
||||||
|
from paddle.vision.datasets import Cifar10
|
||||||
|
from pyinfinitensor.onnx import OnnxStub, backend
|
||||||
|
import onnx
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
def run_cifar_train_and_infer():
|
||||||
|
|
||||||
|
paddle.device.set_device("gpu")
|
||||||
|
|
||||||
|
transform = T.Compose(
|
||||||
|
[
|
||||||
|
T.Resize(224),
|
||||||
|
T.ToTensor(),
|
||||||
|
T.Normalize(
|
||||||
|
mean=[0.5, 0.5, 0.5],
|
||||||
|
std=[0.5, 0.5, 0.5],
|
||||||
|
to_rgb=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 下载数据集并初始化 DataSet
|
||||||
|
train_dataset = paddle.vision.datasets.Cifar10(mode='train', transform=transform)
|
||||||
|
test_dataset = paddle.vision.datasets.Cifar10(mode='test', transform=transform)
|
||||||
|
|
||||||
|
# 模型组网并初始化网络
|
||||||
|
inception = paddle.vision.models.InceptionV3(num_classes=10)
|
||||||
|
model = paddle.Model(inception)
|
||||||
|
|
||||||
|
# 模型训练的配置准备,准备损失函数,优化器和评价指标
|
||||||
|
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
|
||||||
|
paddle.nn.CrossEntropyLoss(),
|
||||||
|
paddle.metric.Accuracy())
|
||||||
|
|
||||||
|
# 模型训练
|
||||||
|
model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)
|
||||||
|
# 模型评估
|
||||||
|
model.evaluate(test_dataset, batch_size=64, verbose=1)
|
||||||
|
|
||||||
|
# export to ONNX
|
||||||
|
save_path = 'onnx.save/inception' # 需要保存的路径
|
||||||
|
x_spec = paddle.static.InputSpec([1, 3, 224, 224], 'float32', 'x') # 为模型指定输入的形状和数据类型,支持持 Tensor 或 InputSpec ,InputSpec 支持动态的 shape。
|
||||||
|
paddle.onnx.export(inception, save_path, input_spec=[x_spec], opset_version=11)
|
||||||
|
|
||||||
|
# 加载onnx模型并放到Infinitensor中
|
||||||
|
model_path = save_path + ".onnx"
|
||||||
|
onnx_model = onnx.load(model_path)
|
||||||
|
gofusion_model = OnnxStub(onnx_model, backend.cuda_runtime())
|
||||||
|
model = gofusion_model
|
||||||
|
model.init()
|
||||||
|
|
||||||
|
# 启动推理
|
||||||
|
cifar10_test = Cifar10(
|
||||||
|
mode="test",
|
||||||
|
transform=transform, # apply transform to every image
|
||||||
|
backend="cv2", # use OpenCV as image transform backend
|
||||||
|
)
|
||||||
|
batch_size = 1
|
||||||
|
total_size = 0
|
||||||
|
total_acc = 0.0
|
||||||
|
for data in itertools.islice(iter(cifar10_test), 10000):
|
||||||
|
images, labels = data
|
||||||
|
next(model.inputs.items().__iter__())[1].copyin_float(images.reshape([3*224*224]).tolist())
|
||||||
|
model.run()
|
||||||
|
outputs = next(model.outputs.items().__iter__())[1].copyout_float()
|
||||||
|
outputs = paddle.to_tensor(outputs)
|
||||||
|
outputs = paddle.reshape(outputs, (1, 10))
|
||||||
|
labels = paddle.to_tensor(labels)
|
||||||
|
labels = paddle.reshape(labels, (1,1))
|
||||||
|
acc = paddle.metric.accuracy(outputs, labels)
|
||||||
|
total_acc += acc
|
||||||
|
total_size += batch_size
|
||||||
|
print("test acc: {}".format(total_acc.numpy() / total_size))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_cifar_train_and_infer()
|
|
@ -0,0 +1,31 @@
|
||||||
|
## Description
|
||||||
|
|
||||||
|
This is a doc to tell you how to run paddle*.py in your machine. If your model run on other machines except Nvidia, you may need to make some change.
|
||||||
|
|
||||||
|
## What do we do in paddle*.py files?
|
||||||
|
|
||||||
|
1. Train model and evalute model with Cifar10 dataset
|
||||||
|
|
||||||
|
2. Export paddle model to onnx model
|
||||||
|
|
||||||
|
3. Load onnx model, infer with InfiniTensor and calculate the inference accuracy
|
||||||
|
|
||||||
|
## Command
|
||||||
|
|
||||||
|
1. Go to `/examples/python` folder
|
||||||
|
|
||||||
|
2. Run the following command
|
||||||
|
|
||||||
|
1. ```
|
||||||
|
python paddle_resnet.py
|
||||||
|
python paddle_densenet.py
|
||||||
|
python paddle_inception.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## What should I do if I use other device(MLU, XPU, NPU)?
|
||||||
|
|
||||||
|
You need to change this code:
|
||||||
|
|
||||||
|
```
|
||||||
|
paddle.device.set_device("gpu") # Change gpu to mlu, xpu or npu
|
||||||
|
```
|
|
@ -0,0 +1,81 @@
|
||||||
|
|
||||||
|
import paddle
|
||||||
|
import paddle.vision.transforms as T
|
||||||
|
from paddle.vision.datasets import Cifar10
|
||||||
|
from pyinfinitensor.onnx import OnnxStub, backend
|
||||||
|
import onnx
|
||||||
|
import itertools
|
||||||
|
from paddle.vision.models.resnet import BasicBlock
|
||||||
|
|
||||||
|
def run_cifar_train_and_infer():
|
||||||
|
|
||||||
|
paddle.device.set_device("gpu")
|
||||||
|
|
||||||
|
transform = T.Compose(
|
||||||
|
[
|
||||||
|
T.Resize(224),
|
||||||
|
T.ToTensor(),
|
||||||
|
T.Normalize(
|
||||||
|
mean=[0.5, 0.5, 0.5],
|
||||||
|
std=[0.5, 0.5, 0.5],
|
||||||
|
to_rgb=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 下载数据集并初始化 DataSet
|
||||||
|
train_dataset = paddle.vision.datasets.Cifar10(mode='train', transform=transform)
|
||||||
|
test_dataset = paddle.vision.datasets.Cifar10(mode='test', transform=transform)
|
||||||
|
|
||||||
|
# 模型组网并初始化网络
|
||||||
|
resnet = paddle.vision.models.ResNet(BasicBlock, depth=18, num_classes=10)
|
||||||
|
model = paddle.Model(resnet)
|
||||||
|
|
||||||
|
# 模型训练的配置准备,准备损失函数,优化器和评价指标
|
||||||
|
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
|
||||||
|
paddle.nn.CrossEntropyLoss(),
|
||||||
|
paddle.metric.Accuracy())
|
||||||
|
|
||||||
|
# 模型训练
|
||||||
|
model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)
|
||||||
|
# 模型评估
|
||||||
|
model.evaluate(test_dataset, batch_size=64, verbose=1)
|
||||||
|
|
||||||
|
# export to ONNX
|
||||||
|
save_path = 'onnx.save/resnet' # 需要保存的路径
|
||||||
|
x_spec = paddle.static.InputSpec([1, 3, 224, 224], 'float32', 'x') # 为模型指定输入的形状和数据类型,支持持 Tensor 或 InputSpec ,InputSpec 支持动态的 shape。
|
||||||
|
paddle.onnx.export(resnet, save_path, input_spec=[x_spec], opset_version=11)
|
||||||
|
|
||||||
|
# 加载onnx模型并放到Infinitensor中
|
||||||
|
model_path = save_path + ".onnx"
|
||||||
|
onnx_model = onnx.load(model_path)
|
||||||
|
gofusion_model = OnnxStub(onnx_model, backend.cuda_runtime())
|
||||||
|
model = gofusion_model
|
||||||
|
model.init()
|
||||||
|
|
||||||
|
# 启动推理
|
||||||
|
cifar10_test = Cifar10(
|
||||||
|
mode="test",
|
||||||
|
transform=transform, # apply transform to every image
|
||||||
|
backend="cv2", # use OpenCV as image transform backend
|
||||||
|
)
|
||||||
|
batch_size = 1
|
||||||
|
total_size = 0
|
||||||
|
total_acc = 0.0
|
||||||
|
for data in itertools.islice(iter(cifar10_test), 10000):
|
||||||
|
images, labels = data
|
||||||
|
next(model.inputs.items().__iter__())[1].copyin_float(images.reshape([3*224*224]).tolist())
|
||||||
|
model.run()
|
||||||
|
outputs = next(model.outputs.items().__iter__())[1].copyout_float()
|
||||||
|
outputs = paddle.to_tensor(outputs)
|
||||||
|
outputs = paddle.reshape(outputs, (1, 10))
|
||||||
|
labels = paddle.to_tensor(labels)
|
||||||
|
labels = paddle.reshape(labels, (1,1))
|
||||||
|
acc = paddle.metric.accuracy(outputs, labels)
|
||||||
|
total_acc += acc
|
||||||
|
total_size += batch_size
|
||||||
|
print("test acc: {}".format(total_acc.numpy() / total_size))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_cifar_train_and_infer()
|
Loading…
Reference in New Issue