【问题标题】:Create custom datagenerator in Keras using my own dataset使用我自己的数据集在 Keras 中创建自定义数据生成器
【发布时间】:2020-03-25 10:42:06
【问题描述】:

我想在自己的数据集上创建自己的自定义 DataGenerator。我已阅读所有图像并将位置及其标签存储在两个名为 imageslabels 的变量中。我已经编写了这个自定义生成器:

def data_gen(img_folder, y, batch_size):
    c = 0
    n_image = list(np.arange(0,len(img_folder),1)) #List of training images
    random.shuffle(n_image)

    while (True):
        img = np.zeros((batch_size, 224, 224, 3)).astype('float')   #Create zero arrays to store the batches of training images
        label = np.zeros((batch_size)).astype('float')  #Create zero arrays to store the batches of label images

        for i in range(c, c+batch_size): #initially from 0 to 16, c = 0.

            train_img = imread(img_folder[n_image[i]])
            # row,col= train_img.shape
            train_img = cv2.resize(train_img, (224,224), interpolation = cv2.INTER_LANCZOS4)
            train_img = train_img.reshape(224, 224, 3)
#            binary_img = binary_img[:,:128//2]

            img[i-c] = train_img #add to array - img[0], img[1], and so on.

            label[i-c] = y[n_image[i]]

        c+=batch_size
        if(c+batch_size>=len((img_folder))):
            c=0
            random.shuffle(n_image)
                      # print "randomizing again"
        yield img, label

我想知道的是如何向此生成器添加其他增强功能,例如 flipcroprotate?此外,我应该如何yield 这些扩充,以便它们与正确的标签相关联。

请告诉我。

【问题讨论】:

    标签: python-3.x keras deep-learning data-augmentation data-generation


    【解决方案1】:

    您可以在train_img 上添加flipcroprotate,然后再将其放入img。也就是说,

        # ....
        While(True):
            # ....
    
            # add your data augmentation function here
            train_img = data_augmentor(train_img)
    
            img[i-c] = train_img
    
            # ....
    

    【讨论】:

    • 所以你是在暗示train_img 本身就是一个增强图像的list
    • 是的,当然,为了保持某些图像不变,您可以掷硬币来确定它是否每次都使用数据增强。
    猜你喜欢
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2018-10-23
    • 2019-09-28
    • 2021-03-07
    • 2022-01-25
    相关资源
    最近更新 更多