【问题标题】:How to obtain same augmented images using ImageDataGenerator in keras or tensorflow?如何在 keras 或 tensorflow 中使用 ImageDataGenerator 获得相同的增强图像?
【发布时间】:2018-09-14 03:58:53
【问题描述】:

我正在处理grayscale 大小为75 by 75 的图像,并希望使用ImageDataGenerator 执行一些增强技术。

但是想知道如果我们多次运行是否可以重复output consistently。我不是在谈论时代,而是喜欢运行整个代码来模仿完全相同的增强图像以获得相同的结果。

我附上sample grayscale image:

import matplotlib.pyplot as plt
import numpy as np
from scipy import misc, ndimage
from keras.preprocessing.image import ImageDataGenerator

gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, 
       height_shift_range=0.1, zoom_range=0.1, # shear_range=0.15, 
       channel_shift_range=10., horizontal_flip=True, vertical_flip = True,
       rescale = 0.2, fill_mode = 'wrap')

image_path = '/trial_img.png' # grayscale image

# Obtain image
# data_format = [#num_images,height,width,#num_of_channels]
# where, #num_images = 1 and #num_of_channels = 1, height = width = 75
image = np.expand_dims(ndimage.imread(image_path),0) # add num_images dimension
image = np.expand_dims(image, axis=3) # add num_of_channels dimension
plt.imshow(image.reshape(75,75), cmap = 'gray')

# Trial #1
# Generate batches of augmented images from this image
aug_iter = gen.flow(image)
# Get 10 samples of augmented images
aug_images1 = [next(aug_iter)[0].reshape(75,75).astype(np.uint8) for i in range(10)]

# Trial #2
aug_iter = gen.flow(image)
aug_images2 = [next(aug_iter)[0].reshape(75,75).astype(np.uint8) for i in range(10)]

# check if equal
truth = []
for val in range(10):
    truth.append((aug_images1[val] == aug_images2[val]).all()) # check images
np.asarray(truth).all() # check if all images are same

如何在上述代码中一致地重复增强的输出?

我知道这段代码写得很糟糕,任何关于代码优化的建议也非常感谢。

谢谢,

戈皮

【问题讨论】:

    标签: python image-processing tensorflow keras


    【解决方案1】:

    您可以将seed 设置为flow 方法:

    aug_iter = gen.flow(image, seed = 0)
    

    通过将此参数设置为特定整数,您将始终获得相同的随机洗牌/转换序列。

    【讨论】:

      【解决方案2】:

      您可以运行生成器并保存图像,然后只需加载图像:

      # Trial #1
      # Generate batches of augmented images from this image
      aug_iter = gen.flow(image)
      # Get 10 samples of augmented images
      aug_images1 = [next(aug_iter)[0].reshape(75,75).astype(np.uint8) for i in range(10)]
      

      如果内存不是问题,可以用 numpy 保存:

      aug_images1 = np.array(aug_images1)
      np.save(filename, aug_images1)
      

      然后加载它:

      aug_images1 = np.load(filename)
      

      如果您愿意,您可以使用 Pillow 等图像库将每个图像保存为适当的图像文件(占用更少的内存):

      from PIL import Image
      
      for (im,filename in zip(aug_images1,list_of_names)):
          im = Image.fromarray(im) #make sure you have a uint8 from 0 to 255 array.
          im.save(filename)
      

      稍后,加载文件:

      aug_images1 = [np.array(image.open(filename)) for filename in list_of_names]
      aug_images1 = np.array(aug_images1)
      

      使用 ImageDataGenerator 加载文件

      如果您不想一次将所有图像加载到内存中,使用保存的图像,您可以创建一个新的ImageDataGenerator,但无需任何增强,只是一个纯图像加载器。

      然后使用gen.flow_from_directory()从目录中获取图片。

      在文档中阅读更多信息:https://keras.io/preprocessing/image/

      【讨论】:

      • 这是获得一致图像的好方法,但我想在运行时创建它们。谢谢你的回答,需要的时候我也会用这种方式。
      猜你喜欢
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多