blob: 17d4ad5f6f135bf50282b45448974cc17a63b0f0 [file] [log] [blame]
Filip Kujawadc7d47c2023-04-08 16:16:51 -07001#include "yolov5.h"
2
Filip Kujawa8c76e5d2023-04-08 16:20:27 -07003#include <tensorflow/lite/c/common.h>
Filip Kujawa26a23662023-04-08 16:19:13 -07004#include <tensorflow/lite/interpreter.h>
5#include <tensorflow/lite/kernels/register.h>
6#include <tensorflow/lite/model.h>
Filip Kujawa8c76e5d2023-04-08 16:20:27 -07007#include <tflite/public/edgetpu.h>
Filip Kujawa26a23662023-04-08 16:19:13 -07008#include <tflite/public/edgetpu_c.h>
9
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070010#include <opencv2/dnn.hpp>
Filip Kujawadc7d47c2023-04-08 16:16:51 -070011
12#include "gflags/gflags.h"
13#include "glog/logging.h"
14
15DEFINE_double(conf_threshold, 0.9,
16 "Threshold value for confidence scores. Detections with a "
17 "confidence score below this value will be ignored.");
18
19DEFINE_double(
20 nms_threshold, 0.5,
21 "Threshold value for non-maximum suppression. Detections with an "
22 "intersection-over-union value below this value will be removed.");
23
24DEFINE_int32(nthreads, 6, "Number of threads to use during inference.");
25
26namespace y2023 {
27namespace vision {
28
Filip Kujawa26a23662023-04-08 16:19:13 -070029class YOLOV5Impl : public YOLOV5 {
30 public:
31 // Takes a model path as string and and loads a pre-trained
32 // YOLOv5 model from the specified path.
33 void LoadModel(const std::string path);
34
35 // Takes an image and returns a Detection.
36 std::vector<Detection> ProcessImage(cv::Mat image);
37
38 private:
39 // Convert an OpenCV Mat object to a tensor input
40 // that can be fed to the TensorFlow Lite model.
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070041 void ConvertCVMatToTensor(cv::Mat src, uint8_t *in);
Filip Kujawa26a23662023-04-08 16:19:13 -070042
43 // Resizes, converts color space, and converts
44 // image data type before inference.
45 void Preprocess(cv::Mat image);
46
47 // Converts a TensorFlow Lite tensor to a 2D vector.
48 std::vector<std::vector<float>> TensorToVector2D(TfLiteTensor *src_tensor,
49 const int rows,
50 const int columns);
51
52 // Performs non-maximum suppression to remove overlapping bounding boxes.
53 void NonMaximumSupression(const std::vector<std::vector<float>> &orig_preds,
54 const int rows, const int columns,
55 std::vector<Detection> *detections,
56 std::vector<int> *indices);
57 // Models
58 std::unique_ptr<tflite::FlatBufferModel> model_;
59 std::unique_ptr<tflite::Interpreter> interpreter_;
60 tflite::StderrReporter error_reporter_;
61
62 // Parameters of interpreter's input
63 int input_;
64 int in_height_;
65 int in_width_;
66 int in_channels_;
67 int in_type_;
68
69 // Parameters of original image
70 int img_height_;
71 int img_width_;
72
73 // Input of the interpreter
74 uint8_t *input_8_;
75
76 // Subtract this offset from class labels to get the actual label.
77 static constexpr int kClassIdOffset = 5;
78};
79
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070080std::unique_ptr<YOLOV5> MakeYOLOV5() { return std::make_unique<YOLOV5Impl>(); }
Filip Kujawa26a23662023-04-08 16:19:13 -070081
82void YOLOV5Impl::LoadModel(const std::string path) {
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070083 LOG(INFO) << "Load model: start";
84
85
86 tflite::ops::builtin::BuiltinOpResolver resolver;
87
88 model_ = tflite::FlatBufferModel::VerifyAndBuildFromFile(path.c_str());
89
90 /*
91 auto model_impl = model_->GetModel();
92 model_impl->subgraphs();
93 LOG(INFO) << model_impl;
94 LOG(INFO) << model_impl->subgraphs();
95 auto subgraphs = model_impl->subgraphs();
96 LOG(INFO) << subgraphs->size();
97 LOG(INFO) << subgraphs->Get(0)->inputs()->size();
98 LOG(INFO) << subgraphs->Get(0)->inputs()->Get(0);
99 (void)subgraphs;
100 */
101
102 LOG(INFO) << "Load model: Build Model from file";
103
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700104 CHECK(model_);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700105 CHECK(model_->initialized());
106 CHECK_EQ(tflite::InterpreterBuilder(*model_, resolver)(&interpreter_),
107 kTfLiteOk);
108 LOG(INFO) << "Load model: Interpreter builder done";
109 /*
110 LOG(INFO) << &interpreter_->primary_subgraph();
111 LOG(INFO) << interpreter_->subgraph(0);
112 LOG(INFO) << interpreter_->subgraphs_size();
113 LOG(INFO) << interpreter_->subgraph(0)->inputs().size();
114 LOG(INFO) << interpreter_->inputs().size();
115 */
116
117 //interpreter_->SetExternalContext(kTfLiteEdgeTpuContext, edgetpu_context.get());
118 // LOG(INFO) << "After set external context";
119
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700120 size_t num_devices;
121 std::unique_ptr<edgetpu_device, decltype(&edgetpu_free_devices)> devices(
122 edgetpu_list_devices(&num_devices), &edgetpu_free_devices);
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700123
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700124 //const auto &available_tpus =
125 // edgetpu::EdgeTpuManager::GetSingleton()->EnumerateEdgeTpu();
126 //LOG(INFO) << "Available tpus: " << available_tpus.size();
127
128 LOG(INFO) << "Load model: Getting devices";
129 CHECK_EQ(num_devices, 1ul);
130 const auto &device = devices.get()[0];
131 (void )device;
132 LOG(INFO) << "Load model: Got Device";
133
134 auto *delegate = edgetpu_create_delegate(device.type, device.path, nullptr, 0);
135
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700136 interpreter_->ModifyGraphWithDelegate(delegate);
137
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700138
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700139 TfLiteStatus status = interpreter_->AllocateTensors();
140 CHECK_EQ(status, kTfLiteOk);
141 CHECK(interpreter_);
142
143 LOG(INFO) << "Load model: Allocate tensors success";
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700144 input_ = interpreter_->inputs()[0];
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700145 LOG(INFO) << "After set inputs";
146 LOG(INFO) << input_;
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700147 TfLiteIntArray *dims = interpreter_->tensor(input_)->dims;
148 in_height_ = dims->data[1];
149 in_width_ = dims->data[2];
150 in_channels_ = dims->data[3];
151 in_type_ = interpreter_->tensor(input_)->type;
152 input_8_ = interpreter_->typed_tensor<uint8_t>(input_);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700153
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700154
155 interpreter_->SetNumThreads(FLAGS_nthreads);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700156
157 LOG(INFO) << "End of load";
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700158}
159
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700160void YOLOV5Impl::ConvertCVMatToTensor(cv::Mat src, uint8_t *in) {
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700161 CHECK(src.type() == CV_8UC3);
162 int n = 0, nc = src.channels(), ne = src.elemSize();
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700163 LOG(INFO) << "ConvertCVMatToTensor - Rows " << src.rows;
164 LOG(INFO) << "ConvertCVMatToTensor - Cols " << src.cols;
165 for (int y = 0; y < src.rows; ++y) {
166 for (int x = 0; x < src.cols; ++x) {
167 for (int c = 0; c < nc; ++c) {
168 (void)ne;
169 (void)n;
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700170 in[n++] = src.data[y * src.step + x * ne + c];
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700171 }
172 }
173 }
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700174}
175
Filip Kujawa26a23662023-04-08 16:19:13 -0700176std::vector<std::vector<float>> YOLOV5Impl::TensorToVector2D(
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700177 TfLiteTensor *src_tensor, const int rows, const int columns) {
178 auto scale = src_tensor->params.scale;
179 auto zero_point = src_tensor->params.zero_point;
180 std::vector<std::vector<float>> result_vec;
181 for (int32_t i = 0; i < rows; i++) {
182 std::vector<float> row_values;
183 for (int32_t j = 0; j < columns; j++) {
184 float val_float =
185 ((static_cast<int32_t>(src_tensor->data.uint8[i * columns + j])) -
186 zero_point) *
187 scale;
188 row_values.push_back(val_float);
189 }
190 result_vec.push_back(row_values);
191 }
192 return result_vec;
193}
194
Filip Kujawa26a23662023-04-08 16:19:13 -0700195void YOLOV5Impl::NonMaximumSupression(
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700196 const std::vector<std::vector<float>> &orig_preds, const int rows,
197 const int columns, std::vector<Detection> *detections,
198 std::vector<int> *indices)
199
200{
201 std::vector<float> scores;
202 double confidence;
203 cv::Point class_id;
204
205 for (int i = 0; i < rows; i++) {
206 if (orig_preds[i][4] > FLAGS_conf_threshold) {
207 int left = (orig_preds[i][0] - orig_preds[i][2] / 2) * img_width_;
208 int top = (orig_preds[i][1] - orig_preds[i][3] / 2) * img_height_;
209 int w = orig_preds[i][2] * img_width_;
210 int h = orig_preds[i][3] * img_height_;
211
212 for (int j = 5; j < columns; j++) {
213 scores.push_back(orig_preds[i][j] * orig_preds[i][4]);
214 }
215
216 cv::minMaxLoc(scores, nullptr, &confidence, nullptr, &class_id);
217 if (confidence > FLAGS_conf_threshold) {
218 Detection detection{cv::Rect(left, top, w, h), confidence,
219 class_id.x - kClassIdOffset};
220 detections->push_back(detection);
221 }
222 }
223 }
224
225 std::vector<cv::Rect> boxes;
226 std::vector<float> confidences;
227
228 for (const Detection &d : *detections) {
229 boxes.push_back(d.box);
230 confidences.push_back(d.confidence);
231 }
232
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700233 (void)indices;
234 // TODO(FILIP): Fix linker error.
235 // cv::dnn::NMSBoxes(boxes, confidences, FLAGS_conf_threshold,
236 // FLAGS_nms_threshold, *indices);
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700237}
238
Filip Kujawa26a23662023-04-08 16:19:13 -0700239std::vector<Detection> YOLOV5Impl::ProcessImage(cv::Mat frame) {
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700240 img_height_ = frame.rows;
241 img_width_ = frame.cols;
242
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700243 //Preprocess;
244 cv::resize(frame, frame, cv::Size(in_height_, in_width_), cv::INTER_CUBIC);
245 cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
246 frame.convertTo(frame, CV_8U);
247
248 LOG(INFO) << "After preprocess - Before convert to tensor";
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700249 ConvertCVMatToTensor(frame, input_8_);
250
251 // Inference
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700252 LOG(INFO) << "Before Invoke";
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700253 TfLiteStatus status = interpreter_->Invoke();
254 CHECK_EQ(status, kTfLiteOk);
255
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700256 LOG(INFO) << "After invoke, status checked";
257
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700258 int output_tensor_index = interpreter_->outputs()[0];
259 TfLiteIntArray *out_dims = interpreter_->tensor(output_tensor_index)->dims;
260 int num_rows = out_dims->data[1];
261 int num_columns = out_dims->data[2];
262
263 TfLiteTensor *src_tensor = interpreter_->tensor(interpreter_->outputs()[0]);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700264
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700265 std::vector<std::vector<float>> orig_preds =
266 TensorToVector2D(src_tensor, num_rows, num_columns);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700267 LOG(INFO) << "After tensor to vector 2D";
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700268
269 std::vector<int> indices;
270 std::vector<Detection> detections;
271
272 NonMaximumSupression(orig_preds, num_rows, num_columns, &detections,
273 &indices);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700274 LOG(INFO) << "After NMS";
275 for (size_t i = 0; i < interpreter_->outputs().size(); i++) {
276 LOG(INFO) << "Detection #" << i << " | " << interpreter_->outputs()[i];
277 }
278 if (detections.size() > 0) {
279 LOG(INFO) << "Detection ID: " << detections[0].class_id;
280 LOG(INFO) << "Confidence" << detections[0].confidence;
281 }
Filip Kujawadc7d47c2023-04-08 16:16:51 -0700282 return detections;
283};
284
285} // namespace vision
286} // namespace y2023