Создание собственного генератора данных в Keras с использованием моего собственного набора данных - PullRequest
0 голосов
/ 25 марта 2020

Я хочу создать свой собственный DataGenerator на своем собственном наборе данных. Я прочитал все изображения и сохранил местоположения и их метки в двух переменных с именами images и labels. Я написал этот собственный генератор:

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

Что я хочу знать, так это как я могу добавить другие дополнения, такие как flip, crop, rotate к этому генератору? Более того, как мне yield эти дополнения, чтобы они были связаны с правильной меткой.

Пожалуйста, дайте мне знать.

1 Ответ

1 голос
/ 25 марта 2020

Вы можете добавить flip, crop, rotate на train_img, прежде чем поместить его в img. То есть

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

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

        img[i-c] = train_img

        # ....
...