gpt4 book ai didi

ios - 我想实现实例规范化

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:15 24 4
gpt4 key购买 nike

我正在写一个 metal cnn 代码。Metal提供了MPSCNNLocalContrastNormalization,由于实例规范化的概念略有不同,我打算将其实现为内核函数。

但是,问题在于,当从内核函数的输入接收到的纹理中的特征为 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);
}
}

最佳答案

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com