Created
October 25, 2020 05:57
-
-
Save TokyoYoshida/632b4c8070aa6c937539e4ae261a2740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Requirement already up-to-date: coremltools in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (4.0)\n", | |
"Requirement already satisfied, skipping upgrade: protobuf>=3.1.0 in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (3.13.0)\n", | |
"Requirement already satisfied, skipping upgrade: attrs in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (19.1.0)\n", | |
"Requirement already satisfied, skipping upgrade: sympy in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (1.3)\n", | |
"Requirement already satisfied, skipping upgrade: attr in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (0.3.1)\n", | |
"Requirement already satisfied, skipping upgrade: scipy in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (1.2.1)\n", | |
"Requirement already satisfied, skipping upgrade: numpy>=1.14.5 in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (1.16.2)\n", | |
"Requirement already satisfied, skipping upgrade: packaging in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (19.0)\n", | |
"Requirement already satisfied, skipping upgrade: tqdm in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (4.31.1)\n", | |
"Requirement already satisfied, skipping upgrade: six>=1.10.0 in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from coremltools) (1.12.0)\n", | |
"Requirement already satisfied, skipping upgrade: setuptools in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from protobuf>=3.1.0->coremltools) (40.8.0)\n", | |
"Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in ~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages (from packaging->coremltools) (2.3.1)\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install -U coremltools" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# シンプルなモデル" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"coreml_model_path= \"my_model_raw.mlmodel\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import coremltools\n", | |
"spec = coremltools.utils.load_spec(coreml_model_path)\n", | |
"builder = coremltools.models.neural_network.NeuralNetworkBuilder(spec=spec)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"mlmodel = coremltools.models.MLModel(spec)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'output1': array([2.56040733e-21, 7.25779588e-15, 1.99342376e-10, 1.11184195e-09,\n", | |
" 5.92091055e-05, 9.99939799e-01, 9.72097268e-07, 1.13292452e-14,\n", | |
" 4.43997455e-23, 5.00404492e-33, 0.00000000e+00, 0.00000000e+00,\n", | |
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", | |
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00])}" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mlmodel.predict({'input1': np.array([2.0,3.0])})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# ビルダーを使ったモデル" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"coreml_model_path= \"my_model_with_builder.mlmodel\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import coremltools\n", | |
"spec = coremltools.utils.load_spec(coreml_model_path)\n", | |
"builder = coremltools.models.neural_network.NeuralNetworkBuilder(spec=spec)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"mlmodel = coremltools.models.MLModel(spec)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'output2': array([[[4.41472361e-22, 2.22987627e-18, 8.84581228e-11, 2.78765794e-14,\n", | |
" 2.81287589e-06, 9.99946475e-01, 5.06445758e-05, 2.29461781e-16,\n", | |
" 1.80691190e-27, 6.91833068e-35, 0.00000000e+00, 0.00000000e+00,\n", | |
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", | |
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]])}" | |
] | |
}, | |
"execution_count": 84, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mlmodel.predict({'input1': np.array([2.0,3.0])})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# うまくいかない例" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "RuntimeError", | |
"evalue": "Error compiling model: \"Error reading protobuf spec. validator error: Layer 'Reshape' consumes an input named 'activation_18' which is not present in this network.\".", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-85-c3bb55dc8865>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mmlmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcoremltools\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMLModel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mspec\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mmlmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'input1'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages/coremltools/models/model.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, data, useCPUOnly, **kwargs)\u001b[0m\n\u001b[1;32m 365\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 366\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_framework_error\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 367\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_framework_error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 368\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 369\u001b[0m raise Exception(\n", | |
"\u001b[0;32m~/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages/coremltools/models/model.py\u001b[0m in \u001b[0;36m_get_proxy_and_spec\u001b[0;34m(filename, use_cpu_only)\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0m_MLModelProxy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muse_cpu_only\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mspecification\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 114\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mRuntimeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m _warnings.warn(\n", | |
"\u001b[0;31mRuntimeError\u001b[0m: Error compiling model: \"Error reading protobuf spec. validator error: Layer 'Reshape' consumes an input named 'activation_18' which is not present in this network.\"." | |
] | |
} | |
], | |
"source": [ | |
"coreml_model_path= \"mistakemodel.mlmodel\"\n", | |
"\n", | |
"import coremltools\n", | |
"spec = coremltools.utils.load_spec(coreml_model_path)\n", | |
"builder = coremltools.models.neural_network.NeuralNetworkBuilder(spec=spec)\n", | |
"\n", | |
"mlmodel = coremltools.models.MLModel(spec)\n", | |
"\n", | |
"mlmodel.predict({'input1': np.array([2.0,3.0])})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment