Skip to main content
 首页 » 编程设计

python之使用 Keras 提取同一图像的多个补丁

2024年10月17日9lidabo

我在包含多种尺寸图像的数据集上训练/测试机器学习模型。我知道 Keras 允许我们使用 target_size 参数提取固定大小的随机补丁:

gen = ImageDataGenerator(width_shift_range=.9, height_shift_range=.9) 
data = gen.flow_from_directory('/path/to/dataset/train', 
          target_size=(224, 224), 
            classes=10, 
            batch_size=32, 
            seed=0) 
 
for _ in range(data.N // data.batch_size): 
    X, y = next(data) 

对于每次迭代,X 包含 32 个补丁(每个不同的样本一个)。在所有迭代中,我可以访问数据集中每个样本的一个补丁。

问题:提取同一样本的多个补丁的最佳方法是什么?

类似于:

data = gen.flow_from_directory(..., nb_patches=10) 
X, y = next(data) 
# X contains 320 rows (10 patches for each 32 sample in the batch) 

我知道我可以编写第二个 for 循环并在数据集上迭代多次,但这看起来有点困惑。我还想更有力地保证我确实在获取示例样本的补丁。

请您参考如下方法:

skimage 有一个实用方法,允许您用重叠或不重叠的片段修补图像。

查看 view_as_windows 和 view_as_blocks

http://scikit-image.org/docs/dev/api/skimage.util.html?highlight=view_as_windows#skimage.util.view_as_windows