<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 4.1.0</li>
<li>Operating System / Platform =&gt; Windows 64 Bit</li>
<li>Compiler =&gt; Visual Studio 2017 (15.9)</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">The build of <code class="notranslate">opencv_highgui</code> fails with several errors of the form <code class="notranslate">X already defined in window.obj</code>. The other projects compile successfully.</p>
<h5 dir="auto">Steps to reproduce</h5>
<ol dir="auto">
<li>Clone the <code class="notranslate">opencv</code> and <code class="notranslate">opencv_contrib</code> repos, checkout <code class="notranslate">4.1.0</code> in both.</li>
<li>Run CMake, enable <code class="notranslate">WITH_CUDA</code>, <code class="notranslate">OPENCV_ENABLE_NONFREE</code> and set <code class="notranslate">OPENCV_EXTRA_MODULES_PATH</code> to the <code class="notranslate">opencv_contrib\modules</code> directory.</li>
<li>Generate the solution.</li>
<li>Open the solution in Visual Studio 2017 and launch the build.</li>
</ol>
<p dir="auto">The build runs smoothly and all projects except one succeed. The one that fails is <code class="notranslate">opencv_highgui</code>, with these errors:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="60&gt;opencv_highgui_main.cpp
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(250): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(252): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(254): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(255): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(257): warning C4702: unreachable code
60&gt;window_w32.obj : error LNK2005: &quot;void __cdecl cv::setWindowTitle(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;,class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)&quot; (?setWindowTitle@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvCreateTrackbar already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvCreateTrackbar2 already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvDestroyAllWindows already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvDestroyWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetTrackbarPos already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetWindowHandle already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetWindowName already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvInitSystem already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvMoveWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvNamedWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvResizeWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetMouseCallback already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarMax already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarMin already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarPos already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvShowImage already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvStartWindowThread already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvWaitKey already defined in window.obj
60&gt;   Creating library C:/OpenCV/opencv/build/lib/Release/opencv_highgui410.lib and object C:/OpenCV/opencv/build/lib/Release/opencv_highgui410.exp
60&gt;C:\OpenCV\opencv\build\bin\Release\opencv_highgui410.dll : fatal error LNK1169: one or more multiply defined symbols found
60&gt;Done building project &quot;opencv_highgui.vcxproj&quot; -- FAILED."><pre class="notranslate"><code class="notranslate">60&gt;opencv_highgui_main.cpp
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(250): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(252): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(254): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(255): warning C4702: unreachable code
60&gt;c:\opencv\opencv\modules\highgui\src\window.cpp(257): warning C4702: unreachable code
60&gt;window_w32.obj : error LNK2005: "void __cdecl cv::setWindowTitle(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;,class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)" (?setWindowTitle@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvCreateTrackbar already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvCreateTrackbar2 already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvDestroyAllWindows already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvDestroyWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetTrackbarPos already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetWindowHandle already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvGetWindowName already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvInitSystem already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvMoveWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvNamedWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvResizeWindow already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetMouseCallback already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarMax already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarMin already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvSetTrackbarPos already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvShowImage already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvStartWindowThread already defined in window.obj
60&gt;window_w32.obj : error LNK2005: cvWaitKey already defined in window.obj
60&gt;   Creating library C:/OpenCV/opencv/build/lib/Release/opencv_highgui410.lib and object C:/OpenCV/opencv/build/lib/Release/opencv_highgui410.exp
60&gt;C:\OpenCV\opencv\build\bin\Release\opencv_highgui410.dll : fatal error LNK1169: one or more multiply defined symbols found
60&gt;Done building project "opencv_highgui.vcxproj" -- FAILED.
</code></pre></div>
<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; : 4.1.1.26</li>
<li>Operating System / Platform =&gt; Ubuntu 18.04</li>
<li>Compiler =&gt; ython3.5</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Hello I am having the following issue. I am trying to freeze <a href="https://github.com/qubvel/efficientnet/blob/master/efficientnet/model.py">EfficientNet</a> taken from this repo and use the <code class="notranslate">protobuf</code> file for using it with OpenCV <strong>dnn</strong> module.</p>
<p dir="auto">The model is a simple classification network. Right after the the Feature Extractor specified in the link, I just try add an extra dense &amp; and an extra classification layer of <code class="notranslate">num_classes</code>.</p>
<p dir="auto">I have frozen several networks and used them with the dnn library before and I know all the issues that can arise, which I have already tried.</p>
<p dir="auto">Example code in python (also seen in attached script below):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="net = cv2.dnn.readNetFromTensorflow('frozen.pb') 
x_cv = np.random.random((224, 224, 3)).astype(np.uint8)
blob = cv2.dnn.blobFromImage(x_cv, 1.0, (224, 224), (0, 0, 0))
net.setInput(blob)
# (1 x 200)
s = net.forward().shape
print(s)"><pre class="notranslate"><code class="notranslate">net = cv2.dnn.readNetFromTensorflow('frozen.pb') 
x_cv = np.random.random((224, 224, 3)).astype(np.uint8)
blob = cv2.dnn.blobFromImage(x_cv, 1.0, (224, 224), (0, 0, 0))
net.setInput(blob)
# (1 x 200)
s = net.forward().shape
print(s)
</code></pre></div>
<p dir="auto">However life is not that easy. This is the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error: OpenCV(4.1.1) /io/opencv/modules/dnn/src/layers/eltwise_layer.cpp:116: error: (-215:Assertion failed) inputs[0] == inputs[i] in function 'getMemoryShapes'"><pre class="notranslate"><code class="notranslate">error: OpenCV(4.1.1) /io/opencv/modules/dnn/src/layers/eltwise_layer.cpp:116: error: (-215:Assertion failed) inputs[0] == inputs[i] in function 'getMemoryShapes'
</code></pre></div>
<p dir="auto">Eventually I Found out that the problem is caused when using the <a href="https://github.com/qubvel/efficientnet/blob/master/efficientnet/model.py#L238">Squeeze and excitation module</a> <strong>(SE module for short)</strong>. If I disable the flag &amp; remove those modules from the network, the forward pass works.</p>
<p dir="auto">In my opinion is the <strong>flow of the graph of the SE Module</strong> that is not acceptable from OpenCV  from going to an average pooling to 1x1 convs and finally multiplying the features.</p>
<h5 dir="auto">Some remarks:</h5>
<p dir="auto">I had this issue with SE modules in the past, with the difference that the model was using more basic operations such as:</p>
<ul dir="auto">
<li>
<p dir="auto"><code class="notranslate">reduce_mean</code> across the features instead of average pooling</p>
</li>
<li>
<p dir="auto"><code class="notranslate">fully connected</code> layers instead of 1x1 Convolutions, since they result in the same number of features.</p>
</li>
</ul>
<p dir="auto">Additionally:</p>
<ul dir="auto">
<li>
<p dir="auto">I used the <code class="notranslate">optimize_for_inference_lib</code> tool with no success.</p>
</li>
<li>
<p dir="auto">Using the following command: <code class="notranslate">net.getUnconnectedOutLayersNames()</code> gave me the following results:</p>
<p dir="auto"><code class="notranslate">['block1a_se_squeeze/Mean/flatten', 'block2a_se_squeeze/Mean/flatten', 'block2b_se_squeeze/Mean/flatten', 'block3a_se_squeeze/Mean/flatten', 'block3b_se_squeeze/Mean/flatten', 'block4a_se_squeeze/Mean/flatten', 'block4b_se_squeeze/Mean/flatten', 'block4c_se_squeeze/Mean/flatten', 'block5a_se_squeeze/Mean/flatten', 'block5b_se_squeeze/Mean/flatten', 'block5c_se_squeeze/Mean/flatten', 'block6a_se_squeeze/Mean/flatten', 'block6b_se_squeeze/Mean/flatten', 'block6c_se_squeeze/Mean/flatten', 'block6d_se_squeeze/Mean/flatten', 'block7a_se_squeeze/Mean/flatten', 'avg_pool/Mean/flatten', 'dense_1/Softmax']</code></p>
</li>
</ul>
<h5 dir="auto">Follow up</h5>
<p dir="auto">I would like to know if there is a possible way to add the SE Module either by</p>
<ul dir="auto">
<li>reshaping the network in a compatible manner</li>
<li>transforming the graph and using a <code class="notranslate">.pbtxt</code> file additionally</li>
<li>register the module as custom &amp; override the <code class="notranslate">getMemoryShapes()</code></li>
</ul>
<p dir="auto">Of course the easiest solution would be preferrable :)</p>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">Running the attached <code class="notranslate">.py</code> (extension currently txt) file, will follow the pipeline:</p>
<ul dir="auto">
<li>Initialize EfficientnetB0 (smallest) with SE modules.</li>
<li>Create a dummy training set and run keras training <code class="notranslate">fit()</code> function.</li>
<li>Save the model as keras extension and reset graph</li>
<li>Load &amp; freeze model with standard keras freezing method. You should see <code class="notranslate">344 variables</code> frozen in the terminal.</li>
<li>Load model for OpenCV, create dummy blob &amp; run <code class="notranslate">net.forward()</code>. It will crash with the aforementioned error.</li>
</ul>
<p dir="auto"><a href="https://github.com/opencv/opencv/files/3624944/opencv_efficientnet.txt">opencv_efficientnet.txt</a></p>
<h5 dir="auto">Making the model <code class="notranslate">freezable</code>.</h5>
<p dir="auto">If you jump to <strong>line 254</strong> of the attached script, you can override the <code class="notranslate">has_se=True</code> to <code class="notranslate">False</code>. This will make the model compatible with OpenCV, proving the detailed description above that this is the factor that makes it show the assertion error.</p>