【问题标题】:Slow image opening python, recommendation for increased speed?python打开图像慢,建议提高速度?
【发布时间】:2016-07-14 16:21:22
【问题描述】:

我正在做一些非常基本的图像增强来训练卷积网络,而且速度非常慢。我想知道是否有人对在 python 中打开、翻转和关闭图像的更快方法有建议?它有大约 100,000 张图像要处理,需要几个小时。

print 'Example of image in train.txt: ' + image_file[0]
print 'Example of annotation in train.txt: ' + annot_file[0]
train_file.close()

for i in range(len(image_file)):
    temp_image = imread(image_file[i])
    temp_annot = imread(annot_file[i])

    temp_image_name = image_file[i][:-4] + '_augmented_lrflip.png'
    temp_annot_name = annot_file[i][:-4] + '_augmented_lrflip.png'
    imsave(temp_image_name,np.fliplr(temp_image))
    imsave(temp_annot_name,np.fliplr(temp_annot))

    image_file.append(temp_image_name)
    annot_file.append(temp_annot_name)

    temp_image_name = image_file[i][:-4] + '_augmented_lr_ud_flip.png'
    temp_annot_name = annot_file[i][:-4] + '_augmented_lr_ud_flip.png'
    imsave(temp_image_name,np.fliplr(np.flipud(temp_image)))
    imsave(temp_annot_name,np.fliplr(np.flipud(temp_annot)))

    image_file.append(temp_image_name)
    annot_file.append(temp_annot_name)

    temp_image_name = image_file[i][:-4] + '_augmented_udflip.png'
    temp_annot_name = annot_file[i][:-4] + '_augmented_udflip.png'
    imsave(temp_image_name,np.flipud(temp_image))
    imsave(temp_annot_name,np.flipud(temp_annot))

    image_file.append(temp_image_name)
    annot_file.append(temp_annot_name)

train_file_mod = open('train_augmented.txt', 'wb')
for i in range(len(image_file)):
    train_file_mod.write(image_file[i] + ' ' + annot_file[i] + '\n')

train_file_mod.close()

【问题讨论】:

    标签: python numpy python-imaging-library


    【解决方案1】:

    我建议使用Keras(这是在 Theano 或 TensorFlow 之上的深度学习抽象层)。它已经内置了ImageDataGenerator。您基本上可以使用它从数据集中生成不同的图像(旋转、展开、填充)。

    【讨论】:

    • 我改用了 cv2.imwrite,它的速度要快得多。它似乎解决了我的问题。不过,我会看看 keras 以备将来使用。
    【解决方案2】:

    我会试试 PIL 或 Pillow 包。

    PIL 文档:http://www.pythonware.com/products/pil/

    枕头文档:https://pillow.readthedocs.io/en/3.3.x/

    如果您想将图像转换为 numpy 数组并以这种方式处理它们,这里有一些示例: http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 2019-09-27
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2019-08-21
      相关资源
      最近更新 更多