1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #28752 from abhishek-gola:net_profiling

Added net profiling support #28752

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Abhishek Gola
2026-05-14 17:17:38 +05:30
committed by GitHub
parent 851e0196b4
commit 873a4635c6
12 changed files with 361 additions and 2 deletions
+5
View File
@@ -5,6 +5,7 @@
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "common.hpp"
@@ -91,6 +92,8 @@ static bool readStringList( const string& filename, vector<string>& l )
int main(int argc, char** argv)
{
utils::logging::setLogLevel(utils::logging::LOG_LEVEL_INFO);
CommandLineParser parser(argc, argv, keys);
if (!parser.has("@alias") || parser.has("help"))
@@ -150,6 +153,7 @@ int main(int argc, char** argv)
Net net = readNetFromONNX(model, engine);
net.setPreferableBackend(getBackendID(backend));
net.setPreferableTarget(getTargetID(target));
net.setProfilingMode(DNN_PROFILE_SUMMARY);
//! [Read and initialize network]
// Create a window
@@ -229,6 +233,7 @@ int main(int argc, char** argv)
timeRecorder.start();
prob = net.forward();
timeRecorder.stop();
net.printPerfProfile();
//! [Make forward pass]
//! [Get a class with a highest score]
+4
View File
@@ -72,6 +72,7 @@ def main(func_args=None):
help()
exit(1)
cv.utils.logging.setLogLevel(cv.utils.logging.LOG_LEVEL_INFO)
args.model = findModel(args.model, args.sha1)
args.labels = findFile(args.labels)
@@ -88,6 +89,8 @@ def main(func_args=None):
net = cv.dnn.readNetFromONNX(args.model, engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
if hasattr(cv.dnn, 'DNN_PROFILE_SUMMARY'):
net.setProfilingMode(cv.dnn.DNN_PROFILE_SUMMARY)
winName = 'Deep learning image classification in OpenCV'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
@@ -138,6 +141,7 @@ def main(func_args=None):
t0 = cv.getTickCount()
out = net.forward()
t = (cv.getTickCount() - t0) / cv.getTickFrequency()
net.printPerfProfile()
(h, w, _) = frame.shape
roi_rows = min(300, h)
+6
View File
@@ -6,6 +6,7 @@
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <mutex>
#include <thread>
@@ -161,6 +162,8 @@ private:
int main(int argc, char** argv)
{
utils::logging::setLogLevel(utils::logging::LOG_LEVEL_INFO);
CommandLineParser parser(argc, argv, keys);
string zooFile = parser.get<String>("zoo");
@@ -225,6 +228,7 @@ int main(int argc, char** argv)
int backend = getBackendID(parser.get<String>("backend"));
net.setPreferableBackend(backend);
net.setPreferableTarget(getTargetID(parser.get<String>("target")));
net.setProfilingMode(DNN_PROFILE_SUMMARY);
//![read_net]
// Create a window
@@ -302,6 +306,7 @@ int main(int argc, char** argv)
//![forward]
vector<Mat> outs;
net.forward(outs, net.getUnconnectedOutLayersNames());
net.printPerfProfile();
predictionsQueue.push(outs);
//![forward]
}
@@ -372,6 +377,7 @@ int main(int argc, char** argv)
tickMeter.start();
net.forward(outs, net.getUnconnectedOutLayersNames());
tickMeter.stop();
net.printPerfProfile();
classIds.clear();
confidences.clear();
+5
View File
@@ -71,6 +71,7 @@ if args.alias is None or hasattr(args, 'help'):
help()
exit(1)
cv.utils.logging.setLogLevel(cv.utils.logging.LOG_LEVEL_INFO)
args.model = findModel(args.model, args.sha1)
if args.config is not None:
args.config = findModel(args.config, args.config_sha1)
@@ -104,6 +105,8 @@ if args.backend != "default" or args.target != "cpu":
net = cv.dnn.readNet(args.model, args.config, "", engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
if hasattr(cv.dnn, 'DNN_PROFILE_SUMMARY'):
net.setProfilingMode(cv.dnn.DNN_PROFILE_SUMMARY)
outNames = net.getUnconnectedOutLayersNames()
confThreshold = args.thr
@@ -340,6 +343,7 @@ def processingThreadBody():
futureOutputs.append(net.forwardAsync())
else:
outs = net.forward(outNames)
net.printPerfProfile()
predictionsQueue.put(copy.deepcopy(outs))
while futureOutputs and futureOutputs[0].wait_for(0):
@@ -408,6 +412,7 @@ else:
net.setInput(blob)
outs = net.forward(outNames)
net.printPerfProfile()
boxes, classIds, confidences, indices = postprocess(frame, outs)
drawPred(classIds, confidences, boxes, indices, (stdSize*max(frame.shape[:2]))/stdImgSize, (stdWeight*max(frame.shape[:2]))//stdImgSize)
+7 -1
View File
@@ -5,6 +5,7 @@
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "common.hpp"
@@ -139,6 +140,8 @@ static void showLegend(FontFace fontFace)
int main(int argc, char **argv)
{
utils::logging::setLogLevel(utils::logging::LOG_LEVEL_INFO);
CommandLineParser parser(argc, argv, keys);
const string modelName = parser.get<String>("@alias");
@@ -218,7 +221,8 @@ int main(int argc, char **argv)
Net net = readNetFromONNX(model, engine);
net.setPreferableBackend(getBackendID(backend));
net.setPreferableTarget(getTargetID(target));
//! [Read and initialize network]
net.setProfilingMode(DNN_PROFILE_SUMMARY);
//! [Read and initialize network]
// Create a window
static const string kWinName = "Deep learning semantic segmentation in OpenCV";
namedWindow(kWinName, WINDOW_AUTOSIZE);
@@ -263,6 +267,7 @@ int main(int argc, char **argv)
{
vector<Mat> output;
net.forward(output, net.getUnconnectedOutLayersNames());
net.printPerfProfile();
Mat pred = output[0].reshape(1, output[0].size[2]);
pred.convertTo(pred, CV_8U, 255.0);
@@ -284,6 +289,7 @@ int main(int argc, char **argv)
{
//! [Make forward pass]
Mat score = net.forward();
net.printPerfProfile();
//! [Make forward pass]
Mat segm;
colorizeSegmentation(score, segm);
+6 -1
View File
@@ -73,6 +73,7 @@ def main(func_args=None):
help()
exit(1)
cv.utils.logging.setLogLevel(cv.utils.logging.LOG_LEVEL_INFO)
args.model = findModel(args.model, args.sha1)
if args.labels is not None:
args.labels = findFile(args.labels)
@@ -105,6 +106,8 @@ def main(func_args=None):
net = cv.dnn.readNetFromONNX(args.model, engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
if hasattr(cv.dnn, 'DNN_PROFILE_SUMMARY'):
net.setProfilingMode(cv.dnn.DNN_PROFILE_SUMMARY)
winName = 'Deep learning semantic segmentation in OpenCV'
cv.namedWindow(winName, cv.WINDOW_AUTOSIZE)
@@ -138,6 +141,7 @@ def main(func_args=None):
t0 = cv.getTickCount()
if args.alias == 'u2netp':
output = net.forward(net.getUnconnectedOutLayersNames())
net.printPerfProfile()
pred = output[0][0, 0, :, :]
mask = (pred * 255).astype(np.uint8)
mask = cv.resize(mask, (frame.shape[1], frame.shape[0]), interpolation=cv.INTER_AREA)
@@ -149,6 +153,7 @@ def main(func_args=None):
frame = cv.addWeighted(frame, 0.25, foreground_overlay, 0.75, 0)
else:
score = net.forward()
net.printPerfProfile()
numClasses = score.shape[1]
height = score.shape[2]
@@ -176,4 +181,4 @@ def main(func_args=None):
cv.imshow(winName, frame)
if __name__ == "__main__":
main()
main()