我在网上找到了此代码(请参阅评论中的链接),并根据自己的目的对其进行了修改。
非常慢。我要加载约20K的图像。
我该如何加快速度?
# https://medium.com/@ashok.tankala/build-the-mnist-model-with-your-own-handwritten-digits-using-tensorflow-keras-and-python-f8ec9f871fd3
# To load images to features and labels
def load_images_to_data(image_label, image_directory, features_data, label_data):
list_of_files = os.listdir(image_directory)
print('Load: '+repr(image_directory)+' Count: '+str(len(list_of_files)))
for file in sorted(list_of_files):
image_file_name = os.path.join(image_directory, file)
if ".png" in image_file_name:
img = Image.open(image_file_name).convert("RGB")
im2arr=np.asarray(img)
im2arr = im2arr.reshape(1, 224, 224, 3)
if features_data.size ==0:
features_data = im2arr
label_data = [image_label]
else:
features_data = np.append(features_data, im2arr, axis=0)
label_data = np.append(label_data, [image_label], axis=0)
return features_data, label_data