Created
December 20, 2016 00:39
-
-
Save eshmaapps/a65790470f01cd5b146eb6bc9f5a44b2 to your computer and use it in GitHub Desktop.
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Using Convolutional Neural Networks" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Welcome to the first week of the first deep learning certificate! We're going to use convolutional neural networks (CNNs) to allow our computer to see - something that is only possible thanks to deep learning." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"hidden": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: Unable to get the number of gpus available: unknown error)\n", | |
"WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu0 is not available (error: Unable to get the number of gpus available: unknown error)\n" | |
] | |
} | |
], | |
"source": [ | |
"%matplotlib inline\n", | |
"from theano.sandbox import cuda\n", | |
"cuda.use('gpu0')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"hidden": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using Theano backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"from __future__ import division,print_function\n", | |
"\n", | |
"import os, json\n", | |
"from glob import glob\n", | |
"import numpy as np\n", | |
"np.set_printoptions(precision=4, linewidth=100)\n", | |
"from matplotlib import pyplot as plt\n", | |
"from keras.preprocessing import image\n", | |
"from IPython.display import FileLink" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"hidden": true | |
}, | |
"source": [ | |
"We have created a file most imaginatively called 'utils.py' to store any little convenience functions we'll want to use. We will discuss these as we use them." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false, | |
"hidden": true, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/ubuntu/nbs\n" | |
] | |
} | |
], | |
"source": [ | |
"%cd ~/nbs/\n", | |
"import utils; reload(utils)\n", | |
"from utils import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"batch_size=64" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"from keras.layers import BatchNormalization, Flatten, Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D\n", | |
"from keras.models import Model, Sequential\n", | |
"from keras.optimizers import Adam, RMSprop" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Use a pretrained VGG model with our **Vgg16** class" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Our first step is simply to use a model that has been fully created for us, which can recognise a wide variety (1,000 categories) of images. We will use 'VGG', which won the 2014 Imagenet competition, and is a very simple model to create and understand. The VGG Imagenet team created both a larger, slower, slightly more accurate model (*VGG 19*) and a smaller, faster model (*VGG 16*). We will be using VGG 16 since the much slower performance of VGG19 is generally not worth the very minor improvement in accuracy.\n", | |
"\n", | |
"We have created a python class, *Vgg16*, which makes using the VGG 16 model very straightforward. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/ubuntu/nbs/facialrecog\n" | |
] | |
} | |
], | |
"source": [ | |
"%cd ~/nbs/facialrecog\n", | |
"current_dir = os.getcwd()\n", | |
"DATA_HOME_DIR = current_dir\n", | |
"path = current_dir + '/'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found 4 images belonging to 1 classes.\n" | |
] | |
} | |
], | |
"source": [ | |
"test_batches = get_batches(path+'test', shuffle=False, batch_size=64)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found 963 images belonging to 610 classes.\n", | |
"Found 5770 images belonging to 610 classes.\n" | |
] | |
} | |
], | |
"source": [ | |
"val_batches = get_batches(path+'valid', batch_size=batch_size)\n", | |
"gen_t = image.ImageDataGenerator(rotation_range=15, height_shift_range=0.05, \n", | |
" shear_range=0.1, channel_shift_range=20, width_shift_range=0.1)\n", | |
"batches = get_batches(path+'train', gen_t, batch_size=batch_size)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"val_classes = val_batches.classes\n", | |
"trn_classes = batches.classes\n", | |
"val_labels = onehot(val_classes)\n", | |
"trn_labels = onehot(trn_classes)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"ename": "MemoryError", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mMemoryError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-13-3538aade5b02>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mvgg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mVgg16\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvgg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mlast_conv_idx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mConvolution2D\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32mvgg16.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self)\u001b[0m\n", | |
"\u001b[0;32mvgg16.pyc\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(self)\u001b[0m\n", | |
"\u001b[0;32mvgg16.pyc\u001b[0m in \u001b[0;36mFCBlock\u001b[0;34m(self)\u001b[0m\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/models.pyc\u001b[0m in \u001b[0;36madd\u001b[0;34m(self, layer)\u001b[0m\n\u001b[1;32m 306\u001b[0m output_shapes=[self.outputs[0]._keras_shape])\n\u001b[1;32m 307\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 308\u001b[0;31m \u001b[0moutput_tensor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 309\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput_tensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 310\u001b[0m raise Exception('All layers in a Sequential model '\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, x, mask)\u001b[0m\n\u001b[1;32m 485\u001b[0m '`layer.build(batch_input_shape)`')\n\u001b[1;32m 486\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_shapes\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 487\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_shapes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 488\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 489\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_shapes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/layers/core.pyc\u001b[0m in \u001b[0;36mbuild\u001b[0;34m(self, input_shape)\u001b[0m\n\u001b[1;32m 693\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 694\u001b[0m self.W = self.init((input_dim, self.output_dim),\n\u001b[0;32m--> 695\u001b[0;31m name='{}_W'.format(self.name))\n\u001b[0m\u001b[1;32m 696\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbias\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 697\u001b[0m self.b = K.zeros((self.output_dim,),\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/initializations.pyc\u001b[0m in \u001b[0;36mglorot_uniform\u001b[0;34m(shape, name, dim_ordering)\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0mfan_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfan_out\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_fans\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdim_ordering\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdim_ordering\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m6.\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfan_in\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfan_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 59\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0muniform\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 60\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/initializations.pyc\u001b[0m in \u001b[0;36muniform\u001b[0;34m(shape, scale, name)\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0muniform\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscale\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.05\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mK\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom_uniform_variable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mscale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc\u001b[0m in \u001b[0;36mrandom_uniform_variable\u001b[0;34m(shape, low, high, dtype, name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrandom_uniform_variable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhigh\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0m_FLOATX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 140\u001b[0;31m return variable(np.random.uniform(low=low, high=high, size=shape),\n\u001b[0m\u001b[1;32m 141\u001b[0m dtype=dtype, name=name)\n\u001b[1;32m 142\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13575)\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2902)\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;31mMemoryError\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"vgg = Vgg16()\n", | |
"model=vgg.model\n", | |
"last_conv_idx = [i for i,l in enumerate(model.layers) if type(l) is Convolution2D][-1]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"conv_layers = model.layers[:last_conv_idx+1]\n", | |
"conv_model = Sequential(conv_layers)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"conv_feat = conv_model.predict_generator(batches, batches.nb_sample)\n", | |
"save_array(path+'results/my_conv_feat.dat', conv_feat)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"conv_val_feat = conv_model.predict_generator(val_batches, val_batches.nb_sample)\n", | |
"save_array(path+'results/my_conv_val_feat.dat', conv_val_feat)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found 4 images belonging to 1 classes.\n" | |
] | |
} | |
], | |
"source": [ | |
"test_batches = get_batches(path+'test', shuffle=False, batch_size=64)\n", | |
"conv_test_feat = conv_model.predict_generator(test_batches, test_batches.nb_sample)\n", | |
"save_array(path+'results/my_conv_test_feat.dat', conv_test_feat)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def get_bn_layers(p):\n", | |
" return [\n", | |
" MaxPooling2D(input_shape=conv_layers[-1].output_shape[1:]),\n", | |
" Flatten(),\n", | |
" Dropout(p/2),\n", | |
" Dense(256, activation='relu'),\n", | |
" BatchNormalization(),\n", | |
" Dropout(p/2),\n", | |
" Dense(512, activation='relu'),\n", | |
" BatchNormalization(),\n", | |
" Dropout(p),\n", | |
" Dense(610, activation='softmax')\n", | |
" ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"ename": "MemoryError", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mMemoryError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-15-8790b33e70e3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mconv_val_feat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m'results/my_conv_val_feat.dat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mconv_test_feat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m'results/my_conv_test_feat.dat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mconv_feat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m'results/my_conv_feat.dat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32mutils.pyc\u001b[0m in \u001b[0;36mload_array\u001b[0;34m(fname)\u001b[0m\n", | |
"\u001b[0;32mbcolz/carray_ext.pyx\u001b[0m in \u001b[0;36mbcolz.carray_ext.carray.__getitem__ (bcolz/carray_ext.c:25668)\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;31mMemoryError\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"\n", | |
"conv_val_feat = load_array(path+'results/my_conv_val_feat.dat')\n", | |
"conv_test_feat = load_array(path+'results/my_conv_test_feat.dat')\n", | |
"conv_feat = load_array(path+'results/my_conv_feat.dat')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"val_classes = val_batches.classes\n", | |
"trn_classes = batches.classes\n", | |
"val_labels = onehot(val_classes)\n", | |
"trn_labels = onehot(trn_classes)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"p=0.7" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 5770 samples, validate on 963 samples\n", | |
"Epoch 1/10\n", | |
"5770/5770 [==============================] - 2s - loss: 7.8366 - acc: 0.0023 - val_loss: 6.5577 - val_acc: 0.0010\n", | |
"Epoch 2/10\n", | |
"5770/5770 [==============================] - 2s - loss: 7.4893 - acc: 0.0050 - val_loss: 6.4835 - val_acc: 0.0052\n", | |
"Epoch 3/10\n", | |
"5770/5770 [==============================] - 2s - loss: 7.2147 - acc: 0.0050 - val_loss: 6.4956 - val_acc: 0.0104\n", | |
"Epoch 4/10\n", | |
"5770/5770 [==============================] - 2s - loss: 6.9468 - acc: 0.0104 - val_loss: 6.4926 - val_acc: 0.0135\n", | |
"Epoch 5/10\n", | |
"5770/5770 [==============================] - 2s - loss: 6.7351 - acc: 0.0116 - val_loss: 6.4934 - val_acc: 0.0156\n", | |
"Epoch 6/10\n", | |
"5770/5770 [==============================] - 2s - loss: 6.4616 - acc: 0.0199 - val_loss: 6.4651 - val_acc: 0.0197\n", | |
"Epoch 7/10\n", | |
"5770/5770 [==============================] - 2s - loss: 6.2319 - acc: 0.0322 - val_loss: 6.4485 - val_acc: 0.0301\n", | |
"Epoch 8/10\n", | |
"5770/5770 [==============================] - 2s - loss: 6.0362 - acc: 0.0416 - val_loss: 6.4391 - val_acc: 0.0322\n", | |
"Epoch 9/10\n", | |
"5770/5770 [==============================] - 2s - loss: 5.7827 - acc: 0.0653 - val_loss: 6.4306 - val_acc: 0.0353\n", | |
"Epoch 10/10\n", | |
"5770/5770 [==============================] - 2s - loss: 5.5176 - acc: 0.0828 - val_loss: 6.4288 - val_acc: 0.0374\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f79860d4bd0>" | |
] | |
}, | |
"execution_count": 67, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bn_model = Sequential(get_bn_layers(p))\n", | |
"bn_model.compile(Adam(lr=1e-4), loss='categorical_crossentropy', metrics=['accuracy'])\n", | |
"bn_model.fit(conv_feat, trn_labels, batch_size=batch_size, nb_epoch=10, \n", | |
" validation_data=(conv_val_feat, val_labels))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 5770 samples, validate on 963 samples\n", | |
"Epoch 1/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0570 - acc: 0.9931 - val_loss: 8.2239 - val_acc: 0.0301\n", | |
"Epoch 2/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0575 - acc: 0.9913 - val_loss: 8.2207 - val_acc: 0.0312\n", | |
"Epoch 3/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0543 - acc: 0.9922 - val_loss: 8.2572 - val_acc: 0.0270\n", | |
"Epoch 4/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0522 - acc: 0.9913 - val_loss: 8.2878 - val_acc: 0.0301\n", | |
"Epoch 5/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0573 - acc: 0.9899 - val_loss: 8.2983 - val_acc: 0.0322\n", | |
"Epoch 6/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0551 - acc: 0.9913 - val_loss: 8.2227 - val_acc: 0.0249\n", | |
"Epoch 7/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0482 - acc: 0.9924 - val_loss: 8.4236 - val_acc: 0.0270\n", | |
"Epoch 8/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0512 - acc: 0.9920 - val_loss: 8.5516 - val_acc: 0.0363\n", | |
"Epoch 9/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0563 - acc: 0.9915 - val_loss: 8.2993 - val_acc: 0.0249\n", | |
"Epoch 10/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0512 - acc: 0.9927 - val_loss: 8.2070 - val_acc: 0.0249\n", | |
"Epoch 11/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0587 - acc: 0.9903 - val_loss: 8.1971 - val_acc: 0.0249\n", | |
"Epoch 12/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0500 - acc: 0.9934 - val_loss: 8.2294 - val_acc: 0.0260\n", | |
"Epoch 13/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0471 - acc: 0.9932 - val_loss: 8.4773 - val_acc: 0.0322\n", | |
"Epoch 14/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0429 - acc: 0.9936 - val_loss: 8.6017 - val_acc: 0.0332\n", | |
"Epoch 15/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0491 - acc: 0.9913 - val_loss: 8.4052 - val_acc: 0.0291\n", | |
"Epoch 16/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0472 - acc: 0.9917 - val_loss: 8.3859 - val_acc: 0.0322\n", | |
"Epoch 17/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0476 - acc: 0.9915 - val_loss: 8.3207 - val_acc: 0.0280\n", | |
"Epoch 18/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0528 - acc: 0.9906 - val_loss: 8.2627 - val_acc: 0.0301\n", | |
"Epoch 19/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0490 - acc: 0.9931 - val_loss: 8.2996 - val_acc: 0.0239\n", | |
"Epoch 20/20\n", | |
"5770/5770 [==============================] - 2s - loss: 0.0464 - acc: 0.9917 - val_loss: 8.4025 - val_acc: 0.0301\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f79a4834190>" | |
] | |
}, | |
"execution_count": 76, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bn_model.fit(conv_feat, trn_labels, batch_size=batch_size, nb_epoch=20, \n", | |
" validation_data=(conv_val_feat, val_labels))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Exception in thread Thread-15:\n", | |
"Traceback (most recent call last):\n", | |
" File \"/home/ubuntu/anaconda2/lib/python2.7/threading.py\", line 801, in __bootstrap_inner\n", | |
" self.run()\n", | |
" File \"/home/ubuntu/anaconda2/lib/python2.7/threading.py\", line 754, in run\n", | |
" self.__target(*self.__args, **self.__kwargs)\n", | |
" File \"/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/training.py\", line 425, in data_generator_task\n", | |
" generator_output = next(generator)\n", | |
"TypeError: instancemethod object is not an iterator\n", | |
"\n" | |
] | |
}, | |
{ | |
"ename": "ValueError", | |
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-75-547c9d011859>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbn_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevaluate_generator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconv_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict_generator\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mconv_test_feat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/models.pyc\u001b[0m in \u001b[0;36mevaluate_generator\u001b[0;34m(self, generator, val_samples, max_q_size, nb_worker, pickle_safe, **kwargs)\u001b[0m\n\u001b[1;32m 914\u001b[0m \u001b[0mmax_q_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmax_q_size\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 915\u001b[0m \u001b[0mnb_worker\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnb_worker\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 916\u001b[0;31m pickle_safe=pickle_safe)\n\u001b[0m\u001b[1;32m 917\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 918\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpredict_generator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgenerator\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mval_samples\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_q_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnb_worker\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpickle_safe\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc\u001b[0m in \u001b[0;36mevaluate_generator\u001b[0;34m(self, generator, val_samples, max_q_size, nb_worker, pickle_safe)\u001b[0m\n\u001b[1;32m 1527\u001b[0m pickle_safe=pickle_safe)\n\u001b[1;32m 1528\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1529\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0mprocessed_samples\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mval_samples\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1530\u001b[0m \u001b[0mgenerator_output\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1531\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_stop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_set\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" | |
] | |
} | |
], | |
"source": [ | |
"bn_model.save_weights(path+'results/conv8.h5')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"bn_model.load_weights(path+'results/conv8.h5')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'bn_model' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-12-255103b3e492>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mtest_x\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_batches\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpreds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbn_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_x\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtest_batches\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnb_sample\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'bn_model' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"test_x = np.array(test_batches)\n", | |
"\n", | |
"preds = bn_model.predict(test_x, test_batches.nb_sample)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"filenames = val_batches.filenames\n", | |
"\n", | |
"n_view = 4\n", | |
"def plots_idx(idx, titles=None):\n", | |
" plots([image.load_img(path + 'valid/' + filenames[i]) for i in idx], titles=titles)\n", | |
"probs = model.predict_proba(val_features, batch_size=batch_size)[:,0]\n" | |
] | |
} | |
], | |
"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" | |
}, | |
"nav_menu": {}, | |
"nbpresent": { | |
"slides": { | |
"28b43202-5690-4169-9aca-6b9dabfeb3ec": { | |
"id": "28b43202-5690-4169-9aca-6b9dabfeb3ec", | |
"prev": null, | |
"regions": { | |
"3bba644a-cf4d-4a49-9fbd-e2554428cf9f": { | |
"attrs": { | |
"height": 0.8, | |
"width": 0.8, | |
"x": 0.1, | |
"y": 0.1 | |
}, | |
"content": { | |
"cell": "f3d3a388-7e2a-4151-9b50-c20498fceacc", | |
"part": "whole" | |
}, | |
"id": "3bba644a-cf4d-4a49-9fbd-e2554428cf9f" | |
} | |
} | |
}, | |
"8104def2-4b68-44a0-8f1b-b03bf3b2a079": { | |
"id": "8104def2-4b68-44a0-8f1b-b03bf3b2a079", | |
"prev": "28b43202-5690-4169-9aca-6b9dabfeb3ec", | |
"regions": { | |
"7dded777-1ddf-4100-99ae-25cf1c15b575": { | |
"attrs": { | |
"height": 0.8, | |
"width": 0.8, | |
"x": 0.1, | |
"y": 0.1 | |
}, | |
"content": { | |
"cell": "fe47bd48-3414-4657-92e7-8b8d6cb0df00", | |
"part": "whole" | |
}, | |
"id": "7dded777-1ddf-4100-99ae-25cf1c15b575" | |
} | |
} | |
} | |
}, | |
"themes": {} | |
}, | |
"toc": { | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": 6, | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment