我一直在尝试使用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数组中的前两个图像,而不翻转其余的图像。其余的变成空白。有关示例,请参见here。我已经研究过this post和this one关于在.flow()上进行迭代的问题,但是仍然不确定为什么当我在.flow()上进行迭代时为什么只有2张图像有效。有任何想法吗?
我也不确定图像名称的含义,它看起来像是随机生成的数字,但不确定在何处定义。
感谢您的帮助
答案 0 :(得分:0)
所以我设法找到了解决方案。我必须将每个图像转换成一个大小为数组(1,行,列,通道)的数组,然后,如果有意义的话,对该数组中的每个图像(始终为1)进行扩充。最初,我有一个for循环来循环浏览目录中的所有图像,并制作一个大数组(total_images,行,cols,通道),然后完成该数组后就对其进行扩充。由于某种原因,它不会遍历整个数组,因此只会执行前几个图像。所以我将for循环更改为:
#image-wise
for imgname in range(1, len(imgs))
imgdatas = np.ndarray((1,208,336,1), dtype=np.uint8) # size of array to always contain 1 image
imglabels = np.ndarray((1,208,336,1), dtype=np.uint8)
img = load_img(OG_PATH + '/(%d).png' %(imgname), grayscale = True)
label = load_img(GT_PATH + '(%d).png' %(imgname), grayscale = True)
#convert images to arrays
img = img_to_array(img)
label = img_to_array(label)
#append to one big array
imgdatas[i] = img
imglabels[i] = label
#apply morph to og images
print('saving to file')
seed = 1
a = 0
for batch in morph_img.flow(
imgdatas,
batch_size = 1,
save_to_dir = 'morphed_og_path/',
save_prefix = str(imgname),
save_format = 'png',
seed = seed): # I added the seed as well so my originals and labels were being augmented the same way
a+=1
if a > 20:
break
print ('done with the OGs')
#apply morph to label images
b = 0
for batch in morph_label.flow(
imglabels,
batch_size = 1,
save_to_dir = 'morphed_labels_path/',
save_prefix = str(imgname),
save_format = 'png',
seed = seed):
b+=1
if b > 20:
break
print('done with labels')
它可以按照我想要的方式工作,但是我知道它的效率很低,而且我相信还有更好的方法。因此,仍然欢迎其他答案。