【发布时间】:2019-02-25 08:20:36
【问题描述】:
我正在将 Nvidia 的渐进式 GAN 生成器转换为 coreML。我已经设法将所有内容都转移到了 coreML,但像素标准化 (Lambda) 层除外,我计划在 Swift/Metal 中将其实现为自定义 coreML 层。
在 TensorFlow.Keras 中,我将像素范数实现为
def pixelwise_norm(a):
return a / tf.sqrt(tf.reduce_mean(a * a, axis=3, keep_dims=True) + 1e-8)
现在,我几乎没有使用过着色器/Metal,但按照此处的说明:http://machinethink.net/blog/coreml-custom-layers/,我设置了一个自定义层以使用 Metal 进行前馈操作。我正在使用 MTLComputePipelineState (调用?编码?)图层操作的以下着色器:
#include <metal_stdlib>
using namespace metal;
kernel void pixelwise_norm(
texture2d_array<half, access::read> inTexture [[texture(0)]],
texture2d_array<half, access::write> outTexture [[texture(1)]],
ushort3 gid [[thread_position_in_grid]])
{
if (gid.x >= outTexture.get_width() ||
gid.y >= outTexture.get_height()) {
return;
}
const float4 x = float4(inTexture.read(gid.xy, gid.z));
const float4 y = 0.0000001f + (x / sqrt(pow(x,2)));
outTexture.write(half4(y), gid.xy, gid.z);
}
我无法确定“reduce_mean”的金属等价物,现在这个着色器实现了一个 ~tensorflow ~ 操作,如
return a / tf.sqrt((a * a) + 1e-8)
有没有人指点? 谢谢
【问题讨论】:
标签: c++ tensorflow keras metal coreml