对于python来说,当然可以直接使用scipy包中的loadmat方法导入
from scipy.io import loadmatmat = loadmat(r"D:/dataset/ModelNet/traindata.mat")
这里的数据是对于ModelNet-10数据集单视角下的处理结果。
这里我们尝试将mat文件转换成py文件,其具有与cifar10相同的形式。
"""This file is to translate the .mat file to .py file."""import numpy as npimport pickleimport h5py# data pathtrainpath = 'D:/dataset/ModelNet/traindata.mat'# open filesfile = h5py.File(trainpath, 'r')# the data is transposed via h5py.# get the data and labelsdata = file['testdata'][:]#print(data.shape)label = file['testlabel'][:]# tanpose, if neededdata = np.transpose(data)#label = label.astype(np.int32)data_len = len(data)# create new inter_data =[[]]inter_label = np.zeros((data_len,))# data tranferfor i in range(data_len):img = data[i, :]if inter_data == [[]]:inter_data = [img]else:inter_data = np.concatenate((inter_data, [img]), axis = 0)inter_label[i] = label[0, i]# let the label to int typeinter_label = inter_label.astype(np.int32)# 创建字典来进行保存inter_dict = {'data': inter_data, 'label': inter_label}f_train = open('D:/dataset/ModelNet/train','wb')pickle.dump(inter_dict, f_train)
如此保存的文件可直接放入tensorflow或者keras构建的网络中。