Skip to main content
 首页 » 编程设计

python之图像打开 python 速度慢,建议提高速度

2025年04月02日12mfryf

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

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() 

请您参考如下方法:

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