提问人:SEMANTICALLY_INVALID 提问时间:2/15/2022 更新时间:2/19/2022 访问量:177
如何在 rust 中在 Arrayfire 中实现 Max Pooling,而无需编写自己的 cuda 代码
How can I implement Max Pooling in Arrayfire in rust without resorting to writing my own cuda code
问:
我正在尝试弄清楚如何在 Arrayfire 上实现最大池化。我目前最好的方法是遍历每个卷积输出,并应用一个函数,该函数应用四个内核,[1 0 0 0]、[0 1 0 0]、[0 0 1 0]、[0 0 0 1],并生成四个输出,然后我可以比较每个像素的最大值。
我的问题是,在张量库中像这样循环似乎非常缓慢和不正确,但我还没有想出更好的解决方案
答:
1赞
SEMANTICALLY_INVALID
2/15/2022
#1
我已经确定了以下几点: 使用 seq 索引困境,然后获取最大值
#[test]
fn maxfilt____() {
let fourxfour = Array::new(&(0..16).into_iter().collect::<Vec<_>>(), dim4!(4, 4, 1, 1));
let dim0 = fourxfour.dims()[0] as i32;
let dim1 = fourxfour.dims()[1] as i32;
let q1_indices = &[seq!(0, dim0 - 1, 2), seq!(0, dim1 - 1, 2), seq!(), seq!()];
let q2_indices = &[seq!(0, dim0 - 1, 2), seq!(1, dim1 - 1, 2), seq!(), seq!()];
let q3_indices = &[seq!(1, dim0 - 1, 2), seq!(0, dim1 - 1, 2), seq!(), seq!()];
let q4_indices = &[seq!(1, dim0 - 1, 2), seq!(1, dim1 - 1, 2), seq!(), seq!()];
let q1s = index(&fourxfour, q1_indices);
let q2s = index(&fourxfour, q2_indices);
let q3s = index(&fourxfour, q3_indices);
let q4s = index(&fourxfour, q4_indices);
let max = maxof(&q1s, &maxof(&q2s, &maxof(&q3s, &q4s, false), false), false);
af_print!("max", max);
}
1赞
pradeep
2/19/2022
#2
您可以使用 wrap 和 unwrap 来执行此操作,也许更有效。
逻辑如下:
- 将大小为 2x2 的窗口展开成列
- 沿列执行最大值
- 回绕回原始图像形状
我认为这可能是更快的索引偏移位置,这可能会导致内存读取不理想。
以下是上述功能相关文档的链接
展开 - https://arrayfire.org/arrayfire-rust/arrayfire/fn.unwrap.html 包装 - https://arrayfire.org/arrayfire-rust/arrayfire/fn.wrap.html
虽然我确实在 rust 文档中写了一个示例,但我认为 C++ 文档上的图像插图在理解发生的事情方面要好得多。下面给出的是这些链接
展开 - https://arrayfire.org/docs/group__image__func__unwrap.htm 包装 - https://arrayfire.org/docs/group__image__func__wrap.htm
希望这会有所帮助
评论