Upload first version
This commit is contained in:
parent
f95d5a63b0
commit
3bdf2003ec
898 changed files with 1405811 additions and 1 deletions
41
examples/resnet/cpp/src/CMakeLists.txt
Executable file
41
examples/resnet/cpp/src/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,41 @@
|
|||
cmake_minimum_required(VERSION 3.5)
|
||||
project(resnet_demo)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Set NNSDK path
|
||||
set(NNSDK_ROOT "${CMAKE_SOURCE_DIR}/../../../../dependency/nnsdk")
|
||||
include_directories(${NNSDK_ROOT}/include)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/../../../../common)
|
||||
|
||||
# Set dependency path
|
||||
set(3RDPARTY_DIR "${CMAKE_SOURCE_DIR}/../../../../dependency")
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
|
||||
if (ANDROID_ABI STREQUAL "arm64-v8a")
|
||||
link_directories(${NNSDK_ROOT}/lib/android/arm64-v8a)
|
||||
else()
|
||||
link_directories(${NNSDK_ROOT}/lib/android/armeabi-v7a)
|
||||
endif()
|
||||
# Android needs log
|
||||
link_libraries(log)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
link_directories(${NNSDK_ROOT}/lib/linux/lib64_yocto)
|
||||
endif()
|
||||
|
||||
# Find OpenCV
|
||||
message(STATUS "OpenCV_DIR: ${OpenCV_DIR}")
|
||||
find_package(OpenCV REQUIRED)
|
||||
include_directories(${OpenCV_INCLUDE_DIRS})
|
||||
|
||||
|
||||
add_executable(resnet_demo
|
||||
main.cpp
|
||||
postprocess.cpp
|
||||
${CMAKE_SOURCE_DIR}/../../../../common/model_loader.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(resnet_demo
|
||||
${OpenCV_LIBS}
|
||||
nnsdk
|
||||
)
|
||||
63
examples/resnet/cpp/src/main.cpp
Executable file
63
examples/resnet/cpp/src/main.cpp
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "nn_sdk.h"
|
||||
#include "postprocess.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 4) {
|
||||
std::cout << "Usage: " << argv[0] << " <model.adla> <image_dir> <labels.txt>\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto labels = load_labels(argv[3]);
|
||||
|
||||
aml_config cfg{};
|
||||
cfg.typeSize = sizeof(cfg);
|
||||
cfg.modelType = ADLA_LOADABLE;
|
||||
cfg.nbgType = NN_ADLA_FILE;
|
||||
cfg.path = argv[1];
|
||||
void* ctx = aml_module_create(&cfg);
|
||||
if (!ctx) return -1;
|
||||
|
||||
std::vector<float> input_buffer(kInputW * kInputH * 3);
|
||||
|
||||
for (auto& it : fs::directory_iterator(argv[2])) {
|
||||
cv::Mat img = cv::imread(it.path().string());
|
||||
if (img.empty()) continue;
|
||||
|
||||
std::cout << "============================================================" << std::endl;
|
||||
std::cout << "Processing: " << it.path().filename() << std::endl;
|
||||
|
||||
preprocess(img, input_buffer.data());
|
||||
|
||||
nn_input in{};
|
||||
in.typeSize = sizeof(in);
|
||||
in.input_type = BINARY_RAW_DATA;
|
||||
in.input = (unsigned char*)input_buffer.data();
|
||||
in.size = input_buffer.size() * sizeof(float);
|
||||
in.info.valid = 1;
|
||||
in.info.input_format = AML_INPUT_MODEL_NHWC;
|
||||
in.info.input_data_type = AML_INPUT_FP32;
|
||||
aml_module_input_set(ctx, &in);
|
||||
|
||||
aml_output_config_t outcfg{};
|
||||
outcfg.typeSize = sizeof(outcfg);
|
||||
outcfg.format = AML_OUTDATA_FLOAT32;
|
||||
nn_output* out = (nn_output*)aml_module_output_get(ctx, outcfg);
|
||||
|
||||
if (out && out->num > 0) {
|
||||
float* data = (float*)out->out[0].buf;
|
||||
int size = out->out[0].size / sizeof(float);
|
||||
postprocess_topk(data, size, labels, 5);
|
||||
}
|
||||
std::cout << "============================================================\n" << std::endl;
|
||||
}
|
||||
|
||||
aml_module_destroy(ctx);
|
||||
return 0;
|
||||
}
|
||||
46
examples/resnet/cpp/src/postprocess.cpp
Executable file
46
examples/resnet/cpp/src/postprocess.cpp
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
#include "postprocess.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
void preprocess(const cv::Mat& src, float* dst) {
|
||||
cv::Mat rgb, resized;
|
||||
cv::cvtColor(src, rgb, cv::COLOR_BGR2RGB);
|
||||
cv::resize(rgb, resized, cv::Size(kInputW, kInputH));
|
||||
|
||||
for (int i = 0; i < kInputH; ++i) {
|
||||
for (int j = 0; j < kInputW; ++j) {
|
||||
cv::Vec3f pixel = resized.at<cv::Vec3b>(i, j);
|
||||
int idx = (i * kInputW + j) * 3;
|
||||
dst[idx + 0] = (pixel[0] - MEAN[0]) / STD[0];
|
||||
dst[idx + 1] = (pixel[1] - MEAN[1]) / STD[1];
|
||||
dst[idx + 2] = (pixel[2] - MEAN[2]) / STD[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void postprocess_topk(float* logits, int size, const std::vector<std::string>& labels, int k) {
|
||||
std::vector<int> indices(size);
|
||||
std::iota(indices.begin(), indices.end(), 0);
|
||||
|
||||
std::partial_sort(indices.begin(), indices.begin() + k, indices.end(),
|
||||
[&](int a, int b) { return logits[a] > logits[b]; });
|
||||
|
||||
std::cout << "\nTop-" << k << " Results:" << std::endl;
|
||||
for (int i = 0; i < k; ++i) {
|
||||
int idx = indices[i];
|
||||
std::string name = (idx < (int)labels.size()) ? labels[idx] : "N/A";
|
||||
printf("%d: %-20s score=%.6f\n", i + 1, name.c_str(), logits[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> load_labels(const std::string& path) {
|
||||
std::vector<std::string> labels;
|
||||
std::ifstream f(path);
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
if(!line.empty()) labels.push_back(line);
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
19
examples/resnet/cpp/src/postprocess.h
Executable file
19
examples/resnet/cpp/src/postprocess.h
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef RESNET_POSTPROCESS_H
|
||||
#define RESNET_POSTPROCESS_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
static const float MEAN[3] = {123.675f, 116.28f, 103.53f};
|
||||
static const float STD[3] = {58.395f, 58.395f, 58.395f};
|
||||
static constexpr int kInputW = 224;
|
||||
static constexpr int kInputH = 224;
|
||||
|
||||
void preprocess(const cv::Mat& src, float* dst);
|
||||
|
||||
void postprocess_topk(float* logits, int size, const std::vector<std::string>& labels, int k = 5);
|
||||
|
||||
std::vector<std::string> load_labels(const std::string& path);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue