【问题标题】:iOS Metal how to compute standard deviation in kernel function?iOS Metal如何计算核函数的标准差?
【发布时间】:2021-05-30 13:31:19
【问题描述】:

我有一个内核金属函数,它接收元素的 X、Y 网格位置。我想对某些元素应用统计过滤,并且需要知道该值与其邻居的标准差。

我可以自己在内核函数中实现该函数,但我很好奇是否有更好/更有效的方法。

iOS Metal 是否提供现成的数学函数,可用于计算少数(10 个左右)成员的标准差?

【问题讨论】:

  • 您是否需要对网格中的每个元素相对于其周围环境进行此计算?还是从一个大网格中取出一个或几个这样的元素?
  • 它们中的每个元素 (307000) 与其最近的邻居(在网格中)的关系 - 例如每个元素 10 个水平样本和 10 个垂直样本。

标签: math statistics metal ios14


【解决方案1】:

我认为您应该研究一下 Metal Performance Shaders 框架。有几十种图像处理过滤器可能有用,尤其是在您使用标准过滤器(例如高斯模糊)时。对于这个特殊问题,MPSImageStatisticsMeanAndVariance 图像过滤器可能是您最好的选择。在对过滤器工作进行编码并写入一些输出纹理(或MPSImage;有关更多信息,请参阅下面链接的文档)之后,您将访问方差并简单地对从每个线程读取的方差值使用sqrt 函数在 MSL 中(每次从输出纹理中读取)。它可能看起来像

import MetalPerformanceShaders

let device = MTLCreateSystemDefaultDevice()!
let commandQueue = device.makeCommandQueue()!

let sourceTextureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm,
                                                                       width: 100, height: 100,
                                                                       mipmapped: false)
let sourceTexture = device.makeTexture(descriptor: sourceTextureDescriptor)!

let destinationTextureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm,
                                                                            width: 100, height: 100,
                                                                            mipmapped: false)
let destinationTexture = device.makeTexture(descriptor: destinationTextureDescriptor)!


// Do encoding work
let commandBuffer = commandQueue.makeCommandBuffer()!

// Create the `MPSImageStatisticsMeanAndVariance` filter
let filter = MPSImageStatisticsMeanAndVariance(device: device)

filter.encode(commandBuffer: commandBuffer, sourceTexture: sourceTexture, destinationTexture: destinationTexture)

// Do more work with the destination texture
let customEncoder = commandBuffer.makeComputeCommandEncoder()!
customEncoder.setComputePipelineState(...)
customEncoder.setTexture(destinationTexture, index: 0)
...

更多关于 MPSImageStatisticsMeanAndVariance 和 MPS 图像过滤器的文档可以分别在 Apple 开发者网站 herehere 上找到。这可能不是您正在寻找的确切内容,但 MPS 中可能还有其他资源可供使用

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2016-04-14
    • 2020-01-28
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多