【问题标题】:usage of preprocessing_function from ImageDataGenerator on keras在 keras 上使用 ImageDataGenerator 中的 preprocessing_function
【发布时间】:2021-06-09 15:01:00
【问题描述】:

我注意到有一个 preprocess_input 函数根据您要在 tensorflow.keras.applications 中使用的模型而有所不同。

我正在使用ImageDataGenerator 类来扩充我的数据。更具体地说,我使用的是 CustomDataGenerator,它扩展自 ImageDataGenerator 类并添加了颜色转换。

看起来是这样的:

class CustomDataGenerator(ImageDataGenerator):
    def __init__(self, color=False, **kwargs):
        super().__init__(preprocessing_function=self.augment_color, **kwargs)

        self.hue = None

        if color:
            self.hue = random.random()

    def augment_color(self, img):
        if not self.hue or random.random() < 1/3:
            return img

        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        img_hsv[:, :, 0] = self.hue

        return cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)

我在ImageDataGenerator 上使用了rescale=1./255,但有些模型需要不同的预处理。

所以当我尝试时

CustomDataGenerator(preprocessing_function=tf.keras.applications.xception.preprocess_input)

我收到此错误:

__init__() got multiple values for keyword argument 'preprocessing_function'

【问题讨论】:

    标签: python tensorflow machine-learning keras


    【解决方案1】:

    问题是,你已经在这里传递了一次preprocessing_function

    super().__init__(preprocessing_function=self.augment_color, **kwargs)
    

    然后再次从

    CustomDataGenerator(preprocessing_function=tf.keras.applications.xception.preprocess_input)
    

    所以现在是这样的

    super().__init__(preprocessing_function=self.augment_color, preprocessing_function=tf.keras.applications.xception.preprocess_input)
    

    删除其中一个就可以了。

    编辑 1: 如果您想保留两者,最好将它们合并到一个预处理方法中并将其作为 preprocessing_function 传递

    在 CustomDataGenerator 中添加以下方法

        def preprocess(self, image):
            image = self.augment_color(image)
            return tf.keras.applications.xception.preprocess_input(image)
    

    将其用作预处理函数

    super().__init__(preprocessing_function=self.preprocess, **kwargs)
    

    【讨论】:

    • 嗯,我为什么要删除它们中的任何一个?我的意思是,我仍然希望完成颜色转换
    • 所以我猜你想要多重预处理?类似的东西在这里stackoverflow.com/questions/52458017/…stackoverflow.com/questions/57890259/…
    • 嗯,基本上我想根据当前模型的 preprocess_input 函数预处理我的数据集。但我也想应用颜色转换
    • 那么最好将这些预处理合并到一个方法中,并且只将其作为 preprocessing_function 传递
    • 但是文档说 preprocessing_function 函数是在所有转换完成后应用的。所以颜色转换将撤消像素值重新缩放/移位
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 2019-11-20
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2016-09-02
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多