Skip to content

Instantly share code, notes, and snippets.

@catdance124
Created July 30, 2019 08:43
Show Gist options
  • Save catdance124/fb78ee86e4ea21d28c6e2a53e70c221d to your computer and use it in GitHub Desktop.
Save catdance124/fb78ee86e4ea21d28c6e2a53e70c221d to your computer and use it in GitHub Desktop.
MMdnn/caffe->keras
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "MMdnn/caffe->keras",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/catdance124/fb78ee86e4ea21d28c6e2a53e70c221d/colaboratory.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "xitplqMNk_Hc",
"outputId": "126c2a8c-9deb-4bd9-fe1b-ae0f842f664a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 464
}
},
"source": [
"!pip3 install numpy==1.16.2\n",
"!pip3 install mmdnn"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting numpy==1.16.2\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/35/d5/4f8410ac303e690144f0a0603c4b8fd3b986feb2749c435f7cdbb288f17e/numpy-1.16.2-cp36-cp36m-manylinux1_x86_64.whl (17.3MB)\n",
"\u001b[K |████████████████████████████████| 17.3MB 2.8MB/s \n",
"\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.\u001b[0m\n",
"\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n",
"\u001b[?25hInstalling collected packages: numpy\n",
" Found existing installation: numpy 1.16.4\n",
" Uninstalling numpy-1.16.4:\n",
" Successfully uninstalled numpy-1.16.4\n",
"Successfully installed numpy-1.16.2\n"
],
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"application/vnd.colab-display-data+json": {
"pip_warning": {
"packages": [
"numpy"
]
}
}
},
"metadata": {
"tags": []
}
},
{
"output_type": "stream",
"text": [
"Collecting mmdnn\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a5/20/1fb6420b806c546392c045f98ff3d0ede51011db2b56f9552a18a1b31506/mmdnn-0.2.5-py2.py3-none-any.whl (317kB)\n",
"\u001b[K |████████████████████████████████| 317kB 2.8MB/s \n",
"\u001b[?25hRequirement already satisfied: pillow>=3.1.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (4.3.0)\n",
"Requirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (1.16.2)\n",
"Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (3.7.1)\n",
"Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (1.12.0)\n",
"Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow>=3.1.0->mmdnn) (0.46)\n",
"Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.0->mmdnn) (41.0.1)\n",
"Installing collected packages: mmdnn\n",
"Successfully installed mmdnn-0.2.5\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qYOX88CR1hH9",
"colab_type": "text"
},
"source": [
"--------------------------ランタイム再起動--------------------------"
]
},
{
"cell_type": "code",
"metadata": {
"id": "PZpwDlQG1eH2",
"colab_type": "code",
"outputId": "00f2a5be-61bc-42fa-e26c-89b4dece196e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 153
}
},
"source": [
"!pip3 install numpy==1.16.2\n",
"!pip3 install mmdnn"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Requirement already satisfied: numpy==1.16.2 in /usr/local/lib/python3.6/dist-packages (1.16.2)\n",
"Requirement already satisfied: mmdnn in /usr/local/lib/python3.6/dist-packages (0.2.5)\n",
"Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (1.12.0)\n",
"Requirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (1.16.2)\n",
"Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (3.7.1)\n",
"Requirement already satisfied: pillow>=3.1.0 in /usr/local/lib/python3.6/dist-packages (from mmdnn) (4.3.0)\n",
"Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.0->mmdnn) (41.0.1)\n",
"Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from pillow>=3.1.0->mmdnn) (0.46)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xCSYS7JHrqLQ",
"colab_type": "code",
"outputId": "3c83bf0d-c04a-4ba3-9fbf-96494eadb033",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 425
}
},
"source": [
"!mmconvert -h"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"usage: mmconvert [-h]\n",
" [--srcFramework {caffe,caffe2,cntk,mxnet,keras,tensorflow,tf,pytorch}]\n",
" [--inputWeight INPUTWEIGHT] [--inputNetwork INPUTNETWORK]\n",
" --dstFramework\n",
" {caffe,caffe2,cntk,mxnet,keras,tensorflow,coreml,pytorch,onnx}\n",
" --outputModel OUTPUTMODEL [--dump_tag {SERVING,TRAINING}]\n",
"\n",
"optional arguments:\n",
" -h, --help show this help message and exit\n",
" --srcFramework {caffe,caffe2,cntk,mxnet,keras,tensorflow,tf,pytorch}, -sf {caffe,caffe2,cntk,mxnet,keras,tensorflow,tf,pytorch}\n",
" Source toolkit name of the model to be converted.\n",
" --inputWeight INPUTWEIGHT, -iw INPUTWEIGHT\n",
" Path to the model weights file of the external tool\n",
" (e.g caffe weights proto binary, keras h5 binary\n",
" --inputNetwork INPUTNETWORK, -in INPUTNETWORK\n",
" Path to the model network file of the external tool\n",
" (e.g caffe prototxt, keras json\n",
" --dstFramework {caffe,caffe2,cntk,mxnet,keras,tensorflow,coreml,pytorch,onnx}, -df {caffe,caffe2,cntk,mxnet,keras,tensorflow,coreml,pytorch,onnx}\n",
" Format of model at srcModelPath (default is to auto-\n",
" detect).\n",
" --outputModel OUTPUTMODEL, -om OUTPUTMODEL\n",
" Path to save the destination model\n",
" --dump_tag {SERVING,TRAINING}\n",
" Tensorflow model dump type\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "S8G-ZXc81tVk",
"colab_type": "text"
},
"source": [
"ファイルを取得"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Y1Lvlxejuteh",
"colab_type": "code",
"outputId": "752f74c9-1ac7-4ba1-93bb-9f5c9aa2a694",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
}
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/drive\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "PQK4g2kOu3sh",
"colab_type": "code",
"colab": {}
},
"source": [
"!ln -s /content/drive/My\\ Drive/Colab\\ Notebooks/files vgg"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_GhWPmMH74qc",
"colab_type": "code",
"outputId": "116394d2-b13c-42a8-8b1d-daddb453e9ae",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"source": [
"!ls vgg"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
" deploy-vgg16.prototxt\t'dl4us-master.zip (Unzipped Files)'\n",
" dl4us-master.zip\t minc-vgg16.caffemodel\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YlimGCPC1xvz",
"colab_type": "text"
},
"source": [
"caffeをインストール"
]
},
{
"cell_type": "code",
"metadata": {
"id": "esIY4_lJwqnt",
"colab_type": "code",
"colab": {}
},
"source": [
"!apt install -y caffe-cuda"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Am8TypoY2sDZ",
"colab_type": "text"
},
"source": [
"学習済みwight,モデルを指定し,変換"
]
},
{
"cell_type": "code",
"metadata": {
"id": "KFWUMeKqvWJL",
"colab_type": "code",
"colab": {}
},
"source": [
"!mmconvert --srcFramework caffe --inputWeight vgg/minc-vgg16.caffemodel --inputNetwork vgg/deploy-vgg16.prototxt --dstFramework keras --outputModel ~/minc-vgg16.h5 --inputShape 10,3,224,224"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DYKlhuoUzzM_",
"colab_type": "code",
"outputId": "3f118e9a-5978-472e-daa3-29667f6a262a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"!ls ~"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"minc-vgg16.h5\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jPYIgahQ-CgS",
"colab_type": "code",
"outputId": "c20175a4-d4b8-4150-d444-eb6ff3536f8e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"import keras\n",
"model = keras.models.load_model('/root/minc-vgg16.h5')\n",
"loss = keras.losses.categorical_crossentropy\n",
"optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999)\n",
"\n",
"model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy'])\n",
"model.summary()"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"data (InputLayer) (None, 224, 224, 3) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_1 (ZeroPaddin (None, 226, 226, 3) 0 \n",
"_________________________________________________________________\n",
"conv1_1 (Conv2D) (None, 224, 224, 64) 1792 \n",
"_________________________________________________________________\n",
"relu1_1 (Activation) (None, 224, 224, 64) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_2 (ZeroPaddin (None, 226, 226, 64) 0 \n",
"_________________________________________________________________\n",
"conv1_2 (Conv2D) (None, 224, 224, 64) 36928 \n",
"_________________________________________________________________\n",
"relu1_2 (Activation) (None, 224, 224, 64) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_3 (ZeroPaddin (None, 225, 225, 64) 0 \n",
"_________________________________________________________________\n",
"pool1 (MaxPooling2D) (None, 112, 112, 64) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_4 (ZeroPaddin (None, 114, 114, 64) 0 \n",
"_________________________________________________________________\n",
"conv2_1 (Conv2D) (None, 112, 112, 128) 73856 \n",
"_________________________________________________________________\n",
"relu2_1 (Activation) (None, 112, 112, 128) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_5 (ZeroPaddin (None, 114, 114, 128) 0 \n",
"_________________________________________________________________\n",
"conv2_2 (Conv2D) (None, 112, 112, 128) 147584 \n",
"_________________________________________________________________\n",
"relu2_2 (Activation) (None, 112, 112, 128) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_6 (ZeroPaddin (None, 113, 113, 128) 0 \n",
"_________________________________________________________________\n",
"pool2 (MaxPooling2D) (None, 56, 56, 128) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_7 (ZeroPaddin (None, 58, 58, 128) 0 \n",
"_________________________________________________________________\n",
"conv3_1 (Conv2D) (None, 56, 56, 256) 295168 \n",
"_________________________________________________________________\n",
"relu3_1 (Activation) (None, 56, 56, 256) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_8 (ZeroPaddin (None, 58, 58, 256) 0 \n",
"_________________________________________________________________\n",
"conv3_2 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"relu3_2 (Activation) (None, 56, 56, 256) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_9 (ZeroPaddin (None, 58, 58, 256) 0 \n",
"_________________________________________________________________\n",
"conv3_3 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"relu3_3 (Activation) (None, 56, 56, 256) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_10 (ZeroPaddi (None, 57, 57, 256) 0 \n",
"_________________________________________________________________\n",
"pool3 (MaxPooling2D) (None, 28, 28, 256) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_11 (ZeroPaddi (None, 30, 30, 256) 0 \n",
"_________________________________________________________________\n",
"conv4_1 (Conv2D) (None, 28, 28, 512) 1180160 \n",
"_________________________________________________________________\n",
"relu4_1 (Activation) (None, 28, 28, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_12 (ZeroPaddi (None, 30, 30, 512) 0 \n",
"_________________________________________________________________\n",
"conv4_2 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"relu4_2 (Activation) (None, 28, 28, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_13 (ZeroPaddi (None, 30, 30, 512) 0 \n",
"_________________________________________________________________\n",
"conv4_3 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"relu4_3 (Activation) (None, 28, 28, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_14 (ZeroPaddi (None, 29, 29, 512) 0 \n",
"_________________________________________________________________\n",
"pool4 (MaxPooling2D) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_15 (ZeroPaddi (None, 16, 16, 512) 0 \n",
"_________________________________________________________________\n",
"conv5_1 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"relu5_1 (Activation) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_16 (ZeroPaddi (None, 16, 16, 512) 0 \n",
"_________________________________________________________________\n",
"conv5_2 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"relu5_2 (Activation) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_17 (ZeroPaddi (None, 16, 16, 512) 0 \n",
"_________________________________________________________________\n",
"conv5_3 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"relu5_3 (Activation) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"zero_padding2d_18 (ZeroPaddi (None, 15, 15, 512) 0 \n",
"_________________________________________________________________\n",
"pool5 (MaxPooling2D) (None, 7, 7, 512) 0 \n",
"_________________________________________________________________\n",
"fc6_0 (Flatten) (None, 25088) 0 \n",
"_________________________________________________________________\n",
"fc6_1 (Dense) (None, 4096) 102764544 \n",
"_________________________________________________________________\n",
"relu6 (Activation) (None, 4096) 0 \n",
"_________________________________________________________________\n",
"fc7_1 (Dense) (None, 4096) 16781312 \n",
"_________________________________________________________________\n",
"relu7 (Activation) (None, 4096) 0 \n",
"_________________________________________________________________\n",
"fc8-20_1 (Dense) (None, 23) 94231 \n",
"_________________________________________________________________\n",
"prob (Activation) (None, 23) 0 \n",
"=================================================================\n",
"Total params: 134,354,775\n",
"Trainable params: 134,354,775\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py:292: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.\n",
" warnings.warn('No training configuration found in save file: '\n"
],
"name": "stderr"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment