python - 为未来训练深度学习保存提取特征的最佳方法
问题描述
我正在使用 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,但我遇到了将列表输入深度学习模型的问题。如何以正确的格式保存这些数据?
解决方案
我有两个建议:
保存每个文件的特征,例如 cat.png 将其保存为 cat.npy;当您浏览文件列表(cat.png、dog.png、snake.png)时,首先检查该功能是否已经创建并直接加载 .npy 文件。
第二种方法是使用字典数据结构,使用样本的索引作为键,提取的特征作为值。例子:
index = 123
feature = extract_feature(...)
dictionary[index] = feature
您可以将此字典保存到pickle。然后下次加载它,并直接从字典中提取索引的特征。
推荐阅读
- r - e1071 svm() 函数的最大迭代次数是多少?
- wso2 - WSO2-IS:用户存储在任何更改后删除
- javascript - 尝试在 nuxt 中使用库时获取意外令牌 {
- java - 前台服务在 Android 9 (API 28) 中不起作用
- flutter - 更新的值在 Widget 重绘时被覆盖
- r - 用返回而不是R中的打印来抑制消息?
- email - 带有 gmail smtp 的 Jenkins 电子邮件通知
- php - Libxml_disable_entitiy_loader(false); 仍然没有加载外部实体
- python - 如何将减号转换为 - 内部用户输入?
- c++ - weak_ptr 重置会影响 shared_ptr?