Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add yolov6 c++ and yolov6 pybind #16

Merged
merged 11 commits into from
Jul 14, 2022
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ build-debug.sh
*dist
fastdeploy.egg-info
.setuptools-cmake-build
fastdeploy/version.py
fastdeploy/version.py
fastdeploy/LICENSE*
fastdeploy/ThirdPartyNotices*
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ option(ENABLE_VISION_VISUALIZE "if to enable visualize vision model result toolb
option(ENABLE_OPENCV_CUDA "if to enable opencv with cuda, this will allow process image with GPU." OFF)
option(ENABLE_DEBUG "if to enable print debug information, this may reduce performance." OFF)

# Whether to build fastdeply with vision/text/... examples, only for testings.
option(WTIH_VISION_EXAMPLES "Whether to build fastdeply with vision examples" ON)

if(ENABLE_DEBUG)
add_definitions(-DFASTDEPLOY_DEBUG)
endif()
Expand All @@ -50,6 +53,13 @@ option(BUILD_FASTDEPLOY_PYTHON "if build python lib for fastdeploy." OFF)
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

if (WTIH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
# ENABLE_VISION and ENABLE_VISION_VISUALIZE must be ON if enable vision examples.
message(STATUS "Found WTIH_VISION_EXAMPLES ON, so, force ENABLE_VISION and ENABLE_VISION_VISUALIZE ON")
set(ENABLE_VISION ON CACHE BOOL "force to enable vision models usage" FORCE)
set(ENABLE_VISION_VISUALIZE ON CACHE BOOL "force to enable visualize vision model result toolbox" FORCE)
endif()

add_definitions(-DFASTDEPLOY_LIB)
file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/*.cc)
file(GLOB_RECURSE DEPLOY_ORT_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/backends/ort/*.cc)
Expand Down Expand Up @@ -170,6 +180,13 @@ endif()
set_target_properties(fastdeploy PROPERTIES VERSION ${FASTDEPLOY_VERSION})
target_link_libraries(fastdeploy ${DEPEND_LIBS})

# add examples after prepare include paths for third-parties
if (WTIH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples)
add_definitions(-DWTIH_VISION_EXAMPLES)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/examples/bin)
add_subdirectory(examples)
endif()

include(external/summary.cmake)
fastdeploy_summary()

Expand Down
8 changes: 8 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.jpg
*.png
*.jpeg
*.onnx
*.engine
*.pd*
*.nb
bin
22 changes: 22 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function(add_fastdeploy_executable field url model)
# temp target name/file var in function scope
set(TEMP_TARGET_FILE ${PROJECT_SOURCE_DIR}/examples/${field}/${url}_${model}.cc)
set(TEMP_TARGET_NAME ${field}_${url}_${model})
if (EXISTS ${TEMP_TARGET_FILE} AND TARGET fastdeploy)
add_executable(${TEMP_TARGET_NAME} ${TEMP_TARGET_FILE})
target_link_libraries(${TEMP_TARGET_NAME} PUBLIC fastdeploy)
message(STATUS "Found source file: [${field}/${url}_${model}.cc], ADD!!! fastdeploy executable: [${TEMP_TARGET_NAME}] !")
else ()
message(WARNING "Can not found source file: [${field}/${url}_${model}.cc], SKIP!!! fastdeploy executable: [${TEMP_TARGET_NAME}] !")
endif()
unset(TEMP_TARGET_FILE)
unset(TEMP_TARGET_NAME)
endfunction()

# vision examples
if (WTIH_VISION_EXAMPLES)
add_fastdeploy_executable(vision ultralytics yolov5)
add_fastdeploy_executable(vision meituan yolov6)
endif()

# other examples ...
11 changes: 11 additions & 0 deletions examples/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
images/*.jpg
images/*.jpeg
images/*.png
models/*.onnx
models/*.pd*
models/*.engine
models/*.trt
models/*.nb
outputs/*.jpg
outputs/*.jpeg
outputs/*.png
3 changes: 3 additions & 0 deletions examples/resources/images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.jpg
*.jpeg
*.png
5 changes: 5 additions & 0 deletions examples/resources/models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.onnx
*.engine
*.pd*
*.nb
*.trt
3 changes: 3 additions & 0 deletions examples/resources/outputs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.jpg
*.png
*.jpeg
53 changes: 53 additions & 0 deletions examples/vision/meituan_yolov6.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.

#include "fastdeploy/vision.h"

int main() {
namespace vis = fastdeploy::vision;

std::string model_file = "../resources/models/yolov6s.onnx";
std::string img_path = "../resources/images/bus.jpg";
std::string vis_path = "../resources/outputs/meituan_yolov6_vis_result.jpg";

auto model = vis::meituan::YOLOv6(model_file);
if (!model.Initialized()) {
std::cerr << "Init Failed." << std::endl;
return -1;
} else {
std::cout << "Init Done! Dynamic Mode: "
<< model.IsDynamicShape() << std::endl;
}
model.EnableDebug();

cv::Mat im = cv::imread(img_path);
cv::Mat vis_im = im.clone();

vis::DetectionResult res;
if (!model.Predict(&im, &res)) {
std::cerr << "Prediction Failed." << std::endl;
return -1;
} else {
std::cout << "Prediction Done!" << std::endl;
}

// 输出预测框结果
std::cout << res.Str() << std::endl;

// 可视化预测结果
vis::Visualize::VisDetection(&vis_im, res);
cv::imwrite(vis_path, vis_im);
std::cout << "Detect Done! Saved: " << vis_path << std::endl;
return 0;
}
52 changes: 52 additions & 0 deletions examples/vision/ultralytics_yolov5.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.

#include "fastdeploy/vision.h"

int main() {
namespace vis = fastdeploy::vision;

std::string model_file = "../resources/models/yolov5s.onnx";
std::string img_path = "../resources/images/bus.jpg";
std::string vis_path = "../resources/outputs/ultralytics_yolov5_vis_result.jpg";

auto model = vis::ultralytics::YOLOv5(model_file);
if (!model.Initialized()) {
std::cerr << "Init Failed! Model: " << model_file << std::endl;
return -1;
} else {
std::cout << "Init Done! Model:" << model_file << std::endl;
}
model.EnableDebug();

cv::Mat im = cv::imread(img_path);
cv::Mat vis_im = im.clone();

vis::DetectionResult res;
if (!model.Predict(&im, &res)) {
std::cerr << "Prediction Failed." << std::endl;
return -1;
} else {
std::cout << "Prediction Done!" << std::endl;
}

// 输出预测框结果
std::cout << res.Str() << std::endl;

// 可视化预测结果
vis::Visualize::VisDetection(&vis_im, res);
cv::imwrite(vis_path, vis_im);
std::cout << "Detect Done! Saved: " << vis_path << std::endl;
return 0;
}
1 change: 1 addition & 0 deletions fastdeploy/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import requests
import time
import zipfile
import tarfile
import hashlib
import tqdm
import logging
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/fastdeploy_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def runtime_option(self):
@property
def initialized(self):
if self._model is None:
return false
return False
return self._model.initialized()
1 change: 1 addition & 0 deletions fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifdef ENABLE_VISION
#include "fastdeploy/vision/ppcls/model.h"
#include "fastdeploy/vision/ultralytics/yolov5.h"
#include "fastdeploy/vision/meituan/yolov6.h"
#endif

#include "fastdeploy/vision/visualize/visualize.h"
1 change: 1 addition & 0 deletions fastdeploy/vision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
from . import evaluation
from . import ppcls
from . import ultralytics
from . import meituan
from . import visualize
120 changes: 120 additions & 0 deletions fastdeploy/vision/meituan/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright (c) 2022 PaddlePaddle Authors. 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.

from __future__ import absolute_import
import logging
from ... import FastDeployModel, Frontend
from ... import fastdeploy_main as C


class YOLOv6(FastDeployModel):
def __init__(self,
model_file,
params_file="",
runtime_option=None,
model_format=Frontend.ONNX):
# 调用基函数进行backend_option的初始化
# 初始化后的option保存在self._runtime_option
super(YOLOv6, self).__init__(runtime_option)

self._model = C.vision.meituan.YOLOv6(
model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "YOLOv6 initialize failed."

def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
return self._model.predict(input_image, conf_threshold,
nms_iou_threshold)

# BOOL: 查看输入的模型是否为动态维度的
def is_dynamic_shape(self):
return self._model.is_dynamic_shape()

# 一些跟YOLOv6模型有关的属性封装
# 多数是预处理相关,可通过修改如model.size = [1280, 1280]改变预处理时resize的大小(前提是模型支持)
@property
def size(self):
return self._model.size

@property
def padding_value(self):
return self._model.padding_value

@property
def is_no_pad(self):
return self._model.is_no_pad

@property
def is_mini_pad(self):
return self._model.is_mini_pad

@property
def is_scale_up(self):
return self._model.is_scale_up

@property
def stride(self):
return self._model.stride

@property
def max_wh(self):
return self._model.max_wh

@size.setter
def size(self, wh):
assert isinstance(wh, [list, tuple]),\
"The value to set `size` must be type of tuple or list."
assert len(wh) == 2,\
"The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
len(wh))
self._model.size = wh

@padding_value.setter
def padding_value(self, value):
assert isinstance(
value,
list), "The value to set `padding_value` must be type of list."
self._model.padding_value = value

@is_no_pad.setter
def is_no_pad(self, value):
assert isinstance(
value, bool), "The value to set `is_no_pad` must be type of bool."
self._model.is_no_pad = value

@is_mini_pad.setter
def is_mini_pad(self, value):
assert isinstance(
value,
bool), "The value to set `is_mini_pad` must be type of bool."
self._model.is_mini_pad = value

@is_scale_up.setter
def is_scale_up(self, value):
assert isinstance(
value,
bool), "The value to set `is_scale_up` must be type of bool."
self._model.is_scale_up = value

@stride.setter
def stride(self, value):
assert isinstance(
value, int), "The value to set `stride` must be type of int."
self._model.stride = value

@max_wh.setter
def max_wh(self, value):
assert isinstance(
value, float), "The value to set `max_wh` must be type of float."
self._model.max_wh = value
Loading