Repository: opencv
Issue: Failing `Test_Caffe_layers` not calling `UpgradeNetAsNeeded` for external ProtoBuf, building on Guix
Labels: bug,test,priority: low,category: dnn

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => 3.4.12
- Operating System / Platform => GNU Guix up to date at 24/10/2020
- Compiler => gcc 7.5.0

##### Detailed description

First, thanks for the amazing work you do!

I'm upgrading Guix's OpenCV package from 3.4.3 to 3.4.12, and running into a test failure that I cannot diagnose further. The `Test_Caffe_layers.{Accum,DataAugmentation,Resample,Correlation}` all fail with the following message (only reproduced for `Accum`):

```
[ RUN      ] Test_Caffe_layers.Accum/0, where GetParam() = OCV/CPU
[libprotobuf ERROR google/protobuf/text_format.cc:309] Error parsing text-format opencv_caffe.NetParameter: 22:15: Message type "opencv_caffe.LayerParameter" has no field named "accum_param".
unknown file: Failure
C++ exception with description "OpenCV(3.4.12) /tmp/guix-build-opencv-3.4.12.drv-0/opencv-3.4.12-checkout/modules/dnn/src/caffe/caffe_io.cpp:1157: error: (-2:Unspecified error) FAILED: ReadProtoFromTextFile(param_file, param). Failed to parse NetParameter file: /tmp/guix-build-opencv-3.4.12.drv-0/opencv-extra/testdata/dnn/layers/accum.prototxt in function 'ReadNetParamsFromTextFileOrDie'
" thrown in the test body.
[  FAILED  ] Test_Caffe_layers.Accum/0, where GetParam() = OCV/CPU (1 ms)
```

I'm building the 3.4.12 release with the following configure options. Also note tha the `opencv/3rdparty` folder is removed, since the policy for Guix is to package all libraries directly in the distro, so whatever is not yet packaged is disabled (hence the ProtoBuf comment, which is packaged and provided externally).
```guile
(list "-DWITH_IPP=OFF"
      "-DWITH_ITT=OFF"
      "-DWITH_CAROTENE=OFF"
      "-DWITH_QUIRC=OFF"
      "-DWITH_OPENEXR=OFF"
      "-DENABLE_PRECOMPILED_HEADERS=OFF"

      "-DCPU_BASELINE=SSE2"
      "-DCPU_DISPATCH=NEON;VFPV3;FP16;SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2;POPCNT;AVX;FP16;AVX2;FMA3;AVX_512F;AVX512_SKX"
      "-DCPU_DISPATCH_REQUIRE=SSE3,SSSE3,SSE4_1,SSE4_2,AVX,AVX2"

      "-DBUILD_PERF_TESTS=OFF"
      "-DBUILD_TESTS=ON"

      ;; opencv-contrib and opencv-extra are cloned in the parent directory
      "-DOPENCV_EXTRA_MODULES_PATH=../opencv-contrib/modules"
      "-DOPENCV_TEST_DATA_PATH=../opencv-extra/testdata"

      ;; Is ON by default and would try to rebuild 3rd-party protobuf,
      ;; which we had removed, which would lead to an error:
      "-DBUILD_PROTOBUF=OFF"

      ;; Rebuild protobuf files, because we have a slightly different
      ;; version than the included one. If we would not update, we
      ;; would get a compile error later:
      "-DPROTOBUF_UPDATE_FILES=ON"

      ;; face disabled, because it downloads extra binaries from
      ;; https://github.com/opencv/opencv_3rdparty defined in
      ;; modules/face/CMakeLists.txt . If it's really needed, we should
      ;; patch opencv-contrib/modules/face/CMakeLists.txt so that it
      ;; gets the necessary binary test data from a native-input we
      ;; download before the build phase. The same goes for xfeatures2d
      ;; below.
      "-DBUILD_opencv_face=OFF"

      ;; xfeatures2d disabled, because it downloads extra binaries from
      ;; https://github.com/opencv/opencv_3rdparty
      ;; defined in xfeatures2d/cmake/download_{vgg|bootdesc}.cmake
      "-DBUILD_opencv_xfeatures2d=OFF")
```

Here is the full build+test log: 
[89p9pdv297vawa15l29xl5pf9lfiyi-opencv-3.4.12.txt](https://github.com/opencv/opencv/files/5465209/89p9pdv297vawa15l29xl5pf9lfiyi-opencv-3.4.12.txt)

My understanding is that since ProtoBuf is found externally, `ReadNetParamsFromTextFileOrDie` should report the failure of `ReadProtoFromTextFile` to load the `.proto` file, then upgrade with `UpgradeNetAsNeeded`. But apparently it fails and stops at `CHECK(ReadProtoFromTextFile(param_file, param))`.

##### Steps to reproduce

I'm not sure this will reproduce on other distributions, as it looks like a basic test failure which would have been caught a long time ago. If you have a Guix system though, reproduce by replacing the `opencv` package definition in `gnu/packages/image-processing.scm` with the contents of this file

[opencv-guix.txt](https://github.com/opencv/opencv/files/5465171/opencv-guix.txt)

If you don't have a Guix system, I'm happy to try whatever you tell me to understand this failure! Many thanks either way.

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to latest OpenCV version and the issue is still there (I upgraded to 3.4.12, which is the stable version to be packaged, not the tip of the 3.4 branch)
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->