OpenCV::dnn & profiling

1. readNetFrom*()

1.1 readNetFromCaffe()

Reads a network model stored in Caffe framework's format.

retval = cv.dnn.readNetFromCaffe( prototxt[, caffeModel] )

prototxt
path to the .prototxt file with text description of the network architecture

caffeModel
path to the .caffefmodel file with learned network

Returns
Net object
[src]

src

readNetFromCaffe()

CaffeImporter()

populateNet()

// opencv-3.4.1/modules/dnn/src/caffe/caffe_importer.cpp

Net readNetFromCaffe(const String &prototxt, const String &caffeModel /*= String()*/)
{
    CaffeImporter caffeImporter(prototxt.c_str(), caffeModel.c_str());
    Net net;
    caffeImporter.populateNet(net);
    return net;
}

CaffeImporter(const char *pototxt, const char *caffeModel)
{
    CV_TRACE_FUNCTION();

    ReadNetParamsFromTextFileOrDie(pototxt, &net);

    if (caffeModel && caffeModel[0])
        ReadNetParamsFromBinaryFileOrDie(caffeModel, &netBinary);
}

void populateNet(Net dstNet)
{
    CV_TRACE_FUNCTION();

    int layersSize = net.layer_size();
    layerCounter.clear();
    addedBlobs.clear();
    addedBlobs.reserve(layersSize + 1);

    //setup input layer names
    std::vector<String> netInputs(net.input_size());
    {
        for (int inNum = 0; inNum < net.input_size(); inNum++)
        {
            addedBlobs.push_back(BlobNote(net.input(inNum), 0, inNum));
            netInputs[inNum] = net.input(inNum);
        }
    }

    for (int li = 0; li < layersSize; li++)
    {
        const caffe::LayerParameter &layer = net.layer(li);
        String name = layer.name();
        String type = layer.type();
        LayerParams layerParams;

        extractLayerParams(layer, layerParams);
        extractBinaryLayerParms(layer, layerParams);

        int repetitions = layerCounter[name]++;
        if (repetitions)
            name += String("_") + toString(repetitions);

        if (type == "Input")
        {
            for (int outNum = 0; outNum < layer.top_size(); outNum++)
            {
                addOutput(layer, 0, outNum);
                addedBlobs.back().outNum = netInputs.size();
                netInputs.push_back(addedBlobs.back().name);
            }
            continue;
        }

        int id = dstNet.addLayer(name, type, layerParams);

        for (int inNum = 0; inNum < layer.bottom_size(); inNum++)
            addInput(layer.bottom(inNum), id, inNum, dstNet);

        for (int outNum = 0; outNum < layer.top_size(); outNum++)
            addOutput(layer, id, outNum);
    }
    dstNet.setInputsNames(netInputs);

    addedBlobs.clear();
}

1.2 readNetFromDarknet()

Reads a network model stored in Darknet model files.

retval = cv.dnn.readNetFromDarknet( cfgFile[, darknetModel] )

cfgFile
path to the .cfg file with text description of the network architecture

darknetModel
path to the .weights file with learned network

Returns
Net object

Network object that ready to do forward, throw an exception in failure cases

1.3 readNetFromTensorflow()

Reads a network model stored in TensorFlow framework's format.

retval = cv.dnn.readNetFromTensorflow( model[, config] )

model
path to the .pb file with binary protobuf description of the network architecture

config
path to the .pbtxt file that contains text graph definition in protobuf format. Resulting Net object is built by text graph using weights from a binary one that let us make it more flexible

Returns
Net object

src

readNetFromTensorflow()

TFImporter()

populateNet()

// opencv-3.4.1/modules/dnn/src/tensorflow/tf_importer.cpp

Net readNetFromTensorflow(const String &model, const String &config)
{
    TFImporter importer(model.c_str(), config.c_str());
    Net net;
    importer.populateNet(net);
    return net;
}

TFImporter::TFImporter(const char *model, const char *config)
{
    if (model && model[0])
        ReadTFNetParamsFromBinaryFileOrDie(model, &netBin);
    if (config && config[0])
        ReadTFNetParamsFromTextFileOrDie(config, &netTxt);
}

void TFImporter::populateNet(Net dstNet)
{
}

1.4 *Importer是否有基类?

2. blobFromImage()

Creates 4-dimensional blob from image. Optionally resizes and crops image from center, subtract mean values, scales values by scalefactor, swap Blue and Red channels.

retval = cv.dnn.blobFromImage( image[, scalefactor[, size[, mean[, swapRB[, crop]]]]] )

images
input image (with 1-, 3- or 4-channels)

scalefactor
multiplier for image values

size
spatial size for output image

mean
scalar with mean values which are subtracted from channels. Values are intended to be in (mean-R, mean-G, mean-B) order if image has BGR ordering and swapRB is true

swapRB
flag which indicates that swap first and last channels in 3-channel image is necessary

crop
flag which indicates whether image will be cropped after resize or not

if crop is true, input image is resized so one side after resize is equal to corresponding dimension in size and another one is equal or larger. Then, crop from the center is performed. If crop is false, direct resize without cropping and preserving aspect ratio is performed

Returns
4-dimansional Mat [src] with NCHW dimensions order

N : batch [深度学习中的Batch_Size]

C : in_channels

H : in_height

W : in_width

3. forward()

Runs forward pass to compute output of layer with name outputName

retval = cv.dnn_Net.forward( [, outputName] )

outputName
name for layer which output is needed to get

Returns
blob for first output of specified layer

for example:

[ [ [ [0. 5. 0.9643989 0.02526344 0.4001894 0.3808335 0.99173486][0. 15. 0.9997172 0.07266828 0.13153285 0.8781574 1.0013719 ] ] ] ]

0 : 意义不明

5 : "bottle" & 15 : "person"

["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]

0.9643989 & 0.9997172 : confidence

0.02526344 0.4001894 0.3808335 0.99173486 & 0.07266828 0.13153285 0.8781574 1.0013719 : relative positon(RP)

box(startX, startY, endX, endY) = RP * [WIDTH, HEIGHT, WIDTH, HEIGHT]

src

forward()

setUpNet()

getLayerData()

forwardToLayer()

getBlob()

forwardLayer()

struct LayerData

// opencv-3.4.1/modules/dnn/src/dnn.cpp

Mat Net::forward(const String& outputName)
{
    CV_TRACE_FUNCTION();

    String layerName = outputName;

    if (layerName.empty())
        layerName = getLayerNames().back();

    impl->setUpNet();
    impl->forwardToLayer(impl->getLayerData(layerName));

    return impl->getBlob(layerName);
}

void setUpNet(const std::vector<LayerPin>& blobsToKeep_ = std::vector<LayerPin>())
{
    CV_TRACE_FUNCTION();

    if (!netWasAllocated || this->blobsToKeep != blobsToKeep_)
    {
        clear();

        allocateLayers(blobsToKeep_);
        computeNetOutputLayers();
        initBackend();

        if (!netWasAllocated )
        {
#ifdef HAVE_HALIDE
            if (preferableBackend == DNN_BACKEND_HALIDE)
                compileHalide();
#else
            CV_Assert(preferableBackend != DNN_BACKEND_HALIDE);
#endif
        }

        netWasAllocated = true;
        this->blobsToKeep = blobsToKeep_;
    }
}

void forwardToLayer(LayerData &ld, bool clearFlags = true)
{
    CV_TRACE_FUNCTION();

    if (clearFlags)
    {
        MapIdToLayerData::iterator it;
        for (it = layers.begin(); it != layers.end(); it++)
            it->second.flag = 0;
    }

    //already was forwarded
    if (ld.flag)
        return;

    //forward parents
    MapIdToLayerData::iterator it;
    for (it = layers.begin(); it != layers.end() && (it->second.id < ld.id); ++it)
    {
        LayerData &ld = it->second;
        if (ld.flag)
            continue;
        forwardLayer(ld);
    }

    //forward itself
    forwardLayer(ld);
}

4. CV_TRACE_FUNCTION()

A tracing macro in
build-in tracing framework,
which relies on ITT API, an Intel®-provided profiling framework.

Intel® VTune™ Amplifier
correlates OpenCV activity with metrics for CPU and GPU,
captures most Intel APIs beyond OpenCV, for example OpenCL.

Intel® SEAPI
translator of itt_notify calls into several OS specific and third party tracing formats.

OpenCV-bundled trace analyzer tool

tracing macro list

  • CV_TRACE_FUNCTION() - the macro is already inserted into many OpenCV functions. If you want to trace calls of your own function, put it in the beginning of the function body.

  • CV_TRACE_FUNCTION_SKIP_NESTED()

  • CV_TRACE_REGION("myregion") - C++ scoped region trace.

  • CV_TRACE_REGION_NEXT("next_step")

  • CV_TRACE_ARG(arg)

  • CV_TRACE_ARG_VALUE(width_id, "width", size.width())

猜你喜欢

转载自www.cnblogs.com/humz/p/9146078.html