Skip to content

Commit

Permalink
#91 yolo모델 최적화
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Jun 4, 2023
1 parent 7f803c2 commit 14dfeb3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.Surface


class Yolo {
external fun loadModel(mgr: AssetManager?, modelid: Int = 1, cpugpu: Int = 1): Boolean
external fun loadModel(mgr: AssetManager?, modelid: Int = 1, useGpu: Int = 0): Boolean
external fun openCamera(facing: Int): Boolean
external fun closeCamera(): Boolean
external fun setOutputWindow(surface: Surface?): Boolean
Expand Down
2 changes: 1 addition & 1 deletion feature/camera/src/main/jni/ndkcamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ NdkCamera::NdkCamera() {

// setup imagereader and its surface
{
AImageReader_new(640, 640, AIMAGE_FORMAT_YUV_420_888, /*maxImages*/3, &image_reader);
AImageReader_new(320, 320, AIMAGE_FORMAT_YUV_420_888, /*maxImages*/4, &image_reader);

AImageReader_ImageListener listener;
listener.context = this;
Expand Down
47 changes: 22 additions & 25 deletions feature/camera/src/main/jni/yolo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "cpu.h"

static const float prob_threshold = 0.4f;
static const float prob_threshold = 0.35f;
static const float nms_threshold = 0.45f;

static float fast_exp(float x) {
Expand Down Expand Up @@ -136,13 +136,13 @@ generate_proposals(std::vector<GridAndStride> grid_strides, const ncnn::Mat &pre
// find label with max score
int label = -1;
float score = -FLT_MAX;
for (int k = 0; k < num_class; k++) {
float confidence = scores[k];
if (confidence > score) {
label = k;
score = confidence;
}

float confidence = scores[0];
if (confidence > score) {
label = 0;
score = confidence;
}

float box_prob = sigmoid(score);
if (box_prob >= prob_threshold) {
ncnn::Mat bbox_pred(reg_max_1, 4, (void *) pred.row(i));
Expand All @@ -155,8 +155,8 @@ generate_proposals(std::vector<GridAndStride> grid_strides, const ncnn::Mat &pre
softmax->load_param(pd);

ncnn::Option opt;
opt.num_threads = 1;
opt.use_packing_layout = false;
opt.num_threads = 2;
opt.use_packing_layout = true;

softmax->create_pipeline(opt);

Expand Down Expand Up @@ -206,7 +206,7 @@ Yolo::Yolo() {


int
Yolo::load(AAssetManager *mgr, const char *modeltype, int _target_size, const float *_mean_vals, const float *_norm_vals, bool use_gpu) {
Yolo::load(AAssetManager *mgr, const int *target_size, const float *_mean_vals, const float *_norm_vals, bool use_gpu) {
yolo.clear();
blob_pool_allocator.clear();
workspace_pool_allocator.clear();
Expand All @@ -227,7 +227,9 @@ Yolo::load(AAssetManager *mgr, const char *modeltype, int _target_size, const fl
yolo.load_param(mgr, "medicinesdetector.param");
yolo.load_model(mgr, "medicinesdetector.bin");

target_size = _target_size;
net_h = target_size[0];
net_w = target_size[1];

mean_vals[0] = _mean_vals[0];
mean_vals[1] = _mean_vals[1];
mean_vals[2] = _mean_vals[2];
Expand All @@ -248,12 +250,12 @@ int Yolo::detect(const cv::Mat &rgb, std::vector<Object> &objects) {
int h = height;
float scale = 1.f;
if (w > h) {
scale = (float) target_size / w;
w = target_size;
scale = net_w / (float) w;
w = net_w;
h = h * scale;
} else {
scale = (float) target_size / h;
h = target_size;
scale = net_h / (float) h;
h = net_h;
w = w * scale;
}

Expand All @@ -265,7 +267,7 @@ int Yolo::detect(const cv::Mat &rgb, std::vector<Object> &objects) {
ncnn::Mat in_pad;
ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, ncnn::BORDER_CONSTANT, 0.f);

in_pad.substract_mean_normalize(0, norm_vals);
in_pad.substract_mean_normalize(mean_vals, norm_vals);

ncnn::Extractor ex = yolo.create_extractor();

Expand All @@ -276,7 +278,7 @@ int Yolo::detect(const cv::Mat &rgb, std::vector<Object> &objects) {
ncnn::Mat out;
ex.extract("output0", out);

// might have stride=64
// might have stride=64
std::vector<GridAndStride> grid_strides;
generate_grids_and_stride(in_pad.w, in_pad.h, grid_strides);

Expand Down Expand Up @@ -319,7 +321,7 @@ int Yolo::detect(const cv::Mat &rgb, std::vector<Object> &objects) {
return a.rect.area() > b.rect.area();
}
} objects_area_greater;
std::sort(objects.begin(), objects.end(), objects_area_greater);
// std::sort(objects.begin(), objects.end(), objects_area_greater);

return 0;
}
Expand All @@ -328,13 +330,8 @@ static const int color = 255;
static const cv::Scalar detectedRectColor(color, color, color);


int Yolo::draw(cv::Mat &rgb, const std::vector<Object> &objects) {
if (objects.empty())
return 0;

void Yolo::draw(cv::Mat &rgb, const std::vector<Object> &objects) {
for (const auto &obj: objects) {
cv::rectangle(rgb, obj.rect, detectedRectColor, 2);
cv::rectangle(rgb, obj.rect, detectedRectColor, 2, cv::LINE_AA);
}

return 0;
}
11 changes: 5 additions & 6 deletions feature/camera/src/main/jni/yolo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ class Yolo {
public:
Yolo();

int load(const char *modeltype, int target_size, const float *mean_vals, const float *norm_vals, bool use_gpu = true);

int load(AAssetManager *mgr, const char *modeltype, int target_size, const float *mean_vals, const float *norm_vals, bool use_gpu =
true);
int
load(AAssetManager *mgr, const int *target_size, const float *mean_vals, const float *norm_vals, bool use_gpu = true);

int detect(const cv::Mat &rgb, std::vector<Object> &objects);

int draw(cv::Mat &rgb, const std::vector<Object> &objects);
void draw(cv::Mat &rgb, const std::vector<Object> &objects);

private:
ncnn::Net yolo;
int target_size;
float mean_vals[3];
float norm_vals[3];
int net_h;
int net_w;
ncnn::UnlockedPoolAllocator blob_pool_allocator;
ncnn::PoolAllocator workspace_pool_allocator;
};
Expand Down
44 changes: 16 additions & 28 deletions feature/camera/src/main/jni/yolov8ncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static int draw_fps(cv::Mat &rgb) {
}

char text[32];
sprintf(text, "FPS=%d", (int) avg_fps);

int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
Expand All @@ -135,7 +136,7 @@ static int draw_fps(cv::Mat &rgb) {
cv::Scalar(255, 255, 255), -1);

cv::putText(rgb, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(0, 0, 0));

return 0;
}
Expand All @@ -159,7 +160,7 @@ static std::vector<Object> objs;

void MyNdkCamera::on_image_render(cv::Mat &rgb) const {
{
// ncnn::MutexLockGuard g(lock);
ncnn::MutexLockGuard g(lock);

if (g_yolo) {
objs.clear();
Expand All @@ -171,7 +172,7 @@ void MyNdkCamera::on_image_render(cv::Mat &rgb) const {
}
}

// draw_fps(rgb);
draw_fps(rgb);
}

// return detectedObjects;
Expand Down Expand Up @@ -239,34 +240,21 @@ JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved) {
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_android_mediproject_feature_camera_aimodel_Yolo_loadModel(JNIEnv *env, jobject thiz, jobject assetManager, jint modelid,
jint cpugpu) {
if (modelid < 0 || modelid > 6 || cpugpu < 0 || cpugpu > 1) {
return JNI_FALSE;
}

jint useGpu) {
AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);

const int target_sizes[] =
{
640,
640,
};

const float mean_vals[][3] =
{
{103.53f, 116.28f, 123.675f},
{103.53f, 116.28f, 123.675f},
};
const int target_sizes[] = {
320,
320,
};

const float norm_vals[][3] =
{
{1 / 255.f, 1 / 255.f, 1 / 255.f},
{1 / 255.f, 1 / 255.f, 1 / 255.f},
};
const float mean_vals[] =
{0.f, 0.f, 0.f};

const float norm_vals[] =
{1 / 255.f, 1 / 255.f, 1 / 255.f};

const char *modeltype = "s";
int target_size = target_sizes[(int) modelid];
bool use_gpu = (int) cpugpu == 1;
bool use_gpu = (int) useGpu == 1;

// reload
{
Expand All @@ -279,7 +267,7 @@ Java_com_android_mediproject_feature_camera_aimodel_Yolo_loadModel(JNIEnv *env,
} else {
if (!g_yolo)
g_yolo = new Yolo;
g_yolo->load(mgr, modeltype, target_size, mean_vals[(int) modelid], norm_vals[(int) modelid], use_gpu);
g_yolo->load(mgr, target_sizes, mean_vals, norm_vals);
}
}

Expand Down

0 comments on commit 14dfeb3

Please sign in to comment.