ImageDataGenerator的预处理函数在keras上的使用

2024-05-15 00:30:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到有一个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'

Tags: selfimginputinitfunctionrandomcv2hsv
1条回答
网友
1楼 · 发布于 2024-05-15 00:30:51

问题是,您已经在这里传递了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: 如果您想保留这两种方法,最好将它们合并到一个预处理方法中,并将其作为预处理函数传递

将以下方法添加到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)

相关问题 更多 >

    热门问题