0

我一直在尝试使用 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 个图像有效。有任何想法吗?

另外我不确定图像的名称是什么意思,它看起来像是一个随机生成的数字,但不确定它是在哪里定义的。

谢谢你的帮助

4

1 回答 1

0

所以我设法得到了解决方案。我必须将我的每个图像转换为一个大小为(1、行、列、通道)的数组,然后,如果有意义的话,对该数组中的每个图像(始终为 1)进行扩充。起初我有一个 for 循环来循环浏览目录中的所有图像并制作一个大数组(total_images、rows、cols、channels),然后我在完成后扩充了该数组。出于某种原因,它不会循环遍历整个数组,它只会执行前几张图像。所以我将 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')

它按我想要的方式工作,但我知道它确实效率低下,我相信有更好的方法。因此,仍然欢迎其他答案。

于 2018-09-07T01:16:12.183 回答