【问题标题】:Tensorflow auto split imageTensorFlow 自动分割图像
【发布时间】:2020-03-23 11:31:12
【问题描述】:

假设我有这样的目录。

full_dataset
|---horse <= 40 images of horse
|---donkey <= 50 images of donkey
|---cow <= 80 images of cow
|---zebra <= <= 30 images of zebra

然后我用 tensorflow 写这个

image_generator = ImageDataGenerator(rescale=1./255)    
my_dataset = image_generator.flow_from_directory(batch_size=32,
                                                 directory='full_dataset',
                                                 shuffle=True,
                                                 target_size=(280, 280),
                                                 class_mode='categorical')

但我想自动拆分该文件,而无需手动将目录更改为训练文件夹和测试文件夹。我不想像https://www.tensorflow.org/tutorials/images/classification那样手动拆分它)

我做了什么,失败了

(x_train, y_train),(x_test, y_test) = my_dataset.load_data()

【问题讨论】:

    标签: tensorflow image-processing train-test-split


    【解决方案1】:

    您不必使用 tensorflow 或 keras 来划分数据集。如果你安装了 sklearn 包,那么你可以简单地使用它:

    from sklearn.model_selection import train_test_split
    X = ...
    Y = ...
    x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)
    

    您也可以将 numpy 用于相同目的:

    import numpy
    X = ...
    Y = ...
    test_size = 0.2
    train_nsamples = (1-test_size) * len(Y)
    x_train, x_test, y_train, y_test = X[:train_nsamples,:], X[train_nsamples:, :], Y[:train_nsamples, ], Y[train_nsamples:,]
    

    在 Keras 中:

    from keras.datasets import mnist
    import numpy as np
    from sklearn.model_selection import train_test_split
    
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x = np.concatenate((x_train, x_test))
    y = np.concatenate((y_train, y_test))
    
    train_size = 0.7
    x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=train_size)
    

    【讨论】:

    • 您好,感谢您的回答,但我很抱歉。当我使用“full_dataset”文件夹中的图像作为输入时如何输入 X 和 Y。如果是熊猫数据框,那就更容易了。但它是图像
    • @Ichsan,使用 X 和 Y 输入的 mnist 数据更新了答案。
    • 如何对我的名为“full_dataset”的文件夹使用“load_data()”函数?如果它使用 Mnist 数据集,它就可以工作。但我想尝试使用我的名为“full_dataset”的文件夹,就像我在问题中显示的那样
    【解决方案2】:

    经过一天的反复试验和挣扎,我找到了解决方案。

    第一路

    import glob
    horse = glob.glob('full_dataset/horse/*.*')
    donkey = glob.glob('full_dataset/donkey/*.*')
    cow = glob.glob('full_dataset/cow/*.*')
    zebra = glob.glob('full_dataset/zebra/*.*')
    
    data = []
    labels = []
    
    for i in horse:   
        image=tf.keras.preprocessing.image.load_img(i, color_mode='RGB', 
        target_size= (280,280))
        image=np.array(image)
        data.append(image)
        labels.append(0)
    for i in donkey:   
        image=tf.keras.preprocessing.image.load_img(i, color_mode='RGB', 
        target_size= (280,280))
        image=np.array(image)
        data.append(image)
        labels.append(1)
    for i in cow:   
        image=tf.keras.preprocessing.image.load_img(i, color_mode='RGB', 
        target_size= (280,280))
        image=np.array(image)
        data.append(image)
        labels.append(2)
    for i in zebra:   
        image=tf.keras.preprocessing.image.load_img(i, color_mode='RGB', 
        target_size= (280,280))
        image=np.array(image)
        data.append(image)
        labels.append(3)
    
    data = np.array(data)
    labels = np.array(labels)
    
    from sklearn.model_selection import train_test_split
    X_train, X_test, ytrain, ytest = train_test_split(data, labels, test_size=0.2,
                                                    random_state=42)
    

    第二种方式

    image_generator = ImageDataGenerator(rescale=1/255, validation_split=0.2)    
    
    train_dataset = image_generator.flow_from_directory(batch_size=32,
                                                     directory='full_dataset',
                                                     shuffle=True,
                                                     target_size=(280, 280), 
                                                     subset="training",
                                                     class_mode='categorical')
    
    validation_dataset = image_generator.flow_from_directory(batch_size=32,
                                                     directory='full_dataset',
                                                     shuffle=True,
                                                     target_size=(280, 280), 
                                                     subset="validation",
                                                     class_mode='categorical')
    

    第二种方式的主要缺点,您不能用于显示图片。写validation_dataset[1]会出错。但如果我使用第一种方法,它会起作用:X_test[1]

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2016-07-23
      • 2018-11-14
      • 1970-01-01
      • 2022-10-17
      • 2021-03-26
      • 2019-02-06
      • 2015-12-18
      • 2016-12-03
      相关资源
      最近更新 更多