1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > matlab读取.mb15格式数据 两分钟搞定Python读取matlab的.mat数据

matlab读取.mb15格式数据 两分钟搞定Python读取matlab的.mat数据

时间:2022-01-24 07:42:37

相关推荐

matlab读取.mb15格式数据 两分钟搞定Python读取matlab的.mat数据

Matlab是学术界非常受欢迎的科学计算平台,matlab提供强大的数据计算以及仿真功能。在Matlab中数据集通常保存为.mat格式。那么如果我们想要在Python中加载.mat数据应该怎么办呢?所以今天就给大家分享一个使用python加载.mat数据的方法。我将使用Stanford Cars Dataset数据集作为例子为大家演示使用方法。数据集

Stanford Cars Dataset数据集是一个关于车辆图像分类的数据集,该数据集保存格式为.mat形式。数据及下载地址为:

https://ai.stanford.edu/~jkrause/cars/car_dataset.html

加载.mat文件

Scipy是一个非常流行的用于科学计算的python库,很自然地,它们有一种方法可以让你读入.mat文件。阅读它们绝对是一件容易的事。您可以在一行代码中完成它:

fromscipy.ioimportloadmat

annots=loadmat('cars_train_annos.mat')

格式化数据

通过loadmat方法加载数据后会返回一个Python字典的数据结构,我们可以查看数据关键字,代码如下:

annots.keys()

>dict_keys(['__header__','__version__','__globals__','annotations'])

下边是关于数据集描述的文档,从中我们可以查看关于数据及更详细的描述,也可以验证通过Python加载后数据是否正确。

Thisfilegivesdocumentationforthecars196dataset.

(http://ai.stanford.edu/~jkrause/cars/car_dataset.html)

————————————————————

Metadata/Annotations

————————————————————

Descriptionsofthefilesareasfollows:

-cars_meta.mat:

Containsacellarrayofclassnames,oneforeachclass.

-cars_train_annos.mat:

Containsthevariable‘annotations’,whichisastructarrayoflength

num_imagesandwhereeachelementhasthefields:

bbox_x1:Minx-valueoftheboundingbox,inpixels

bbox_x2:Maxx-valueoftheboundingbox,inpixels

bbox_y1:Miny-valueoftheboundingbox,inpixels

bbox_y2:Maxy-valueoftheboundingbox,inpixels

class:Integralidoftheclasstheimagebelongsto.

fname:Filenameoftheimagewithinthefolderofimages.

-cars_test_annos.mat:

Sameformatas‘cars_train_annos.mat’,excepttheclassisnotprovided.

————————————————————

Submissionfileformat

————————————————————

Filesforsubmissionshouldbe.txtfileswiththeclasspredictionfor

imageMonlineM.NotethatimageMcorrespondstotheMthannotationin

theprovidedannotationfile.Anexampleofafileinthisformatis

train_perfect_preds.txt

Includedinthedevkitareascriptforevaluatingtrainingaccuracy,

eval_train.m.Usageis:

(inMATLAB)

>>[accuracy,confusion_matrix]=eval_train(‘train_perfect_preds.txt’)

Ifyourtrainingpredictionsworkwiththisfunctionthenyourtesting

predictionsshouldbegoodtogofortheevaluationserver,assuming

thatthey’reinthesameformatasyourtrainingpredictions.

从文档中可以看到,annotations变量中包含我们想要的结构数据,包括标签、图像文件名以及图像边界框信息,因此我们只需处理annotations变量并从中提取我们想要的信息。

type(annots[‘annotations’]),annots[‘annotations’].shape

>(numpy.ndarray,(1,8144))

type(annots['annotations'][0][0]),annots['annotations'][0][0].shape

>(numpy.void,())

从.mat中提取的数据以numpy.ndarray格式存储,此数组中的项的数据类型是numpy.void。

annots[‘annotations’][0][0][‘bbox_x1’],annots[‘annotations’][0][0][‘fname’]

>(array([[39]],dtype=uint8),array(['00001.jpg'],dtype='

接下来我们通过循环将字典中的annotations变量信息提取出来,并将它们存储在列表中:

[item.flat[0]foriteminannots[‘annotations’][0][0]]

>[39,116,569,375,14,'00001.jpg']

将数据转换成Pandas Dataframe

现在我们用python加载好matlab数据文件,为方便后续的处理,我们将数据转换为pandas格式。转换过程十分简单,具体代码如下:

data=[[row.flat[0]forrowinline]forlineinannots[‘annotations’][0]]

columns=[‘bbox_x1’,‘bbox_y1’,‘bbox_x2’,‘bbox_y2’,‘class’,‘fname’]

df_train=pd.DataFrame(data,columns=columns)

转换后数据形式如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。