Skip to content

Instantly share code, notes, and snippets.

@yusuke0519
Created October 9, 2015 13:19
Show Gist options
  • Save yusuke0519/ceaef801cfd516ef9c86 to your computer and use it in GitHub Desktop.
Save yusuke0519/ceaef801cfd516ef9c86 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graph Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Load Data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import\n",
"from __future__ import print_function\n",
"from keras.datasets import cifar10\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"from keras.models import Sequential, Graph\n",
"from keras.layers.core import Dense, Dropout, Activation, Flatten\n",
"from keras.layers.convolutional import Convolution2D, MaxPooling2D\n",
"from keras.optimizers import SGD, Adadelta, Adagrad\n",
"from keras.utils import np_utils, generic_utils\n",
"from six.moves import range"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X_train shape: (50000, 3, 32, 32)\n",
"50000 train samples\n",
"10000 test samples\n"
]
}
],
"source": [
"batch_size = 32\n",
"nb_classes = 10\n",
"nb_epoch = 2\n",
"\n",
"# input image dimensions\n",
"img_rows, img_cols = 32, 32\n",
"# the CIFAR10 images are RGB\n",
"img_channels = 3\n",
"\n",
"# the data, shuffled and split between tran and test sets\n",
"(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n",
"print('X_train shape:', X_train.shape)\n",
"print(X_train.shape[0], 'train samples')\n",
"print(X_test.shape[0], 'test samples')\n",
"\n",
"# convert class vectors to binary class matrices\n",
"Y_train = np_utils.to_categorical(y_train, nb_classes)\n",
"Y_test = np_utils.to_categorical(y_test, nb_classes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. CNN using Sequential"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model = Sequential()\n",
"\n",
"model.add(Convolution2D(32, 3, 3, border_mode='full',\n",
" input_shape=(img_channels, img_rows, img_cols)))\n",
"model.add(Activation('sigmoid'))\n",
"model.add(Convolution2D(32, 3, 3))\n",
"model.add(Activation('sigmoid'))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"\n",
"model.add(Convolution2D(64, 3, 3, border_mode='full'))\n",
"model.add(Activation('sigmoid'))\n",
"model.add(Convolution2D(64, 3, 3))\n",
"model.add(Activation('sigmoid'))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"\n",
"model.add(Flatten())\n",
"model.add(Dense(512))\n",
"model.add(Activation('relu'))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(nb_classes))\n",
"model.add(Activation('softmax'))\n",
"\n",
"# let's train the model using SGD + momentum (how original).\n",
"sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
"model.compile(loss='categorical_crossentropy', optimizer=sgd)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not using data augmentation or normalization\n",
"Epoch 1/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3068 \n",
"Epoch 2/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3035 \n",
"Epoch 3/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3034 \n",
"Epoch 4/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3034 \n",
"Epoch 5/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3034 \n",
"Epoch 6/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3034 \n",
"Epoch 7/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3035 \n",
"Epoch 8/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3032 \n",
"Epoch 9/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3034 \n",
"Epoch 10/10\n",
"50000/50000 [==============================] - 42s - loss: 2.3033 \n",
"10000/10000 [==============================] - 2s \n",
"Test score: 2.30308167725\n"
]
}
],
"source": [
"print(\"Not using data augmentation or normalization\")\n",
"\n",
"X_train = X_train.astype(\"float32\")\n",
"X_test = X_test.astype(\"float32\")\n",
"X_train /= 255\n",
"X_test /= 255\n",
"model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch)\n",
"score = model.evaluate(X_test, Y_test, batch_size=batch_size)\n",
"print('Test score:', score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. CNN usint graph"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"model = Graph()\n",
"model.add_input(name='input', input_shape=(img_channels, img_rows, img_cols))\n",
"\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv1', input='input')\n",
"\n",
"\n",
"model.add_node(Flatten(), name='flatten', input='conv1')\n",
"model.add_node(Dense(512, activation='relu'), name='fc1', input='flatten')\n",
"model.add_node(Dense(nb_classes, activation='softmax'), name='fc2', input='fc1')\n",
"\n",
"model.add_output(name='output', input='fc2')\n",
"\n",
"# let's train the model using SGD + momentum (how original).\n",
"sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
"model.compile(loss={'output': 'categorical_crossentropy'}, optimizer=sgd)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not using data augmentation or normalization\n",
"Epoch 1/2\n",
"50000/50000 [==============================] - 25s - loss: 2.3034 \n",
"Epoch 2/2\n",
"50000/50000 [==============================] - 25s - loss: 2.3033 \n",
"Test score: 2.30363765755\n"
]
}
],
"source": [
"print(\"Not using data augmentation or normalization\")\n",
"\n",
"X_train = X_train.astype(\"float32\")\n",
"X_test = X_test.astype(\"float32\")\n",
"X_train /= 255\n",
"X_test /= 255\n",
"model.fit({'input': X_train, 'output': Y_train}, batch_size=batch_size, nb_epoch=nb_epoch)\n",
"score = model.evaluate({'input': X_test, 'output': Y_test}, batch_size=batch_size)\n",
"print('Test score:', score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. multi-column CNN"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = Graph()\n",
"model.add_input(name='input1', input_shape=(img_channels, img_rows, img_cols))\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv1', input='input1')\n",
"model.add_input(name='input2', input_shape=(img_channels, img_rows, img_cols))\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv2', input='input2')\n",
"\n",
"\n",
"model.add_node(Flatten(), name='flatten', inputs=['conv1', 'conv2'])\n",
"model.add_node(Dense(512, activation='relu'), name='fc1', input='flatten')\n",
"model.add_node(Dense(nb_classes, activation='softmax'), name='fc2', input='fc1')\n",
"\n",
"model.add_output(name='output', input='fc2')\n",
"\n",
"# let's train the model using SGD + momentum (how original).\n",
"sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
"model.compile(loss={'output': 'categorical_crossentropy'}, optimizer=sgd)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not using data augmentation or normalization\n",
"Epoch 1/2\n",
"50000/50000 [==============================] - 49s - loss: 2.3034 \n",
"Epoch 2/2\n",
"50000/50000 [==============================] - 50s - loss: 2.3032 \n",
"Test score: 2.30391892204\n"
]
}
],
"source": [
"print(\"Not using data augmentation or normalization\")\n",
"\n",
"X_train = X_train.astype(\"float32\")\n",
"X_test = X_test.astype(\"float32\")\n",
"X_train /= 255\n",
"X_test /= 255\n",
"model.fit({'input1': X_train, 'input2': X_train, 'output': Y_train}, batch_size=batch_size, nb_epoch=nb_epoch)\n",
"score = model.evaluate({'input1': X_test, 'input2': X_test, 'output': Y_test}, batch_size=batch_size)\n",
"print('Test score:', score)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. multi-column CNN"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model = Graph()\n",
"model.add_input(name='input1', input_shape=(1, img_rows, img_cols))\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv1', input='input1')\n",
"model.add_input(name='input2', input_shape=(1, img_rows, img_cols))\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv2', input='input2')\n",
"model.add_input(name='input3', input_shape=(1, img_rows, img_cols))\n",
"model.add_node(Convolution2D(32, 3, 3, border_mode='full', activation='relu'), name='conv3', input='input3')\n",
"\n",
"model.add_node(Flatten(), name='flatten', inputs=['conv1', 'conv2', 'conv3'])\n",
"model.add_node(Dense(512, activation='relu'), name='fc1', input='flatten')\n",
"model.add_node(Dense(nb_classes, activation='softmax'), name='fc2', input='fc1')\n",
"\n",
"model.add_output(name='output', input='fc2')\n",
"\n",
"# let's train the model using SGD + momentum (how original).\n",
"sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
"model.compile(loss={'output': 'categorical_crossentropy'}, optimizer=sgd)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not using data augmentation or normalization\n",
"Epoch 1/2\n",
"22816/50000 [============>.................] - ETA: 38s - loss: 2.3031"
]
}
],
"source": [
"print(\"Not using data augmentation or normalization\")\n",
"\n",
"X_train = X_train.astype(\"float32\")\n",
"X_test = X_test.astype(\"float32\")\n",
"X_train /= 255\n",
"X_test /= 255\n",
"model.fit({'input1': X_train[:, 0:1, ...], 'input2': X_train[:, 1:2, ...], 'input3': X_train[:, 2:3, ...], 'output': Y_train}, batch_size=batch_size, nb_epoch=nb_epoch)\n",
"score = model.evaluate({'input1': X_train[:, 0:1, ...], 'input2': X_train[:, 1:2, ...], 'input3': X_train[:, 2:3, ...], 'output': Y_train}, batch_size=batch_size)\n",
"print('Test score:', score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@yusuke0519
Copy link
Author

kerasを0.20にあげても動作することを確認。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment