Skip to content

Commit

Permalink
build overloads in detection networks
Browse files Browse the repository at this point in the history
  • Loading branch information
Serafadam committed Jan 17, 2025
1 parent 24f3e29 commit 7465c97
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 109 deletions.
53 changes: 33 additions & 20 deletions bindings/python/src/pipeline/node/DetectionNetworkBindings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <pybind11/cast.h>

#include <memory>

#include "Common.hpp"
Expand Down Expand Up @@ -36,17 +37,11 @@ void bind_detectionnetwork(pybind11::module& m, void* pCallstack) {

// DetectionNetwork Node
detectionNetwork
#define DETECTION_NETWORK_BUILD_ARGS \
Node::Output& input, \
NNArchive& nnArchive
#define DETECTION_NETWORK_BUILD_PYARGS \
py::arg("input"), \
py::arg("nnArchive")
#define DETECTION_NETWORK_ARGS \
float confidenceThreshold
#define DETECTION_NETWORK_PYARGS \
py::arg("confidenceThreshold") = 0.5
// TODO (Zimamazim) Automatically fetch default arguments to avoid duplicity
#define DETECTION_NETWORK_BUILD_ARGS Node::Output &input, NNArchive &nnArchive
#define DETECTION_NETWORK_BUILD_PYARGS py::arg("input"), py::arg("nnArchive")
#define DETECTION_NETWORK_ARGS float confidenceThreshold
#define DETECTION_NETWORK_PYARGS py::arg("confidenceThreshold") = 0.5
// TODO (Zimamazim) Automatically fetch default arguments to avoid duplicity
#define DETECTION_NETWORK_CODE(OP) self OP setConfidenceThreshold(confidenceThreshold);
.def(
"build",
Expand All @@ -70,7 +65,17 @@ void bind_detectionnetwork(pybind11::module& m, void* pCallstack) {
py::arg("model"),
py::arg("fps") = 30.0f)
.def("build",
py::overload_cast<const std::shared_ptr<Camera>&, NNArchive, float>(&DetectionNetwork::build),
py::overload_cast<const std::shared_ptr<Camera>&, const NNArchive&, float>(&DetectionNetwork::build),
py::arg("input"),
py::arg("nnArchive"),
py::arg("fps") = 30.0f)
.def("build",
py::overload_cast<const std::shared_ptr<ReplayVideo>&, NNModelDescription, float>(&DetectionNetwork::build),
py::arg("input"),
py::arg("model"),
py::arg("fps") = 30.0f)
.def("build",
py::overload_cast<const std::shared_ptr<ReplayVideo>&, const NNArchive&, float>(&DetectionNetwork::build),
py::arg("input"),
py::arg("nnArchive"),
py::arg("fps") = 30.0f)
Expand All @@ -94,15 +99,23 @@ void bind_detectionnetwork(pybind11::module& m, void* pCallstack) {
py::arg("numNCEPerThread"),
DOC(dai, node, DetectionNetwork, setNumNCEPerInferenceThread))
.def("getNumInferenceThreads", &DetectionNetwork::getNumInferenceThreads, DOC(dai, node, DetectionNetwork, getNumInferenceThreads))
.def("setNNArchive", py::overload_cast<const NNArchive&>(&DetectionNetwork::setNNArchive), py::arg("archive"), DOC(dai, node, DetectionNetwork, setNNArchive))
.def("setNNArchive", py::overload_cast<const NNArchive&, int>(&DetectionNetwork::setNNArchive), py::arg("archive"), py::arg("numShaves"), DOC(dai, node, DetectionNetwork, setNNArchive))
.def("setFromModelZoo", py::overload_cast<NNModelDescription, bool>(&DetectionNetwork::setFromModelZoo), py::arg("description"), py::arg("useCached") = false, DOC(dai, node, DetectionNetwork, setFromModelZoo))
.def("setNNArchive",
py::overload_cast<const NNArchive&>(&DetectionNetwork::setNNArchive),
py::arg("archive"),
DOC(dai, node, DetectionNetwork, setNNArchive))
.def("setNNArchive",
py::overload_cast<const NNArchive&, int>(&DetectionNetwork::setNNArchive),
py::arg("archive"),
py::arg("numShaves"),
DOC(dai, node, DetectionNetwork, setNNArchive))
.def("setFromModelZoo",
py::overload_cast<NNModelDescription, bool>(&DetectionNetwork::setFromModelZoo),
py::arg("description"),
py::arg("useCached") = false,
DOC(dai, node, DetectionNetwork, setFromModelZoo))
.def("setBlob", py::overload_cast<dai::OpenVINO::Blob>(&DetectionNetwork::setBlob), py::arg("blob"), DOC(dai, node, DetectionNetwork, setBlob))
.def("setBlob", py::overload_cast<const dai::Path&>(&DetectionNetwork::setBlob), py::arg("path"), DOC(dai, node, DetectionNetwork, setBlob, 2))
.def("setModelPath",
&DetectionNetwork::setModelPath,
py::arg("modelPath"),
DOC(dai, node, DetectionNetwork, setModelPath))
.def("setModelPath", &DetectionNetwork::setModelPath, py::arg("modelPath"), DOC(dai, node, DetectionNetwork, setModelPath))
.def("setNumShavesPerInferenceThread",
&DetectionNetwork::setNumShavesPerInferenceThread,
py::arg("numShavesPerInferenceThread"),
Expand Down Expand Up @@ -139,7 +152,7 @@ void bind_detectionnetwork(pybind11::module& m, void* pCallstack) {
.def("getConfidenceThreshold", &DetectionNetwork::getConfidenceThreshold, DOC(dai, node, DetectionNetwork, getConfidenceThreshold));
// ALIAS
// daiNodeModule.attr("DetectionNetwork").attr("Properties") = detectionNetworkProperties;

// YoloDetectionNetwork node
yoloDetectionNetwork.def("setNumClasses", &YoloDetectionNetwork::setNumClasses, py::arg("numClasses"), DOC(dai, node, YoloDetectionNetwork, setNumClasses))
.def("setCoordinateSize", &YoloDetectionNetwork::setCoordinateSize, py::arg("coordinates"), DOC(dai, node, YoloDetectionNetwork, setCoordinateSize))
Expand Down
93 changes: 67 additions & 26 deletions bindings/python/src/pipeline/node/NeuralNetworkBindings.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#include "NodeBindings.hpp"
#include "Common.hpp"

#include "depthai/pipeline/Pipeline.hpp"
#include "NodeBindings.hpp"
#include "depthai/pipeline/Node.hpp"
#include "depthai/pipeline/Pipeline.hpp"
#include "depthai/pipeline/node/NeuralNetwork.hpp"


void bind_neuralnetwork(pybind11::module& m, void* pCallstack){

void bind_neuralnetwork(pybind11::module& m, void* pCallstack) {
using namespace dai;
using namespace dai::node;

// Node and Properties declare upfront
py::class_<NeuralNetworkProperties, std::shared_ptr<NeuralNetworkProperties>> neuralNetworkProperties(m, "NeuralNetworkProperties", DOC(dai, NeuralNetworkProperties));
py::class_<NeuralNetworkProperties, std::shared_ptr<NeuralNetworkProperties>> neuralNetworkProperties(
m, "NeuralNetworkProperties", DOC(dai, NeuralNetworkProperties));
auto neuralNetwork = ADD_NODE(NeuralNetwork);

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Call the rest of the type defines, then perform the actual bindings
Callstack* callstack = (Callstack*) pCallstack;
Callstack* callstack = (Callstack*)pCallstack;
auto cb = callstack->top();
callstack->pop();
cb(m, pCallstack);
Expand All @@ -29,13 +27,11 @@ void bind_neuralnetwork(pybind11::module& m, void* pCallstack){
///////////////////////////////////////////////////////////////////////

// Properties
neuralNetworkProperties
.def_readwrite("blobSize", &NeuralNetworkProperties::blobSize)
neuralNetworkProperties.def_readwrite("blobSize", &NeuralNetworkProperties::blobSize)
.def_readwrite("blobUri", &NeuralNetworkProperties::blobUri)
.def_readwrite("numFrames", &NeuralNetworkProperties::numFrames)
.def_readwrite("numThreads", &NeuralNetworkProperties::numThreads)
.def_readwrite("numNCEPerThread", &NeuralNetworkProperties::numNCEPerThread)
;
.def_readwrite("numNCEPerThread", &NeuralNetworkProperties::numNCEPerThread);

// Node
neuralNetwork.def_readonly("input", &NeuralNetwork::input, DOC(dai, node, NeuralNetwork, input))
Expand All @@ -49,21 +45,67 @@ void bind_neuralnetwork(pybind11::module& m, void* pCallstack){
py::arg("numNCEPerThread"),
DOC(dai, node, NeuralNetwork, setNumNCEPerInferenceThread))
.def("getNumInferenceThreads", &NeuralNetwork::getNumInferenceThreads, DOC(dai, node, NeuralNetwork, getNumInferenceThreads))
.def("setNNArchive", py::overload_cast<const NNArchive&>(&NeuralNetwork::setNNArchive), py::arg("nnArchive"), DOC(dai, node, NeuralNetwork, setNNArchive))
.def("setNNArchive", py::overload_cast<const NNArchive&, int>(&NeuralNetwork::setNNArchive), py::arg("nnArchive"), py::arg("numShaves"), DOC(dai, node, NeuralNetwork, setNNArchive, 2))
.def("setFromModelZoo", py::overload_cast<NNModelDescription, bool>(&NeuralNetwork::setFromModelZoo), py::arg("description"), py::arg("useCached"), DOC(dai, node, NeuralNetwork, setFromModelZoo))
.def("build", py::overload_cast<dai::Node::Output&, const NNArchive&>(&NeuralNetwork::build), py::arg("input"), py::arg("nnArchive"), DOC(dai, node, NeuralNetwork, build))
.def("build", py::overload_cast<const std::shared_ptr<Camera>&, dai::NNModelDescription, float>(&NeuralNetwork::build), py::arg("input"), py::arg("modelDesc"), py::arg("fps")=30.0f, DOC(dai, node, NeuralNetwork, build,2))
.def("build", py::overload_cast<const std::shared_ptr<Camera>&, dai::NNArchive, float>(&NeuralNetwork::build), py::arg("input"), py::arg("nnArchive"), py::arg("fps")=30.0f, DOC(dai, node, NeuralNetwork, build, 3))
.def("build", [](NeuralNetwork& self, const std::shared_ptr<Camera>& input, const std::string& model, float fps) {
return self.build(input, NNModelDescription{model}, fps);
}, py::arg("input"), py::arg("model"), py::arg("fps")=30.0f, DOC(dai, node, NeuralNetwork, build))
.def("setNNArchive",
py::overload_cast<const NNArchive&>(&NeuralNetwork::setNNArchive),
py::arg("nnArchive"),
DOC(dai, node, NeuralNetwork, setNNArchive))
.def("setNNArchive",
py::overload_cast<const NNArchive&, int>(&NeuralNetwork::setNNArchive),
py::arg("nnArchive"),
py::arg("numShaves"),
DOC(dai, node, NeuralNetwork, setNNArchive, 2))
.def("setFromModelZoo",
py::overload_cast<NNModelDescription, bool>(&NeuralNetwork::setFromModelZoo),
py::arg("description"),
py::arg("useCached"),
DOC(dai, node, NeuralNetwork, setFromModelZoo))
.def("build",
py::overload_cast<dai::Node::Output&, const NNArchive&>(&NeuralNetwork::build),
py::arg("input"),
py::arg("nnArchive"),
DOC(dai, node, NeuralNetwork, build))
.def("build",
py::overload_cast<const std::shared_ptr<Camera>&, dai::NNModelDescription, float>(&NeuralNetwork::build),
py::arg("input"),
py::arg("modelDesc"),
py::arg("fps") = 30.0f,
DOC(dai, node, NeuralNetwork, build, 2))
.def("build",
py::overload_cast<const std::shared_ptr<Camera>&, const dai::NNArchive&, float>(&NeuralNetwork::build),
py::arg("input"),
py::arg("nnArchive"),
py::arg("fps") = 30.0f,
DOC(dai, node, NeuralNetwork, build, 3))
.def(
"build",
[](NeuralNetwork& self, const std::shared_ptr<Camera>& input, const std::string& model, float fps) {
return self.build(input, NNModelDescription{model}, fps);
},
py::arg("input"),
py::arg("model"),
py::arg("fps") = 30.0f,
DOC(dai, node, NeuralNetwork, build))
.def(
"build",
[](NeuralNetwork& self, const std::shared_ptr<ReplayVideo>& input, const dai::NNArchive& nnArchive, float fps) {
return self.build(input, nnArchive, fps);
},
py::arg("input"),
py::arg("nnArchive"),
py::arg("fps") = 30.0f,
DOC(dai, node, NeuralNetwork, build, 4))
.def(
"build",
[](NeuralNetwork& self, const std::shared_ptr<ReplayVideo>& input, const std::string& model, float fps) {
return self.build(input, NNModelDescription{model}, fps);
},
py::arg("input"),
py::arg("model"),
py::arg("fps") = 30.0f,
DOC(dai, node, NeuralNetwork, build, 5))
.def("setBlob", py::overload_cast<dai::OpenVINO::Blob>(&NeuralNetwork::setBlob), py::arg("blob"), DOC(dai, node, NeuralNetwork, setBlob))
.def("setBlob", py::overload_cast<const dai::Path&>(&NeuralNetwork::setBlob), py::arg("path"), DOC(dai, node, NeuralNetwork, setBlob, 2))
.def("setModelPath",
&NeuralNetwork::setModelPath,
py::arg("modelPath"),
DOC(dai, node, NeuralNetwork, setModelPath))
.def("setModelPath", &NeuralNetwork::setModelPath, py::arg("modelPath"), DOC(dai, node, NeuralNetwork, setModelPath))
.def("setNumShavesPerInferenceThread",
&NeuralNetwork::setNumShavesPerInferenceThread,
py::arg("numShavesPerInferenceThread"),
Expand All @@ -77,5 +119,4 @@ void bind_neuralnetwork(pybind11::module& m, void* pCallstack){
;
// Properties alias
daiNodeModule.attr("NeuralNetwork").attr("Properties") = neuralNetworkProperties;

}
Loading

0 comments on commit 7465c97

Please sign in to comment.