Skip to content

Commit 1b5e6a0

Browse files
author
teng
committed
Add GPU optional
1 parent 5906dfe commit 1b5e6a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+20700
-22
lines changed

‎iOS_YOLOv5NCNN/YOLOv5NCNN.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5F13A99E256F767800E45BA3 /* glslang.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F13A99D256F767800E45BA3 /* glslang.framework */; };
11+
5F13A9A0256F851000E45BA3 /* ncnn.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F13A99F256F851000E45BA3 /* ncnn.framework */; };
1012
5F37E19E24B97F9600BF9E94 /* yolov4-tiny-opt.param in Resources */ = {isa = PBXBuildFile; fileRef = 5F37E19C24B97F9400BF9E94 /* yolov4-tiny-opt.param */; };
1113
5F37E19F24B97F9600BF9E94 /* yolov4-tiny-opt.bin in Resources */ = {isa = PBXBuildFile; fileRef = 5F37E19D24B97F9600BF9E94 /* yolov4-tiny-opt.bin */; };
1214
5F37E1A224B97FD900BF9E94 /* YoloV4.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5F37E1A024B97FD900BF9E94 /* YoloV4.mm */; };
@@ -26,7 +28,6 @@
2628
5FF8B4AE256DD8F90002375F /* openmp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5FF8B4AD256DD8F90002375F /* openmp.framework */; };
2729
5FF8B4B2256DD9CB0002375F /* opencv2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5FF8B4B1256DD9CA0002375F /* opencv2.framework */; };
2830
5FF8B4B6256DDAD20002375F /* opencv2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5FF8B4B5256DDAD20002375F /* opencv2.framework */; };
29-
5FF8B4BE256DF23E0002375F /* ncnn.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5FF8B4BD256DF23E0002375F /* ncnn.framework */; };
3031
5FF8B4C1256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.bin in Resources */ = {isa = PBXBuildFile; fileRef = 5FF8B4BF256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.bin */; };
3132
5FF8B4C2256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.param in Resources */ = {isa = PBXBuildFile; fileRef = 5FF8B4C0256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.param */; };
3233
5FF8B4C5256DF31D0002375F /* nanodet_m.param in Resources */ = {isa = PBXBuildFile; fileRef = 5FF8B4C3256DF31D0002375F /* nanodet_m.param */; };
@@ -37,6 +38,8 @@
3738
/* End PBXBuildFile section */
3839

3940
/* Begin PBXFileReference section */
41+
5F13A99D256F767800E45BA3 /* glslang.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = glslang.framework; sourceTree = "<group>"; };
42+
5F13A99F256F851000E45BA3 /* ncnn.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ncnn.framework; sourceTree = "<group>"; };
4043
5F37E19C24B97F9400BF9E94 /* yolov4-tiny-opt.param */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "yolov4-tiny-opt.param"; sourceTree = "<group>"; };
4144
5F37E19D24B97F9600BF9E94 /* yolov4-tiny-opt.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; path = "yolov4-tiny-opt.bin"; sourceTree = "<group>"; };
4245
5F37E1A024B97FD900BF9E94 /* YoloV4.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = YoloV4.mm; sourceTree = "<group>"; };
@@ -65,7 +68,6 @@
6568
5FF8B4AD256DD8F90002375F /* openmp.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = openmp.framework; sourceTree = "<group>"; };
6669
5FF8B4B1256DD9CA0002375F /* opencv2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = opencv2.framework; path = "../局域网共享/opencv2.framework"; sourceTree = "<group>"; };
6770
5FF8B4B5256DDAD20002375F /* opencv2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = opencv2.framework; sourceTree = "<group>"; };
68-
5FF8B4BD256DF23E0002375F /* ncnn.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ncnn.framework; sourceTree = "<group>"; };
6971
5FF8B4BF256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; path = "MobileNetV2-YOLOv3-Nano-coco.bin"; sourceTree = "<group>"; };
7072
5FF8B4C0256DF2780002375F /* MobileNetV2-YOLOv3-Nano-coco.param */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "MobileNetV2-YOLOv3-Nano-coco.param"; sourceTree = "<group>"; };
7173
5FF8B4C3256DF31D0002375F /* nanodet_m.param */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = nanodet_m.param; sourceTree = "<group>"; };
@@ -82,9 +84,10 @@
8284
buildActionMask = 2147483647;
8385
files = (
8486
5FF8B4AE256DD8F90002375F /* openmp.framework in Frameworks */,
87+
5F13A9A0256F851000E45BA3 /* ncnn.framework in Frameworks */,
8588
5FF8B4B6256DDAD20002375F /* opencv2.framework in Frameworks */,
8689
5FF8B4B2256DD9CB0002375F /* opencv2.framework in Frameworks */,
87-
5FF8B4BE256DF23E0002375F /* ncnn.framework in Frameworks */,
90+
5F13A99E256F767800E45BA3 /* glslang.framework in Frameworks */,
8891
);
8992
runOnlyForDeploymentPostprocessing = 0;
9093
};
@@ -111,7 +114,8 @@
111114
5F40C2A924B1647800C84041 /* YOLOv5NCNN */ = {
112115
isa = PBXGroup;
113116
children = (
114-
5FF8B4BD256DF23E0002375F /* ncnn.framework */,
117+
5F13A99F256F851000E45BA3 /* ncnn.framework */,
118+
5F13A99D256F767800E45BA3 /* glslang.framework */,
115119
5FF8B4B5256DDAD20002375F /* opencv2.framework */,
116120
5FF8B4AD256DD8F90002375F /* openmp.framework */,
117121
5FD3E2D424B4171C0049450B /* CameraCapture */,

‎iOS_YOLOv5NCNN/YOLOv5NCNN/NanoDet.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct {
1818

1919
class NanoDet {
2020
public:
21-
NanoDet(const char *param, const char *bin, bool useGPU);
21+
NanoDet(bool useGPU);
2222

2323
~NanoDet();
2424

‎iOS_YOLOv5NCNN/YOLOv5NCNN/NanoDet.mm‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "NanoDet.h"
22

3-
bool NanoDet::hasGPU = true;
4-
bool NanoDet::toUseGPU = true;
3+
bool NanoDet::hasGPU = false;
4+
bool NanoDet::toUseGPU = false;
55
NanoDet *NanoDet::detector = nullptr;
66

77
inline float fast_exp(float x) {
@@ -34,8 +34,16 @@ int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
3434
return 0;
3535
}
3636

37-
NanoDet::NanoDet(const char *param, const char *bin, bool useGPU) {
37+
NanoDet::NanoDet(bool useGPU) {
38+
#if NCNN_VULKAN
39+
ncnn::create_gpu_instance();
40+
hasGPU = ncnn::get_gpu_count();
41+
#endif
42+
toUseGPU = hasGPU && useGPU;
43+
3844
Net = new ncnn::Net();
45+
Net->opt.use_vulkan_compute = toUseGPU;
46+
Net->opt.use_fp16_arithmetic = true;
3947
NSString *parmaPath = [[NSBundle mainBundle] pathForResource:@"nanodet_m" ofType:@"param"];
4048
NSString *binPath = [[NSBundle mainBundle] pathForResource:@"nanodet_m" ofType:@"bin"];
4149
int rp = Net->load_param([parmaPath UTF8String]);
@@ -51,6 +59,9 @@ int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
5159
NanoDet::~NanoDet() {
5260
Net->clear();
5361
delete Net;
62+
#if NCNN_VULKAN
63+
ncnn::destroy_gpu_instance();
64+
#endif
5465
}
5566

5667
std::vector<BoxInfo> NanoDet::detect(UIImage *image, float score_threshold, float nms_threshold) {
@@ -73,9 +84,11 @@ int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
7384
auto ex = Net->create_extractor();
7485
ex.set_light_mode(true);
7586
ex.set_num_threads(4);
76-
// if (toUseGPU) { // 消除提示
77-
// ex.set_vulkan_compute(toUseGPU);
78-
// }
87+
#if NCNN_VULKAN
88+
if (toUseGPU) {
89+
ex.set_vulkan_compute(toUseGPU);
90+
}
91+
#endif
7992
ex.input("input.1", input);
8093
std::vector<std::vector<BoxInfo>> results;
8194
results.resize(this->num_class);

‎iOS_YOLOv5NCNN/YOLOv5NCNN/ViewController.mm‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#import <AVFoundation/AVFoundation.h>
2323
#import <AVFoundation/AVMediaFormat.h>
2424
#import "ELCameraControlCapture.h"
25-
#import <opencv2/opencv.hpp>
25+
#import <opencv2/opencv.hpp> // download opencv2.framework to project
2626

2727

2828
@interface ViewController ()
@@ -311,13 +311,13 @@ - (void)detectImage:(UIImage *)image {
311311
- (void)createModel {
312312
if (!self.yolo && self.USE_MODEL == W_YOLOV5S) {
313313
NSLog(@"new YoloV5");
314-
self.yolo = new YoloV5("", "");
314+
self.yolo = new YoloV5(self.USE_GPU);
315315
} else if (!self.yolov4 && self.USE_MODEL == W_YOLOV4TINY) {
316316
NSLog(@"new YoloV4");
317-
self.yolov4 = new YoloV4("", "", 0);
317+
self.yolov4 = new YoloV4(self.USE_GPU, 0);
318318
} else if (!self.yolov4 && self.USE_MODEL == W_MOBILENETV2_YOLOV3_NANO) {
319319
NSLog(@"new YoloV3-nano");
320-
self.yolov4 = new YoloV4("", "", 1);
320+
self.yolov4 = new YoloV4(self.USE_GPU, 1);
321321
} else if (!self.yolov4 && self.USE_MODEL == W_SIMPLE_POSE) {
322322
NSLog(@"new Simple-Pose");
323323
} else if (!self.yolov4 && self.USE_MODEL == W_YOLACT) {
@@ -334,10 +334,10 @@ - (void)createModel {
334334
NSLog(@"new yolov5s custom op");
335335
} else if (!self.nanoDet && self.USE_MODEL == W_NANODET) {
336336
NSLog(@"new nanodet");
337-
self.nanoDet = new NanoDet("", "", NO);
337+
self.nanoDet = new NanoDet(self.USE_GPU);
338338
} else if (!self.yolov4 && self.USE_MODEL == W_YOLO_FASTEST_XL) {
339339
NSLog(@"new yolo fastest xl");
340-
self.yolov4 = new YoloV4("", "", 2);
340+
self.yolov4 = new YoloV4(self.USE_GPU, 2);
341341
}
342342
}
343343

‎iOS_YOLOv5NCNN/YOLOv5NCNN/WelcomeVC.m‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ @interface WelcomeVC ()
2626
@property (strong, nonatomic) IBOutlet UIButton *btnYOLOFastestXL;
2727

2828
@property (strong, nonatomic) IBOutlet UIImageView *btnUseGPU;
29+
@property (assign, nonatomic) Boolean useGPU;
2930

3031
@end
3132

@@ -38,7 +39,32 @@ - (void)viewDidLoad {
3839
self.title = @"TENG";
3940
}
4041

42+
- (void)changeMode {
43+
self.useGPU = NO;
44+
self.btnUseGPU.userInteractionEnabled = YES;
45+
UITapGestureRecognizer *modeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeNcnnMode)];
46+
[self.btnUseGPU addGestureRecognizer:modeTap];
47+
}
48+
49+
- (void)changeNcnnMode {
50+
self.useGPU = self.useGPU ? NO : YES;
51+
NSString *title = @"Warning";
52+
NSString *message = @"ohhhhh";
53+
if (self.useGPU) {
54+
title = @"Warning";
55+
message = @"If the GPU is too old, it may not work well in GPU mode.";
56+
} else {
57+
title = @"Warning";
58+
message = @"Run on CPU.";
59+
}
60+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
61+
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
62+
[alert addAction:sure];
63+
[self presentViewController:alert animated:YES completion:nil];
64+
}
65+
4166
- (void)initView {
67+
[self changeMode];
4268
[_btnYolov5s addTarget:self action:@selector(pressYolov5s:) forControlEvents:UIControlEventTouchUpInside];
4369
[_btnYolov4tiny addTarget:self action:@selector(pressYolov4tiny:) forControlEvents:UIControlEventTouchUpInside];
4470
[_btnMBV2Yolov3nano addTarget:self action:@selector(pressMBNv2Yolov3Nano:) forControlEvents:UIControlEventTouchUpInside];
@@ -56,72 +82,84 @@ - (void)initView {
5682
- (void)pressYolov5s:(UIButton *)btn {
5783
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
5884
vc.USE_MODEL = W_YOLOV5S;
85+
vc.USE_GPU = self.useGPU;
5986
[self.navigationController pushViewController:vc animated:NO];
6087
}
6188

6289
- (void)pressYolov4tiny:(UIButton *)btn {
6390
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
6491
vc.USE_MODEL = W_YOLOV4TINY;
92+
vc.USE_GPU = self.useGPU;
6593
[self.navigationController pushViewController:vc animated:NO];
6694
}
6795

6896
- (void)pressMBNv2Yolov3Nano:(UIButton *)btn {
6997
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
7098
vc.USE_MODEL = W_MOBILENETV2_YOLOV3_NANO;
99+
vc.USE_GPU = self.useGPU;
71100
[self.navigationController pushViewController:vc animated:NO];
72101
}
73102

74103
- (void)pressSimplePose:(UIButton *)btn {
75104
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
76105
vc.USE_MODEL = W_SIMPLE_POSE;
106+
vc.USE_GPU = self.useGPU;
77107
[self.navigationController pushViewController:vc animated:NO];
78108
}
79109

80110
- (void)pressYolact:(UIButton *)btn {
81111
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
82112
vc.USE_MODEL = W_YOLACT;
113+
vc.USE_GPU = self.useGPU;
83114
[self.navigationController pushViewController:vc animated:NO];
84115
}
85116

86117
- (void)pressFaceLandmark:(UIButton *)btn {
87118
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
88119
vc.USE_MODEL = W_FACE_LANDMARK;
120+
vc.USE_GPU = self.useGPU;
89121
[self.navigationController pushViewController:vc animated:NO];
90122
}
91123

92124
- (void)pressDBFace:(UIButton *)btn {
93125
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
94126
vc.USE_MODEL = W_DBFACE;
127+
vc.USE_GPU = self.useGPU;
95128
[self.navigationController pushViewController:vc animated:NO];
96129
}
97130

98131
- (void)pressMBNv2FCN:(UIButton *)btn {
99132
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
100133
vc.USE_MODEL = W_MOBILENETV2_FCN;
134+
vc.USE_GPU = self.useGPU;
101135
[self.navigationController pushViewController:vc animated:NO];
102136
}
103137

104138
- (void)pressMBNv3SEG:(UIButton *)btn {
105139
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
106140
vc.USE_MODEL = W_MOBILENETV3_SEG;
141+
vc.USE_GPU = self.useGPU;
107142
[self.navigationController pushViewController:vc animated:NO];
108143
}
109144

110145
- (void)pressYOLOv5CustomOP:(UIButton *)btn {
111146
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
112147
vc.USE_MODEL = W_YOLOV5S_CUSTOM_OP;
148+
vc.USE_GPU = self.useGPU;
113149
[self.navigationController pushViewController:vc animated:NO];
114150
}
115151

116152
- (void)pressNanoDet:(UIButton *)btn {
117153
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
118154
vc.USE_MODEL = W_NANODET;
155+
vc.USE_GPU = self.useGPU;
119156
[self.navigationController pushViewController:vc animated:NO];
120157
}
121158

122159
- (void)pressYOLOFastestXL:(UIButton *)btn {
123160
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
124161
vc.USE_MODEL = W_YOLO_FASTEST_XL;
162+
vc.USE_GPU = self.useGPU;
125163
[self.navigationController pushViewController:vc animated:NO];
126164
}
127165

‎iOS_YOLOv5NCNN/YOLOv5NCNN/YoloV4.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
class YoloV4 {
3535
public:
36-
YoloV4(const char* param, const char* bin, const int yoloType);
36+
YoloV4(bool useGPU, const int yoloType);
3737
~YoloV4();
3838
std::vector<BoxInfo> detectv4(UIImage *image, float threshold, float nms_threshold);
3939
std::vector<std::string> labels{"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
@@ -54,6 +54,7 @@ class YoloV4 {
5454
public:
5555
static YoloV4 *detector;
5656
static bool hasGPU;
57+
static bool toUseGPU;
5758
};
5859

5960

‎iOS_YOLOv5NCNN/YOLOv5NCNN/YoloV4.mm‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77
********************************************************************************************/
88

99
bool YoloV4::hasGPU = false;
10+
bool YoloV4::toUseGPU = false;
1011
YoloV4 *YoloV4::detector = nullptr;
1112

12-
YoloV4::YoloV4(const char* param, const char* bin, const int yoloType) {
13+
YoloV4::YoloV4(bool useGPU, const int yoloType) {
14+
#if NCNN_VULKAN
15+
ncnn::create_gpu_instance();
16+
hasGPU = ncnn::get_gpu_count();
17+
#endif
18+
toUseGPU = hasGPU && useGPU;
19+
1320
Net = new ncnn::Net();
21+
Net->opt.use_vulkan_compute = toUseGPU;
22+
Net->opt.use_fp16_arithmetic = true;
1423
NSString *parmaPath = nil;
1524
NSString *binPath = nil;
1625
if (yoloType == 0) {
@@ -35,6 +44,9 @@
3544
YoloV4::~YoloV4() {
3645
Net->clear();
3746
delete Net;
47+
#if NCNN_VULKAN
48+
ncnn::destroy_gpu_instance();
49+
#endif
3850
}
3951

4052
std::vector<BoxInfo> YoloV4::detectv4(UIImage *image, float threshold, float nms_threshold) {
@@ -53,7 +65,11 @@
5365
auto ex = Net->create_extractor();
5466
ex.set_light_mode(true);
5567
ex.set_num_threads(4);
56-
// ex.set_vulkan_compute(hasGPU);
68+
#if NCNN_VULKAN
69+
if (toUseGPU) {
70+
ex.set_vulkan_compute(hasGPU);
71+
}
72+
#endif
5773
ex.input(0, in_net);
5874
std::vector<BoxInfo> result;
5975
ncnn::Mat blob;

‎iOS_YOLOv5NCNN/YOLOv5NCNN/YoloV5.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef struct BoxInfo {
4040

4141
class YoloV5 {
4242
public:
43-
YoloV5(const char* param, const char* bin);
43+
YoloV5(bool useGPU);
4444
~YoloV5();
4545
std::vector<BoxInfo> dectect(UIImage *image, float threshold, float nms_threshold);
4646
std::vector<std::string> labels{"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
@@ -69,6 +69,7 @@ class YoloV5 {
6969
public:
7070
static YoloV5 *detector;
7171
static bool hasGPU;
72+
static bool toUseGPU;
7273

7374
};
7475

0 commit comments

Comments
 (0)