【问题标题】:Best way to save extracted features for future training deep learning为未来训练深度学习保存提取特征的最佳方法
【发布时间】:2020-09-15 22:28:56
【问题描述】:

我正在使用 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,但我遇到了将列表输入深度学习模型的问题。如何以正确的格式保存这些数据?

【问题讨论】:

标签: python tensorflow keras deep-learning


【解决方案1】:

我有两个建议:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 2019-10-20
    • 2018-01-09
    • 1970-01-01
    • 2019-12-30
    • 2014-06-03
    • 2017-07-23
    • 2021-05-05
    相关资源
    最近更新 更多