首页 > 解决方案 > 为未来训练深度学习保存提取特征的最佳方法

问题描述

我正在使用 VGG19 架构从我的图像中提取特征。这是我这样做的代码:

model = VGG19(include_top=False)
image_paths = glob.glob('train/*/*')

def extract_features(model, path):
  img_path = path
  img = image.load_img(img_path, target_size=(224,224))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)
  x = preprocess_input(x)
  features = model.predict(x)

for path in image_paths:
  extract_features(model, path)

我想将每个功能保存为我以后可以在torch或tf中使用它进行深度学习的格式。通常,我只会将每个附加features到一个列表中并将该列表保存为 csv,但我遇到了将列表输入深度学习模型的问题。如何以正确的格式保存这些数据?

标签: pythontensorflowkerasdeep-learning

解决方案


我有两个建议:

保存每个文件的特征,例如 cat.png 将其保存为 cat.npy;当您浏览文件列表(cat.png、dog.png、snake.png)时,首先检查该功能是否已经创建并直接加载 .npy 文件。

第二种方法是使用字典数据结构,使用样本的索引作为键,提取的特征作为值。例子:

index = 123
feature = extract_feature(...)
dictionary[index] = feature

您可以将此字典保存到pickle。然后下次加载它,并直接从字典中提取索引的特征。


推荐阅读