【问题标题】:Does this piece of code augment the input data?这段代码是否增加了输入数据?
【发布时间】:2020-08-02 10:09:22
【问题描述】:
model=tf.keras.Sequential(
[
    Conv2D(filters=1, kernel_size=(3, 3), activation='relu', strides=1, padding='same'),    
    MaxPooling2D(pool_size=(4, 4)),
    
    Flatten(),
    Dense(6,activation="softmax")
    
])

model.compile(loss='CategoricalCrossentropy',
              optimizer='adam',
              metrics=['accuracy'])

train_datagen = ImageDataGenerator(                                  # Real time data augmentation
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(            # Loads the File which contains the images.
        'train',                                                # Returns x (numpy array containing a batch of images)
        target_size=(150, 150),                                 # and correcponding labels
        class_mode='categorical')

model.fit( train_generator,
        batch_size=100,
        epochs=1)

此代码的输出不提供有关数据增强的任何信息。那么谁能澄清一下这段代码是否增加了可用数据?

【问题讨论】:

  • train_datagen = ImageDataGenerator( # 实时数据增强 rescale=1./255,shear_range=0.2, zoom_range=0.2, Horizo​​ntal_flip=True) 这增强了输入数据

标签: python tensorflow keras conv-neural-network data-augmentation


【解决方案1】:

下面的代码扩充了您编写的输入图像

train_datagen = ImageDataGenerator(# Real time data augmentation
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

但是,没有内置的方法来测试数据是否已被扩充。您只能检查数据的结果以了解它是否被增强并获取您打算获取的任何信息。

例如,了解rescale=1./255 扩充是否有效的一种方法是打印出train_generator 的结果以检查值是否在0 - 1 (normalized)0 - 255 (default) 之间。

image, label = next(train_generator) # return a single iteration batch
print(image.shape) #single batch list, if batch not specified (default is 32)
print(image[0].shape) # single image shape
print(image[0]) # single image output

你会得到这样的输出

这表明调整大小的图像有效,否则你会得到这样的东西

这表明调整大小的图像不适用。 我相信您可以将类似的方法用于您打算确认的其他增强功能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多