Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonkeras:图像读取和加载_Python_Numpy_Keras - Fatal编程技术网

pythonkeras:图像读取和加载

pythonkeras:图像读取和加载,python,numpy,keras,Python,Numpy,Keras,我想从多个目录中读取图像。我使用了Keras图像数据生成器。在应用了预处理函数之后,现在我想在单个列表中加载整个数据。我尝试了以下代码 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 = I

我想从多个目录中读取图像。我使用了Keras图像数据生成器。在应用了预处理函数之后,现在我想在单个列表中加载整个数据。我尝试了以下代码

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和opencv命令。那也太慢了。
谢谢。

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

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

    您可以尝试
    ImageDataGenerator(重缩放=1/255)
    谢谢。将文件保存为npy数组并使用它是一次性工作。你的第二个选择对我有用!