<p dir="auto">Hi,</p>
<p dir="auto">My OS: Ubuntu 16.04.4<br>
GCC/G++: <strong>Ubuntu 5.4.0-6ubuntu1~16.04.9</strong><br>
OpenCV: manually installed 3.4.1<br>
I'm actually building VLC with OpenCV.<br>
And, I came across the following <strong>ERROR</strong> message:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/usr/local/include/opencv2/core/cvdef.h:485:1: error: unknown type name ‘namespace’
 namespace cv {
 ^
/usr/local/include/opencv2/core/cvdef.h:485:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 namespace cv {
              ^"><pre class="notranslate"><code class="notranslate">/usr/local/include/opencv2/core/cvdef.h:485:1: error: unknown type name ‘namespace’
 namespace cv {
 ^
/usr/local/include/opencv2/core/cvdef.h:485:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 namespace cv {
              ^
</code></pre></div>
<p dir="auto">And in <strong>/usr/local/include/opencv2/core/cvdef.h</strong>, the code snippet is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="// Integer types portatibility
#ifdef OPENCV_STDINT_HEADER
#include OPENCV_STDINT_HEADER
#else
#if defined(_MSC_VER) &amp;&amp; _MSC_VER &lt; 1600 /* MSVS 2010 */
namespace cv {
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
}
#elif defined(_MSC_VER) || __cplusplus &gt;= 201103L
#include &lt;cstdint&gt;
namespace cv {
using std::int8_t;
using std::uint8_t;
using std::int16_t;
using std::uint16_t;
using std::int32_t;
using std::uint32_t;
using std::int64_t;
using std::uint64_t;
}
#else
#include &lt;stdint.h&gt;
namespace cv {
typedef ::int8_t int8_t;
typedef ::uint8_t uint8_t;
typedef ::int16_t int16_t;
typedef ::uint16_t uint16_t;
typedef ::int32_t int32_t;
typedef ::uint32_t uint32_t;
typedef ::int64_t int64_t;
typedef ::uint64_t uint64_t;
}
#endif
#endif"><pre class="notranslate"><code class="notranslate">// Integer types portatibility
#ifdef OPENCV_STDINT_HEADER
#include OPENCV_STDINT_HEADER
#else
#if defined(_MSC_VER) &amp;&amp; _MSC_VER &lt; 1600 /* MSVS 2010 */
namespace cv {
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
}
#elif defined(_MSC_VER) || __cplusplus &gt;= 201103L
#include &lt;cstdint&gt;
namespace cv {
using std::int8_t;
using std::uint8_t;
using std::int16_t;
using std::uint16_t;
using std::int32_t;
using std::uint32_t;
using std::int64_t;
using std::uint64_t;
}
#else
#include &lt;stdint.h&gt;
namespace cv {
typedef ::int8_t int8_t;
typedef ::uint8_t uint8_t;
typedef ::int16_t int16_t;
typedef ::uint16_t uint16_t;
typedef ::int32_t int32_t;
typedef ::uint32_t uint32_t;
typedef ::int64_t int64_t;
typedef ::uint64_t uint64_t;
}
#endif
#endif
</code></pre></div>
<p dir="auto">It looks if the build goes to <strong>#include &lt;stdint.h&gt;</strong>,<br>
VLC's compiler is using <strong>gcc</strong> instead of <strong>g++</strong>?<br>
And I wonder why <strong>__cplusplus &gt;= 201103L</strong> ?<br>
Here is my case:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."><pre class="notranslate"><code class="notranslate">$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</code></pre></div>
<p dir="auto">In such a case, how do you comapre <strong>201103L</strong> and ** (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609**?</p>
<p dir="auto">Cheers<br>
Pei</p>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 4.4.0-pre</li>
<li>Operating System / Platform =&gt; Ubuntu 18.04</li>
<li>Compiler =&gt; qmake (Qt5)</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<ol dir="auto">
<li>
<p dir="auto">The current version of OpenCV does not support ONNX models with dynamic input/output shape</p>
</li>
<li>
<p dir="auto">Bug Report:<br>
Suppose that an ONNX model has a fixed input shape ( H: 736, W: 1280 )<br>
Then, I input an image with a wrong shape ( H: 1280, W: 736 )<br>
However, OpenCV would not throw an exception, and the output is not right.</p>
</li>
</ol>
<h5 dir="auto">Steps to reproduce</h5>
<ol dir="auto">
<li>I provide an ONNX model with a dynamic input shape:<br>
<a href="https://drive.google.com/file/d/1ihzycC2R_HzUAH941P_ghxA1eDjezy6c/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/1ihzycC2R_HzUAH941P_ghxA1eDjezy6c/view?usp=sharing</a><br>
It is generated by the following codes:</li>
</ol>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    output_onnx = 'DB_dynamic_input.onnx'
    print(&quot;==&gt; Exporting model to ONNX format at '{}'&quot;.format(output_onnx))
    input_names = [&quot;input&quot;]
    output_names = [&quot;out&quot;]
    inputs = torch.randn((1, 3, 576, 800), dtype=torch.float32)
    dynamic_axes = {'input': {0: 'batch_size', 2: 'height', 3: 'width'}, 'out': {0: 'batch', 1: 'height', 2: 'width'}}
    torch_out = torch.onnx.export(model, inputs, output_onnx, export_params=True, verbose=True,
                                  input_names=input_names, output_names=output_names,
                                  opset_version=11,
                                  dynamic_axes=dynamic_axes)"><pre class="notranslate">    <span class="pl-s1">output_onnx</span> <span class="pl-c1">=</span> <span class="pl-s">'DB_dynamic_input.onnx'</span>
    <span class="pl-en">print</span>(<span class="pl-s">"==&gt; Exporting model to ONNX format at '{}'"</span>.<span class="pl-en">format</span>(<span class="pl-s1">output_onnx</span>))
    <span class="pl-s1">input_names</span> <span class="pl-c1">=</span> [<span class="pl-s">"input"</span>]
    <span class="pl-s1">output_names</span> <span class="pl-c1">=</span> [<span class="pl-s">"out"</span>]
    <span class="pl-s1">inputs</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>((<span class="pl-c1">1</span>, <span class="pl-c1">3</span>, <span class="pl-c1">576</span>, <span class="pl-c1">800</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">torch</span>.<span class="pl-s1">float32</span>)
    <span class="pl-s1">dynamic_axes</span> <span class="pl-c1">=</span> {<span class="pl-s">'input'</span>: {<span class="pl-c1">0</span>: <span class="pl-s">'batch_size'</span>, <span class="pl-c1">2</span>: <span class="pl-s">'height'</span>, <span class="pl-c1">3</span>: <span class="pl-s">'width'</span>}, <span class="pl-s">'out'</span>: {<span class="pl-c1">0</span>: <span class="pl-s">'batch'</span>, <span class="pl-c1">1</span>: <span class="pl-s">'height'</span>, <span class="pl-c1">2</span>: <span class="pl-s">'width'</span>}}
    <span class="pl-s1">torch_out</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">onnx</span>.<span class="pl-en">export</span>(<span class="pl-s1">model</span>, <span class="pl-s1">inputs</span>, <span class="pl-s1">output_onnx</span>, <span class="pl-s1">export_params</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">verbose</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
                                  <span class="pl-s1">input_names</span><span class="pl-c1">=</span><span class="pl-s1">input_names</span>, <span class="pl-s1">output_names</span><span class="pl-c1">=</span><span class="pl-s1">output_names</span>,
                                  <span class="pl-s1">opset_version</span><span class="pl-c1">=</span><span class="pl-c1">11</span>,
                                  <span class="pl-s1">dynamic_axes</span><span class="pl-c1">=</span><span class="pl-s1">dynamic_axes</span>)</pre></div>
<p dir="auto">Then, I test it with onnxruntime</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    onnx_path = 'DB_dynamic_input.onnx'
    session = onnxruntime.InferenceSession(onnx_path)
    session.get_modelmeta()
    input_name = session.get_inputs()[0].name
    output_name = session.get_outputs()[0].name
    inputs = torch.randn((4, 3, 736, 1280), dtype=torch.float32).numpy()
    preds = session.run([output_name], {input_name: inputs})
    print(preds[0].shape)"><pre class="notranslate">    <span class="pl-s1">onnx_path</span> <span class="pl-c1">=</span> <span class="pl-s">'DB_dynamic_input.onnx'</span>
    <span class="pl-s1">session</span> <span class="pl-c1">=</span> <span class="pl-s1">onnxruntime</span>.<span class="pl-v">InferenceSession</span>(<span class="pl-s1">onnx_path</span>)
    <span class="pl-s1">session</span>.<span class="pl-en">get_modelmeta</span>()
    <span class="pl-s1">input_name</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">get_inputs</span>()[<span class="pl-c1">0</span>].<span class="pl-s1">name</span>
    <span class="pl-s1">output_name</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">get_outputs</span>()[<span class="pl-c1">0</span>].<span class="pl-s1">name</span>
    <span class="pl-s1">inputs</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>((<span class="pl-c1">4</span>, <span class="pl-c1">3</span>, <span class="pl-c1">736</span>, <span class="pl-c1">1280</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">torch</span>.<span class="pl-s1">float32</span>).<span class="pl-en">numpy</span>()
    <span class="pl-s1">preds</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">run</span>([<span class="pl-s1">output_name</span>], {<span class="pl-s1">input_name</span>: <span class="pl-s1">inputs</span>})
    <span class="pl-en">print</span>(<span class="pl-s1">preds</span>[<span class="pl-c1">0</span>].<span class="pl-s1">shape</span>)</pre></div>
<p dir="auto">and get an output with a right shape</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(4, 736, 1280)"><pre class="notranslate"><code class="notranslate">(4, 736, 1280)
</code></pre></div>
<p dir="auto">When I try to import it in OpenCV, I will get the following Exception:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.4.0-pre) /home/hannibal/Files/Tools/opencv/modules/dnn/src/layers/pooling_layer.cpp:1186: error: (-215:Assertion failed) (outShape[2 + i] - 1) * strides[i] &lt; inpShape[i] + pads_end[i] in function 'getMemoryShapes'"><pre class="notranslate"><code class="notranslate">terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.4.0-pre) /home/hannibal/Files/Tools/opencv/modules/dnn/src/layers/pooling_layer.cpp:1186: error: (-215:Assertion failed) (outShape[2 + i] - 1) * strides[i] &lt; inpShape[i] + pads_end[i] in function 'getMemoryShapes'
</code></pre></div>
<ol start="2" dir="auto">
<li>This bug can be reproduced by using the <strong>DB_IC15_resnet50.onnx</strong> provided in my tutorial:<br>
<a href="https://github.com/HannibalAPE/opencv/blob/text_det_recog_demo/doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown">https://github.com/HannibalAPE/opencv/blob/text_det_recog_demo/doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown</a><br>
The input shape of it is fixed, when it is converted to ONNX format.<br>
The right input shape should be (1, 3, 736, 1280), but you can input a blob with a wrong shape (1, 3, 1280, 736).</li>
</ol>
<h5 dir="auto">Issue submission checklist</h5>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues,<br>
answers.opencv.org, Stack Overflow, etc and have not found solution

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I updated to latest OpenCV version and the issue is still there

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files: videos, images, onnx, etc

</li>
</ul>