Skip to content

Commit aea25e7

Browse files
committed
Merge pull request opencv#9676 from jrobble:fix_caffe_swaprb
2 parents 89b6e68 + c67ad49 commit aea25e7

File tree

10 files changed

+47
-51
lines changed

10 files changed

+47
-51
lines changed

modules/dnn/test/test_caffe_importer.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ TEST(Reproducibility_AlexNet, Accuracy)
8080
Mat sample = imread(_tf("grace_hopper_227.png"));
8181
ASSERT_TRUE(!sample.empty());
8282

83-
Size inputSize(227, 227);
84-
85-
if (sample.size() != inputSize)
86-
resize(sample, sample, inputSize);
87-
88-
net.setInput(blobFromImage(sample), "data");
83+
net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data");
8984
Mat out = net.forward("prob");
9085
Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy"));
9186
normAssert(ref, out);
@@ -105,17 +100,17 @@ TEST(Reproducibility_FCN, Accuracy)
105100
Mat sample = imread(_tf("street.png"));
106101
ASSERT_TRUE(!sample.empty());
107102

108-
Size inputSize(500, 500);
109-
if (sample.size() != inputSize)
110-
resize(sample, sample, inputSize);
111-
112103
std::vector<int> layerIds;
113104
std::vector<size_t> weights, blobs;
114105
net.getMemoryConsumption(shape(1,3,227,227), layerIds, weights, blobs);
115106

116-
net.setInput(blobFromImage(sample), "data");
107+
net.setInput(blobFromImage(sample, 1.0f, Size(500, 500), Scalar(), false), "data");
117108
Mat out = net.forward("score");
118-
Mat ref = blobFromNPY(_tf("caffe_fcn8s_prob.npy"));
109+
110+
Mat refData = imread(_tf("caffe_fcn8s_prob.png"), IMREAD_ANYDEPTH);
111+
int shape[] = {1, 21, 500, 500};
112+
Mat ref(4, shape, CV_32FC1, refData.data);
113+
119114
normAssert(ref, out);
120115
}
121116
#endif
@@ -136,10 +131,7 @@ TEST(Reproducibility_SSD, Accuracy)
136131
if (sample.channels() == 4)
137132
cvtColor(sample, sample, COLOR_BGRA2BGR);
138133

139-
sample.convertTo(sample, CV_32F);
140-
resize(sample, sample, Size(300, 300));
141-
142-
Mat in_blob = blobFromImage(sample);
134+
Mat in_blob = blobFromImage(sample, 1.0f, Size(300, 300), Scalar(), false);
143135
net.setInput(in_blob, "data");
144136
Mat out = net.forward("detection_out");
145137

@@ -152,7 +144,7 @@ TEST(Reproducibility_ResNet50, Accuracy)
152144
Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),
153145
findDataFile("dnn/ResNet-50-model.caffemodel", false));
154146

155-
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(224,224));
147+
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false);
156148
ASSERT_TRUE(!input.empty());
157149

158150
net.setInput(input);
@@ -167,7 +159,7 @@ TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
167159
Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
168160
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
169161

170-
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1, Size(227,227));
162+
Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false);
171163
ASSERT_TRUE(!input.empty());
172164

173165
net.setInput(input);
@@ -180,7 +172,7 @@ TEST(Reproducibility_SqueezeNet_v1_1, Accuracy)
180172
TEST(Reproducibility_AlexNet_fp16, Accuracy)
181173
{
182174
const float l1 = 1e-5;
183-
const float lInf = 2e-4;
175+
const float lInf = 3e-3;
184176

185177
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
186178
const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
@@ -190,7 +182,7 @@ TEST(Reproducibility_AlexNet_fp16, Accuracy)
190182

191183
Mat sample = imread(findDataFile("dnn/grace_hopper_227.png", false));
192184

193-
net.setInput(blobFromImage(sample, 1, Size(227, 227)));
185+
net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false));
194186
Mat out = net.forward();
195187
Mat ref = blobFromNPY(findDataFile("dnn/caffe_alexnet_prob.npy", false));
196188
normAssert(ref, out, "", l1, lInf);
@@ -212,7 +204,7 @@ TEST(Reproducibility_GoogLeNet_fp16, Accuracy)
212204
inpMats.push_back( imread(_tf("googlenet_1.png")) );
213205
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
214206

215-
net.setInput(blobFromImages(inpMats), "data");
207+
net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
216208
Mat out = net.forward("prob");
217209

218210
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));

modules/dnn/test/test_googlenet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ TEST(Reproducibility_GoogLeNet, Accuracy)
6666
inpMats.push_back( imread(_tf("googlenet_1.png")) );
6767
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
6868

69-
net.setInput(blobFromImages(inpMats), "data");
69+
net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
7070
Mat out = net.forward("prob");
7171

7272
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
@@ -84,7 +84,7 @@ TEST(IntermediateBlobs_GoogLeNet, Accuracy)
8484
blobsNames.push_back("inception_4c/1x1");
8585
blobsNames.push_back("inception_4c/relu_1x1");
8686
std::vector<Mat> outs;
87-
Mat in = blobFromImage(imread(_tf("googlenet_0.png")));
87+
Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);
8888
net.setInput(in, "data");
8989
net.forward(outs, blobsNames);
9090
CV_Assert(outs.size() == blobsNames.size());
@@ -109,7 +109,7 @@ TEST(SeveralCalls_GoogLeNet, Accuracy)
109109
inpMats.push_back( imread(_tf("googlenet_1.png")) );
110110
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
111111

112-
net.setInput(blobFromImages(inpMats), "data");
112+
net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
113113
Mat out = net.forward();
114114

115115
Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
@@ -118,7 +118,7 @@ TEST(SeveralCalls_GoogLeNet, Accuracy)
118118
std::vector<String> blobsNames;
119119
blobsNames.push_back("conv1/7x7_s2");
120120
std::vector<Mat> outs;
121-
Mat in = blobFromImage(inpMats[0]);
121+
Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);
122122
net.setInput(in, "data");
123123
net.forward(outs, blobsNames);
124124
CV_Assert(outs.size() == blobsNames.size());

samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
8686
// Forward image through network.
8787
Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,
8888
new Size(IN_WIDTH, IN_HEIGHT),
89-
new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), true);
89+
new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false);
9090
net.setInput(blob);
9191
Mat detections = net.forward();
9292

samples/dnn/caffe_googlenet.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,26 @@ int main(int argc, char **argv)
9191
String modelBin = "bvlc_googlenet.caffemodel";
9292
String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
9393

94-
//! [Read and initialize network]
95-
Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
96-
//! [Read and initialize network]
97-
98-
//! [Check that network was read successfully]
99-
if (net.empty())
100-
{
101-
std::cerr << "Can't load network by using the following files: " << std::endl;
102-
std::cerr << "prototxt: " << modelTxt << std::endl;
103-
std::cerr << "caffemodel: " << modelBin << std::endl;
104-
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
105-
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
106-
exit(-1);
94+
Net net;
95+
try {
96+
//! [Read and initialize network]
97+
net = dnn::readNetFromCaffe(modelTxt, modelBin);
98+
//! [Read and initialize network]
99+
}
100+
catch (cv::Exception& e) {
101+
std::cerr << "Exception: " << e.what() << std::endl;
102+
//! [Check that network was read successfully]
103+
if (net.empty())
104+
{
105+
std::cerr << "Can't load network by using the following files: " << std::endl;
106+
std::cerr << "prototxt: " << modelTxt << std::endl;
107+
std::cerr << "caffemodel: " << modelBin << std::endl;
108+
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
109+
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
110+
exit(-1);
111+
}
112+
//! [Check that network was read successfully]
107113
}
108-
//! [Check that network was read successfully]
109114

110115
//! [Prepare blob]
111116
Mat img = imread(imageFile);
@@ -115,9 +120,9 @@ int main(int argc, char **argv)
115120
exit(-1);
116121
}
117122

118-
//GoogLeNet accepts only 224x224 RGB-images
119-
Mat inputBlob = blobFromImage(img, 1, Size(224, 224),
120-
Scalar(104, 117, 123)); //Convert Mat to batch of images
123+
//GoogLeNet accepts only 224x224 BGR-images
124+
Mat inputBlob = blobFromImage(img, 1.0f, Size(224, 224),
125+
Scalar(104, 117, 123), false); //Convert Mat to batch of images
121126
//! [Prepare blob]
122127

123128
Mat prob;

samples/dnn/fcn_semsegm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ int main(int argc, char **argv)
113113
exit(-1);
114114
}
115115

116-
resize(img, img, Size(500, 500)); //FCN accepts 500x500 RGB-images
117-
Mat inputBlob = blobFromImage(img); //Convert Mat to batch of images
116+
resize(img, img, Size(500, 500)); //FCN accepts 500x500 BGR-images
117+
Mat inputBlob = blobFromImage(img, 1, Size(), Scalar(), false); //Convert Mat to batch of images
118118
//! [Prepare blob]
119119

120120
//! [Set input blob]

samples/dnn/googlenet_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get_class_list():
1111
with open('synset_words.txt', 'rt') as f:
1212
return [x[x.find(" ") + 1:] for x in f]
1313

14-
blob = dnn.blobFromImage(cv2.imread('space_shuttle.jpg'), 1, (224, 224), (104, 117, 123))
14+
blob = dnn.blobFromImage(cv2.imread('space_shuttle.jpg'), 1, (224, 224), (104, 117, 123), false)
1515
print("Input:", blob.shape, blob.dtype)
1616

1717
net = dnn.readNetFromCaffe('bvlc_googlenet.prototxt', 'bvlc_googlenet.caffemodel')

samples/dnn/mobilenet_ssd_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
while True:
4242
# Capture frame-by-frame
4343
ret, frame = cap.read()
44-
blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth, inHeight), meanVal)
44+
blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth, inHeight), meanVal, false)
4545
net.setInput(blob)
4646
detections = net.forward()
4747

samples/dnn/resnet_ssd_face_python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
cols = frame.shape[1]
2828
rows = frame.shape[0]
2929

30-
net.setInput(dnn.blobFromImage(cv.resize(frame, (inWidth, inHeight)),
31-
1.0, (inWidth, inHeight), (104., 177., 123.)))
30+
net.setInput(dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (104.0, 177.0, 123.0), false))
3231
detections = net.forward()
3332

3433
perf_stats = net.getPerfProfile()

samples/dnn/ssd_mobilenet_object_detection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int main(int argc, char** argv)
9797
//! [Prepare blob]
9898

9999
Mat inputBlob = blobFromImage(frame, inScaleFactor,
100-
Size(inWidth, inHeight), meanVal); //Convert Mat to batch of images
100+
Size(inWidth, inHeight), meanVal, false); //Convert Mat to batch of images
101101
//! [Prepare blob]
102102

103103
//! [Set input blob]

samples/dnn/ssd_object_detection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int main(int argc, char** argv)
8686
//! [Prepare blob]
8787
Mat preprocessedFrame = preprocess(frame);
8888

89-
Mat inputBlob = blobFromImage(preprocessedFrame); //Convert Mat to batch of images
89+
Mat inputBlob = blobFromImage(preprocessedFrame, 1.0f, Size(), Scalar(), false); //Convert Mat to batch of images
9090
//! [Prepare blob]
9191

9292
//! [Set input blob]

0 commit comments

Comments
 (0)