Skip to content

Commit 9c6c69a

Browse files
arrybnvpisarev
authored andcommitted
Fixed and added tests (#987)
1 parent ee79c1f commit 9c6c69a

16 files changed

+192
-108
lines changed

modules/dnn/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,20 @@ ocv_add_perf_tests()
4848
# ----------------------------------------------------------------------------
4949
# Download pre-trained models for complex testing on GoogLeNet and AlexNet
5050
# ----------------------------------------------------------------------------
51-
OCV_OPTION(${the_module}_DOWNLOAD_CAFFE_MODELS "Use GoogLeNet Caffe model for testing" OFF IF BUILD_TESTS AND DEFINED ENV{OPENCV_TEST_DATA_PATH})
52-
if(BUILD_TESTS AND DEFINED ENV{OPENCV_TEST_DATA_PATH} AND (DOWNLOAD_EXTERNAL_TEST_DATA OR ${the_module}_DOWNLOAD_CAFFE_MODELS))
51+
OCV_OPTION(${the_module}_DOWNLOAD_MODELS "Use GoogLeNet Caffe model for testing" OFF IF BUILD_TESTS AND DEFINED ENV{OPENCV_TEST_DATA_PATH})
52+
if(BUILD_TESTS AND DEFINED ENV{OPENCV_TEST_DATA_PATH} AND (DOWNLOAD_EXTERNAL_TEST_DATA OR ${the_module}_DOWNLOAD_MODELS))
5353
add_custom_command( TARGET opencv_test_${name} POST_BUILD
5454
COMMAND ${CMAKE_COMMAND} -Dmodel=GoogleNet -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_model.cmake)
55+
add_custom_command( TARGET opencv_test_${name} POST_BUILD
56+
COMMAND ${CMAKE_COMMAND} -Dmodel=Alexnet -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_model.cmake)
57+
add_custom_command( TARGET opencv_test_${name} POST_BUILD
58+
COMMAND ${CMAKE_COMMAND} -Dmodel=Inception -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_model.cmake)
59+
add_custom_command( TARGET opencv_test_${name} POST_BUILD
60+
COMMAND ${CMAKE_COMMAND} -Dmodel=Enet -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_model.cmake)
5561
add_definitions(-DENABLE_CAFFE_MODEL_TESTS=1)
62+
add_definitions(-DENABLE_CAFFE_ALEXNET_TEST=1)
63+
add_definitions(-DENABLE_TF_INCEPTION_TESTS=1)
64+
add_definitions(-DENABLE_TORCH_ENET_TESTS=1)
5665
endif()
5766

5867
# ----------------------------------------------------------------------------

modules/dnn/cmake/download_model.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ set(GG16_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/VGG_ILSVRC_16_layers.caffemodel")
88
set(voc-fcn32s_url "http://dl.caffe.berkeleyvision.org/fcn32s-heavy-pascal.caffemodel")
99
set(voc-fcn32s_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/fcn32s-heavy-pascal.caffemodel")
1010

11+
set(Alexnet_url "http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel")
12+
set(Alexnet_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/bvlc_alexnet.caffemodel")
13+
set(Alexnet_sha "9116a64c0fbe4459d18f4bb6b56d647b63920377")
14+
15+
set(Inception_url "https://github.com/petewarden/tf_ios_makefile_example/raw/master/data/tensorflow_inception_graph.pb")
16+
set(Inception_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/tensorflow_inception_graph.pb")
17+
18+
set(Enet_url "https://www.dropbox.com/sh/dywzk3gyb12hpe5/AABoUwqQGWvClUu27Z1EWeu9a/model-best.net?dl=0")
19+
set(Enet_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/Enet-model-best.net")
20+
21+
set(Fcn_url "http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel")
22+
set(Fcn_dst "$ENV{OPENCV_TEST_DATA_PATH}/dnn/fcn8s-heavy-pascal.caffemodel")
23+
1124
if(NOT model)
1225
set(model "GoogleNet")
1326
endif()

modules/dnn/samples/caffe_googlenet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ int main(int argc, char **argv)
115115
}
116116

117117
resize(img, img, Size(224, 224)); //GoogLeNet accepts only 224x224 RGB-images
118+
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
118119
dnn::Blob inputBlob = dnn::Blob::fromImages(img); //Convert Mat to dnn::Blob batch of images
119120
//! [Prepare blob]
120121

modules/dnn/src/caffe/caffe.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ message LayerParameter {
425425

426426
// The blobs containing the numeric parameters of the layer.
427427
repeated BlobProto blobs = 7;
428-
428+
429429
// Specifies on which bottoms the backpropagation should be skipped.
430430
// The size must be either 0 or equal to the number of bottoms.
431431
repeated bool propagate_down = 11;
@@ -571,7 +571,7 @@ message ContrastiveLossParameter {
571571
// Hadsell paper. New models should probably use this version.
572572
// legacy_version = true uses (margin - d^2). This is kept to support /
573573
// reproduce existing models and results
574-
optional bool legacy_version = 2 [default = false];
574+
optional bool legacy_version = 2 [default = false];
575575
}
576576

577577
message ConvolutionParameter {

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void BaseConvolutionLayerImpl::allocate(const std::vector<Blob*> &inputs, std::v
111111
if (!is1x1())
112112
{
113113
colRowBlob.create(colRowBlobShape, input.type(), allocFlags);
114+
colRowBlob.setTo(0);
114115
}
115116
}
116117

@@ -250,11 +251,11 @@ void ConvolutionLayerImpl::im2row(const Mat &srcImg, Mat &dstRow)
250251
if (srcImg.type() == CV_32F)
251252
im2row_CpuPBody<float>::run(srcImg.ptr<float>(), inpGroupCn, inpH, inpW, kernel.height,
252253
kernel.width, pad.height, pad.width, stride.height, stride.width,
253-
dilation.height, dilation.width, outW, outH, colMat.ptr<float>());
254+
dilation.height, dilation.width, outH, outW, colMat.ptr<float>());
254255
if (srcImg.type() == CV_64F)
255256
im2row_CpuPBody<double>::run(srcImg.ptr<double>(), inpGroupCn, inpH, inpW, kernel.height,
256257
kernel.width, pad.height, pad.width, stride.height, stride.width,
257-
dilation.height, dilation.width, outW, outH, colMat.ptr<double>());
258+
dilation.height, dilation.width, outH, outW, colMat.ptr<double>());
258259

259260
dstRow = colMat;
260261
}
@@ -268,11 +269,9 @@ void ConvolutionLayerImpl::im2row(const UMat &srcImg, UMat &dstCol)
268269

269270
void DeConvolutionLayerImpl::computeInpOutShape(const Blob &inpBlob)
270271
{
271-
BlobShape bs0 = blobs[0].shape();
272-
BlobShape bs1 = blobs[1].shape();
273-
CV_Assert(!bias || blobs[1].total() == (size_t)blobs[0].channels());
272+
CV_Assert(!bias || blobs[1].total() == (size_t)blobs[0].num());
274273

275-
numOutput = blobs[0].channels();
274+
numOutput = blobs[0].num();
276275

277276
inpH = inpBlob.rows();
278277
inpW = inpBlob.cols();
@@ -282,13 +281,13 @@ void DeConvolutionLayerImpl::computeInpOutShape(const Blob &inpBlob)
282281
outW = stride.width * (inpW - 1) + kernel.width - 2 * pad.width + adjustPad.width;
283282
outCn = numOutput;
284283

285-
group = inpCn / blobs[0].num();
284+
group = inpCn / blobs[0].channels();
286285
outGroupCn = outCn / group;
287286
inpGroupCn = inpCn / group;
288287
ksize = outGroupCn * kernel.height * kernel.width;
289288

290289
CV_Assert(inpCn % group == 0 && outCn % group == 0);
291-
CV_Assert(blobs[0].channels() == outCn && blobs[0].num() == inpCn / group);
290+
CV_Assert(blobs[0].num() == outCn && blobs[0].channels() == inpCn / group);
292291

293292
colRowBlobShape = BlobShape(ksize, inpH * inpW);
294293
}

modules/dnn/src/layers/lrn_layer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,23 @@ static XMat getPlane(XMat &m, int n, int cn)
106106
void LRNLayerImpl::channelNoramlization(Blob &src, Blob &dst)
107107
{
108108
if (!useOpenCL)
109-
channelNoramlization_<Mat>(src, dst);
109+
channelNormalization_<Mat>(src, dst);
110110
else
111111
{
112112
//channelNoramlization_ocl(src.getRefConst<UMat>(), dst.getRef<UMat>()); //consumes a lot of memory
113-
channelNoramlization_<UMat>(src, dst);
113+
channelNormalization_<UMat>(src, dst);
114114
}
115115
}
116116

117117
template<typename XMat>
118-
void LRNLayerImpl::channelNoramlization_(Blob &srcBlob, Blob &dstBlob)
118+
void LRNLayerImpl::channelNormalization_(Blob &srcBlob, Blob &dstBlob)
119119
{
120120
int num = srcBlob.num();
121121
int channels = srcBlob.channels();
122122
int ksize = (size - 1) / 2;
123123
int sizeNormFactor = normBySize ? size : 1;
124124

125-
XMat srcMat = srcBlob.getRefConst<XMat>();
125+
XMat srcMat = srcBlob.getRefConst<XMat>().clone();
126126
XMat dstMat = dstBlob.getRef<XMat>();
127127

128128
for (int n = 0; n < num; n++)
@@ -156,7 +156,7 @@ void LRNLayerImpl::channelNoramlization_(Blob &srcBlob, Blob &dstBlob)
156156
}
157157
}
158158

159-
bool LRNLayerImpl::channelNoramlization_ocl(const UMat &src, UMat &dst)
159+
bool LRNLayerImpl::channelNormalization_ocl(const UMat &src, UMat &dst)
160160
{
161161
#ifdef HAVE_OPENCL
162162
if (src.offset != 0 || dst.offset != 0) //TODO: add offset

modules/dnn/src/layers/lrn_layer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class LRNLayerImpl : public LRNLayer
5656

5757
void channelNoramlization(Blob &src, Blob &dst);
5858
template<typename XMat>
59-
void channelNoramlization_(Blob &src, Blob &dst);
60-
bool channelNoramlization_ocl(const UMat &src, UMat &dst);
59+
void channelNormalization_(Blob &src, Blob &dst);
60+
bool channelNormalization_ocl(const UMat &src, UMat &dst);
6161

6262
void spatialNormalization(Blob &src, Blob &dst);
6363
template<typename XMat>
@@ -67,8 +67,8 @@ class LRNLayerImpl : public LRNLayer
6767

6868
public:
6969

70-
LRNLayerImpl(int type = CHANNEL_NRM, int size = 5, double alpha = 1, double beta = 0.75, double bias = 1,
71-
bool normBySize = true);
70+
LRNLayerImpl(int type = CHANNEL_NRM, int size = 5, double alpha = 1,
71+
double beta = 0.75, double bias = 1, bool normBySize = true);
7272
void allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
7373
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
7474
};

modules/dnn/src/layers/max_unpooling_layer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void MaxUnpoolLayerImpl::forward(std::vector<Blob*> &inputs, std::vector<Blob> &
4444
for(int i_n = 0; i_n < outputs.size(); i_n++)
4545
{
4646
Blob& outBlob = outputs[i_n];
47+
outBlob.setTo(0);
4748
CV_Assert(input.channels() == outBlob.channels());
4849

4950
for (int i_c = 0; i_c < input.channels(); i_c++)

modules/dnn/src/layers/reshape_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void ReshapeLayerImpl::forward(std::vector<Blob*> &inputs, std::vector<Blob> &ou
7373
{
7474
for (size_t i = 0; i < outputs.size(); i++)
7575
{
76-
Blob& srcBlob = *inputs[i];
76+
Blob srcBlob = *inputs[i];
7777
BlobShape inputShape = inputs[i]->shape();
7878
bool channelsReduced = inputShape.dims() > outShapes[i].dims() ||
7979
(inputShape.dims() == 4 && inputShape[1] > outShapes[i][1]);

modules/dnn/src/tensorflow/tf_importer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ void TFImporter::populateNet(Net dstNet)
640640
if(hasLayerAttr(layer, "bias")) {
641641
layerParams.set("bias", getLayerAttr(layer, "bias").f());
642642
}
643-
layerParams.set("norm_sz", false);
643+
layerParams.set("norm_by_size", false);
644644

645645
int id = dstNet.addLayer(name, "LRN", layerParams);
646646
layer_id[name] = id;

0 commit comments

Comments
 (0)