add Net::getUnconnectedOutLayersNames method

This commit is contained in:
Dmitry Kurtaev 2018-09-25 18:10:45 +03:00
parent a610be6335
commit f8398d80bc
4 changed files with 21 additions and 20 deletions

View File

@ -535,6 +535,11 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
/** @brief Returns indexes of layers with unconnected outputs.
*/
CV_WRAP std::vector<int> getUnconnectedOutLayers() const;
/** @brief Returns names of layers with unconnected outputs.
*/
CV_WRAP std::vector<String> getUnconnectedOutLayersNames() const;
/** @brief Returns input and output shapes for all layers in loaded model;
* preliminary inferencing isn't necessary.
* @param netInputShapes shapes for all input blobs in net input layer.

View File

@ -2789,6 +2789,18 @@ std::vector<int> Net::getUnconnectedOutLayers() const
return layersIds;
}
std::vector<String> Net::getUnconnectedOutLayersNames() const
{
std::vector<int> ids = getUnconnectedOutLayers();
const size_t n = ids.size();
std::vector<String> names(n);
for (size_t i = 0; i < n; ++i)
{
names[i] = impl->layers[ids[i]].name;
}
return names;
}
void Net::getLayersShapes(const ShapesVec& netInputShapes,
std::vector<int>& layersIds,
std::vector<ShapesVec>& inLayersShapes,

View File

@ -86,6 +86,7 @@ int main(int argc, char** argv)
Net net = readNet(parser.get<String>("model"), parser.get<String>("config"), parser.get<String>("framework"));
net.setPreferableBackend(parser.get<int>("backend"));
net.setPreferableTarget(parser.get<int>("target"));
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
// Create a window
static const std::string kWinName = "Deep learning object detection in OpenCV";
@ -125,7 +126,7 @@ int main(int argc, char** argv)
net.setInput(imInfo, "im_info");
}
std::vector<Mat> outs;
net.forward(outs, getOutputsNames(net));
net.forward(outs, outNames);
postprocess(frame, outs, net);
@ -265,17 +266,3 @@ void callback(int pos, void*)
{
confThreshold = pos * 0.01f;
}
std::vector<String> getOutputsNames(const Net& net)
{
static std::vector<String> names;
if (names.empty())
{
std::vector<int> outLayers = net.getUnconnectedOutLayers();
std::vector<String> layersNames = net.getLayerNames();
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
return names;
}

View File

@ -78,14 +78,11 @@ if args.classes:
net = cv.dnn.readNet(args.model, args.config, args.framework)
net.setPreferableBackend(args.backend)
net.setPreferableTarget(args.target)
outNames = net.getUnconnectedOutLayersNames()
confThreshold = args.thr
nmsThreshold = args.nms
def getOutputsNames(net):
layersNames = net.getLayerNames()
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
def postprocess(frame, outs):
frameHeight = frame.shape[0]
frameWidth = frame.shape[1]
@ -213,7 +210,7 @@ while cv.waitKey(1) < 0:
if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN
frame = cv.resize(frame, (inpWidth, inpHeight))
net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info')
outs = net.forward(getOutputsNames(net))
outs = net.forward(outNames)
postprocess(frame, outs)