Keras中的预处理功能不起作用

时间:2018-09-12 01:32:54

标签: python keras image-preprocessing

例如,在我的2D语义分割任务中,标签中的所有像素值都不是0,1,2,而是0,127,255。 所以我只想向标签数据集的ImageDataGenerator添加一个预处理函数,

我的代码:

SEED = 111
batch_size = 2
image_datagen = ImageDataGenerator(
    horizontal_flip=True,
    zca_epsilon=9,
    # fill_mode='nearest',
)
image_generator = image_datagen.flow_from_directory(
    directory="/xxx/images",
    class_mode=None,
    batch_size=batch_size,
    seed=SEED,
)


def preprocessing_function(image):
    # if I have 3 categories, I need to convert 0,10,20 to 0,1,2 for example 
    return image


label_datagen = ImageDataGenerator(
    horizontal_flip=True,
    zca_epsilon=9,
    rescale=1,
    preprocessing_function=preprocessing_function,
    # fill_mode='nearest',
)
label_generator = image_datagen.flow_from_directory(
    directory="/xxx/labels",
    class_mode=None,
    batch_size=batch_size,
    seed=SEED,
)

train_generator = zip(image_generator, label_generator)
print(len(image_generator))
i = 0
for image_batch, label_batch in iter(train_generator):
    print(image_batch.shape, label_batch.shape) # (2, 256, 256, 3) (2, 256, 256, 3)
    print(image_batch.dtype, label_batch.dtype) # float32 float32
    i += 1
    if i == 5:
        break

但似乎我的

  

preprocessing_function(image)

对我的标签数据没有影响。

我以正确的方式使用预处理功能吗?我该如何修复?

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

如果我通过预处理功能标记数据的ImageDataGenerator(),则需要使用:

label_batch = label_datagen.standardize(label_batch)

在我的每个标签批次上激活预处理功能。