<h3 dir="auto">1. Problem</h3>
<p dir="auto">I implenmented a layer with reference to the <a href="tutorial_dnn_custom_layers">tutorial_dnn_custom_layers</a>， I want to get the type of output is <code class="notranslate">np.long</code>,  but the default type is <code class="notranslate">np.float32</code>, <strong>Can I set the output type?</strong></p>
<h3 dir="auto">2. some key code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class ALayer(object):
    def __init__(self, params, blobs):
        ...

    # Our layer receives one inputs. We need to find the max
    def getMemoryShapes(self, inputs):
        ...

    def forward(self, inputs):
        data = inputs[0]
        # find max ids on axis
        res = np.argmax(data, axis=self.axis).astype(np.float32)
        print(res, res.shape, res.dtype)
        return [res]"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">ALayer</span>(<span class="pl-s1">object</span>):
    <span class="pl-k">def</span> <span class="pl-en">__init__</span>(<span class="pl-s1">self</span>, <span class="pl-s1">params</span>, <span class="pl-s1">blobs</span>):
        ...

    <span class="pl-c"># Our layer receives one inputs. We need to find the max</span>
    <span class="pl-k">def</span> <span class="pl-en">getMemoryShapes</span>(<span class="pl-s1">self</span>, <span class="pl-s1">inputs</span>):
        ...

    <span class="pl-k">def</span> <span class="pl-en">forward</span>(<span class="pl-s1">self</span>, <span class="pl-s1">inputs</span>):
        <span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">inputs</span>[<span class="pl-c1">0</span>]
        <span class="pl-c"># find max ids on axis</span>
        <span class="pl-s1">res</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">argmax</span>(<span class="pl-s1">data</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s1">self</span>.<span class="pl-s1">axis</span>).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>)
        <span class="pl-en">print</span>(<span class="pl-s1">res</span>, <span class="pl-s1">res</span>.<span class="pl-s1">shape</span>, <span class="pl-s1">res</span>.<span class="pl-s1">dtype</span>)
        <span class="pl-k">return</span> [<span class="pl-s1">res</span>]</pre></div>
<p dir="auto">This code is run  after registered the ALayer.</p>
<ul dir="auto">
<li>type error<br>
I  droped the <code class="notranslate">astype(np.float32)</code> in <code class="notranslate">forward</code> function,  I got the type error like below after registered the ALayer.</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="pyopencv_dnn.hpp:175: error: (-215:Assertion failed) pyOutputs[i].type() == outputs[i].type() in function 'pycvLayer::forward'"><pre class="notranslate"><code class="notranslate">pyopencv_dnn.hpp:175: error: (-215:Assertion failed) pyOutputs[i].type() == outputs[i].type() in function 'pycvLayer::forward'
</code></pre></div>
<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.2.0</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">The CUDA backend can support mixed-precision inference with various types: FP32, FP16, INT32, (U)INT8 and possibly INT4 and INT1. It's fairly easy to implement as cuDNN already has convolution primitives for many of these types and the existing CUDA backend codebase is fully template-based.</p>
<p dir="auto">Before this can be implemented, some issues need to be sorted out:</p>
<ol dir="auto">
<li>Is it required?</li>
<li>APIs to configure mixed-precision
<ul dir="auto">
<li>APIs to allow the user to control the mixed-precision configuration</li>
<li>ability to import quantized models and use the quantized weights during inference</li>
</ul>
</li>
</ol>
<p dir="auto">Other ideas:</p>
<ol dir="auto">
<li>Default Mixed-precision policies
<ul dir="auto">
<li>example: fp16 for convolutions and fp32 for the rest?</li>
</ul>
</li>
<li>Automatic Mixed-Precision
<ul dir="auto">
<li>the user provides a representative dataset and the AMP system automatically figures out a good configuration
<ul dir="auto">
<li>a similar thing would be required to implement in-house quantization</li>
</ul>
</li>
</ul>
</li>
</ol>
<p dir="auto">IE supports mixed-precision inference. A generic <code class="notranslate">cv::dnn</code> API for mixed-precision could be used for all the backends that support it.</p>