yolo.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef YOLO_HPP__
  2. #define YOLO_HPP__
  3. #include <vector>
  4. #include <iomanip>
  5. #include "common/memory.hpp"
  6. #include "infer/infer.hpp"
  7. #include "common/image.hpp"
  8. #include "common/data.hpp"
  9. #include "infer/slice/slice.hpp"
  10. #include "infer/trt/affine.hpp"
  11. #include "common/data.hpp"
  12. #ifdef TRT10
  13. #include "common/tensorrt.hpp"
  14. namespace TensorRT = TensorRT10;
  15. #else
  16. #include "common/tensorrt8.hpp"
  17. namespace TensorRT = TensorRT8;
  18. #endif
  19. namespace yolo
  20. {
  21. class YoloModelImpl : public Infer
  22. {
  23. public:
  24. ModelType model_type_;
  25. std::vector<std::string> class_names_;
  26. // for sahi crop image
  27. std::shared_ptr<slice::SliceImage> slice_;
  28. std::shared_ptr<TensorRT::Engine> trt_;
  29. std::string engine_file_;
  30. tensor::Memory<int> box_count_;
  31. tensor::Memory<float> affine_matrix_;
  32. tensor::Memory<float> invert_affine_matrix_;
  33. tensor::Memory<float> mask_affine_matrix_;
  34. tensor::Memory<float> input_buffer_, bbox_predict_, segment_predict_, output_boxarray_;
  35. std::vector<std::shared_ptr<tensor::Memory<float>>> box_segment_cache_;
  36. int network_input_width_, network_input_height_;
  37. affine::Norm normalize_;
  38. std::vector<int> bbox_head_dims_;
  39. std::vector<int> segment_head_dims_;
  40. bool isdynamic_model_ = false;
  41. bool has_segment_ = false;
  42. float confidence_threshold_;
  43. float nms_threshold_;
  44. int num_classes_ = 0;
  45. int device_id_ = 0;
  46. YoloModelImpl() = default;
  47. virtual ~YoloModelImpl() = default;
  48. void adjust_memory(int batch_size);
  49. void preprocess(int ibatch, void *stream = nullptr);
  50. void cal_affine_matrix(affine::LetterBoxMatrix &affine, void *stream = nullptr);
  51. std::shared_ptr<data::InstanceSegmentMap> decode_segment(int imemory, float* pbox);
  52. bool load(const std::string &engine_file, ModelType model_type, const std::vector<std::string>& names, float confidence_threshold, float nms_threshold);
  53. virtual Result forward(const tensor::Image &image, int slice_width, int slice_height, float overlap_width_ratio, float overlap_height_ratio, void *stream = nullptr);
  54. virtual Result forward(const tensor::Image &image, void *stream = nullptr);
  55. virtual Result forwards(void *stream = nullptr);
  56. };
  57. Infer *loadraw(const std::string &engine_file, ModelType model_type, const std::vector<std::string>& names, float confidence_threshold,
  58. float nms_threshold);
  59. std::shared_ptr<Infer> load_yolo(const std::string &engine_file, ModelType model_type, const std::vector<std::string>& names, int gpu_id, float confidence_threshold, float nms_threshold);
  60. } // namespace yolo
  61. #endif // YOLO_HPP__