使用我自己的数据集在 Keras 中创建自定义数据生成器
Create custom datagenerator in Keras using my own dataset
我想在我自己的数据集上创建我自己的自定义 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
这些扩充,以便它们与正确的标签相关联。
请告诉我。
您可以在 train_img
上添加 flip
、crop
、rotate
,然后再将其放入 img
。即,
# ....
While(True):
# ....
# add your data augmentation function here
train_img = data_augmentor(train_img)
img[i-c] = train_img
# ....
我想在我自己的数据集上创建我自己的自定义 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
这些扩充,以便它们与正确的标签相关联。
请告诉我。
您可以在 train_img
上添加 flip
、crop
、rotate
,然后再将其放入 img
。即,
# ....
While(True):
# ....
# add your data augmentation function here
train_img = data_augmentor(train_img)
img[i-c] = train_img
# ....