iOS CoreML 模型转换工具coremltools(一)

2017-06-13  本文已影响0人  RichardLH

翻译自:http://pythonhosted.org/coremltools/index.html 

coremltools

Core ML 是苹果提供的一个易于集成到app中的机器学习库. 它目前支持iOS, watchOS,macOS 和 tvOS. Core ML 引入了公共文件格式 (.mlmodel) ,它支持机器学习方法包括深度神经网络 (卷积和循环), 基于树的集合 (boosted trees, 随机森林, 决策树) 和广义 线性模型. Core ML 名可以直接集成到 Xcode中.

coremltools是一个Python工具包,用于:

将由知名的机器学习工具(包括 Keras, Caffe, scikit-learn, libsvm 和 XGBoost)训练的模型 转换为 Core ML 格式的模型(.mlmodel).

采用简单的API 编写  Core ML 格式模型.

做预测.

安装

coremltools依赖以下库:

numpy (1.12.1+)

protobuf (3.1.0+)

此外, 如果你想转换第三方的训练模型,那么请安装以下依赖库:

Keras (==1.2.2) with Tensorflow (1.0.x, 1.1.x)

Xgboost (0.6+)

scikit-learn (0.15+)

libSVM

安装coremltools参考standard python package installation steps. 假如你已经安装python, 执行:

pipinstall-Ucoremltools

即可安装coremltools.

模型转换

coremltools 使用支持的库来便捷的转换模型. 以下案例 展示了 如何将 Caffe 模型 (Inception) 转换为 Core ML格式 (.mlmodel)

支持的文件:bvlc_alexnet.caffemodel,deploy.prototxt,class_labels.txt

importcoremltools# Convert a caffe model to a classifier in Core MLcoreml_model=coremltools.converters.caffe.convert(('bvlc_alexnet.caffemodel','deploy.prototxt'),predicted_feature_name='class_labels.txt')# Now save the modelcoreml_model.save('BVLCObjectClassifier.mlmodel')

Here is another example with scikit-learn:

fromsklearn.linear_modelimportLinearRegressionimportpandasaspd# Load datadata=pd.read_csv('houses.csv')# Train a modelmodel=LinearRegression()model.fit(data[["bedroom","bath","size"]],data["price"])# Convert and save the scikit-learn modelimportcoremltoolscoreml_model=coremltools.converters.sklearn.convert(model,["bedroom","bath","size"],"price")

模型接口

模型转换完, 你可以编辑模型的元数据,这些信息可以在XCode中展示出来. 许可证信息, 作者信息和其他的信息以及输入和输出描述信息.

# Set model metadatacoreml_model.author='John Smith'coreml_model.license='BSD'coreml_model.short_description='Predicts the price of a house in the Seattle area.'# Set feature descriptions manuallymodel.input_description['bedroom']='Number of bedrooms'model.input_description['bathrooms']='Number of bathrooms'model.input_description['size']='Size (in square feet)'# Set the output descriptionsmodel.output_description['price']='Price of the house'# Save the modelmodel.save('HousePricer.mlmodel')

模型评估

模型转换完毕, 你可以使用Core ML 验证预测的结果并同原始模型进行对比. 为了采用代码的方式进行验证, 我们提供了一个简便的模型评估方式.

以下案例 我们采用已经转换后的HousePricer.mlmodel来做预测:

importcoremltools# Load the modelmodel=coremltools.models.MLModel('HousePricer.mlmodel')# Make predictionspredictions=model.predict({'bedroom':1.0,'bath':1.0,'size':1240})

支持的转换

Core ML 支持许多集成工具训练模型的转换. 下表列出了模型类型支持的转换工具:

模型 类型支持的工具包

神经网络Keras (1.2.2), Caffe 1.0

基于树的集合XGboost (0.6), scikit-learn 0.18.1

广义线性回归scikit-learn (0.18.1)

支持向量机libSVM (3.22), scikit-learn (0.18.1)

特征工程scikit-learn (0.18.1)

Pipelinesscikit-learn (0.18.1)

模型规范

Core ML的重要组件是表示机器学习模型的公共规范. 这个规范定义在protobuf文件中,可以由protobuf支持的语言创建并使用(例如.Python, C++,Java, C#, Perl, 等等).

在上层,  protobuf 规范由以下组成:

模型描述: 编码模型的输入和输出名称和类型信息.

模型参数: 代表一个特定实例模型的参数集合.

元数据: 模型的信息包括 来源, 许可证 和作者.

内容

Converters

Models

Utilities

上一篇下一篇

猜你喜欢

热点阅读