SavyaSanchi-Sharma commited on
Commit ·
8049596
1
Parent(s): cac97e7
ssd mobilenet v2 coco 2018
Browse files
.gitignore
CHANGED
|
@@ -1,3 +1 @@
|
|
| 1 |
ssd_mobilenet_v2_coco_2018_03_29/demo
|
| 2 |
-
|
| 3 |
-
**/demo
|
|
|
|
| 1 |
ssd_mobilenet_v2_coco_2018_03_29/demo
|
|
|
|
|
|
ssd_mobilenet_v2_coco_2018_03_29/README.md
CHANGED
|
@@ -19,18 +19,22 @@ python demo.py --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image exa
|
|
| 19 |
```
|
| 20 |
|
| 21 |
### C++
|
| 22 |
-
The C++ demo runs inference with
|
|
|
|
|
|
|
| 23 |
```bash
|
|
|
|
| 24 |
OCV=/path/to/opencv # OpenCV source tree
|
| 25 |
OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
|
| 26 |
g++ -std=c++17 demo.cpp -o demo \
|
|
|
|
| 27 |
-I$OCV/include \
|
| 28 |
-I$OCV/modules/core/include \
|
| 29 |
-
-I$OCV/modules/dnn/include \
|
| 30 |
-I$OCV/modules/imgproc/include \
|
| 31 |
-I$OCV/modules/imgcodecs/include \
|
| 32 |
-I$OCVBUILD \
|
| 33 |
-
-L$
|
|
|
|
| 34 |
./demo --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
|
| 35 |
```
|
| 36 |
|
|
|
|
| 19 |
```
|
| 20 |
|
| 21 |
### C++
|
| 22 |
+
The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
|
| 23 |
+
Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases — this build
|
| 24 |
+
uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
|
| 25 |
```bash
|
| 26 |
+
ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
|
| 27 |
OCV=/path/to/opencv # OpenCV source tree
|
| 28 |
OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
|
| 29 |
g++ -std=c++17 demo.cpp -o demo \
|
| 30 |
+
-I$ORT/include \
|
| 31 |
-I$OCV/include \
|
| 32 |
-I$OCV/modules/core/include \
|
|
|
|
| 33 |
-I$OCV/modules/imgproc/include \
|
| 34 |
-I$OCV/modules/imgcodecs/include \
|
| 35 |
-I$OCVBUILD \
|
| 36 |
+
-L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
|
| 37 |
+
-L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
|
| 38 |
./demo --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
|
| 39 |
```
|
| 40 |
|
ssd_mobilenet_v2_coco_2018_03_29/demo.cpp
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#include <
|
| 2 |
#include <opencv2/imgproc.hpp>
|
| 3 |
#include <opencv2/imgcodecs.hpp>
|
| 4 |
#include <array>
|
|
@@ -35,22 +35,39 @@ int main(int argc, char** argv)
|
|
| 35 |
resize(rgb, rgb, Size(300, 300));
|
| 36 |
if (!rgb.isContinuous()) rgb = rgb.clone();
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
|
| 47 |
-
for (size_t i = 0; i <
|
| 48 |
{
|
| 49 |
const std::string& n = out_str[i];
|
| 50 |
-
if (n.find("detection_boxes") != std::string::npos) boxes =
|
| 51 |
-
else if (n.find("detection_scores") != std::string::npos) scores =
|
| 52 |
-
else if (n.find("detection_classes") != std::string::npos) classes =
|
| 53 |
-
else if (n.find("num_detections") != std::string::npos) num =
|
| 54 |
}
|
| 55 |
if (!boxes || !scores || !classes || !num)
|
| 56 |
{
|
|
|
|
| 1 |
+
#include <onnxruntime_cxx_api.h>
|
| 2 |
#include <opencv2/imgproc.hpp>
|
| 3 |
#include <opencv2/imgcodecs.hpp>
|
| 4 |
#include <array>
|
|
|
|
| 35 |
resize(rgb, rgb, Size(300, 300));
|
| 36 |
if (!rgb.isContinuous()) rgb = rgb.clone();
|
| 37 |
|
| 38 |
+
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
|
| 39 |
+
Ort::SessionOptions so;
|
| 40 |
+
Ort::Session session(env, model.c_str(), so);
|
| 41 |
+
Ort::AllocatorWithDefaultOptions alloc;
|
| 42 |
+
|
| 43 |
+
auto in_name = session.GetInputNameAllocated(0, alloc);
|
| 44 |
+
const char* in_names[] = {in_name.get()};
|
| 45 |
+
|
| 46 |
+
size_t out_count = session.GetOutputCount();
|
| 47 |
+
std::vector<Ort::AllocatedStringPtr> out_holders;
|
| 48 |
+
std::vector<std::string> out_str;
|
| 49 |
+
std::vector<const char*> out_names;
|
| 50 |
+
for (size_t i = 0; i < out_count; ++i)
|
| 51 |
+
{
|
| 52 |
+
out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
|
| 53 |
+
out_str.push_back(out_holders.back().get());
|
| 54 |
+
out_names.push_back(out_str.back().c_str());
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
std::array<int64_t, 4> shape = {1, 300, 300, 3};
|
| 58 |
+
auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
|
| 59 |
+
Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, 300 * 300 * 3, shape.data(), shape.size());
|
| 60 |
+
|
| 61 |
+
auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
|
| 62 |
|
| 63 |
const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
|
| 64 |
+
for (size_t i = 0; i < out_count; ++i)
|
| 65 |
{
|
| 66 |
const std::string& n = out_str[i];
|
| 67 |
+
if (n.find("detection_boxes") != std::string::npos) boxes = outs[i].GetTensorMutableData<float>();
|
| 68 |
+
else if (n.find("detection_scores") != std::string::npos) scores = outs[i].GetTensorMutableData<float>();
|
| 69 |
+
else if (n.find("detection_classes") != std::string::npos) classes = outs[i].GetTensorMutableData<float>();
|
| 70 |
+
else if (n.find("num_detections") != std::string::npos) num = outs[i].GetTensorMutableData<float>();
|
| 71 |
}
|
| 72 |
if (!boxes || !scores || !classes || !num)
|
| 73 |
{
|
ssd_mobilenet_v2_coco_2018_03_29/demo.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
|
| 5 |
import cv2 as cv
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
here = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
|
|
@@ -22,10 +23,9 @@ def main():
|
|
| 22 |
|
| 23 |
rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
res = net.forward(onames)
|
| 29 |
boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
|
| 30 |
scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
|
| 31 |
classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
|
|
|
|
| 4 |
|
| 5 |
import cv2 as cv
|
| 6 |
import numpy as np
|
| 7 |
+
import onnxruntime as ort
|
| 8 |
|
| 9 |
here = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
|
|
|
|
| 23 |
|
| 24 |
rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
|
| 25 |
|
| 26 |
+
sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
|
| 27 |
+
res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
|
| 28 |
+
onames = [o.name for o in sess.get_outputs()]
|
|
|
|
| 29 |
boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
|
| 30 |
scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
|
| 31 |
classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
|