[Coremltools] sklearn ๋ชจ๋ธ mlmodel๋กœ convertํ•˜๊ธฐ(Jupyter notebook ์˜ค๋ฅ˜ ํ•ด๊ฒฐ)

2021. 8. 27. 14:50ใ†Machine Learning, Deep Learning/Jupyter Notebook

๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ถ€์ŠคํŠธ์ฝ”์Šค ํŒŒ์ด๋„ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋จธ์‹ ๋Ÿฌ๋‹์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ์ •ํ™•๋„๋ฅผ 95%๊นŒ์ง€ ์˜ฌ๋ฆฌ๊ณ  ๋‚˜๋‹ˆ ์ด ๋ชจ๋ธ์„ ์ด์šฉํ•ด์„œ ๋‹จ์ˆœํ•œ ํ”„๋กœ์ ํŠธ์„ฑ ์•ฑ์„ ํ•˜๋‚˜ ๋งŒ๋“ค๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

CoreML์„ ์ง์ ‘ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ๋„ ํ•  ๊ฒธ, scikit์˜ Linear Regression์„ ์ ์šฉํ•ด ํ•™์Šตํ•œ ๋ชจ๋ธ์„ .mlmodel๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๋‹ด์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

 

0. CoreML๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด coremltools ์„ค์น˜ ์ง„ํ–‰

condaํ™˜๊ฒฝ ์œ ๋ฌด์— ๋”ฐ๋ผ ๋ช…๋ น์–ด๊ฐ€ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

 

<anaconda๋ฅผ ํ†ตํ•ด>

conda install -c conda-forge coremltools

<pip ํ†ตํ•ด>

pip install coremltools==5.0b3

 

=> https://github.com/apple/coremltools ์—์„œ ๊ฐ€์žฅ ์ตœ๊ทผ ๋ฒ„์ „์„ ๊นŒ๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋”ฐ๋กœ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์ด ๋ช…๋ น์–ด๋ฅผ ์ฒจ๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€๋ง ํ•ด๋ณด์‹œ๋ฉด ๊ทธ๋ƒฅ pip install coremltools ๋กœ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

 

1. Jupyter Notebook - Conda ํ™˜๊ฒฝ์€ coreML ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

model = LinearRegression() #์„ ํ˜• ํšŒ๊ท€ model

import coremltools
core_ml_model = coremltools.converters.sklearn.convert(model) #convertํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •

 

NameError: name '_tree' is not defined ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋‹ˆ, ์ด๊ฒŒ scikit ๋ฒ„์ „์„ coremltool์ด ์ง€์›์„ ๋ชปํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

sklearn์€ ํ˜„์žฌ ์ตœ์‹  ๋ฒ„์ „์ด 0.24.1์ธ๋ฐ์š”, coremltools์ด ํ™•์‹คํžˆ ์ง€์›ํ•˜๋Š” ๊ฑด sklearn 0.19.2์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ conda์—์„œ sklearn์„ ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ๋ฅผ ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ

 

sklearn 0.19.2๋Š” ๋˜.. Python3๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋‹ˆ ์ƒˆ๋กœ์šด ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋œน๋‹ˆ๋‹ค.

๊ฐ€์ƒํ™˜๊ฒฝ์„ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š”๊ฒŒ ํŽธํ•˜์‹œ๋‹ค๋ฉด ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€์—์„œ ๋‚˜์˜ค๋Š”๋Œ€๋กœ ๋‚ฎ์ถฐ์ฃผ์‹œ๊ณ  ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๊ธด ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์ €๋Š” ๊ตฌ๊ธ€ ์ฝ”๋žฉ์œผ๋กœ ๋ฐฉํ–ฅ์„ ํ‹€์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

2. Colab์—์„œ scikit ๋‹ค์šด ๊ทธ๋ ˆ์ด๋“œ + coremltools ์„ค์น˜ํ•˜๊ธฐ

 

Google Colab์€ ์ด๋ ‡๊ฒŒ ํ•œ ๋ฒˆ Run ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ ์„ค์น˜๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒŒ ๋‹จ์ ์ด์ž ์žฅ์ ์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ ‡๋‹ค๋ฉด ๋‹ค์šด๊ทธ๋ ˆ์ด๋“œ ๋ฒ„์ „์„ ์„ค์น˜ํ•˜๊ณ  coremltools๋„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

Google Colab์€ conda ํ™˜๊ฒฝ์ด ์•„๋‹ˆ๋‹ˆ ๋ช…๋ น์–ด๊ฐ€ pip์ž…๋‹ˆ๋‹ค.

 

!pip install --force-reinstall 'scikit-learn==0.19.2'

 

 

 

3. Convert ํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ

model = LinearRegression() #์„ ํ˜• ํšŒ๊ท€ model

import coremltools
coreml_model = coremltools.converters.sklearn.convert(model) #convertํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •
coreml_model.save('/MyModel.mlmodel')

 

์ด๋•Œ convert(model, input ์ด๋ฆ„๋“ค, output์ด๋ฆ„)์ด ์ธ์ž๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ํ•˜๋‹จ์—์„œ mlmodel ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋˜์‹ค ๊ฒ๋‹ˆ๋‹ค.

 

save('~๊ฒฝ๋กœ/๋‚ด๋ชจ๋ธํŒŒ์ผ์ด๋ฆ„.mlmodel')๋กœ ์ €์žฅ์„ ํ•ด์ฃผ๋ฉด

๊ทธ ๊ฒฝ๋กœ์— mlmodel ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค!!

์ƒ์„ฑ๋œ scores.mlmodel

 

์—ด์—ฌ๋ณด๊ฒŒ ๋˜๋ฉด

 

์•ž์„œ ๋ง์”€๋“œ๋ฆฐ input๊ณผ output์ด ๋ช…์‹œ๋˜์–ด ์žˆ์ฃ .

Description ๊ฐ™์€ ๋ถ€๋ถ„์— ์„ค๋ช…๋„ ์ฝ”๋“œ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ ์ด mlmodel๋ฅผ xcode ํ”„๋กœ์ ํŠธ์— ์—ฐ๊ฒฐํ•ด์„œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค(๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ~.~)