-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from zpye/pybind11
Pybind11
- Loading branch information
Showing
19 changed files
with
266 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
yolo_result_* |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <pybind11/eigen/tensor.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "engine.h" | ||
#include "tensor.h" | ||
#include "types.h" | ||
|
||
using namespace SimpleInfer; | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(simpleinfer, m) { | ||
m.doc() = "pybind11 SimpleInfer"; | ||
|
||
m.def("InitializeContext", &InitializeContext); | ||
|
||
py::enum_<DataType>(m, "DataType") | ||
.value("None", DataType::kNone) | ||
.value("Float32", DataType::kFloat32); | ||
|
||
py::enum_<Status>(m, "Status") | ||
.value("Success", Status::kSuccess) | ||
.value("Fail", Status::kFail) | ||
.value("Empty", Status::kEmpty) | ||
.value("ErrorShape", Status::kErrorShape) | ||
.value("ErrorContext", Status::kErrorContext) | ||
.value("Unsupport", Status::kUnsupport); | ||
|
||
py::class_<Tensor>(m, "Tensor") | ||
.def(py::init<>()) | ||
.def(py::init<DataType, std::vector<int>>()) | ||
.def("GetDataType", | ||
static_cast<const DataType (Tensor::*)() const>( | ||
&Tensor::GetDataType)) | ||
.def("Shape", | ||
static_cast<const std::vector<int>& (Tensor::*)() const>( | ||
&Tensor::Shape)) | ||
.def("SetTensorDim4", | ||
static_cast<Status (Tensor::*)(const EigenTensorMap<float, 4>&)>( | ||
&Tensor::SetEigenTensor<float, 4>), | ||
py::return_value_policy::reference) | ||
.def("GetTensorDim4", | ||
static_cast<EigenTensorMap<float, 4> (Tensor::*)() const>( | ||
&Tensor::GetEigenTensor<float, 4>), | ||
py::return_value_policy::reference); | ||
|
||
py::class_<Engine>(m, "Engine") | ||
.def(py::init<>()) | ||
.def("LoadModel", | ||
static_cast<Status (Engine::*)(const std::string&, | ||
const std::string&)>( | ||
&Engine::LoadModel)) | ||
.def("Release", static_cast<Status (Engine::*)()>(&Engine::Release)) | ||
.def("InputNames", | ||
static_cast<const std::vector<std::string> (Engine::*)()>( | ||
&Engine::InputNames)) | ||
.def("OutputNames", | ||
static_cast<const std::vector<std::string> (Engine::*)()>( | ||
&Engine::OutputNames)) | ||
.def("Input", | ||
static_cast<Status (Engine::*)(const std::string&, const Tensor&)>( | ||
&Engine::Input)) | ||
.def("Forward", static_cast<Status (Engine::*)()>(&Engine::Forward)) | ||
.def("Extract", | ||
static_cast<Status (Engine::*)(const std::string&, Tensor&)>( | ||
&Engine::Extract)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from setuptools import setup, find_packages | ||
|
||
setup(name='SimpleInfer', | ||
version='0.1', | ||
description='SimpleInfer is a simple neural network inference framework.', | ||
author='zpye', | ||
author_email='[email protected]', | ||
url='https://github.com/zpye/SimpleInfer', | ||
packages=['simpleinfer'], | ||
package_dir={'': '.'}, | ||
package_data={'simpleinfer': ['simpleinfer.pyd']}, | ||
install_requires=['numpy'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SimpleInfer | ||
from .simpleinfer import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import simpleinfer as infer | ||
import numpy as np | ||
import os | ||
|
||
if __name__ == '__main__': | ||
infer.InitializeContext() | ||
|
||
engine = infer.Engine() | ||
|
||
cur_file_path = os.path.dirname(os.path.realpath(__file__)) | ||
model_path = cur_file_path + '/../../3rdparty/tmp/yolo/demo/' | ||
rc = engine.LoadModel(model_path + 'yolov5n_small.pnnx.param', | ||
model_path + 'yolov5n_small.pnnx.bin') | ||
print('LoadModel', rc) | ||
|
||
input_names = engine.InputNames() | ||
output_names = engine.OutputNames() | ||
print('input_names: ', input_names) | ||
print('output_names: ', output_names) | ||
|
||
# input | ||
input_shape = [4, 320, 320, 3] # NHWC | ||
input_np = np.ones(input_shape, dtype = np.float32) * 42.0 | ||
input_tensor = infer.Tensor(infer.DataType.Float32, input_shape) | ||
rc = input_tensor.SetTensorDim4(input_np) | ||
print('SetTensorDim4', rc) | ||
|
||
rc = engine.Input(input_names[0], input_tensor) | ||
print('Input', rc) | ||
|
||
rc = engine.Forward() | ||
print('Forward', rc) | ||
|
||
output_tensor = infer.Tensor() | ||
rc = engine.Extract(output_names[0], output_tensor) | ||
print('Extract', rc) | ||
|
||
output_np = output_tensor.GetTensorDim4() | ||
print(output_np.dtype, output_np.shape) | ||
|
||
print(output_np[0, 0, 0, :]) |
Oops, something went wrong.