我正在使用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,但我遇到了将列表反馈到深度学习模型的问题。如何以正确的格式保存此数据?
发布于 2020-10-27 15:20:02
我有两个建议:
按文件保存特征,例如对于cat.png,将其另存为cat.npy;在浏览文件列表(cat.png、dog.png、snake.png)时,首先检查特征是否已创建,然后直接加载.npy文件。
第二种方法是使用字典数据结构,其中使用样本的索引作为关键字,并将提取的特征作为值。示例:
index = 123
feature = extract_feature(...)
dictionary[index] = feature
您可以将此词典保存到pickle。然后在下次加载它,并直接从字典中提取索引的特征。
https://stackoverflow.com/questions/63910662
复制相似问题