Issue 1:
System information (version)

OpenCV => 4.1.2

Operating System / Platform => macOS Catalina, Version 10.15.1

Compiler => Xcode, Version 11.2.1 (11B500)

Detailed description

I installed opencv4 through homebrew.

Unfortunately, cv::medianBlur() crushed every time it runs.

I checked the input variable format, kernel size as the doc says. I am sure the problem is not caused by these reasons.

I think the problem is from the opencv lib itself. The homebrew installation process summary is as follows:

==> Summary

🍺 /usr/local/Cellar/opencv/4.1.2: 753 files, 228.2MB

==> Caveats

==> cmake

Emacs Lisp files have been installed to:

/usr/local/share/emacs/site-lisp/cmake

==> openblas

openblas is keg-only, which means it was not symlinked into /usr/local,

because macOS provides BLAS and LAPACK in the Accelerate framework.

For compilers to find openblas you may need to set:

export LDFLAGS="-L/usr/local/opt/openblas/lib"

export CPPFLAGS="-I/usr/local/opt/openblas/include"

For pkg-config to find openblas you may need to set:

export PKG_CONFIG_PATH="/usr/local/opt/openblas/lib/pkgconfig"

==> gettext

gettext is keg-only, which means it was not symlinked into /usr/local,

because macOS provides the BSD gettext library & some software gets confused if both are in the library path.

If you need to have gettext first in your PATH run:

echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile

For compilers to find gettext you may need to set:

export LDFLAGS="-L/usr/local/opt/gettext/lib"

export CPPFLAGS="-I/usr/local/opt/gettext/include"

==> openssl@1.1

A CA file has been bootstrapped using certificates from the system

keychain. To add additional certificates, place .pem files in

/usr/local/etc/openssl@1.1/certs

and run

/usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,

because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:

echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:

export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"

export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:

export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> unbound

To have launchd start unbound now and restart at startup:

sudo brew services start unbound

==> readline

readline is keg-only, which means it was not symlinked into /usr/local,

because macOS provides the BSD libedit library, which shadows libreadline.

In order to prevent conflicts when programs look for libreadline we are

defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:

export LDFLAGS="-L/usr/local/opt/readline/lib"

export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:

export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> sqlite

sqlite is keg-only, which means it was not symlinked into /usr/local,

because macOS provides an older sqlite3.

If you need to have sqlite first in your PATH run:

echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find sqlite you may need to set:

export LDFLAGS="-L/usr/local/opt/sqlite/lib"

export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:

export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"

==> python

Python has been installed as

/usr/local/bin/python3

Unversioned symlinks python, python-config, pip etc. pointing to

python3, python3-config, pip3 etc., respectively, have been installed into

/usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run

brew install python@2

You can install Python packages with

pip3 install

They will install into the site-package directory

/usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

==> glib

Bash completion has been installed to:

/usr/local/etc/bash_completion.d

==> icu4c

icu4c is keg-only, which means it was not symlinked into /usr/local,

because macOS provides libicucore.dylib (but nothing else).

If you need to have icu4c first in your PATH run:

echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile

echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

For compilers to find icu4c you may need to set:

export LDFLAGS="-L/usr/local/opt/icu4c/lib"

export CPPFLAGS="-I/usr/local/opt/icu4c/include"

For pkg-config to find icu4c you may need to set:

export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig"

==> tesseract

This formula contains only the "eng", "osd", and "snum" language data files.

If you need all the other supported languages, brew install tesseract-lang.

The test code is as follows:

#import <opencv2/opencv.hpp>
int main(int argc, const char * argv[])
{
cv::Mat a = cv::Mat::zeros(100, 200, 5);
cv::Mat b;
cv::medianBlur(a, b, 5);
}

The runtime error is "Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)". The program stops at cv::medianBlur(a, b, 5) every time.

Steps to reproduce

homebrew version

Homebrew 2.1.16-89-g2a45343

Homebrew/homebrew-core (git revision 2a800; last commit 2019-11-20)

Homebrew/homebrew-cask (git revision 981f58; last commit 2019-11-20)

opencv4 install

brew install opencv

Test code

As shown above.

Issue 2:
System information (version)

Opencv => 4.0.0 (C++ custom build, includes dnn module)

Operating system => Ubuntu

Compiler => GCC

Detailed description

Summary

I have a transfer learning mobilenetV2 classification model, which I want to use in a C++ code using the opencv's cv2 dnn module.

The simplest way -

I have a custom trained mobilenetV2 classification model's frozen inference graph.

From the frozen inference graph, I generated the .pbtxt file using cv2.dnn.writeTextGraph method and ran the prediction using cv2::dnn::readNetFromTensorflow method

With just the model file reference alone got the below error.

OpenCV(4.0.0) /home/venkat/OPENCV4/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:497: error: (-2:Unspecified error) Input layer not found: module_apply_default/MobilenetV2/expanded_conv/output in function 'connect'

with both the model file and the config file got the below error.

what(): OpenCV(4.0.0) /home/venkat/OPENCV4/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:616: error: (-215:Assertion failed) const_layers.insert(std::make_pair(name, li)).second in function 'addConstNodes'

The complex way

I followed the instructions mentioned in

#11577 (comment)

and

#11452

and

#12075

All the above instructions are typically for the object detection models.

I looked at the https://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py which is for mobilenet ssd model.

So from various above sources, I ended up creating the below code(in python) for converting the model to be compatible with cv2 dnn

with tf.gfile.FastGFile('mobilenet_v2_100_224.pb', 'rb') as f:

graph_def = tf.GraphDef()

graph_def.ParseFromString(f.read())

graph_def = optimize_for_inference_lib.optimize_for_inference(graph_def, ['final_retrain_ops/biases/final_biases'], ['final_result'], tf.float32.as_datatype_enum)
graph_def = TransformGraph(graph_def, ['final_retrain_ops/biases/final_biases'], ['final_result'], ['remove_nodes(op=PlaceholderWithDefault)','sort_by_execution_order'])

for i in reversed(range(len(graph_def.node))):
#code blocks 1
if graph_def.node[i].op == 'Const':
del graph_def.node[i]
#code block 1 ends
for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
'Tpaddings']:
if attr in graph_def.node[i].attr:
print("Deleting")
del graph_def.node[i].attr[attr]
print(len(graph_def.node))
unconnected_nodes = []
for node in graph_def.node:
unconnected_nodes.append(node.name)
for inp in node.input:
if inp in unconnected_nodes:
unconnected_nodes.remove(inp)
unconnected_nodes.remove("final_result")
for name in unconnected_nodes:
for i in range(len(graph_def.node)):
if graph_def.node[i].name == name:
# print(name)
del graph_def.node[i]
break
print("leftover nodes ")
for node in graph_def.node:
print(node.name)

with tf.gfile.FastGFile('graph.pb', 'wb') as f:
f.write(graph_def.SerializeToString())
cv2.dnn.writeTextGraph('graph.pb','graph.pbtxt')

With the graph.pb and graph.pbtxt when I tried to load the model, I encountered the below error.

When I delete the "Const" op nodes (code block 1 in the above code, commented accordingly), I get the following error while writing the text graph.

error: (-215:Assertion failed) nodesMapIt != nodesMap.end() in function 'sortByExecutionOrder'

So I comment the code block 1, so my graph gets generated,

with the below log

...

W1230 17:47:58.718445 139948826896192 optimize_for_inference_lib.py:244] Didn't find expected Conv2D input to 'module_apply_default/MobilenetV2/expanded_conv_16/depthwise/BatchNorm/FusedBatchNorm'

2019-12-30 17:48:00.034702: I tensorflow/tools/graph_transforms/transform_graph.cc:317] Applying remove_nodes

2019-12-30 17:48:00.041844: I tensorflow/tools/graph_transforms/transform_graph.cc:317] Applying sort_by_execution_order

and with the generated graph, when I tried to load in cv2 dnn module, ended up at this error.

error: (-215:Assertion failed) const_layers.insert(std::make_pair(name, li)).second in function 'addConstNodes'

I also tried using the https://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_common.py file to convert, still, I ended up in errors.

Would like to know more on this issue, and the proper way to convert a custom trained mobile net v2 classification model compatible for opencv dnn module.