CoreML创建自己的.mlmodel的心酸历程,足足摆弄了半天,各种坑。
-
pip install -U coremltools
-
从图也可以看出coremltools包含numpy,six,protobuf,coremltools。 因为权限问题导致失败
- sudo pip install -U coremltools
- 遇到[Permission denied]都要加sudo
- Step: 1 Login in root user.
sudo su root
- Step: 2 Uninstall python-pip package if existing.
apt-get purge -y python-pip
- Step: 3 Download files using wget command(File download in pwd )
wget https://bootstrap.pypa.io/get-pip.py
- Step: 4 Run python file.
python ./get-pip.py
- Step: 5 Finaly exicute installation command.
apt-get install python-pip
好吧,这两个问题遍布整个流程,期间各种小问题我已经不记得了,接下来就开始正文了
- 下载coremltools
sudo pip install -U coremltools
- 下载pip.py文件
sudo python get-pip.py
- 下载scikit-learn
sudo pip install -U numpy scipy scikit-learn
- 下载panda
sudo pip install pandas
准备就绪,开工
- csv文件属性Square_Feet,Price
- 记录文件所存储位置,调用的时候需要
- 导入需要的类
- 调用csv文件
- coremltools转换成.mlmodel文件
- author,license,description描述
- 输入Square_Feet,输出Price,保存
-
import coremltools
-
from sklearn.linear_model import LinearRegression
-
import pandas as pd
- load data
-
data = pd.read_csv('/Users/sansi/Desktop/CoreMLModel/input_data.csv')
- train a model
-
model = LinearRegression()
-
model.fit(data[["Square_Feet"]], data["Price"])
- convert and save the scikit-learn model
-
coreml_model = coremltools.converters.sklearn.convert(model, "Square_Feet", "Price")
- set model metadata
-
coreml_model.author = 'tongle'
-
coreml_model.license = 'BSD'
-
coreml_model.short_description = 'Predicts the price of a house in the Seattle area.'
- set feature descriptions manually
-
coreml_model.input_description['Square_Feet'] = 'Size (in square feet)'
- set the output descriptions
-
coreml_model.output_description['Price'] = 'Price of the house'
- save the mdel
-
coreml_model.save('HousePricer.mlmodel')
如果没有一点python功底,我都要死在电脑前了,哈哈