qc903113684 commited on
Commit
efaf754
·
verified ·
1 Parent(s): 1d68287

Upload 7 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/cpp/deeplab2.png filter=lfs diff=lfs merge=lfs -text
37
+ model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/python/deeplab2.png filter=lfs diff=lfs merge=lfs -text
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/README.md ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Model Information
2
+ ### Source model
3
+ - Input shape: 520x520
4
+ - Number of parameters: 40.06M
5
+ - Model size: 160.16M
6
+ - Output shape: 1x21x520x520
7
+
8
+ Source model repository: [deeplabv3](https://github.com/pytorch/vision/blob/main/torchvision/models/segmentation/deeplabv3.py)
9
+
10
+ ### Converted model
11
+
12
+ - Precision: FP32
13
+ - Backend: TFLITE
14
+ - Target Device: FV01 QCS6490
15
+
16
+ ## Inference with AidLite SDK
17
+
18
+ ### SDK installation
19
+ Model Farm uses AidLite SDK as the model inference SDK. For details, please refer to the [AidLite Developer Documentation](https://v2.docs.aidlux.com/en/sdk-api/aidlite-sdk/)
20
+
21
+ - install AidLite SDK
22
+
23
+ ```bash
24
+ # Install the appropriate version of the aidlite sdk
25
+ sudo aid-pkg update
26
+ sudo aid-pkg install aidlite-sdk
27
+ # Download the qnn version that matches the above backend. Eg Install QNN2.23 Aidlite: sudo aid-pkg install aidlite-qnn223
28
+ sudo aid-pkg install aidlite-{QNN VERSION}
29
+ ```
30
+
31
+ - Verify AidLite SDK
32
+
33
+ ```bash
34
+ # aidlite sdk c++ check
35
+ python3 -c "import aidlite ; print(aidlite.get_library_version())"
36
+
37
+ # aidlite sdk python check
38
+ python3 -c "import aidlite ; print(aidlite.get_py_library_version())"
39
+ ```
40
+
41
+ ### Run Demo
42
+ #### python
43
+ ```bash
44
+ cd deeplabv3_resnet50/model_farm_deeplabv3_resnet50_qcs6490_qnn2.16_fp32_aidlite
45
+ python3 python/run_test.py --target_model ./models/cutoff_deeplabv3_resnet50_fp32.tflite --imgs ./python/deeplab2.png --invoke_nums 10
46
+ ```
47
+
48
+ #### c++
49
+ ```bash
50
+ cd deeplabv3_resnet50/model_farm_deeplabv3_resnet50_qcs6490_qnn2.16_fp32_aidlite/cpp
51
+ mkdir build && cd build
52
+ cmake ..
53
+ make
54
+ ./run_test
55
+ ```
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/cpp/CMakeLists.txt ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cmake_minimum_required (VERSION 3.5)
2
+ project("run_test")
3
+
4
+ find_package(OpenCV REQUIRED)
5
+
6
+ message(STATUS "oPENCV Library status:")
7
+ message(STATUS ">version:${OpenCV_VERSION}")
8
+ message(STATUS "Include:${OpenCV_INCLUDE_DIRS}")
9
+
10
+ set(CMAKE_CXX_FLAGS "-Wno-error=deprecated-declarations -Wno-deprecated-declarations")
11
+
12
+ include_directories(
13
+ /usr/local/include
14
+ /usr/include/opencv4
15
+ )
16
+
17
+ link_directories(
18
+ /usr/local/lib/
19
+ )
20
+
21
+ file(GLOB SRC_LISTS
22
+ ${CMAKE_CURRENT_SOURCE_DIR}/run_test.cpp
23
+ )
24
+
25
+ add_executable(run_test ${SRC_LISTS})
26
+
27
+ target_link_libraries(run_test
28
+ aidlite
29
+ ${OpenCV_LIBS}
30
+ pthread
31
+ jsoncpp
32
+ )
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/cpp/deeplab2.png ADDED

Git LFS Details

  • SHA256: 221f9a584e69e2c421cf0207f29c0bc381cbd963d13473063a1f222c8233f637
  • Pointer size: 131 Bytes
  • Size of remote file: 391 kB
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/cpp/run_test.cpp ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include <fstream>
3
+ #include <opencv2/opencv.hpp>
4
+ #include <aidlux/aidlite/aidlite.hpp>
5
+ #include <vector>
6
+ #include <numeric>
7
+ #include <cmath>
8
+ #include <cfloat>
9
+ #include <jsoncpp/json/json.h>
10
+
11
+ using namespace cv;
12
+ using namespace std;
13
+ using namespace Aidlux::Aidlite;
14
+
15
+
16
+ struct Args {
17
+ std::string target_model = "../../models/cutoff_deeplabv3_resnet50_fp32.tflite";
18
+ std::string imgs = "../deeplab2.png";
19
+ int invoke_nums = 1;
20
+ std::string model_type = "TFLITE";
21
+ };
22
+
23
+
24
+ Args parse_args(int argc, char* argv[]) {
25
+ Args args;
26
+ for (int i = 1; i < argc; ++i) {
27
+ std::string arg = argv[i];
28
+ if (arg == "--target_model" && i + 1 < argc) {
29
+ args.target_model = argv[++i];
30
+ } else if (arg == "--imgs" && i + 1 < argc) {
31
+ args.imgs = argv[++i];
32
+ } else if (arg == "--invoke_nums" && i + 1 < argc) {
33
+ args.invoke_nums = std::stoi(argv[++i]);
34
+ } else if (arg == "--model_type" && i + 1 < argc) {
35
+ args.model_type = argv[++i];
36
+ }
37
+ }
38
+ return args;
39
+ }
40
+
41
+ std::string to_lower(const std::string& str) {
42
+ std::string lower_str = str;
43
+ std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), [](unsigned char c) {
44
+ return std::tolower(c);
45
+ });
46
+ return lower_str;
47
+ }
48
+
49
+ static const cv::Vec3b PASCAL_VOC_COLORS[21] = {
50
+ { 0, 0, 0}, {128, 0, 0}, { 0, 128, 0}, {128, 128, 0},
51
+ { 0, 0, 128}, {128, 0, 128}, { 0, 128, 128}, {128, 128, 128},
52
+ { 64, 0, 0}, {192, 0, 0}, { 64, 128, 0}, {192, 128, 0},
53
+ { 64, 0, 128}, {192, 0, 128}, { 64, 128, 128}, {192, 128, 128},
54
+ { 0, 64, 0}, {128, 64, 0}, { 0, 192, 0}, {128, 192, 0},
55
+ { 0, 64, 128}
56
+ };
57
+
58
+ // simple decodeSegMap: classIdx CV_8UC1 → color CV_8UC3
59
+ cv::Mat decodeSegMap(const cv::Mat& classIdx)
60
+ {
61
+ CV_Assert(classIdx.type() == CV_8UC1);
62
+ cv::Mat color(classIdx.size(), CV_8UC3);
63
+ for(int y = 0; y < classIdx.rows; ++y) {
64
+ const uchar* p = classIdx.ptr<uchar>(y);
65
+ cv::Vec3b* q = color.ptr<cv::Vec3b>(y);
66
+ for(int x = 0; x < classIdx.cols; ++x) {
67
+ q[x] = PASCAL_VOC_COLORS[p[x]];
68
+ }
69
+ }
70
+ return color;
71
+ }
72
+
73
+ int transpose(float* src, unsigned int* src_dims, unsigned int* tsp_dims, float* dest){
74
+
75
+ int current_coordinate[4] = {0, 0, 0, 0};
76
+ for(int a = 0; a < src_dims[0]; ++a){
77
+ current_coordinate[0] = a;
78
+ for(int b = 0; b < src_dims[1]; ++b){
79
+ current_coordinate[1] = b;
80
+ for(int c = 0; c < src_dims[2]; ++c){
81
+ current_coordinate[2] = c;
82
+ for(int d = 0; d < src_dims[3]; ++d){
83
+ current_coordinate[3] = d;
84
+
85
+ int old_index = current_coordinate[0]*src_dims[1]*src_dims[2]*src_dims[3] +
86
+ current_coordinate[1]*src_dims[2]*src_dims[3] +
87
+ current_coordinate[2]*src_dims[3] +
88
+ current_coordinate[3];
89
+
90
+ int new_index = current_coordinate[tsp_dims[0]]*src_dims[tsp_dims[1]]*src_dims[tsp_dims[2]]*src_dims[tsp_dims[3]] +
91
+ current_coordinate[tsp_dims[1]]*src_dims[tsp_dims[2]]*src_dims[tsp_dims[3]] +
92
+ current_coordinate[tsp_dims[2]]*src_dims[tsp_dims[3]] +
93
+ current_coordinate[tsp_dims[3]];
94
+
95
+ dest[new_index] = src[old_index];
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ return EXIT_SUCCESS;
102
+ }
103
+
104
+
105
+
106
+ int invoke(const Args& args) {
107
+ std::cout << "Start main ... ... Model Path: " << args.target_model << "\n"
108
+ << "Image Path: " << args.imgs << "\n"
109
+ << "Inference Nums: " << args.invoke_nums << "\n"
110
+ << "Model Type: " << args.model_type << "\n";
111
+ Model* model = Model::create_instance(args.target_model);
112
+ if(model == nullptr){
113
+ printf("Create model failed !\n");
114
+ return EXIT_FAILURE;
115
+ }
116
+ Config* config = Config::create_instance();
117
+ if(config == nullptr){
118
+ printf("Create config failed !\n");
119
+ return EXIT_FAILURE;
120
+ }
121
+ config->implement_type = ImplementType::TYPE_LOCAL;
122
+ std::string model_type_lower = to_lower(args.model_type);
123
+ if (model_type_lower == "qnn"){
124
+ config->framework_type = FrameworkType::TYPE_QNN;
125
+ config->accelerate_type = AccelerateType::TYPE_DSP;
126
+ config->is_quantify_model = 1;
127
+ } else if (model_type_lower == "snpe2" || model_type_lower == "snpe") {
128
+ config->framework_type = FrameworkType::TYPE_SNPE2;
129
+ config->accelerate_type = AccelerateType::TYPE_DSP;
130
+ config->is_quantify_model = 1;
131
+ } else if (model_type_lower == "tflite") {
132
+ config->framework_type = FrameworkType::TYPE_TFLITE;
133
+ config->accelerate_type = AccelerateType::TYPE_GPU;
134
+ } else {
135
+ printf("Unsupported model type: %s\n", args.model_type.c_str());
136
+ return EXIT_FAILURE;
137
+ }
138
+
139
+ unsigned int model_h = 520;
140
+ unsigned int model_w = 520;
141
+ std::vector<std::vector<uint32_t>> input_shapes = {{1,model_h,model_w,3}};
142
+ std::vector<std::vector<uint32_t>> output_shapes = {{1,520,520,21}};
143
+ model->set_model_properties(input_shapes, Aidlux::Aidlite::DataType::TYPE_FLOAT32, output_shapes, Aidlux::Aidlite::DataType::TYPE_FLOAT32);
144
+ std::unique_ptr<Interpreter> fast_interpreter = InterpreterBuilder::build_interpretper_from_model_and_config(model, config);
145
+ if(fast_interpreter == nullptr){
146
+ printf("build_interpretper_from_model_and_config failed !\n");
147
+ return EXIT_FAILURE;
148
+ }
149
+ int result = fast_interpreter->init();
150
+ if(result != EXIT_SUCCESS){
151
+ printf("interpreter->init() failed !\n");
152
+ return EXIT_FAILURE;
153
+ }
154
+ // load model
155
+ fast_interpreter->load_model();
156
+ if(result != EXIT_SUCCESS){
157
+ printf("interpreter->load_model() failed !\n");
158
+ return EXIT_FAILURE;
159
+ }
160
+ printf("detect model load success!\n");
161
+
162
+ cv::Mat frame = cv::imread(args.imgs);
163
+ if (frame.empty()) {
164
+ printf("detect image load failed!\n");
165
+ return 1;
166
+ }
167
+ printf("img_src cols: %d, img_src rows: %d\n", frame.cols, frame.rows);
168
+ cv::Mat input_data;
169
+ cv::Mat frame_clone = frame.clone();
170
+ cv::cvtColor(frame_clone, frame_clone, cv::COLOR_BGR2RGB);
171
+ cv::resize(frame_clone, frame_clone, cv::Size(model_w, model_h));
172
+ frame_clone.convertTo(input_data, CV_32FC3, 1.0f / 255.0f);
173
+
174
+
175
+ float *outdata0 = nullptr;
176
+ std::vector<float> invoke_time;
177
+ for (int i = 0; i < args.invoke_nums; ++i) {
178
+ result = fast_interpreter->set_input_tensor(0, input_data.data);
179
+ if(result != EXIT_SUCCESS){
180
+ printf("interpreter->set_input_tensor() failed !\n");
181
+ return EXIT_FAILURE;
182
+ }
183
+ auto t1 = std::chrono::high_resolution_clock::now();
184
+ result = fast_interpreter->invoke();
185
+ auto t2 = std::chrono::high_resolution_clock::now();
186
+ std::chrono::duration<double> cost_time = t2 - t1;
187
+ invoke_time.push_back(cost_time.count() * 1000);
188
+ if(result != EXIT_SUCCESS){
189
+ printf("interpreter->invoke() failed !\n");
190
+ return EXIT_FAILURE;
191
+ }
192
+ uint32_t out_data_0 = 0;
193
+ result = fast_interpreter->get_output_tensor(0, (void**)&outdata0, &out_data_0);
194
+ if(result != EXIT_SUCCESS){
195
+ printf("interpreter->get_output_tensor() 1 failed !\n");
196
+ return EXIT_FAILURE;
197
+ }
198
+
199
+ }
200
+
201
+ float max_invoke_time = *std::max_element(invoke_time.begin(), invoke_time.end());
202
+ float min_invoke_time = *std::min_element(invoke_time.begin(), invoke_time.end());
203
+ float mean_invoke_time = std::accumulate(invoke_time.begin(), invoke_time.end(), 0.0f) / args.invoke_nums;
204
+ float var_invoketime = 0.0f;
205
+ for (auto time : invoke_time) {
206
+ var_invoketime += (time - mean_invoke_time) * (time - mean_invoke_time);
207
+ }
208
+ var_invoketime /= args.invoke_nums;
209
+ printf("=======================================\n");
210
+ printf("QNN inference %d times :\n --mean_invoke_time is %f \n --max_invoke_time is %f \n --min_invoke_time is %f \n --var_invoketime is %f\n",
211
+ args.invoke_nums, mean_invoke_time, max_invoke_time, min_invoke_time, var_invoketime);
212
+ printf("=======================================\n");
213
+
214
+ // post process
215
+
216
+ unsigned int src_dims[4] = {1, 520,520,21};
217
+ unsigned int tsp_dims[4] = {0,3,1,2};
218
+ unsigned int stride_data_num = 1*520*520*21;
219
+ float* format_data = new float[stride_data_num];
220
+ transpose(outdata0, src_dims, tsp_dims, format_data);
221
+
222
+
223
+ cv::Mat prediction(model_h, model_w, CV_8UC1);
224
+ for(int y = 0; y < model_h; ++y) {
225
+ for(int x = 0; x < model_w; ++x) {
226
+ float maxVal = -FLT_MAX;
227
+ int maxIdx = 0;
228
+ for(int c = 0; c < 21; ++c) {
229
+ int idx = c*model_h*model_w + y*model_w + x;
230
+ float v = format_data[idx];
231
+ if(v > maxVal) {
232
+ maxVal = v;
233
+ maxIdx = c;
234
+ }
235
+ }
236
+ prediction.at<uchar>(y,x) = static_cast<uchar>(maxIdx);
237
+ }
238
+ }
239
+
240
+ // 2) decode to color map
241
+ cv::Mat colorMap = decodeSegMap(prediction);
242
+ // 3) blend with original frame (assumes frame is same size as out_h×out_w)
243
+ cv::Mat resizedFrame;
244
+ cv::resize(frame, resizedFrame, cv::Size(model_w, model_h), 0, 0, cv::INTER_LINEAR);
245
+
246
+ cv::Mat mask_img;
247
+ cv::addWeighted(resizedFrame, 0.5, colorMap, 0.5, 0.0, mask_img);
248
+
249
+ // 4) resize back to original resolution and save
250
+ cv::resize(mask_img, mask_img, cv::Size(model_w, model_h), 0, 0, cv::INTER_NEAREST);
251
+ cv::imwrite("./original_mask_result_TFlite_fp32.jpg", mask_img);
252
+
253
+
254
+
255
+
256
+ fast_interpreter->destory();
257
+ return 0;
258
+ }
259
+
260
+
261
+ int main(int argc, char* argv[]) {
262
+ Args args = parse_args(argc, argv);
263
+ return invoke(args);
264
+ }
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/models/cutoff_deeplabv3_resnet50_fp32.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5207ca983775ed595b4d128f1c56b2e48cee0df9c0bfd4d7998875f78143bb42
3
+ size 158483120
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/python/deeplab2.png ADDED

Git LFS Details

  • SHA256: 221f9a584e69e2c421cf0207f29c0bc381cbd963d13473063a1f222c8233f637
  • Pointer size: 131 Bytes
  • Size of remote file: 391 kB
model_farm_deeplabv3_resnet50_qcs6490_tflite_fp32_aidlite/python/run_test.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import onnxruntime
2
+ import cv2
3
+ import numpy as np
4
+ import aidlite
5
+ import argparse
6
+ import time
7
+
8
+ # Define the helper function
9
+ def decode_segmap(image, nc=21):
10
+ label_colors = np.array([(0, 0, 0), # 0=background
11
+ # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
12
+ (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),
13
+ # 6=bus, 7=car, 8=cat, 9=chair, 10=cow
14
+ (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0),
15
+ # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person
16
+ (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
17
+ # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
18
+ (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)])
19
+ r = np.zeros_like(image).astype(np.uint8)
20
+ g = np.zeros_like(image).astype(np.uint8)
21
+ b = np.zeros_like(image).astype(np.uint8)
22
+ for l in range(0, nc):
23
+ idx = image == l
24
+ r[idx] = label_colors[l, 0]
25
+ g[idx] = label_colors[l, 1]
26
+ b[idx] = label_colors[l, 2]
27
+ rgb = np.stack([r, g, b], axis=2)
28
+ return rgb
29
+
30
+ def run(args):
31
+ print("Start main ... ...")
32
+ # aidlite.set_log_level(aidlite.LogLevel.INFO)
33
+ # aidlite.log_to_stderr()
34
+ # print(f"Aidlite library version : {aidlite.get_library_version()}")
35
+ # print(f"Aidlite python library version : {aidlite.get_py_library_version()}")
36
+ config = aidlite.Config.create_instance()
37
+ if config is None:
38
+ print("Create config failed !")
39
+ return False
40
+
41
+ config.implement_type = aidlite.ImplementType.TYPE_LOCAL
42
+ if args.model_type.lower()=="qnn":
43
+ config.framework_type = aidlite.FrameworkType.TYPE_QNN
44
+ config.accelerate_type = aidlite.AccelerateType.TYPE_DSP
45
+ config.is_quantify_model = 1
46
+ elif args.model_type.lower()=="snpe2" or args.model_type.lower()=="snpe":
47
+ config.framework_type = aidlite.FrameworkType.TYPE_SNPE2
48
+ config.accelerate_type = aidlite.AccelerateType.TYPE_DSP
49
+ config.is_quantify_model = 1
50
+ elif args.model_type.lower()=="tflite":
51
+ config.framework_type = aidlite.FrameworkType.TYPE_TFLITE
52
+ config.accelerate_type = aidlite.AccelerateType.TYPE_GPU
53
+ config.is_quantify_model = 0
54
+
55
+
56
+
57
+ model = aidlite.Model.create_instance(args.target_model)
58
+ if model is None:
59
+ print("Create model failed !")
60
+ return False
61
+
62
+ input_shapes=[[1,520,520,3]]
63
+ output_shapes=[[1,520,520,21]]
64
+ model.set_model_properties(input_shapes, aidlite.DataType.TYPE_FLOAT32,
65
+ output_shapes, aidlite.DataType.TYPE_FLOAT32)
66
+
67
+ interpreter = aidlite.InterpreterBuilder.build_interpretper_from_model_and_config(model, config)
68
+ if interpreter is None:
69
+ print("build_interpretper_from_model_and_config failed !")
70
+ return None
71
+ result = interpreter.init()
72
+ if result != 0:
73
+ print(f"interpreter init failed !")
74
+ return False
75
+ result = interpreter.load_model()
76
+ if result != 0:
77
+ print("interpreter load model failed !")
78
+ return False
79
+ print("detect model load success!")
80
+
81
+ frame = cv2.imread(args.imgs)
82
+ w,h = frame.shape[1],frame.shape[0]
83
+ frame = cv2.resize(frame,(520, 520))
84
+ new_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
85
+ img = new_img/255.0
86
+ img_input = img.astype(np.float32)
87
+
88
+ invoke_time=[]
89
+ for i in range(args.invoke_nums):
90
+ result = interpreter.set_input_tensor(0, img_input.data)
91
+ if result != 0:
92
+ print("interpreter set_input_tensor() failed")
93
+
94
+ t1=time.time()
95
+ result = interpreter.invoke()
96
+ cost_time = (time.time()-t1)*1000
97
+ invoke_time.append(cost_time)
98
+
99
+ if result != 0:
100
+ print("interpreter set_input_tensor() failed")
101
+
102
+ qnn_out = interpreter.get_output_tensor(0)
103
+
104
+ result = interpreter.destory()
105
+ ## time 统计
106
+ max_invoke_time = max(invoke_time)
107
+ min_invoke_time = min(invoke_time)
108
+ mean_invoke_time = sum(invoke_time)/args.invoke_nums
109
+ var_invoketime=np.var(invoke_time)
110
+ print("=======================================")
111
+ print(f"QNN inference {args.invoke_nums} times :\n --mean_invoke_time is {mean_invoke_time} \n --max_invoke_time is {max_invoke_time} \n --min_invoke_time is {min_invoke_time} \n --var_invoketime is {var_invoketime}")
112
+ print("=======================================")
113
+
114
+ # 后处理
115
+ qnn_out1 = qnn_out.reshape(output_shapes[0]).transpose(0,3,1,2)
116
+ prediction = np.argmax(qnn_out1[0],axis=0)
117
+ # print("prediction :",prediction.shape,prediction)
118
+ prediction = prediction.astype(np.uint8)
119
+ test = decode_segmap(prediction,args.cls_num)
120
+
121
+ mask_img = cv2.addWeighted(frame,0.5,test,0.5,0)
122
+ mask_img = cv2.resize(mask_img,(w,h),interpolation=cv2.INTER_NEAREST)
123
+
124
+ cv2.imwrite(f"./python/original_mask_result_TFlite_fp32.jpg",mask_img)
125
+ print("Image segmentation has completed!")
126
+
127
+
128
+
129
+ def parser_args():
130
+ parser = argparse.ArgumentParser(description="Run model benchmarks")
131
+ parser.add_argument('--target_model',type=str,default='./models/cutoff_deeplabv3_resnet50_fp32.tflite',help="inference model path")
132
+ parser.add_argument('--imgs',type=str,default='./python/deeplab2.png',help="Predict images path")
133
+ parser.add_argument('--cls_num',type=int,default=21,help="The number of targets detected")
134
+ parser.add_argument('--invoke_nums',type=int,default=10,help="Inference nums")
135
+ parser.add_argument('--model_type',type=str,default='TFLITE',help="run backend")
136
+ args = parser.parse_args()
137
+ return args
138
+
139
+ if __name__ == "__main__":
140
+ args = parser_args()
141
+ run(args)