Skip to content

Commit 636d636

Browse files
committed
use OutputArrayOfArrays in net forward interface
It allows umat buffers used in net forward interface Signed-off-by: Li Peng <peng.li@intel.com>
1 parent 04edc8f commit 636d636

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
410410
* @param outputName name for layer which output is needed to get
411411
* @details If @p outputName is empty, runs forward pass for the whole network.
412412
*/
413-
CV_WRAP void forward(std::vector<Mat>& outputBlobs, const String& outputName = String());
413+
CV_WRAP void forward(OutputArrayOfArrays outputBlobs, const String& outputName = String());
414414

415415
/** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.
416416
* @param outputBlobs contains blobs for first outputs of specified layers.
417417
* @param outBlobNames names for layers which outputs are needed to get
418418
*/
419-
CV_WRAP void forward(std::vector<Mat>& outputBlobs,
419+
CV_WRAP void forward(OutputArrayOfArrays outputBlobs,
420420
const std::vector<String>& outBlobNames);
421421

422422
/** @brief Runs forward pass to compute outputs of layers listed in @p outBlobNames.

modules/dnn/src/dnn.cpp

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ struct Net::Impl
15451545
CV_Error(Error::StsOutOfRange, "Layer \"" + ld.name + "\" produce only " + toString(ld.outputBlobs.size()) +
15461546
" outputs, the #" + toString(pin.oid) + " was requsted");
15471547
}
1548-
if (preferableBackend != DNN_TARGET_CPU)
1548+
if (preferableBackend != DNN_BACKEND_DEFAULT)
15491549
{
15501550
// Transfer data to CPU if it's require.
15511551
ld.outputBlobsWrappers[pin.oid]->copyToHost();
@@ -1561,10 +1561,35 @@ struct Net::Impl
15611561
return ld.outputBlobs[pin.oid];
15621562
}
15631563

1564+
void getBlob(UMat& umat, const LayerPin& pin)
1565+
{
1566+
CV_TRACE_FUNCTION();
1567+
1568+
if (!pin.valid())
1569+
CV_Error(Error::StsObjectNotFound, "Requested blob not found");
1570+
1571+
LayerData &ld = layers[pin.lid];
1572+
if ((size_t)pin.oid >= ld.outputBlobs.size())
1573+
{
1574+
CV_Error(Error::StsOutOfRange, "Layer \"" + ld.name + "\" produce only " + toString(ld.outputBlobs.size()) +
1575+
" outputs, the #" + toString(pin.oid) + " was requsted");
1576+
}
1577+
1578+
if (ld.umat_outputBlobs.size() > 0 && !ld.umat_outputBlobs[pin.oid].empty())
1579+
umat = ld.umat_outputBlobs[pin.oid];
1580+
else
1581+
umat = UMat();
1582+
}
1583+
15641584
Mat getBlob(String outputName)
15651585
{
15661586
return getBlob(getPinByAlias(outputName));
15671587
}
1588+
1589+
void getBlob(UMat& umat, String outputName)
1590+
{
1591+
getBlob(umat, getPinByAlias(outputName));
1592+
}
15681593
};
15691594

15701595
Net::Net() : impl(new Net::Impl)
@@ -1642,7 +1667,7 @@ Mat Net::forward(const String& outputName)
16421667
return impl->getBlob(layerName);
16431668
}
16441669

1645-
void Net::forward(std::vector<Mat>& outputBlobs, const String& outputName)
1670+
void Net::forward(OutputArrayOfArrays outputBlobs, const String& outputName)
16461671
{
16471672
CV_TRACE_FUNCTION();
16481673

@@ -1658,24 +1683,48 @@ void Net::forward(std::vector<Mat>& outputBlobs, const String& outputName)
16581683
LayerPin pin = impl->getPinByAlias(layerName);
16591684
LayerData &ld = impl->layers[pin.lid];
16601685

1661-
if (ld.umat_outputBlobs.size() > 0)
1686+
if (outputBlobs.isUMat())
16621687
{
1663-
for (int i = 0; i < ld.umat_outputBlobs.size(); i++)
1664-
ld.umat_outputBlobs[i].copyTo(ld.outputBlobs[i]);
1688+
if (ld.umat_outputBlobs.size() > 0)
1689+
{
1690+
UMat umat;
1691+
impl->getBlob(umat, layerName);
1692+
outputBlobs.assign(umat);
1693+
}
1694+
}
1695+
else if (outputBlobs.isMat())
1696+
{
1697+
outputBlobs.assign(impl->getBlob(layerName));
1698+
}
1699+
else if (outputBlobs.isMatVector())
1700+
{
1701+
if (ld.umat_outputBlobs.size() > 0)
1702+
{
1703+
for (int i = 0; i < ld.umat_outputBlobs.size(); i++)
1704+
ld.umat_outputBlobs[i].copyTo(ld.outputBlobs[i]);
1705+
}
1706+
std::vector<Mat> & outputvec = *(std::vector<Mat> *)outputBlobs.getObj();
1707+
outputvec = ld.outputBlobs;
1708+
}
1709+
else if (outputBlobs.isUMatVector())
1710+
{
1711+
if (ld.umat_outputBlobs.size() > 0)
1712+
{
1713+
std::vector<UMat> & outputvec = *(std::vector<UMat> *)outputBlobs.getObj();
1714+
outputvec = ld.umat_outputBlobs;
1715+
}
16651716
}
1666-
1667-
outputBlobs = ld.outputBlobs;
16681717
}
16691718

1670-
void Net::forward(std::vector<Mat>& outputBlobs,
1719+
void Net::forward(OutputArrayOfArrays outputBlobs,
16711720
const std::vector<String>& outBlobNames)
16721721
{
16731722
CV_TRACE_FUNCTION();
16741723

16751724
std::vector<LayerPin> pins;
16761725
for (int i = 0; i < outBlobNames.size(); i++)
16771726
{
1678-
pins.push_back(impl->getPinByAlias(outBlobNames[i]));
1727+
pins.push_back(impl->getPinByAlias(outBlobNames[i]));
16791728
}
16801729

16811730
impl->setUpNet(pins);
@@ -1684,11 +1733,14 @@ void Net::forward(std::vector<Mat>& outputBlobs,
16841733

16851734
impl->forwardToLayer(impl->getLayerData(out.lid));
16861735

1687-
outputBlobs.clear();
1736+
std::vector<Mat> matvec;
16881737
for (int i = 0; i < pins.size(); i++)
16891738
{
1690-
outputBlobs.push_back(impl->getBlob(pins[i]));
1739+
matvec.push_back(impl->getBlob(pins[i]));
16911740
}
1741+
1742+
std::vector<Mat> & outputvec = *(std::vector<Mat> *)outputBlobs.getObj();
1743+
outputvec = matvec;
16921744
}
16931745

16941746
void Net::forward(std::vector<std::vector<Mat> >& outputBlobs,

0 commit comments

Comments
 (0)