Example of transfer learning taken from https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3
Created
April 11, 2017 12:09
-
-
Save mmajewsk/ce488f7ecdbe9c9d37fb62930dbe3649 to your computer and use it in GitHub Desktop.
Keras - Sequential model to Functional example
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": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using Theano backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"import keras" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"#code from https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3\n", | |
"\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers.core import Flatten, Dense, Dropout\n", | |
"from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D\n", | |
"from keras.optimizers import SGD\n", | |
"import cv2, numpy as np\n", | |
"\n", | |
"def VGG_16(weights_path=None):\n", | |
" model = Sequential()\n", | |
" model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))\n", | |
" model.add(Convolution2D(64, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(64, 3, 3, activation='relu'))\n", | |
" model.add(MaxPooling2D((2,2), strides=(2,2)))\n", | |
"\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(128, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(128, 3, 3, activation='relu'))\n", | |
" model.add(MaxPooling2D((2,2), strides=(2,2)))\n", | |
"\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(256, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(256, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(256, 3, 3, activation='relu'))\n", | |
" model.add(MaxPooling2D((2,2), strides=(2,2)))\n", | |
"\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(MaxPooling2D((2,2), strides=(2,2)))\n", | |
"\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(ZeroPadding2D((1,1)))\n", | |
" model.add(Convolution2D(512, 3, 3, activation='relu'))\n", | |
" model.add(MaxPooling2D((2,2), strides=(2,2)))\n", | |
"\n", | |
" model.add(Flatten())\n", | |
" model.add(Dense(4096, activation='relu'))\n", | |
" model.add(Dropout(0.5))\n", | |
" model.add(Dense(4096, activation='relu'))\n", | |
" model.add(Dropout(0.5))\n", | |
" model.add(Dense(1000, activation='softmax'))\n", | |
"\n", | |
" if weights_path:\n", | |
" model.load_weights(weights_path)\n", | |
"\n", | |
" return model\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"md = VGG_16('vgg16_weights.h5')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)\n", | |
"md.compile(optimizer=sgd, loss='categorical_crossentropy')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[<keras.layers.convolutional.ZeroPadding2D at 0x7f1113ac0790>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113ace6d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113ad2050>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113ad2d90>,\n", | |
" <keras.layers.pooling.MaxPooling2D at 0x7f1113a79050>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113a79210>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113b392d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113a87bd0>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113a14910>,\n", | |
" <keras.layers.pooling.MaxPooling2D at 0x7f1113a239d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113a23b90>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113a318d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113a23450>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113a4c850>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f11139dc2d0>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113a4c450>,\n", | |
" <keras.layers.pooling.MaxPooling2D at 0x7f11139f61d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f11139f6390>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113a071d0>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113994290>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113994f90>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f1113a07810>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f11139aff10>,\n", | |
" <keras.layers.pooling.MaxPooling2D at 0x7f11139c1990>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f11139c1b50>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f11139cd510>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f111395ca50>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f111396a890>,\n", | |
" <keras.layers.convolutional.ZeroPadding2D at 0x7f111397a950>,\n", | |
" <keras.layers.convolutional.Convolution2D at 0x7f1113988410>,\n", | |
" <keras.layers.pooling.MaxPooling2D at 0x7f1117231ad0>,\n", | |
" <keras.layers.core.Flatten at 0x7f1113e9cd50>,\n", | |
" <keras.layers.core.Dense at 0x7f11139cd890>,\n", | |
" <keras.layers.core.Dropout at 0x7f1113c85b90>,\n", | |
" <keras.layers.core.Dense at 0x7f1113d12910>,\n", | |
" <keras.layers.core.Dropout at 0x7f1113c7ec50>,\n", | |
" <keras.layers.core.Dense at 0x7f1113c5ca10>]" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"md.layers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"l=md.layers[-10]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[-0.01030169, -0.00784571, -0.00398386],\n", | |
" [-0.00622659, -0.00439948, -0.00803796],\n", | |
" [-0.0081472 , 0.0021277 , -0.00112611]], dtype=float32)" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"l.get_weights()[0][0][0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"func = md.model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'keras.engine.training.Model'> <class 'keras.models.Sequential'>\n" | |
] | |
} | |
], | |
"source": [ | |
"print type(func), type(md)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[-0.01030169, -0.00784571, -0.00398386],\n", | |
" [-0.00622659, -0.00439948, -0.00803796],\n", | |
" [-0.0081472 , 0.0021277 , -0.00112611]], dtype=float32)" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"func.layers[-10].get_weights()[0][0][0]" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda root]", | |
"language": "python", | |
"name": "conda-root-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment