菜鸟教程小白 发表于 2022-12-11 19:02:13

ios - 我想实现实例规范化


                                            <p><p>我正在编写一个 Metalcnn 代码。
Metal 提供 MPSCNNLocalContrastNormalization,
由于 Instance Normalization 的概念略有不同,我打算将其实现为 Kernel Function。</p>

<p>但是,问题是当从核函数的输入中接收到的纹理中的特征是 R、G、B 时,应该获得每个 R、G、B 的均值和方差。
我想获得一些关于如何实现这一点的提示。</p>

<p> <img src="/image/qfwwL.png" alt="enter image description here"/> </p>

<pre><code>kernel void instance_normalization_2darray(texture2d_array&lt;float, access::sample&gt; src [[ texture(0) ]],
                                        texture2d_array&lt;float, access::write&gt; dst [[ texture(1) ]],
                                        uint3 tid []) {

}


    kernel void calculate_avgA(texture2d_array&lt;float, access::read&gt; texture_in [],
                        texture2d_array&lt;float, access::write&gt; texture_out [],
                        uint3 tid [])
{
    int width = texture_in.get_width();
    int height = texture_in.get_height();
    int depth = texture_in.get_array_size();
    float4 outColor;


    uint3 kernelIndex(0,0,0);
    uint3 textureIndex(0,0,0);

    for(int k = 0; k &lt; depth; k++) {
      outColor = (0.0, 0.0, 0.0, 0.0);
      for (int i=0; i &lt; width; i++)
      {
            for (int j=0; j &lt; height; j++)
            {
                kernelIndex = uint3(i, j, k);
                textureIndex = uint3(tid.x + i, tid.y + j, tid.z + k);
                float4 color = texture_in.read(textureIndex.xy, textureIndex.z).rgba;
                outColor += color;
            }
      }
      outColor = outColor / (width * height);
      texture_out.write(float4(outColor.rgba), tid.xy, textureIndex.z);
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>比斯塔先生
我对此有同样的问题,苹果没有快速提供一些功能。
我只是使用 MPSCNNPoolingAverage 来计算内核之前的平均值。
也许这是一种临时方法。
而其他算法并不比这更好,例如我用代码测试后的归约和算法。
因此,我将继续跟踪对此的更好实现。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 我想实现实例规范化,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42340868/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42340868/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 我想实现实例规范化