Created
August 13, 2017 01:58
-
-
Save jph00/dd9d3c106951d3b187de93ee00c90eb2 to your computer and use it in GitHub Desktop.
nbs/keras_raw-xception-149.ipynb
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": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Start" | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%reload_ext autoreload\n%autoreload 2\n%matplotlib inline", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from imports import *\nfrom keras.applications import xception", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Using TensorFlow backend.\n", | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "bs=64; sz=149; lr=2e-3\npath = \"/data/jhoward/fast/dogscats/\"", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "gen = image.ImageDataGenerator(preprocessing_function=xception.preprocess_input)", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 1. Fine-tune last layer of full network" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "K.set_learning_phase(0)", | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "mn=Xception(include_top=False, input_shape=(sz,sz,3), pooling='avg')\nfor l in mn.layers[:-1]: l.trainable=False", | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "outp = Dense(2, activation='softmax')(mn.output)\nm = Model(mn.input, outp)\nK.set_learning_phase(0)\nm.compile(SGD(lr, momentum=0.9), 'categorical_crossentropy', metrics=['accuracy'])", | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"scrolled": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "trn_batches = gen.flow_from_directory(f'{path}train', (sz,sz), batch_size=bs)\nval_batches = gen.flow_from_directory(f'{path}valid', (sz,sz), batch_size=bs, shuffle=False)\nnb_trn = math.ceil(trn_batches.n/bs)\nnb_val = math.ceil(val_batches.n/bs)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Found 23000 images belonging to 2 classes.\nFound 2000 images belonging to 2 classes.\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"scrolled": false, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "K.set_learning_phase(0)\nm.fit_generator(trn_batches, nb_trn, workers=1, epochs=2,\n validation_data=val_batches, validation_steps=nb_val)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Epoch 1/2\n360/360 [==============================] - 68s - loss: 0.3416 - acc: 0.8640 - val_loss: 0.2901 - val_acc: 0.8880\nEpoch 2/2\n360/360 [==============================] - 67s - loss: 0.2644 - acc: 0.8900 - val_loss: 0.2543 - val_acc: 0.8945\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "<keras.callbacks.History at 0x7fb74a4c9940>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## 2. Pre-compute output of penultimate layer and train single layer net" | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "mn=Xception(include_top=False, input_shape=(sz,sz,3), pooling='avg')", | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Precompute pooling output:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fix_batches = gen.flow_from_directory(f'{path}train', (sz,sz), batch_size=bs, shuffle=False)\nval_batches = gen.flow_from_directory(f'{path}valid', (sz,sz), batch_size=bs, shuffle=False)\n\ntrn_acts = mn.predict_generator(generator=fix_batches, verbose=1, \n steps=nb_trn, workers=1)\nval_acts = mn.predict_generator(generator=val_batches, verbose=1,\n steps=nb_val, workers=1)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Found 23000 images belonging to 2 classes.\nFound 2000 images belonging to 2 classes.\n31/32 [============================>.] - ETA: 0s", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Train single layer:" | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "inp = Input(batch_shape=mn.output_shape)\noutp = Dense(1, activation='sigmoid')(inp)\nfc = Model(inp, outp)\nfc.compile(SGD(lr, momentum=0.9), 'binary_crossentropy', metrics=['accuracy'])", | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fc.fit(trn_acts, fix_batches.classes, bs, 2, validation_data=(val_acts, val_batches.classes))", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Train on 23000 samples, validate on 2000 samples\nEpoch 1/2\n23000/23000 [==============================] - 1s - loss: 0.1184 - acc: 0.9540 - val_loss: 0.0836 - val_acc: 0.9690\nEpoch 2/2\n23000/23000 [==============================] - 0s - loss: 0.0802 - acc: 0.9693 - val_loss: 0.0774 - val_acc: 0.9710\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": "<keras.callbacks.History at 0x7fb6d58fce10>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "conda-root-py", | |
"display_name": "Python [conda root]", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.2", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "nbs/keras_raw-xception-149.ipynb", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment