scikit-learn extract_patches 函数背后的 theory/algorithm 是什么?

What is the theory/algorithm behind extract_patches function of scikit-learn?

我正在使用 extract_patches_2dextract_patches 从 2d 图像中提取局部补丁,我希望获得解释补丁提取实施方法的理论和参考资料。

我不确定你指的是什么理论,在幕后这些方法只是巧妙的数组操作(剧透:numpy 数组操作)。

  • 第一个 extract_patches_2d 是对 extract_patches 的简单二维封装,它调用

    extract_patches(image,
                    patch_shape=(p_h, p_w, n_colors),
                    extraction_step=1)
    

    并重塑结果 (source code)。

  • 第二个 extract_patches 也是包装器,这次是 numpy.as_strided。它只准备 2*n 形状并大步将工作委托给

    as_strided(arr, shape=shape, strides=strides)
    

    这是它的 source code.

  • 真正有趣的是numpy.as_strided。来自它的文档:

    as_strided creates a view into the array given the exact strides and shape. This means it manipulates the internal data structure of ndarray and, if done incorrectly, the array elements can point to invalid memory and can corrupt results or crash your program. It is advisable to always use the original x.strides when calculating new strides to avoid reliance on a contiguous memory layout.

    所以,基本上,结果是内存中相同数组的包装器(视图),它提供索引查找并且每个索引都在查找内部的特定区域xnumpy.ndarray.view is the core numpy function which allows to view inside an existing array without memory reallocation. If you wish to dive deep into how numpy performs array manipulation and views, numpy internals 是一个很好的起点。