JeVois  1.21
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>
24
25namespace jevois
26{
27 class StdModule;
28 class RawImage;
29
30 namespace dnn
31 {
32 class PreProcessor;
33
34 namespace postprocessor
35 {
36 // We define all parameters for all derived classes here to avoid duplicate definitions. Different derived classes
37 // will use different subsets of all available parameters:
38 static jevois::ParameterCategory const ParamCateg("DNN Post-Processing Options");
39
40 //! Parameter \relates jevois::dnn::PostProcessorClassify
41 JEVOIS_DECLARE_PARAMETER(classoffset, int, "Offset added to model output when looking up class name. Useful if "
42 "your model uses a background class but your class file does not (use -1), or if your "
43 "model does not use a background class but your class file has one (use 1). If unsure, "
44 "use 0 and check whether reported class names are off.",
45 0, ParamCateg);
46
47 //! Parameter \relates jevois::dnn::PostProcessorClassify
48 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(classes, std::string, "Path to text file with names of object classes",
49 "", ParamCateg);
50
51 //! Parameter \relates jevois::dnn::PostProcessorClassify
52 JEVOIS_DECLARE_PARAMETER(top, unsigned int, "Max number of top-scoring predictions that score above "
53 "threshold to report",
54 5, ParamCateg);
55
56 //! Parameter \relates jevois::dnn::PostProcessorDetect
57 JEVOIS_DECLARE_PARAMETER(maxnbox, unsigned int, "Max number of top-scoring boxes to report (for YOLO flavors, "
58 "this is the max for each scale)",
59 500, ParamCateg);
60
61 //! Parameter \relates jevois::dnn::PostProcessorClassify
62 JEVOIS_DECLARE_PARAMETER(cthresh, float, "Classification threshold (in percent confidence) above which "
63 "predictions will be reported",
64 20.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
65
66 //! Parameter \relates jevois::dnn::PostProcessorDetect
67 JEVOIS_DECLARE_PARAMETER(dthresh, float, "Detection box threshold (in percent confidence) above which "
68 "predictions will be reported. Not all networks use a separate box threshold, "
69 "many only use one threshold confidence threshold (cthresh parameter). The YOLO "
70 "family is an example that uses both box and classification confidences",
71 15.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
72
73 //! Parameter \relates jevois::dnn::PostProcessorClassify
74 JEVOIS_DECLARE_PARAMETER(softmax, bool, "Apply a softmax to classification outputs",
75 false, ParamCateg);
76
77 //! Parameter \relates jevois::dnn::PostProcessorClassify
78 JEVOIS_DECLARE_PARAMETER(scorescale, float, "Scaling factors applied to recognition scores. Mainly "
79 "for debugging if your scores seem too high or too low. If too high, usually "
80 "that means that you should turn on parameter softmax instead.",
81 1.0F, ParamCateg);
82
83 //! Enum \relates jevois::dnn::PostProcessorDetect
84 JEVOIS_DEFINE_ENUM_CLASS(DetectType, (FasterRCNN) (YOLO) (SSD) (TPUSSD) (RAWYOLO) (YOLOv10) (YOLOv10pp) );
85
86 //! Parameter \relates jevois::dnn::PostProcessorDetect
87 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(detecttype, DetectType, "Type of detection output format",
88 DetectType::YOLO, DetectType_Values, ParamCateg);
89
90 //! Parameter \relates jevois::dnn::PostProcessorDetect
91 JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent",
92 45.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
93
94 //! Parameter \relates jevois::dnn::PostProcessorDetect
95 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(anchors, std::string, "For YOLO-type detection models with raw outputs, "
96 "list of anchors. Should be formatted as: w1, h1, w2, h2, ... ; ww1, hh1, ww2, hh2, "
97 "... ; ... where individual entries for a given YOLO layer are separated by commas, "
98 "and successive YOLO layers (from large to small, e.g., first the anchors for 52x52, "
99 "then for 26x26, then for 13x13) are separated by semicolons. Leave empty "
100 "for other models.",
101 "", ParamCateg);
102
103 //! Parameter \relates jevois::dnn::PostProcessorDetect
104 JEVOIS_DECLARE_PARAMETER(alpha, unsigned char, "Alpha channel value for drawn results",
105 64, ParamCateg);
106
107 //! Parameter \relates jevois::dnn::PostProcessorSegment
108 JEVOIS_DECLARE_PARAMETER(bgid, unsigned char, "Class ID for the background, will show as fully transparent in "
109 "semantic segmentation overlays",
110 0, ParamCateg);
111
112 //! Enum \relates jevois::dnn::PostProcessorSegment
113 JEVOIS_DEFINE_ENUM_CLASS(SegType, (ClassesHWC) (ClassesCHW) (ArgMax) );
114
115 //! Parameter \relates jevois::dnn::PostProcessorSegment
116 JEVOIS_DECLARE_PARAMETER(segtype, SegType, "Type of segmentation network output. ClassesHWC: output is 1xHxWxC "
117 "for C classes and we get one score per class; we will show "
118 "the top scoring class for each pixel (e.g., UNet-MobileNet on TPU). ClassesCHW: "
119 "output is 1xCxHxW and the rest is as for ClassesHWC (e.g., DeepLabV3 OpenCV). ArgMax: "
120 "output is HxW, 1xHxW, or 1xHxWx1 and contains the class ID for each pixel "
121 "(e.g., DeepLabV3 on TPU).",
122 SegType::ClassesHWC, SegType_Values, ParamCateg);
123
124 //! Parameter \relates jevois::dnn::PostProcessorPython
125 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pypost, std::string, "Path below " JEVOIS_SHARE_PATH "/ of the python "
126 "post-processor file. Name of class defined in the file must match "
127 "the file name without the trailing '.py'",
128 "", ParamCateg);
129
130 //! Parameter \relates jevois::dnn::PostProcessorDetectYOLO
131 JEVOIS_DECLARE_PARAMETER(scalexy, float, "If 0, use old-style YOLO boxes (YOLOv2/v3/v4); otherwise, this is "
132 "the scale_xy factor for new-style YOLO coordinates (YOLOv5/v7; value is usually 2.0 "
133 "but check the yolo layer in the model's .cfg file)",
134 0.0F, ParamCateg);
135
136 //! Parameter \relates jevois::dnn::PostProcessorDetectYOLO
137 JEVOIS_DECLARE_PARAMETER(sigmoid, bool, "Apply sigmoid to raw YOLO outputs, use when the last conv layers "
138 "just before yolo/detection/region layers have linear activation (most "
139 "YOLOv2/v3/v4 models, but not YOLOv5/v7 which have logistic activation on their "
140 "last conv)",
141 true, ParamCateg);
142 }
143
144 //! Post-Processor for neural network pipeline
145 /*! This is the last step in a deep neural network processing Pipeline. \ingroup dnn */
147 {
148 public:
149
150 //! Inherited constructor ok
152
153 //! Destructor
154 virtual ~PostProcessor();
155
156 //! Freeze/unfreeze parameters that users should not change while running
157 virtual void freeze(bool doit) = 0;
158
159 //! Process outputs
160 virtual void process(std::vector<cv::Mat> const & outs, PreProcessor * preproc) = 0;
161
162 //! Report what happened in last process() to console/output video/GUI
163 virtual void report(jevois::StdModule * mod, jevois::RawImage * outimg = nullptr,
164 jevois::OptGUIhelper * helper = nullptr, bool overlay = true, bool idle = false) = 0;
165 };
166
167 } // namespace dnn
168} // 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(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(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_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(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_DEFINE_ENUM_CLASS(DetectType,(FasterRCNN)(YOLO)(SSD)(TPUSSD)(RAWYOLO)(YOLOv10)(YOLOv10pp))
Enum.
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(maxnbox, unsigned int, "Max number of top-scoring boxes to report (for YOLO flavors, " "this is the max for each scale)", 500, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(alpha, unsigned char, "Alpha channel value for drawn results", 64, 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(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(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:668
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2
A category to which multiple ParameterDef definitions can belong.