我一直在尝试使用 ImageDataGenerator 来增加一些训练数据和相应的标签。
这是我处理它的方式(如果格式有点不对,请道歉)
def create_morph():
i = 0
img_type = 'png'
#get the path to all the images to be morphed
print ('getting morph path...')
imgs = glob(OG_PATH + "/*." + img_type)
#check how many images are in the morph path
print('length of imgs')
print(len(imgs))
#make two identical structured numpy arrays (num of images, rows, cols, binary). This is for loading into later
rows = 208
cols = 336
imgdatas = np.ndarray((len(imgs),rows,cols,1), dtype=np.uint8)
imglabels = np.ndarray((len(imgs),rows,cols,1), dtype=np.uint8)
#image-wise
for imgname in imgs:
print('inside for-loop')
midname = imgname[imgname.rindex("/")+1:]
img = load_img(OG_PATH + "/" + midname,grayscale = True)
label = load_img(GT_PATH + "/" + midname,grayscale = True)
#convert images to arrays
img = img_to_array(img)
label = img_to_array(label)
#make a big npy array
imgdatas[i] = img
imglabels[i] = label
if i % 100 == 0:
print('Done: {0}/{1} images'.format(i, len(imgs)))
i += 1
#setup the morph paramaters
morphData = dict(
horizontal_flip = True,
vertical_flip = True)
#assign the morphing to each label and og image
morph_img = ImageDataGenerator(**morphData)
morph_label = ImageDataGenerator(**morphData)
#apply morph to og images
print('saving to file')
a = 0
b = 0
for batch in morph_img.flow(
imgdatas,
save_to_dir = MORPHED_PATH + '/augment_results_im/',
batch_size = 1,
save_prefix = 'batch',
save_format = 'png'):
a+=1
if a > len(imgdatas):
break
print ('done with the OGs')
#apply morph to label images
for batch in morph_label.flow(
imglabels,
save_to_dir = MORPHED_PATH + '/augment_results_labels/',
batch_size = 1,
save_prefix = 'batch',
save_format = 'png'):
b+=1
if b > len(imgdatas):
break
print('done with labels')
这段代码对我有用,就像我得到翻转的图像一样,但我遇到的问题是它只会翻转前两个图像,而不是我的 imgdatas 和 imglabels 数组中的其余图像。其余的都是空白的。有关示例,请参见此处。我查看了这篇文章和这篇关于迭代 .flow() 的文章,但仍然不确定为什么当我迭代 .flow() 时只有 2 个图像有效。有任何想法吗?
另外我不确定图像的名称是什么意思,它看起来像是一个随机生成的数字,但不确定它是在哪里定义的。
谢谢你的帮助