OGeek|极客世界-中国程序员成长平台

标题: ios - 我想实现实例规范化 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:02
标题: ios - 我想实现实例规范化

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

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

enter image description here

kernel void instance_normalization_2darray(texture2d_array<float, access::sample> src [[ texture(0) ]],
                                        texture2d_array<float, access::write> dst [[ texture(1) ]],
                                        uint3 tid [[thread_position_in_grid]]) {

}


    kernel void calculate_avgA(texture2d_array<float, access::read> texture_in [[texture(0)]],
                          texture2d_array<float, access::write> texture_out [[texture(1)]],
                          uint3 tid [[thread_position_in_grid]])
{
    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 < depth; k++) {
        outColor = (0.0, 0.0, 0.0, 0.0);
        for (int i=0; i < width; i++)
        {
            for (int j=0; j < 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);
    }
}



Best Answer-推荐答案


比斯塔先生 我对此有同样的问题,苹果没有快速提供一些功能。 我只是使用 MPSCNNPoolingAverage 来计算内核之前的平均值。 也许这是一种临时方法。 而其他算法并不比这更好,例如我用代码测试后的归约和算法。 因此,我将继续跟踪对此的更好实现。

关于ios - 我想实现实例规范化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42340868/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4