Skip to content

Commit 7733654

Browse files
committed
Merge pull request opencv#9037 from arrybn:googlenet_test
2 parents cb0a61b + 7d11403 commit 7733654

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

modules/dnn/src/dnn.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ struct Net::Impl
694694
for (it = layers.begin(); it != layers.end(); it++)
695695
{
696696
if (it->second.id != 0) {
697+
it->second.inputBlobs.clear();
697698
it->second.outputBlobs.clear();
698699
it->second.internals.clear();
699700
}
@@ -1106,8 +1107,10 @@ struct Net::Impl
11061107
bnormData->skipFlags[DNN_BACKEND_DEFAULT] = true;
11071108
ld.outputBlobs = layers[lpNext.lid].outputBlobs;
11081109
if( bnormData->consumers.size() == 1 )
1110+
{
11091111
nextData = &layers[bnormData->consumers[0].lid];
1110-
lpNext = LayerPin(bnormData->consumers[0].lid, 0);
1112+
lpNext = LayerPin(bnormData->consumers[0].lid, 0);
1113+
}
11111114
}
11121115
}
11131116

@@ -1124,15 +1127,19 @@ struct Net::Impl
11241127
scaleData->skipFlags[DNN_BACKEND_DEFAULT] = true;
11251128
ld.outputBlobs = layers[lpNext.lid].outputBlobs;
11261129
if( scaleData->consumers.size() == 1 )
1130+
{
11271131
nextData = &layers[scaleData->consumers[0].lid];
1132+
lpNext = LayerPin(scaleData->consumers[0].lid, 0);
1133+
}
11281134
}
11291135
}
11301136

11311137
Ptr<ActivationLayer> nextActivLayer;
11321138
if( nextData )
11331139
nextActivLayer = nextData->layerInstance.dynamicCast<ActivationLayer>();
11341140

1135-
if( !nextActivLayer.empty() && currLayer->setActivation(nextActivLayer) )
1141+
if( !nextActivLayer.empty() && pinsToKeep.count(lpNext) == 0
1142+
&& currLayer->setActivation(nextActivLayer) )
11361143
{
11371144
printf_(("\tfused with %s\n", nextActivLayer->name.c_str()));
11381145
nextData->skipFlags[DNN_BACKEND_DEFAULT] = true;

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
198198
bool setActivation(const Ptr<ActivationLayer>& layer)
199199
{
200200
activ = layer;
201+
if (activ.empty())
202+
reluslope.clear();
201203
return !activ.empty();
202204
}
203205

modules/dnn/test/imagenet_cls_test_alexnet.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ def get_name(self):
146146
return 'DNN'
147147

148148
def get_output(self, input_blob):
149-
self.net.setBlob(self.in_blob_name, input_blob)
150-
self.net.forward()
151-
return self.net.getBlob(self.out_blob_name)
149+
self.net.setInput(input_blob, self.in_blob_name)
150+
return self.net.forward(self.out_blob_name)
152151

153152

154153
class ClsAccEvaluation:

modules/dnn/test/test_googlenet.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,10 @@ static std::string _tf(TString filename)
5656
return (getOpenCVExtraDir() + "/dnn/") + filename;
5757
}
5858

59-
static void launchGoogleNetTest()
59+
TEST(Reproducibility_GoogLeNet, Accuracy)
6060
{
61-
Net net;
62-
{
63-
const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
64-
const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
65-
Ptr<Importer> importer = createCaffeImporter(proto, model);
66-
ASSERT_TRUE(importer != NULL);
67-
importer->populateNet(net);
68-
}
61+
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
62+
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
6963

7064
std::vector<Mat> inpMats;
7165
inpMats.push_back( imread(_tf("googlenet_0.png")) );
@@ -77,14 +71,20 @@ static void launchGoogleNetTest()
7771

7872
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
7973
normAssert(out, ref);
74+
}
75+
76+
TEST(IntermediateBlobs_GoogLeNet, Accuracy)
77+
{
78+
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
79+
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
8080

8181
std::vector<String> blobsNames;
8282
blobsNames.push_back("conv1/7x7_s2");
8383
blobsNames.push_back("conv1/relu_7x7");
8484
blobsNames.push_back("inception_4c/1x1");
8585
blobsNames.push_back("inception_4c/relu_1x1");
8686
std::vector<Mat> outs;
87-
Mat in = blobFromImage(inpMats[0]);
87+
Mat in = blobFromImage(imread(_tf("googlenet_0.png")));
8888
net.setInput(in, "data");
8989
net.forward(outs, blobsNames);
9090
CV_Assert(outs.size() == blobsNames.size());
@@ -95,13 +95,37 @@ static void launchGoogleNetTest()
9595
std::replace( filename.begin(), filename.end(), '/', '#');
9696
Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
9797

98-
//normAssert(outs[i], ref, "", 1E-4, 1E-2);
98+
normAssert(outs[i], ref, "", 1E-4, 1E-2);
9999
}
100100
}
101101

102-
TEST(Reproducibility_GoogLeNet, Accuracy)
102+
TEST(SeveralCalls_GoogLeNet, Accuracy)
103103
{
104-
launchGoogleNetTest();
104+
Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt", false),
105+
findDataFile("dnn/bvlc_googlenet.caffemodel", false));
106+
107+
std::vector<Mat> inpMats;
108+
inpMats.push_back( imread(_tf("googlenet_0.png")) );
109+
inpMats.push_back( imread(_tf("googlenet_1.png")) );
110+
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
111+
112+
net.setInput(blobFromImages(inpMats), "data");
113+
Mat out = net.forward();
114+
115+
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
116+
normAssert(out, ref);
117+
118+
std::vector<String> blobsNames;
119+
blobsNames.push_back("conv1/7x7_s2");
120+
std::vector<Mat> outs;
121+
Mat in = blobFromImage(inpMats[0]);
122+
net.setInput(in, "data");
123+
net.forward(outs, blobsNames);
124+
CV_Assert(outs.size() == blobsNames.size());
125+
126+
ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
127+
128+
normAssert(outs[0], ref, "", 1E-4, 1E-2);
105129
}
106130

107131
}

samples/dnn/torch_enet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ int main(int argc, char **argv)
8585
}
8686

8787
//! [Make forward pass]
88+
tm.start();
8889
Mat result = net.forward(oBlob);
90+
tm.stop();
8991

9092
if (!resultFile.empty()) {
9193
CV_Assert(result.isContinuous());

0 commit comments

Comments
 (0)