Skip to content

Instantly share code, notes, and snippets.

@jph00
Created August 12, 2017 18:24
Show Gist options
  • Save jph00/87b87549fbcee4b810d0ab0622412d4b to your computer and use it in GitHub Desktop.
Save jph00/87b87549fbcee4b810d0ab0622412d4b to your computer and use it in GitHub Desktop.
nbs/keras_raw-xception-149.ipynb
Display the source blob
Display the rendered blob
Raw
{
"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": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "gen = image.ImageDataGenerator(preprocessing_function=xception.preprocess_input)",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## 1. Fine-tune last layer of full network"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mn=Xception(include_top=False, input_shape=(sz,sz,3), pooling='avg')\noutp = Dense(2, activation='softmax')(mn.output)\nm = Model(mn.input, outp)\nfor l in m.layers[-1]: l.trainable=False\nm.compile(SGD(lr, momentum=0.9), 'categorical_crossentropy', metrics=['accuracy'])",
"execution_count": 10,
"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": 11,
"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": "m.fit_generator(trn_batches, nb_trn, workers=1, epochs=3,\n validation_data=val_batches, validation_steps=nb_val)",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Epoch 1/3\n360/360 [==============================] - 69s - loss: 0.3559 - acc: 0.8461 - val_loss: 0.2667 - val_acc: 0.8915\nEpoch 2/3\n360/360 [==============================] - 68s - loss: 0.2660 - acc: 0.8885 - val_loss: 0.2534 - val_acc: 0.8935\nEpoch 3/3\n360/360 [==============================] - 68s - loss: 0.2492 - acc: 0.8935 - val_loss: 0.2408 - val_acc: 0.9015\n"
},
{
"data": {
"text/plain": "<keras.callbacks.History at 0x7f8c8b8e2c88>"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"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": 7,
"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": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "360/360 [==============================] - 67s \n32/32 [==============================] - 6s \n"
}
]
},
{
"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": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fc.fit(trn_acts, fix_batches.classes, bs, 3, validation_data=(val_acts, val_batches.classes))",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Train on 23000 samples, validate on 2000 samples\nEpoch 1/3\n23000/23000 [==============================] - 1s - loss: 0.1217 - acc: 0.9518 - val_loss: 0.0912 - val_acc: 0.9610\nEpoch 2/3\n23000/23000 [==============================] - 1s - loss: 0.0817 - acc: 0.9688 - val_loss: 0.0836 - val_acc: 0.9665\nEpoch 3/3\n23000/23000 [==============================] - 0s - loss: 0.0756 - acc: 0.9713 - val_loss: 0.0797 - val_acc: 0.9665\n"
},
{
"data": {
"text/plain": "<keras.callbacks.History at 0x7f8c4c296e10>"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"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