我已经使用 matlab 创建了几个字典,并使用
.mat
标志将它们保存为 "-v7.3"
文件(因为它被建议作为在 python 中读取 .mat
文件的一种方式)
我在 Matlab 中的代码是:
cnn6 = load("imgnet19k_less_feature_map_20230412_alexnet_cnn6_dict.mat");
dictionary_analyze = cnn6.dictionary_imagenames_features;
keys_in_order = keys(dictionary_analyze);
values_in_order = values(dictionary_analyze);
save("cnn6_keys_in_order_imagenet_19k_v2.mat", "keys_in_order", '-v7.3');
save("cnn6_values_in_order_imagenet_19k_v2.mat", "values_in_order", '-v7.3');
字典的结构是这样的:
在python中,我尝试了以下代码来读取这些
.mat
文件:
import os
import sys
import pickle
import numpy as np
import h5py
import tables
from scipy.io import loadmat
normal_semantic_features_dir = "./normal_semantic_features/"
alexnet_features = ['cnn2', 'cnn4', 'cnn6', 'cnn8']
#dict_cnns = h5py.File(normal_semantic_features_dir + 'cnn2_keys_in_order_imagenet_19k_v2.mat', "r")
f1 = h5py.File(normal_semantic_features_dir + 'cnn8_keys_in_order_imagenet_19k_v2.mat', "r")
f2 = h5py.File(normal_semantic_features_dir + 'cnn8_values_in_order_imagenet_19k_v2.mat', "r")
array_of_keys = dict_cnns['keys_in_order']
print(list(f1.keys()))
结果:
['#refs#', '#subsystem#', 'keys_in_order']
a = f1["keys_in_order"]
type(a)
h5py._hl.dataset.Dataset
for k, v in annots.items():
print(k,"........" ,annots[k])
print("....")
header ....... b'MATLAB 5.0 MAT 文件,平台:PCWIN64,创建于:2023 年 4 月 14 日星期五 13:32:47' .... 版本 ....... 1.0 .... globals ....... [] ....无....... [(b'keys_in_order',b'MCOS',b'string',数组([[3707764736], , , , , ], dtype=uint32))] .... function_workspace ....... [[ 0 1 73 ... 0 0 0]]
我仍然很困惑我应该如何读取这些 mat 文件的内容,并将它们转换为 python 风格的字典。我感谢任何可以帮助我做到这一点的示例代码。