使用我自己的数据集在Keras中创建自定义数据生成器

我想在自己的数据集上创建自己的自定义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,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,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进行这些扩充,以便它们与正确的标签链接。

请让我知道。

czq40034234 回答:使用我自己的数据集在Keras中创建自定义数据生成器

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

    # ....
    While(True):
        # ....

        # add your data augmentation function here
        train_img = data_augmentor(train_img)

        img[i-c] = train_img

        # ....
本文链接:https://www.f2er.com/2584007.html

大家都在问