pythonkeras:图像读取和加载
我想从多个目录中读取图像。我使用了Keras图像数据生成器。在应用了预处理函数之后,现在我想在单个列表中加载整个数据。我尝试了以下代码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
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命令。那也太慢了。
谢谢。每次运行评估代码时,您可以尝试两种方法来加快流程
(无法评论)您可以尝试
ImageDataGenerator(重缩放=1/255)
谢谢。将文件保存为npy数组并使用它是一次性工作。你的第二个选择对我有用!