【问题标题】:Usage of Data Augmentation in Tensorflow 2Tensorflow 2 中数据增强的使用
【发布时间】:2021-01-14 03:41:20
【问题描述】:

我正在尝试使用 6 月发布的新 TensorFlow 对象检测 API。但是我在使用他们提供的数据增强工具时遇到了一些困难。这是因为他们从 TensorFlow 导入了一个 contrib.image,它只存在于 TF 1.x 中。所以,我的问题是:“任何人都知道如何在 TF 2.x 中使用这个数据增强工具?”。

最好的问候。

【问题讨论】:

    标签: tensorflow tensorflow2.0 data-augmentation


    【解决方案1】:

    您可以在此处的 TensorFlow 网站https://www.tensorflow.org/tutorials/images/data_augmentation 上找到使用 TF 2.x 的数据增强教程。

    您还可以使用 ImageDataGenerator 库在 Tf 2.x 中执行数据扩充。

    tf.keras.preprocessing.image.ImageDataGenerator
    

    imagedatagenerator的示例代码sn-p

    import tensorflow as tf
    image = tf.keras.preprocessing.image.load_img('flower.jpeg')
    
    image_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=40,
                                                             width_shift_range=0.2,
                                                              height_shift_range=0.2,
                                                              rescale=1./255,
                                                              shear_range=0.2,
                                                              zoom_range=0.2,
                                                              horizontal_flip=True,
                                                              fill_mode='nearest')
    #convert image to array
    im_array = tf.keras.preprocessing.image.img_to_array(image)
    img = im_array.reshape((1,) + im_array.shape)
    
    #Generate the images
    count = 0
    for batch in image_datagen.flow(img, batch_size=1, save_to_dir ='image_gen' , save_prefix='flower', save_format='jpeg'):
      count +=1
      if count==5:
        break
    #Input image
        import matplotlib.pylab as plt
        image = plt.imread('flower.jpeg')
        plt.imshow(image)
        plt.show()
    

    #After augmentation
    import matplotlib.pylab as plt
    image = plt.imread('image_gen/flower_0_1167.jpeg')
    plt.imshow(image)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2016-09-28
      • 1970-01-01
      • 2018-10-21
      • 2023-03-22
      • 1970-01-01
      • 2020-07-20
      相关资源
      最近更新 更多