Upload first version
This commit is contained in:
parent
f95d5a63b0
commit
3bdf2003ec
898 changed files with 1405811 additions and 1 deletions
0
examples/resnet/README.md
Normal file
0
examples/resnet/README.md
Normal file
0
examples/resnet/cpp/.gitkeep
Normal file
0
examples/resnet/cpp/.gitkeep
Normal file
77
examples/resnet/cpp/build-android.sh
Executable file
77
examples/resnet/cpp/build-android.sh
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
#
|
||||
# Copyright (C) 2024–2025 Amlogic, Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-a <target_abi>]"
|
||||
echo " -a <target_abi> : Target ABI (default: arm64-v8a)"
|
||||
echo " -h : Show this help message"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Default values
|
||||
TARGET_ABI=arm64-v8a
|
||||
|
||||
# Parse arguments
|
||||
while getopts 'a:h' opt; do
|
||||
case "$opt" in
|
||||
a)
|
||||
TARGET_ABI=$OPTARG
|
||||
;;
|
||||
h)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "${ANDROID_NDK_PATH}" ]; then
|
||||
if [ -n "${ANDROID_NDK}" ]; then
|
||||
ANDROID_NDK_PATH=${ANDROID_NDK}
|
||||
elif [ -n "${ANDROID_NDK_HOME}" ]; then
|
||||
ANDROID_NDK_PATH=${ANDROID_NDK_HOME}
|
||||
else
|
||||
echo "Error: ANDROID_NDK_PATH is not set."
|
||||
echo "Please set ANDROID_NDK_PATH to your Android NDK directory."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
ROOT_PWD=$(cd "$(dirname $0)" && pwd)
|
||||
BUILD_DIR=${ROOT_PWD}/build/android
|
||||
|
||||
echo "Building for Android..."
|
||||
echo "NDK_PATH: ${ANDROID_NDK_PATH}"
|
||||
echo "TARGET_ABI: ${TARGET_ABI}"
|
||||
echo "BUILD_DIR: ${BUILD_DIR}"
|
||||
|
||||
mkdir -p ${BUILD_DIR}
|
||||
cd ${BUILD_DIR}
|
||||
|
||||
cmake ../../src \
|
||||
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_PATH}/build/cmake/android.toolchain.cmake \
|
||||
-DANDROID_ABI=${TARGET_ABI} \
|
||||
-DANDROID_PLATFORM=android-24 \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DOpenCV_DIR=${ROOT_PWD}/../../../dependency/opencv/opencv-android-sdk-build/sdk/native/jni/abi-${TARGET_ABI}
|
||||
|
||||
make -j4
|
||||
|
||||
echo "Build complete. Executable in ${BUILD_DIR}/resnet_demo"
|
||||
63
examples/resnet/cpp/build-linux.sh
Executable file
63
examples/resnet/cpp/build-linux.sh
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
#TODO
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-a <target_arch>]"
|
||||
echo " -a <target_arch> : Target architecture (default: aarch64)"
|
||||
echo " -h : Show this help message"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Default values
|
||||
TARGET_ARCH=aarch64
|
||||
|
||||
# Parse arguments
|
||||
while getopts 'a:h' opt; do
|
||||
case "$opt" in
|
||||
a)
|
||||
TARGET_ARCH=$OPTARG
|
||||
;;
|
||||
h)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Default to aarch64-linux-gnu if GCC_COMPILER is not set
|
||||
GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu}
|
||||
|
||||
# Set compilers
|
||||
export CC=${GCC_COMPILER}-gcc
|
||||
export CXX=${GCC_COMPILER}-g++
|
||||
|
||||
# Validate compiler
|
||||
if ! command -v ${CC} &> /dev/null; then
|
||||
echo "Error: Compiler ${CC} not found."
|
||||
echo "Please set GCC_COMPILER environment variable to your cross-compiler path prefix."
|
||||
echo "Example: export GCC_COMPILER=/path/to/toolchain/bin/aarch64-linux-gnu"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ROOT_PWD=$(cd "$(dirname $0)" && pwd)
|
||||
BUILD_DIR=${ROOT_PWD}/build/linux
|
||||
|
||||
echo "Building for Linux..."
|
||||
echo "COMPILER: ${CC}"
|
||||
echo "TARGET_ARCH: ${TARGET_ARCH}"
|
||||
echo "BUILD_DIR: ${BUILD_DIR}"
|
||||
|
||||
mkdir -p ${BUILD_DIR}
|
||||
cd ${BUILD_DIR}
|
||||
|
||||
cmake ../../src \
|
||||
-DCMAKE_SYSTEM_NAME=Linux \
|
||||
-DCMAKE_SYSTEM_PROCESSOR=${TARGET_ARCH} \
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
|
||||
make -j4
|
||||
|
||||
echo "Build complete. Executable in ${BUILD_DIR}/resnet_demo"
|
||||
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
|
||||
0
examples/resnet/model/.gitkeep
Normal file
0
examples/resnet/model/.gitkeep
Normal file
0
examples/resnet/py/.gitkeep
Normal file
0
examples/resnet/py/.gitkeep
Normal file
Loading…
Add table
Add a link
Reference in a new issue