JeVois  1.23
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PostProcessor.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2021 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#pragma once
19
21#include <opencv2/core/core.hpp>
23#include <jevois/Types/Enum.H>
25
26namespace jevois
27{
28 class StdModule;
29 class RawImage;
30
31 namespace dnn
32 {
33 class PreProcessor;
34
35 namespace postprocessor
36 {
37 // We define all parameters for all derived classes here to avoid duplicate definitions. Different derived classes
38 // will use different subsets of all available parameters:
39 static jevois::ParameterCategory const ParamCateg("DNN Post-Processing Options");
40
41 //! Parameter \relates jevois::dnn::PostProcessorClassify
42 JEVOIS_DECLARE_PARAMETER(classoffset, int, "Offset added to model output when looking up class name. Useful if "
43 "your model uses a background class but your class file does not (use -1), or if your "
44 "model does not use a background class but your class file has one (use 1). If unsure, "
45 "use 0 and check whether reported class names are off.",
46 0, ParamCateg);
47
48 //! Parameter \relates jevois::dnn::PostProcessorClassify
49 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(classes, std::string, "Path to text file with names of object classes, or "
50 "comma-separated list of class names. If path is relative, that is to "
52 "", ParamCateg);
53
54 //! Parameter \relates jevois::dnn::PostProcessorClassify
55 JEVOIS_DECLARE_PARAMETER(top, unsigned int, "Max number of top-scoring predictions that score above "
56 "threshold to report",
57 5, ParamCateg);
58
59 //! Parameter \relates jevois::dnn::PostProcessorDetect
60 JEVOIS_DECLARE_PARAMETER(maxnbox, unsigned int, "Max number of top-scoring boxes to report (for YOLO flavors, "
61 "this is the max for each scale), or 0 for no limit",
62 500, ParamCateg);
63
64 //! Parameter \relates jevois::dnn::PostProcessorDetect
65 JEVOIS_DECLARE_PARAMETER(namedonly, bool, "When true, only report (on display and to serial ports) results "
66 "for which we have a class name defined in the class file. This can allow one "
67 "to skip some of the results by simply commenting out their name in the class file "
68 "(make sure your class file then contains class-ID numbers for each named class).",
69 false, ParamCateg);
70
71 //! Parameter \relates jevois::dnn::PostProcessorClassify
72 JEVOIS_DECLARE_PARAMETER(cthresh, float, "Classification threshold (in percent confidence) above which "
73 "predictions will be reported",
74 20.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
75
76 //! Parameter \relates jevois::dnn::PostProcessorDetect
77 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(perclassthresh, std::string, "Per-class thresholds (in percent "
78 "confidence) above which predictions will be reported, one value for "
79 "each class, separated by spaces and/or commas. This is used to override "
80 "'cthresh' when different thresholds are needed for different classes, "
81 "e.g., for YOLO-World. Only supported by YOLOv8 and YOLOv8t "
82 "post-processors. All values must be strictly >0",
83 "", ParamCateg);
84
85 //! Parameter \relates jevois::dnn::PostProcessorDetect
86 JEVOIS_DECLARE_PARAMETER(dthresh, float, "Detection box threshold (in percent confidence) above which "
87 "predictions will be reported. Not all networks use a separate box threshold, "
88 "many only use one threshold confidence threshold (cthresh parameter). The YOLO "
89 "family is an example that uses both box and classification confidences",
90 15.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
91
92 //! Parameter \relates jevois::dnn::PostProcessorPose
93 JEVOIS_DECLARE_PARAMETER(jthresh, float, "Joint confidence threshold for pose detection",
94 50.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
95
96 //! Parameter \relates jevois::dnn::PostProcessorClassify
97 JEVOIS_DECLARE_PARAMETER(softmax, bool, "Apply a softmax to classification outputs",
98 false, ParamCateg);
99
100 //! Parameter \relates jevois::dnn::PostProcessorClassify
101 JEVOIS_DECLARE_PARAMETER(scorescale, float, "Scaling factors applied to recognition scores. Mainly "
102 "for debugging if your scores seem too high or too low. If too high, usually "
103 "that means that you should turn on parameter softmax instead.",
104 1.0F, ParamCateg);
105
106 //! Enum \relates jevois::dnn::PostProcessorDetect
107 JEVOIS_DEFINE_ENUM_CLASS(DetectType, (FasterRCNN) (YOLO) (SSD) (TPUSSD) (RAWYOLO) (YOLOv10) (YOLOv10pp)
108 (YOLOX) (YOLOv8) (YOLOv8t) (YOLOv8seg) (YOLOv8segt) (YOLOjevois) (YOLOjevoist) );
109
110 //! Parameter \relates jevois::dnn::PostProcessorDetect
111 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttype, DetectType, "Type of detection output format",
112 DetectType::YOLO, DetectType_Values, ParamCateg);
113
114 //! Parameter \relates jevois::dnn::PostProcessorDetect
115 JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent",
116 45.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
117
118 //! Parameter \relates jevois::dnn::PostProcessorDetect
119 JEVOIS_DECLARE_PARAMETER(nmsperclass, bool, "When true, apply non-maximum suppression (NMS) separately for each "
120 "object class, rather than globally. This will allow overlapping boxes as long as "
121 "they belong to different classes",
122 false, ParamCateg);
123
124 //! Parameter \relates jevois::dnn::PostProcessorDetect
125 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(anchors, std::string, "For YOLO-type detection models with raw outputs, "
126 "list of anchors. Should be formatted as: w1, h1, w2, h2, ... ; ww1, hh1, ww2, hh2, "
127 "... ; ... where individual entries for a given YOLO layer are separated by commas, "
128 "and successive YOLO layers (from large to small, e.g., first the anchors for 52x52, "
129 "then for 26x26, then for 13x13) are separated by semicolons. Leave empty "
130 "for other models.",
131 "", ParamCateg);
132
133 //! Parameter \relates jevois::dnn::PostProcessorDetect
134 JEVOIS_DECLARE_PARAMETER(masksmooth, bool, "When true, extract smoother instance segmentation masks, "
135 "which may be marginally slower.",
136 true, ParamCateg);
137
138 //! Parameter \relates jevois::dnn::PostProcessorDetect
139 JEVOIS_DECLARE_PARAMETER(alpha, unsigned char, "Alpha channel value for drawn results",
140 64, ParamCateg);
141
142 //! Enum \relates jevois::dnn::PostProcessorDetectOBB
143 JEVOIS_DEFINE_ENUM_CLASS(DetectTypeOBB, (YOLOv8) (YOLOv8t) );
144
145 //! Parameter \relates jevois::dnn::PostProcessorDetectOBB
146 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttypeobb, DetectTypeOBB, "Type of oriented bounding box detection "
147 "output format",
148 DetectTypeOBB::YOLOv8, DetectTypeOBB_Values, ParamCateg);
149
150 //! Parameter \relates jevois::dnn::PostProcessorClassify
151 JEVOIS_DECLARE_PARAMETER(boxclamp, bool, "Clamp detection boxes to fit within the input image. Good to avoid "
152 "having boxes that extend beyond the display and to be able to see object labels "
153 "even on boxes that start at negative coordinates; but will artificially reduce "
154 "the size of detections that extend beyond the display",
155 true, ParamCateg);
156
157 //! Parameter \relates jevois::dnn::PostProcessorSegment
158 JEVOIS_DECLARE_PARAMETER(bgid, unsigned char, "Class ID for the background, will show as fully transparent in "
159 "semantic segmentation overlays",
160 0, ParamCateg);
161
162 //! Enum \relates jevois::dnn::PostProcessorSegment
163 JEVOIS_DEFINE_ENUM_CLASS(SegType, (ClassesHWC) (ClassesCHW) (ArgMax) );
164
165 //! Parameter \relates jevois::dnn::PostProcessorSegment
166 JEVOIS_DECLARE_PARAMETER(segtype, SegType, "Type of segmentation network output. ClassesHWC: output is 1xHxWxC "
167 "for C classes and we get one score per class; we will show "
168 "the top scoring class for each pixel (e.g., UNet-MobileNet on TPU). ClassesCHW: "
169 "output is 1xCxHxW and the rest is as for ClassesHWC (e.g., DeepLabV3 OpenCV). ArgMax: "
170 "output is HxW, 1xHxW, or 1xHxWx1 and contains the class ID for each pixel "
171 "(e.g., DeepLabV3 on TPU).",
172 SegType::ClassesHWC, SegType_Values, ParamCateg);
173
174 //! Parameter \relates jevois::dnn::PostProcessorPython
175 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pypost, std::string, "Path below " JEVOIS_SHARE_PATH "/ of the python "
176 "post-processor file. Name of class defined in the file must match "
177 "the file name without the trailing '.py'",
178 "", ParamCateg);
179
180 //! Parameter \relates jevois::dnn::PostProcessorDetectYOLO
181 JEVOIS_DECLARE_PARAMETER(scalexy, float, "If 0, use old-style YOLO boxes (YOLOv2/v3/v4); otherwise, this is "
182 "the scale_xy factor for new-style YOLO coordinates (YOLOv5/v7; value is usually 2.0 "
183 "but check the yolo layer in the model's .cfg file)",
184 0.0F, ParamCateg);
185
186 //! Parameter \relates jevois::dnn::PostProcessorDetect
187 JEVOIS_DECLARE_PARAMETER(sigmoid, bool, "Apply sigmoid to raw YOLO outputs, use when the last conv layers "
188 "just before yolo/detection/region layers have linear activation (most "
189 "YOLOv2/v3/v4 models, but not YOLOv5/v7 which have logistic activation on their "
190 "last conv)",
191 true, ParamCateg);
192
193 //! Enum \relates jevois::dnn::PostProcessorPose
194 JEVOIS_DEFINE_ENUM_CLASS(PoseType, (YOLOv8HAILO) (YOLOv8) (YOLOv8t) );
195
196 //! Parameter \relates jevois::dnn::PostProcessorPose
197 JEVOIS_DECLARE_PARAMETER(posetype, PoseType, "Type of pose output format",
198 PoseType::YOLOv8HAILO, PoseType_Values, ParamCateg);
199
200 //! Parameter \relates jevois::dnn::PostProcessorPose
201 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(skeleton, std::string, "YAML file name for definition of pose skeleton. "
202 "Defines number of joints and links, their names, and their "
203 "colors for drawing. If file name is relative, it will be with respect "
204 "to " JEVOIS_SHARE_PATH,
205 "dnn/skeletons/Coco17.yml", ParamCateg);
206
207 //! Parameter \relates jevois::dnn::PostProcessorClassify
208 JEVOIS_DECLARE_PARAMETER(serialreport, bool, "Send classification or detection results to serial port",
209 true, ParamCateg);
210 }
211
212 //! Post-Processor for neural network pipeline
213 /*! This is the last step in a deep neural network processing Pipeline. \ingroup dnn */
215 {
216 public:
217
218 //! Inherited constructor ok
220
221 //! Destructor
222 virtual ~PostProcessor();
223
224 //! Freeze/unfreeze parameters that users should not change while running
225 virtual void freeze(bool doit) = 0;
226
227 //! Process outputs
228 virtual void process(std::vector<cv::Mat> const & outs, PreProcessor * preproc) = 0;
229
230 //! Report what happened in last process() to console/output video/GUI
231 virtual void report(jevois::StdModule * mod, jevois::RawImage * outimg = nullptr,
232 jevois::OptGUIhelper * helper = nullptr, bool overlay = true, bool idle = false) = 0;
233 };
234
235 } // namespace dnn
236} // namespace jevois
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
A component of a model hierarchy.
Definition Component.H:182
Helper class to assist modules in creating graphical and GUI elements.
Definition GUIhelper.H:133
friend class Component
Allow Component and DynamicParameter to access our registry data, everyone else is locked out.
A generic range class.
Definition Range.H:81
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
Base class for a module that supports standardized serial messages.
Definition Module.H:234
JEVOIS_DECLARE_PARAMETER(classoffset, int, "Offset added to model output when looking up class name. Useful if " "your model uses a background class but your class file does not (use -1), or if your " "model does not use a background class but your class file has one (use 1). If unsure, " "use 0 and check whether reported class names are off.", 0, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(serialreport, bool, "Send classification or detection results to serial port", true, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(softmax, bool, "Apply a softmax to classification outputs", false, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(scorescale, float, "Scaling factors applied to recognition scores. Mainly " "for debugging if your scores seem too high or too low. If too high, usually " "that means that you should turn on parameter softmax instead.", 1.0F, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(boxclamp, bool, "Clamp detection boxes to fit within the input image. Good to avoid " "having boxes that extend beyond the display and to be able to see object labels " "even on boxes that start at negative coordinates but will artificially reduce " "the size of detections that extend beyond the display", true, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(classes, std::string, "Path to text file with names of object classes, or " "comma-separated list of class names. If path is relative, that is to " JEVOIS_SHARE_PATH, "", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(cthresh, float, "Classification threshold (in percent confidence) above which " "predictions will be reported", 20.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(top, unsigned int, "Max number of top-scoring predictions that score above " "threshold to report", 5, ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(DetectTypeOBB,(YOLOv8)(YOLOv8t))
Enum.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttypeobb, DetectTypeOBB, "Type of oriented bounding box detection " "output format", DetectTypeOBB::YOLOv8, DetectTypeOBB_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(scalexy, float, "If 0, use old-style YOLO boxes (YOLOv2/v3/v4) otherwise, this is " "the scale_xy factor for new-style YOLO coordinates (YOLOv5/v7 value is usually 2.0 " "but check the yolo layer in the model's .cfg file)", 0.0F, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(maxnbox, unsigned int, "Max number of top-scoring boxes to report (for YOLO flavors, " "this is the max for each scale), or 0 for no limit", 500, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(nmsperclass, bool, "When true, apply non-maximum suppression (NMS) separately for each " "object class, rather than globally. This will allow overlapping boxes as long as " "they belong to different classes", false, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(dthresh, float, "Detection box threshold (in percent confidence) above which " "predictions will be reported. Not all networks use a separate box threshold, " "many only use one threshold confidence threshold (cthresh parameter). The YOLO " "family is an example that uses both box and classification confidences", 15.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(perclassthresh, std::string, "Per-class thresholds (in percent " "confidence) above which predictions will be reported, one value for " "each class, separated by spaces and/or commas. This is used to override " "'cthresh' when different thresholds are needed for different classes, " "e.g., for YOLO-World. Only supported by YOLOv8 and YOLOv8t " "post-processors. All values must be strictly >0", "", ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(DetectType,(FasterRCNN)(YOLO)(SSD)(TPUSSD)(RAWYOLO)(YOLOv10)(YOLOv10pp)(YOLOX)(YOLOv8)(YOLOv8t)(YOLOv8seg)(YOLOv8segt)(YOLOjevois)(YOLOjevoist))
Enum.
JEVOIS_DECLARE_PARAMETER(sigmoid, bool, "Apply sigmoid to raw YOLO outputs, use when the last conv layers " "just before yolo/detection/region layers have linear activation (most " "YOLOv2/v3/v4 models, but not YOLOv5/v7 which have logistic activation on their " "last conv)", true, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(alpha, unsigned char, "Alpha channel value for drawn results", 64, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(masksmooth, bool, "When true, extract smoother instance segmentation masks, " "which may be marginally slower.", true, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(anchors, std::string, "For YOLO-type detection models with raw outputs, " "list of anchors. Should be formatted as: w1, h1, w2, h2, ... ww1, hh1, ww2, hh2, " "... ... where individual entries for a given YOLO layer are separated by commas, " "and successive YOLO layers (from large to small, e.g., first the anchors for 52x52, " "then for 26x26, then for 13x13) are separated by semicolons. Leave empty " "for other models.", "", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttype, DetectType, "Type of detection output format", DetectType::YOLO, DetectType_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(namedonly, bool, "When true, only report (on display and to serial ports) results " "for which we have a class name defined in the class file. This can allow one " "to skip some of the results by simply commenting out their name in the class file " "(make sure your class file then contains class-ID numbers for each named class).", false, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent", 45.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(skeleton, std::string, "YAML file name for definition of pose skeleton. " "Defines number of joints and links, their names, and their " "colors for drawing. If file name is relative, it will be with respect " "to " JEVOIS_SHARE_PATH, "dnn/skeletons/Coco17.yml", ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(PoseType,(YOLOv8HAILO)(YOLOv8)(YOLOv8t))
Enum.
JEVOIS_DECLARE_PARAMETER(posetype, PoseType, "Type of pose output format", PoseType::YOLOv8HAILO, PoseType_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(jthresh, float, "Joint confidence threshold for pose detection", 50.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pypost, std::string, "Path below " JEVOIS_SHARE_PATH "/ of the python " "post-processor file. Name of class defined in the file must match " "the file name without the trailing '.py'", "", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(bgid, unsigned char, "Class ID for the background, will show as fully transparent in " "semantic segmentation overlays", 0, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(segtype, SegType, "Type of segmentation network output. ClassesHWC: output is 1xHxWxC " "for C classes and we get one score per class we will show " "the top scoring class for each pixel (e.g., UNet-MobileNet on TPU). ClassesCHW: " "output is 1xCxHxW and the rest is as for ClassesHWC (e.g., DeepLabV3 OpenCV). ArgMax: " "output is HxW, 1xHxW, or 1xHxWx1 and contains the class ID for each pixel " "(e.g., DeepLabV3 on TPU).", SegType::ClassesHWC, SegType_Values, ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(SegType,(ClassesHWC)(ClassesCHW)(ArgMax))
Enum.
Post-Processor for neural network pipeline.
virtual void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc)=0
Process outputs.
virtual void freeze(bool doit)=0
Freeze/unfreeze parameters that users should not change while running.
virtual ~PostProcessor()
Destructor.
virtual void report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false)=0
Report what happened in last process() to console/output video/GUI.
Pre-Processor for neural network pipeline.
Helper class for runtime-configurable, quantized open-vocabulary object detection.
Definition YOLOjevois.H:61
size_t softmax(float const *input, size_t const n, size_t const stride, float const fac, float *output, bool maxonly)
Apply softmax to a float vector.
Definition Utils.C:733
float sigmoid(float x)
Compute sigmoid using fastexp.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2
A category to which multiple ParameterDef definitions can belong.
std::vector< Decodings > nms(std::vector< Decodings > &decodings, const float iou_thr, bool should_nms_cross_classes=false)