【问题标题】:python keras: Image reading and loadingpython keras:图像读取和加载
【发布时间】:2020-06-08 05:55:40
【问题描述】:

我想从多个目录中读取图像。为此,我使用了 Keras Image 数据生成器。应用预处理功能后,现在我想将整个数据加载到一个列表中。我尝试执行以下代码。

def preProcess(X):
    X = X.astype('float32')
    X = (X - 127.5) / 127.5
    return X

batch_sz=32
path = "/content/drive/My Drive/new_net/mini_unet_data/labeled/"
test_datagen = ImageDataGenerator(preprocessing_function=preProcess)
test_gen = test_datagen.flow_from_directory(directory=path,target_size=(256,256),batch_size=32,color_mode="rgb",class_mode="sparse",shuffle=True,seed=42)
test_images=[]
test_labels=[]


for i in range(int(test_gen.n/batch_sz)):
    tmp1, tmp2 = test_gen.next()
    test_images.append(tmp1)
    test_labels.append(tmp2)
test_images = np.asarray(test_images)
test_labels = np.asarray(test_labels)

test_images = np.reshape(test_images,(test_images.shape[0]*test_images.shape[1],test_images.shape[2],test_images.shape[3],test_images.shape[4]))
test_labels = np.squeeze(np.reshape(test_labels,(test_labels.shape[1]*test_labels.shape[0])))

上面的代码可以正常工作。但是加载大约 1500 张图像需要时间(超过 10 分钟)。那么有没有更好的方法来更快地实现这一目标?我尝试使用 glob 和 open cv 命令。这也太慢了。 谢谢。

【问题讨论】:

  • 你可以试试ImageDataGenerator(rescale=1/255)

标签: python numpy keras


【解决方案1】:

每次运行评估代码时,您可以尝试两种方法来加快您的流程。

  1. 将图像转换为 .npy 格式。使用 cv2 读取,然后将其从 BGR 更改为 RGB 并保存。
  2. 您可以生成整个测试集的 npy 数组,然后使用预测/评估生成器。

(无法评论)

【讨论】:

  • 谢谢。将文件保存为 npy 数组然后使用它是一次性的工作。您的第二个选项对我有用!
猜你喜欢
  • 1970-01-01
  • 2017-06-11
  • 2016-09-02
  • 2017-11-05
  • 2021-12-23
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多