Python 保存提取的特征以备将来培训和深入学习的最佳方法
我使用VGG19体系结构从图像中提取特征。以下是我的代码:Python 保存提取的特征以备将来培训和深入学习的最佳方法,python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我使用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.e
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中用于深入学习。通常,我只是将每个
功能添加到一个列表中,并将该列表保存为csv,但我遇到了将列表输入深度学习模型的问题。如何以正确的格式保存此数据?我有两个建议:
保存每个文件的功能,例如,对于cat.png,将其另存为cat.npy;在浏览文件列表(cat.png、dog.png、snake.png)时,首先检查功能是否已经创建,并直接加载.npy文件
第二种方法是使用字典数据结构,其中使用样本的索引作为键,使用提取的特征作为值。例如:
index = 123
feature = extract_feature(...)
dictionary[index] = feature
你可以把这本字典保存到pickle。然后下次加载它,直接从字典中提取索引的功能。如果是numpy:np.save(…)?或者为什么不使用pickle?np.save保存最后的张量是有意义的,但是我想将每个特性添加到什么数据结构中呢?也许您可以在检查点保存模型?