image-processing - Keras ImageDataGenerator 如何查看修改图像的参数

标签 image-processing keras generator

我了解如何以及为何使用 ImageDataGenerator,但我有兴趣了解 ImageDataGenerator 如何影响我的图像,以便我可以决定我是否选择了足够的自由度来扩充我的数据。我看到我可以迭代来自生成器的图像。我正在寻找一种方法来查看它是原始图像还是修改后的图像,如果是后者,在我正在查看的特定实例中修改了哪些参数。我怎么/怎么能看到这个?

最佳答案

大多数转换(翻转除外)将始终修改输入图像。例如,如果您从源代码中指定了 rotation_range:

theta = np.pi / 180 * np.random.uniform(-self.rotation_range, self.rotation_range)

随机数不太可能恰好为 0。

没有方便的方法来打印出应用于每个图像的变换量。您必须修改源代码并在 ImageDataGenerator.random_transform() 中添加一些打印功能。

如果您不想接触源代码(例如,在共享机器上),您可以扩展 ImageDataGenerator 并覆盖 random_transform()

import numpy as np
from keras.preprocessing.image import *


class MyImageDataGenerator(ImageDataGenerator):
    def random_transform(self, x, seed=None):
        # these lines are just copied-and-pasted from the original random_transform()
        img_row_axis = self.row_axis - 1
        img_col_axis = self.col_axis - 1
        img_channel_axis = self.channel_axis - 1

        if seed is not None:
            np.random.seed(seed)

        if self.rotation_range:
            theta = np.pi / 180 * np.random.uniform(-self.rotation_range, self.rotation_range)
        else:
            theta = 0

        if self.height_shift_range:
            tx = np.random.uniform(-self.height_shift_range, self.height_shift_range) * x.shape[img_row_axis]
        else:
            tx = 0

        if self.width_shift_range:
            ty = np.random.uniform(-self.width_shift_range, self.width_shift_range) * x.shape[img_col_axis]
        else:
            ty = 0

        if self.shear_range:
            shear = np.random.uniform(-self.shear_range, self.shear_range)
        else:
            shear = 0

        if self.zoom_range[0] == 1 and self.zoom_range[1] == 1:
            zx, zy = 1, 1
        else:
            zx, zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1], 2)

        transform_matrix = None
        if theta != 0:
            rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                        [np.sin(theta), np.cos(theta), 0],
                                        [0, 0, 1]])
            transform_matrix = rotation_matrix

        if tx != 0 or ty != 0:
            shift_matrix = np.array([[1, 0, tx],
                                     [0, 1, ty],
                                     [0, 0, 1]])
            transform_matrix = shift_matrix if transform_matrix is None else np.dot(transform_matrix, shift_matrix)

        if shear != 0:
            shear_matrix = np.array([[1, -np.sin(shear), 0],
                                    [0, np.cos(shear), 0],
                                    [0, 0, 1]])
            transform_matrix = shear_matrix if transform_matrix is None else np.dot(transform_matrix, shear_matrix)

        if zx != 1 or zy != 1:
            zoom_matrix = np.array([[zx, 0, 0],
                                    [0, zy, 0],
                                    [0, 0, 1]])
            transform_matrix = zoom_matrix if transform_matrix is None else np.dot(transform_matrix, zoom_matrix)

        if transform_matrix is not None:
            h, w = x.shape[img_row_axis], x.shape[img_col_axis]
            transform_matrix = transform_matrix_offset_center(transform_matrix, h, w)
            x = apply_transform(x, transform_matrix, img_channel_axis,
                                fill_mode=self.fill_mode, cval=self.cval)

        if self.channel_shift_range != 0:
            x = random_channel_shift(x,
                                     self.channel_shift_range,
                                     img_channel_axis)
        if self.horizontal_flip:
            if np.random.random() < 0.5:
                x = flip_axis(x, img_col_axis)

        if self.vertical_flip:
            if np.random.random() < 0.5:
                x = flip_axis(x, img_row_axis)

        # print out the trasformations applied to the image
        print('Rotation:', theta / np.pi * 180)
        print('Height shift:', tx / x.shape[img_row_axis])
        print('Width shift:', ty / x.shape[img_col_axis])
        print('Shear:', shear)
        print('Zooming:', zx, zy)

        return x

我只是在函数末尾添加了 5 个 print。其他行是从原始源代码复制和粘贴的。 现在您可以使用它,例如,

gen = MyImageDataGenerator(rotation_range=15,
                           width_shift_range=0.1,
                           height_shift_range=0.1,
                           zoom_range=0.5)
flow = gen.flow_from_directory('data', batch_size=1)
img = next(flow)

并在您的终端上看到这样的信息:

Rotation: -9.185074669096467
Height shift: 0.03791625365979884
Width shift: -0.08398553078553198
Shear: 0
Zooming: 1.40950509832 1.12895574928

关于image-processing - Keras ImageDataGenerator 如何查看修改图像的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246063/

相关文章:

python - 值错误 : axes don't match array when loading previously saved model

python - 修改训练好的模型架构并继续训练 Keras

swift - 如何将字符串转换为 UInt32?

python - python 的嵌套生成器或迭代器

python - 使用 pytesseract OCR 识别图像中的文本

python - model.fit_generator() 形状错误

image - 在 Matlab 中计算中值图像

python - Python2.7 中是否有返回生成器而不是列表的 range(..) 函数?

algorithm - 计算机科学理论 : Image Similarity

c++ - 与已知图像匹配的局部二进制模式