提交 f8398d80 编写于 作者: D Dmitry Kurtaev

add Net::getUnconnectedOutLayersNames method

上级 a610be63
...@@ -535,6 +535,11 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -535,6 +535,11 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
/** @brief Returns indexes of layers with unconnected outputs. /** @brief Returns indexes of layers with unconnected outputs.
*/ */
CV_WRAP std::vector<int> getUnconnectedOutLayers() const; 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; /** @brief Returns input and output shapes for all layers in loaded model;
* preliminary inferencing isn't necessary. * preliminary inferencing isn't necessary.
* @param netInputShapes shapes for all input blobs in net input layer. * @param netInputShapes shapes for all input blobs in net input layer.
......
...@@ -2789,6 +2789,18 @@ std::vector<int> Net::getUnconnectedOutLayers() const ...@@ -2789,6 +2789,18 @@ std::vector<int> Net::getUnconnectedOutLayers() const
return layersIds; 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, void Net::getLayersShapes(const ShapesVec& netInputShapes,
std::vector<int>& layersIds, std::vector<int>& layersIds,
std::vector<ShapesVec>& inLayersShapes, std::vector<ShapesVec>& inLayersShapes,
......
...@@ -86,6 +86,7 @@ int main(int argc, char** argv) ...@@ -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 net = readNet(parser.get<String>("model"), parser.get<String>("config"), parser.get<String>("framework"));
net.setPreferableBackend(parser.get<int>("backend")); net.setPreferableBackend(parser.get<int>("backend"));
net.setPreferableTarget(parser.get<int>("target")); net.setPreferableTarget(parser.get<int>("target"));
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
// Create a window // Create a window
static const std::string kWinName = "Deep learning object detection in OpenCV"; static const std::string kWinName = "Deep learning object detection in OpenCV";
...@@ -125,7 +126,7 @@ int main(int argc, char** argv) ...@@ -125,7 +126,7 @@ int main(int argc, char** argv)
net.setInput(imInfo, "im_info"); net.setInput(imInfo, "im_info");
} }
std::vector<Mat> outs; std::vector<Mat> outs;
net.forward(outs, getOutputsNames(net)); net.forward(outs, outNames);
postprocess(frame, outs, net); postprocess(frame, outs, net);
...@@ -265,17 +266,3 @@ void callback(int pos, void*) ...@@ -265,17 +266,3 @@ void callback(int pos, void*)
{ {
confThreshold = pos * 0.01f; 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;
}
...@@ -78,14 +78,11 @@ if args.classes: ...@@ -78,14 +78,11 @@ if args.classes:
net = cv.dnn.readNet(args.model, args.config, args.framework) net = cv.dnn.readNet(args.model, args.config, args.framework)
net.setPreferableBackend(args.backend) net.setPreferableBackend(args.backend)
net.setPreferableTarget(args.target) net.setPreferableTarget(args.target)
outNames = net.getUnconnectedOutLayersNames()
confThreshold = args.thr confThreshold = args.thr
nmsThreshold = args.nms nmsThreshold = args.nms
def getOutputsNames(net):
layersNames = net.getLayerNames()
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
def postprocess(frame, outs): def postprocess(frame, outs):
frameHeight = frame.shape[0] frameHeight = frame.shape[0]
frameWidth = frame.shape[1] frameWidth = frame.shape[1]
...@@ -213,7 +210,7 @@ while cv.waitKey(1) < 0: ...@@ -213,7 +210,7 @@ while cv.waitKey(1) < 0:
if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN if net.getLayer(0).outputNameToIndex('im_info') != -1: # Faster-RCNN or R-FCN
frame = cv.resize(frame, (inpWidth, inpHeight)) frame = cv.resize(frame, (inpWidth, inpHeight))
net.setInput(np.array([[inpHeight, inpWidth, 1.6]], dtype=np.float32), 'im_info') 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) postprocess(frame, outs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册