将 Tensorflow 图转换为 CoreML

Convert Tensorflow graph to CoreML

我正在尝试将 Tensorflow 图转换为 CoreML,我正在关注 this tutorial。有一段代码我不太明白:

#include <metal_stdlib>
using namespace metal;

kernel void swish(
  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 = x / (1.0f + exp(-x));             
  outTexture.write(half4(y), gid.xy, gid.z);
}

我不明白的是这里gid的用法。网格不是二维的吗? gid.z 是什么意思? gid.x不就是当前像素的x坐标吗?

gid.xgid.y是当前像素的x/y坐标。因此,当您执行 texture.read(gid.xy) 时,它会为您提供 4 个通道的像素数据。

但是神经网络中使用的"images"可能有4个以上的通道。这就是为什么纹理的数据类型是 texture2d_array<> 而不是 texture2d<>

gid.z值是指纹理"slice"在这个数组中的索引。如果image/tensor有32个通道,那么就有8个纹理切片(因为每个纹理最多存储4个通道的数据)。

所以网格确实是三维的:(x, y, slice)。