47
47
#include < iostream>
48
48
#include < sstream>
49
49
#include < iterator>
50
+ #include < numeric>
50
51
#include < opencv2/dnn/shape_utils.hpp>
51
52
#include < opencv2/imgproc.hpp>
52
53
@@ -633,7 +634,7 @@ struct Net::Impl
633
634
inpl.layerInstance = netInputLayer;
634
635
layerNameToId.insert (std::make_pair (inpl.name , inpl.id ));
635
636
636
- lastLayerId = 1 ;
637
+ lastLayerId = 0 ;
637
638
netWasAllocated = false ;
638
639
fusion = true ;
639
640
preferableBackend = DNN_BACKEND_DEFAULT;
@@ -656,6 +657,7 @@ struct Net::Impl
656
657
657
658
bool netWasAllocated;
658
659
bool fusion;
660
+ std::vector<int64> layersTimings;
659
661
660
662
void compileHalide ()
661
663
{
@@ -716,6 +718,8 @@ struct Net::Impl
716
718
it = layers.find (0 );
717
719
CV_Assert (it != layers.end ());
718
720
it->second .skipFlags [DNN_BACKEND_DEFAULT] = true ;
721
+
722
+ layersTimings.clear ();
719
723
}
720
724
721
725
void setUpNet (const std::vector<LayerPin>& blobsToKeep_ = std::vector<LayerPin>())
@@ -1269,6 +1273,8 @@ struct Net::Impl
1269
1273
allocateLayer (lid, layersShapes);
1270
1274
}
1271
1275
1276
+ layersTimings.resize (lastLayerId + 1 , 0 );
1277
+
1272
1278
fuseLayers (blobsToKeep_);
1273
1279
}
1274
1280
@@ -1278,11 +1284,16 @@ struct Net::Impl
1278
1284
1279
1285
Ptr<Layer> layer = ld.layerInstance ;
1280
1286
1287
+ TickMeter tm;
1288
+ tm.start ();
1289
+
1281
1290
if (preferableBackend == DNN_BACKEND_DEFAULT ||
1282
1291
!layer->supportBackend (preferableBackend))
1283
1292
{
1284
1293
if ( !ld.skipFlags [DNN_BACKEND_DEFAULT] )
1285
1294
layer->forward (ld.inputBlobs , ld.outputBlobs , ld.internals );
1295
+ else
1296
+ tm.reset ();
1286
1297
}
1287
1298
else if (!ld.skipFlags [preferableBackend])
1288
1299
{
@@ -1299,6 +1310,9 @@ struct Net::Impl
1299
1310
}
1300
1311
}
1301
1312
1313
+ tm.stop ();
1314
+ layersTimings[ld.id ] = tm.getTimeTicks ();
1315
+
1302
1316
ld.flag = 1 ;
1303
1317
}
1304
1318
@@ -1717,45 +1731,39 @@ std::vector<int> Net::getUnconnectedOutLayers() const
1717
1731
}
1718
1732
1719
1733
void Net::getLayersShapes (const ShapesVec& netInputShapes,
1720
- std::vector<int >* layersIds,
1721
- std::vector<ShapesVec>* inLayersShapes,
1722
- std::vector<ShapesVec>* outLayersShapes) const
1734
+ std::vector<int >& layersIds,
1735
+ std::vector<ShapesVec>& inLayersShapes,
1736
+ std::vector<ShapesVec>& outLayersShapes) const
1723
1737
{
1724
- if ((layersIds || inLayersShapes || outLayersShapes) == false )
1725
- return ;
1726
-
1727
- if (layersIds) layersIds->clear ();
1728
- if (inLayersShapes) inLayersShapes->clear ();
1729
- if (outLayersShapes) outLayersShapes->clear ();
1738
+ layersIds.clear ();
1739
+ inLayersShapes.clear ();
1740
+ outLayersShapes.clear ();
1730
1741
1731
1742
Impl::LayersShapesMap inOutShapes;
1732
1743
impl->getLayersShapes (netInputShapes, inOutShapes);
1733
1744
1734
1745
for (Impl::LayersShapesMap::const_iterator it = inOutShapes.begin ();
1735
1746
it != inOutShapes.end (); it++)
1736
1747
{
1737
- if (layersIds)
1738
- layersIds->push_back (it->first );
1739
- if (inLayersShapes)
1740
- inLayersShapes->push_back (it->second .in );
1741
- if (outLayersShapes)
1742
- outLayersShapes->push_back (it->second .out );
1748
+ layersIds.push_back (it->first );
1749
+ inLayersShapes.push_back (it->second .in );
1750
+ outLayersShapes.push_back (it->second .out );
1743
1751
}
1744
1752
}
1745
1753
1746
1754
void Net::getLayersShapes (const MatShape& netInputShape,
1747
- std::vector<int >* layerIds,
1748
- std::vector<ShapesVec>* inLayersShapes,
1749
- std::vector<ShapesVec>* outLayersShapes) const
1755
+ std::vector<int >& layerIds,
1756
+ std::vector<ShapesVec>& inLayersShapes,
1757
+ std::vector<ShapesVec>& outLayersShapes) const
1750
1758
{
1751
1759
getLayersShapes (ShapesVec (1 , netInputShape),
1752
1760
layerIds, inLayersShapes, outLayersShapes);
1753
1761
}
1754
1762
1755
1763
void Net::getLayerShapes (const MatShape& netInputShape,
1756
1764
const int layerId,
1757
- ShapesVec* inLayerShapes,
1758
- ShapesVec* outLayerShapes) const
1765
+ ShapesVec& inLayerShapes,
1766
+ ShapesVec& outLayerShapes) const
1759
1767
{
1760
1768
getLayerShapes (ShapesVec (1 , netInputShape),
1761
1769
layerId, inLayerShapes, outLayerShapes);
@@ -1764,15 +1772,13 @@ void Net::getLayerShapes(const MatShape& netInputShape,
1764
1772
1765
1773
void Net::getLayerShapes (const ShapesVec& netInputShapes,
1766
1774
const int layerId,
1767
- ShapesVec* inLayerShapes,
1768
- ShapesVec* outLayerShapes) const
1775
+ ShapesVec& inLayerShapes,
1776
+ ShapesVec& outLayerShapes) const
1769
1777
{
1770
1778
LayerShapes shapes;
1771
1779
impl->getLayerShapes (netInputShapes, layerId, shapes);
1772
- if (inLayerShapes)
1773
- *inLayerShapes = shapes.in ;
1774
- if (outLayerShapes)
1775
- *outLayerShapes = shapes.out ;
1780
+ inLayerShapes = shapes.in ;
1781
+ outLayerShapes = shapes.out ;
1776
1782
}
1777
1783
1778
1784
int64 Net::getFLOPS (const std::vector<MatShape>& netInputShapes) const
@@ -1782,7 +1788,7 @@ int64 Net::getFLOPS(const std::vector<MatShape>& netInputShapes) const
1782
1788
int64 flops = 0 ;
1783
1789
std::vector<int > ids;
1784
1790
std::vector<std::vector<MatShape> > inShapes, outShapes;
1785
- getLayersShapes (netInputShapes, & ids, & inShapes, & outShapes);
1791
+ getLayersShapes (netInputShapes, ids, inShapes, outShapes);
1786
1792
CV_Assert (inShapes.size () == outShapes.size ());
1787
1793
CV_Assert (inShapes.size () == ids.size ());
1788
1794
@@ -1867,8 +1873,8 @@ void Net::getMemoryConsumption(const int layerId,
1867
1873
weights += weightsBlob.total ()*weightsBlob.elemSize ();
1868
1874
}
1869
1875
1870
- std::vector<MatShape> outLayerShapes;
1871
- getLayerShapes (netInputShapes, layerId, 0 , & outLayerShapes);
1876
+ ShapesVec inLayerShapes, outLayerShapes;
1877
+ getLayerShapes (netInputShapes, layerId, inLayerShapes, outLayerShapes);
1872
1878
for (int i = 0 ; i < outLayerShapes.size (); i++)
1873
1879
{
1874
1880
blobs += total (outLayerShapes[i]) * sizeof (float );
@@ -1917,9 +1923,9 @@ void Net::getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
1917
1923
weights.clear ();
1918
1924
blobs.clear ();
1919
1925
1920
- std::vector<std::vector<MatShape> > outLayerShapes;
1926
+ std::vector<std::vector<MatShape> > inLayerShapes, outLayerShapes;
1921
1927
1922
- getLayersShapes (netInputShapes, & layerIds, 0 , & outLayerShapes);
1928
+ getLayersShapes (netInputShapes, layerIds, inLayerShapes, outLayerShapes);
1923
1929
1924
1930
for (int i = 0 ; i < layerIds.size (); i++)
1925
1931
{
@@ -1968,6 +1974,13 @@ void Net::setHalideScheduler(const String& scheduler)
1968
1974
impl->halideConfigFile = scheduler;
1969
1975
}
1970
1976
1977
+ int64 Net::getPerfProfile (std::vector<double >& timings)
1978
+ {
1979
+ timings = std::vector<double >(impl->layersTimings .begin () + 1 , impl->layersTimings .end ());
1980
+ int64 total = std::accumulate (timings.begin (), timings.end (), 0 );
1981
+ return total;
1982
+ }
1983
+
1971
1984
// ////////////////////////////////////////////////////////////////////////
1972
1985
1973
1986
Importer::~Importer () {}
0 commit comments