JeVois  1.22
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",
50 "", ParamCateg);
51
52 //! Parameter \relates jevois::dnn::PostProcessorClassify
53 JEVOIS_DECLARE_PARAMETER(top, unsigned int, "Max number of top-scoring predictions that score above "
54 "threshold to report",
55 5, ParamCateg);
56
57 //! Parameter \relates jevois::dnn::PostProcessorDetect
58 JEVOIS_DECLARE_PARAMETER(maxnbox, unsigned int, "Max number of top-scoring boxes to report (for YOLO flavors, "
59 "this is the max for each scale), or 0 for no limit",
60 500, ParamCateg);
61
62 //! Parameter \relates jevois::dnn::PostProcessorDetect
63 JEVOIS_DECLARE_PARAMETER(namedonly, bool, "When true, only report (on display and to serial ports) results "
64 "for which we have a class name defined in the class file. This can allow one "
65 "to skip some of the results by simply commenting out their name in the class file "
66 "(make sure your class file then contains class-ID numbers for each named class).",
67 false, ParamCateg);
68
69 //! Parameter \relates jevois::dnn::PostProcessorClassify
70 JEVOIS_DECLARE_PARAMETER(cthresh, float, "Classification threshold (in percent confidence) above which "
71 "predictions will be reported",
72 20.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
73
74 //! Parameter \relates jevois::dnn::PostProcessorDetect
75 JEVOIS_DECLARE_PARAMETER(dthresh, float, "Detection box threshold (in percent confidence) above which "
76 "predictions will be reported. Not all networks use a separate box threshold, "
77 "many only use one threshold confidence threshold (cthresh parameter). The YOLO "
78 "family is an example that uses both box and classification confidences",
79 15.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
80
81 //! Parameter \relates jevois::dnn::PostProcessorPose
82 JEVOIS_DECLARE_PARAMETER(jthresh, float, "Joint confidence threshold for pose detection",
83 50.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
84
85 //! Parameter \relates jevois::dnn::PostProcessorClassify
86 JEVOIS_DECLARE_PARAMETER(softmax, bool, "Apply a softmax to classification outputs",
87 false, ParamCateg);
88
89 //! Parameter \relates jevois::dnn::PostProcessorClassify
90 JEVOIS_DECLARE_PARAMETER(scorescale, float, "Scaling factors applied to recognition scores. Mainly "
91 "for debugging if your scores seem too high or too low. If too high, usually "
92 "that means that you should turn on parameter softmax instead.",
93 1.0F, ParamCateg);
94
95 //! Enum \relates jevois::dnn::PostProcessorDetect
96 JEVOIS_DEFINE_ENUM_CLASS(DetectType, (FasterRCNN) (YOLO) (SSD) (TPUSSD) (RAWYOLO) (YOLOv10) (YOLOv10pp)
97 (YOLOX) (YOLOv8) (YOLOv8t) (YOLOv8seg) (YOLOv8segt) );
98
99 //! Parameter \relates jevois::dnn::PostProcessorDetect
100 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttype, DetectType, "Type of detection output format",
101 DetectType::YOLO, DetectType_Values, ParamCateg);
102
103 //! Parameter \relates jevois::dnn::PostProcessorDetect
104 JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent",
105 45.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
106
107 //! Parameter \relates jevois::dnn::PostProcessorDetect
108 JEVOIS_DECLARE_PARAMETER(nmsperclass, bool, "When true, apply non-maximum suppression (NMS) separately for each "
109 "object class, rather than globally. This will allow overlapping boxes as long as "
110 "they belong to different classes",
111 false, ParamCateg);
112
113 //! Parameter \relates jevois::dnn::PostProcessorDetect
114 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(anchors, std::string, "For YOLO-type detection models with raw outputs, "
115 "list of anchors. Should be formatted as: w1, h1, w2, h2, ... ; ww1, hh1, ww2, hh2, "
116 "... ; ... where individual entries for a given YOLO layer are separated by commas, "
117 "and successive YOLO layers (from large to small, e.g., first the anchors for 52x52, "
118 "then for 26x26, then for 13x13) are separated by semicolons. Leave empty "
119 "for other models.",
120 "", ParamCateg);
121
122 //! Parameter \relates jevois::dnn::PostProcessorDetect
123 JEVOIS_DECLARE_PARAMETER(masksmooth, bool, "When true, extract smoother instance segmentation masks, "
124 "which may be marginally slower.",
125 true, ParamCateg);
126
127 //! Parameter \relates jevois::dnn::PostProcessorDetect
128 JEVOIS_DECLARE_PARAMETER(alpha, unsigned char, "Alpha channel value for drawn results",
129 64, ParamCateg);
130
131 //! Enum \relates jevois::dnn::PostProcessorDetectOBB
132 JEVOIS_DEFINE_ENUM_CLASS(DetectTypeOBB, (YOLOv8) (YOLOv8t) );
133
134 //! Parameter \relates jevois::dnn::PostProcessorDetectOBB
135 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttypeobb, DetectTypeOBB, "Type of oriented bounding box detection "
136 "output format",
137 DetectTypeOBB::YOLOv8, DetectTypeOBB_Values, ParamCateg);
138
139 //! Parameter \relates jevois::dnn::PostProcessorClassify
140 JEVOIS_DECLARE_PARAMETER(boxclamp, bool, "Clamp detection boxes to fit within the input image. Good to avoid "
141 "having boxes that extend beyond the display and to be able to see object labels "
142 "even on boxes that start at negative coordinates; but will artificially reduce "
143 "the size of detections that extend beyond the display",
144 true, ParamCateg);
145
146 //! Parameter \relates jevois::dnn::PostProcessorSegment
147 JEVOIS_DECLARE_PARAMETER(bgid, unsigned char, "Class ID for the background, will show as fully transparent in "
148 "semantic segmentation overlays",
149 0, ParamCateg);
150
151 //! Enum \relates jevois::dnn::PostProcessorSegment
152 JEVOIS_DEFINE_ENUM_CLASS(SegType, (ClassesHWC) (ClassesCHW) (ArgMax) );
153
154 //! Parameter \relates jevois::dnn::PostProcessorSegment
155 JEVOIS_DECLARE_PARAMETER(segtype, SegType, "Type of segmentation network output. ClassesHWC: output is 1xHxWxC "
156 "for C classes and we get one score per class; we will show "
157 "the top scoring class for each pixel (e.g., UNet-MobileNet on TPU). ClassesCHW: "
158 "output is 1xCxHxW and the rest is as for ClassesHWC (e.g., DeepLabV3 OpenCV). ArgMax: "
159 "output is HxW, 1xHxW, or 1xHxWx1 and contains the class ID for each pixel "
160 "(e.g., DeepLabV3 on TPU).",
161 SegType::ClassesHWC, SegType_Values, ParamCateg);
162
163 //! Parameter \relates jevois::dnn::PostProcessorPython
164 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pypost, std::string, "Path below " JEVOIS_SHARE_PATH "/ of the python "
165 "post-processor file. Name of class defined in the file must match "
166 "the file name without the trailing '.py'",
167 "", ParamCateg);
168
169 //! Parameter \relates jevois::dnn::PostProcessorDetectYOLO
170 JEVOIS_DECLARE_PARAMETER(scalexy, float, "If 0, use old-style YOLO boxes (YOLOv2/v3/v4); otherwise, this is "
171 "the scale_xy factor for new-style YOLO coordinates (YOLOv5/v7; value is usually 2.0 "
172 "but check the yolo layer in the model's .cfg file)",
173 0.0F, ParamCateg);
174
175 //! Parameter \relates jevois::dnn::PostProcessorDetect
176 JEVOIS_DECLARE_PARAMETER(sigmoid, bool, "Apply sigmoid to raw YOLO outputs, use when the last conv layers "
177 "just before yolo/detection/region layers have linear activation (most "
178 "YOLOv2/v3/v4 models, but not YOLOv5/v7 which have logistic activation on their "
179 "last conv)",
180 true, ParamCateg);
181
182 //! Enum \relates jevois::dnn::PostProcessorPose
183 JEVOIS_DEFINE_ENUM_CLASS(PoseType, (YOLOv8HAILO) (YOLOv8) (YOLOv8t) );
184
185 //! Parameter \relates jevois::dnn::PostProcessorPose
186 JEVOIS_DECLARE_PARAMETER(posetype, PoseType, "Type of pose output format",
187 PoseType::YOLOv8HAILO, PoseType_Values, ParamCateg);
188
189 //! Parameter \relates jevois::dnn::PostProcessorPose
190 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(skeleton, std::string, "YAML file name for definition of pose skeleton. "
191 "Defines number of joints and links, their names, and their "
192 "colors for drawing. If file name is relative, it will be with respect "
193 "to " JEVOIS_SHARE_PATH,
194 "dnn/skeletons/Coco17.yml", ParamCateg);
195
196 //! Parameter \relates jevois::dnn::PostProcessorClassify
197 JEVOIS_DECLARE_PARAMETER(serialreport, bool, "Send classification or detection results to serial port",
198 true, ParamCateg);
199 }
200
201 //! Post-Processor for neural network pipeline
202 /*! This is the last step in a deep neural network processing Pipeline. \ingroup dnn */
204 {
205 public:
206
207 //! Inherited constructor ok
209
210 //! Destructor
211 virtual ~PostProcessor();
212
213 //! Freeze/unfreeze parameters that users should not change while running
214 virtual void freeze(bool doit) = 0;
215
216 //! Process outputs
217 virtual void process(std::vector<cv::Mat> const & outs, PreProcessor * preproc) = 0;
218
219 //! Report what happened in last process() to console/output video/GUI
220 virtual void report(jevois::StdModule * mod, jevois::RawImage * outimg = nullptr,
221 jevois::OptGUIhelper * helper = nullptr, bool overlay = true, bool idle = false) = 0;
222 };
223
224 } // namespace dnn
225} // 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_WITH_CALLBACK(classes, std::string, "Path to text file with names of object classes", "", 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(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_DEFINE_ENUM_CLASS(DetectType,(FasterRCNN)(YOLO)(SSD)(TPUSSD)(RAWYOLO)(YOLOv10)(YOLOv10pp)(YOLOX)(YOLOv8)(YOLOv8t)(YOLOv8seg)(YOLOv8segt))
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.
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:716
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)