Skip to content
Permalink
Browse files

Updated keras exporter and added examples and tests for keras exporter.

  • Loading branch information...
Nirmal-Neel committed Feb 27, 2019
1 parent a5e680f commit 8dad85687f9c98636bc0c04567da1ba09b2526a7
@@ -95,13 +95,6 @@ Read the documentation at [Nyoka Documentation](http://docs.nyoka.org).

nyoka requires:

* scikit-learn (>=0.19.1)
* keras (==2.2.4)
* tensorflow (==1.9.0)
* statsmodels (>=0.9.0)
* lightgbm (>=2.1.2)
* xgboost (>=0.8.0)
* sklearn-pandas
* lxml


@@ -256,7 +249,7 @@ predictions = Dense(2, activation=activType)(x)
model_final = Model(inputs =model.input, outputs = predictions,name='predictions')
from nyoka import KerasToPmml
cnn_pmml = KerasToPmml(model_final,predictedClasses=['cats','dogs'])
cnn_pmml = KerasToPmml(model_final,dataSet='image',predictedClasses=['cats','dogs'])
cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)
```
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# keras → PMML\n",
"\n",
"### Exporter: Keras Model to PMML\n",
"\n",
"\n",
"### **STEPS**:\n",
"- The Objective is to do a transfer learning using Mobilenet architecture for 2 classes\n",
"- Build PMML using Nyoka exporter\n",
"- keras version '2.1.5'\n",
"- tensorflow verison '1.9.0'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-13T07:02:21.583886Z",
"start_time": "2018-08-13T07:02:21.458886Z"
}
},
"outputs": [],
"source": [
"from keras import applications\n",
"from keras.layers import Flatten, Dense\n",
"from keras.models import Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loading Mobilenet from keras application module"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3)) #last layer not included"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding a dense layer and using sigmoid activation as the last layer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"activType='sigmoid'\n",
"x = model.output\n",
"x = Flatten()(x)\n",
"x = Dense(1024, activation=\"relu\")(x)\n",
"predictions = Dense(2, activation=activType)(x)\n",
"model_final = Model(inputs =model.input, outputs = predictions,name='predictions')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_final.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exporting the new model to PMML using Nyoka"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from nyoka import KerasToPmml\n",
"cnn_pmml = KerasToPmml(model_final,dataSet='image',predictedClasses=['cats','dogs'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cnn_pmml.export(open('2classMBNet.pmml', \"w\"), 0)"
]
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn.datasets import load_boston\n",
"from sklearn.model_selection import train_test_split\n",
"import pandas as pd\n",
"boston = load_boston()\n",
"import keras\n",
"from keras.models import Sequential\n",
"from keras.layers import *\n",
"import sys\n",
"from nyoka import KerasToPmml"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data = pd.DataFrame(boston.data)\n",
"features = list(boston.feature_names)\n",
"target = 'PRICE'\n",
"data.columns = features\n",
"data['PRICE'] = boston.target"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(data[features], data[target], test_size=0.20, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))\n",
"model.add(Dense(23))\n",
"model.add(Dense(1, kernel_initializer='normal'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.compile(loss='mean_squared_error', optimizer='adam')\n",
"model.fit(x_train, y_train, epochs=1000, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pmmlObj=KerasToPmml(model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pmmlObj.export(open('sequentialModel.pmml','w'),0)"
]
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Oops, something went wrong.

0 comments on commit 8dad856

Please sign in to comment.
You can’t perform that action at this time.