To start you will need to download and unzip the competition data from Kaggle and ensure your directory structure looks like this

utils/
    vgg16.py
    utils.py
lesson1/
    redux.ipynb
    data/
        redux/
            train/
                cat.437.jpg
                dog.9924.jpg
                cat.1029.jpg
                dog.4374.jpg
            test/
                231.jpg
                325.jpg
                1235.jpg
                9923.jpg

You can download the data files from the competition page here or you can download them from the command line using the Kaggle CLI.

You should launch your notebook inside the lesson1 directory

cd lesson1
jupyter notebook

In [1]:
%pwd


Out[1]:
u'/home/ubuntu/courses/deeplearning1/nbs/lesson1'

In [1]:
import os, sys
cur_dir = os.getcwd()
lesson_home_dir = cur_dir
data_dir = cur_dir + '/data/redux'

In [2]:
#Allow relative imports to directories above lesson1/
sys.path.insert(1, os.path.join(sys.path[0], '..'))

#import modules
from utils import *
from vgg16 import Vgg16

#Instantiate plotting tool
#In Jupyter notebooks, you will need to run this command before doing any plotting
%matplotlib inline


Using gpu device 0: Tesla K80 (CNMeM is disabled, cuDNN 5103)
/home/ubuntu/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/__init__.py:600: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.
  warnings.warn(warn)
Using Theano backend.

Action Plan

  1. Create Validation and Sample sets - done
  2. Rearrange image files into their respective directories - done
  3. Finetune and Train model
  4. Generate predictions - done (wrote a function to gen pred file)
  5. Validate predictions - done (can get a prediction on a validation set)
  6. Submit predictions to Kaggle

In [5]:
#Start by setting up the directories. This only should be run once

%cd $data_dir
%mkdir valid
%mkdir results
%mkdir -p sample/train
%mkdir -p sample/test
%mkdir -p sample/valid
%mkdir -p sample/results
%mkdir -p test/unknown


/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux

In [12]:
#Move 2000 training instances to the validation folder - only run once

%cd $data_dir/train
g = glob('*jpg') #gets a list of every file matching the pattern
shuf = np.random.permutation(g)
for i in range(2000):    
    os.rename(shuf[i], data_dir + '/valid/' + shuf[i]) #renaming similar to mv


/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/train

In [14]:
#copy over 200,50 training and validation images to the sample folder - only run once
from shutil import copyfile
g = glob('*.jpg')
shuf = np.random.permutation(g)
for i in range(250):
    if i < 200:
        copyfile(shuf[i], data_dir + '/sample/train/' + shuf[i])
    else:
        copyfile(shuf[i], data_dir +'/sample/valid/' + shuf[i])

In [16]:
#Now create a directory for cats and for dogs - only run once
%cd $data_dir/train
#%mkdir dogs
#%mkdir cats
%mv dog.*.jpg dogs/
%mv cat.*.jpg cats/

%cd $data_dir/valid
#%mkdir dogs
#%mkdir cats
%mv dog.*.jpg dogs/
%mv cat.*.jpg cats/

%cd $data_dir/sample/train
#%mkdir dogs
#%mkdir cats
%mv dog.*.jpg dogs/
%mv cat.*.jpg cats/

%cd $data_dir/sample/valid
#%mkdir dogs
#%mkdir cats
%mv dog.*.jpg dogs/
%mv cat.*.jpg cats/


/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/train
/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/valid
/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/sample/train
/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/sample/valid

In [7]:
%cd $data_dir/test
%mv *.jpg unknown/


/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/test

Finetune the models and train


In [55]:
#set up packages and path
import sys
sys.path.append('/home/ubuntu/courses/deeplearning1/nbs/utils')
from __future__ import division,print_function

import os, json
from glob import glob
import numpy as np
np.set_printoptions(precision=4, linewidth=100)
from matplotlib import pyplot as plt

#pre-trained model package
import vgg16; reload(vgg16)
from vgg16 import Vgg16

def getPath(sample):
    #Takes in a boolean
    if sample:
        return "/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux/sample"
    else:
        return "/home/ubuntu/courses/deeplearning1/nbs/lesson1/data/redux"

In [ ]:

Create a prediction function


In [56]:
from sklearn.metrics import roc_auc_score, log_loss
from keras.layers.core import Flatten, Dense, Dropout, Lambda

import pandas as pd


def getPredsVal(mod, pred_path, batch_size = 8):
    #val batches is a generator from gen.flow_from_directory. 
    val_batches = mod.get_batches(pred_path, shuffle=False, batch_size = batch_size, class_mode='binary')
    #Preds is an array with [p_0, p_1]
    preds = mod.model.predict_generator(val_batches, val_batches.nb_sample)  
    #Need to better understand how much gets returned with the generator's next function
    truth = val_batches.next()[1] 
    print(roc_auc_score(truth, preds[:, 1]))
    print(log_loss(truth, preds[:, 1]))
    return truth, preds[:, 1]

def genSubmissions(mod, test_path, outfile):
    #To get predictions
    #Dogs are 2nd probability
    test_batches, test_preds = vgg.test(test_path)
    ids = [f.split('/')[1].split('.')[0] for f in test_batches.filenames]
    df = pd.DataFrame({'id': ids, 'label': test_preds[:,1]}).sort(columns='id')
    df.to_csv(res_path + '/'+ outfile, index=False)

    
batch_size = 64
path = getPath(False)
res_path = path + '/results'
test_path = path + '/test'

In [6]:
#Now train and fine tune, using code from lesson 1

batch_size = 64
path = getPath(False)
res_path = path + '/results'
test_path = path + '/test'


vgg = Vgg16()
# Grab a few images at a time for training and validation.
# NB: They must be in subdirectories named based on their category
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)

vgg.finetune(batches)

#run multiple epochs
nb_epochs = 3

vgg.model.optimizer.lr = 0.01

for i in range(nb_epochs):
    print('running epoch {}'.format(i))
    vgg.fit(batches, val_batches, nb_epoch = 1)
    vgg.model.save_weights(res_path + 'dog_cats_{}_lrp01.h5'.format(i))
    genSubmissions(vgg, test_path, 'dog_cats_{}_lrp01.csv'.format(i))


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
running epoch 0
Epoch 1/1
23000/23000 [==============================] - 586s - loss: 0.3778 - acc: 0.9678 - val_loss: 0.1582 - val_acc: 0.9835
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)
running epoch 1
Epoch 1/1
23000/23000 [==============================] - 592s - loss: 0.3539 - acc: 0.9740 - val_loss: 0.2167 - val_acc: 0.9810
Found 12500 images belonging to 1 classes.
running epoch 2
Epoch 1/1
23000/23000 [==============================] - 604s - loss: 0.3427 - acc: 0.9759 - val_loss: 0.1956 - val_acc: 0.9865
Found 12500 images belonging to 1 classes.

In [7]:
1+1


Out[7]:
2

In [4]:
#Now train and fine tune top layer, but add regularization
#Things to do: 1st add regularization to the last layer
#Retrain last two dense layers


batch_size = 64
path = getPath(False)
res_path = path + '/results'
test_path = path + '/test'


#First fine tune the model and fit it
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune(batches)
vgg.fit(batches, val_batches, nb_epoch = 1)


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 637s - loss: 0.1248 - acc: 0.9673 - val_loss: 0.0498 - val_acc: 0.9800

In [16]:
#Now reset all of dense layer to trainable and refit
from keras.layers.core import Flatten, Dense, Dropout, Lambda
layers = vgg.model.layers
for l in layers:
    print(type(l))
    
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]

for layer in layers[dense_idx:]: 
    layer.trainable = True


<class 'keras.layers.core.Lambda'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.pooling.MaxPooling2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.pooling.MaxPooling2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.pooling.MaxPooling2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.pooling.MaxPooling2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.convolutional.ZeroPadding2D'>
<class 'keras.layers.convolutional.Convolution2D'>
<class 'keras.layers.pooling.MaxPooling2D'>
<class 'keras.layers.core.Flatten'>
<class 'keras.layers.core.Dense'>
<class 'keras.layers.core.Dropout'>
<class 'keras.layers.core.Dense'>
<class 'keras.layers.core.Dropout'>
<class 'keras.layers.core.Dense'>

In [27]:
vgg.fit(batches, val_batches, nb_epoch = True)
vgg.model.save_weights(res_path + 'dog_cats_Retrain3.h5')
genSubmissions(vgg, test_path, 'dog_cats_Retrain3.csv')


Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [33]:
#Reset just the penultimate dense layer

def trainDenseLayers(l):
    '''
    1 for last layer
    2 for penultimate layer
    3 for just before that
    '''
    vmod = Vgg16()
    batches = vmod.get_batches(path + '/train', batch_size = batch_size)
    val_batches = vmod.get_batches(path + '/valid', batch_size = batch_size * 2)
    vmod.finetune(batches)
    vmod.fit(batches, val_batches, nb_epoch = 1)
    layers = vgg.model.layers
    if l > len(layers):
        l = 0
    dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][-l]
    for layer in layers[dense_idx:]: 
        layer.trainable = True
    vmod.fit(batches, val_batches, nb_epoch = 1)
    return vmod

In [34]:
vgg2 = trainDenseLayers(2)
vgg2.model.save_weights(res_path + 'dog_cats_Retrain2.h5')
genSubmissions(vgg2, test_path, 'dog_cats_Retrain2.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 637s - loss: 0.1248 - acc: 0.9683 - val_loss: 0.0451 - val_acc: 0.9830
Epoch 1/1
23000/23000 [==============================] - 638s - loss: 0.0959 - acc: 0.9777 - val_loss: 0.0573 - val_acc: 0.9795
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [36]:
#Don't fine tune last layer first
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune(batches)
layers = vgg.model.layers
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]
for layer in layers[dense_idx:]: 
    layer.trainable = True

vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_Retrain_NoPre.h5')
genSubmissions(vgg, test_path, 'dog_cats_Retrain_NoPre.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 637s - loss: 0.1177 - acc: 0.9696 - val_loss: 0.0502 - val_acc: 0.9850
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [37]:
'''
Things to try
- training convolutional layers
- regularization instead of dropout (or in addition)
- perturbing the images
'''
1+1


Out[37]:
2

In [38]:
#Instead of calling fine tune, add regularization to the last layer
import vgg16; reload(vgg16)
from vgg16 import Vgg16
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches)
vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_regp01.h5')
genSubmissions(vgg, test_path, 'dog_cats_regp01.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 635s - loss: 0.1493 - acc: 0.9679 - val_loss: 0.0493 - val_acc: 0.9860
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [39]:
#Instead of calling fine tune, add regularization to the last layer
#import vgg16; reload(vgg16)
#from vgg16 import Vgg16
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches, c = 0.001)
vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_regp001.h5')
genSubmissions(vgg, test_path, 'dog_cats_regp001.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 637s - loss: 0.1206 - acc: 0.9676 - val_loss: 0.0407 - val_acc: 0.9855
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [40]:
#Instead of calling fine tune, add regularization to the last layer
#import vgg16; reload(vgg16)
#from vgg16 import Vgg16
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches, c = 0.1)
vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_regp1.h5')
genSubmissions(vgg, test_path, 'dog_cats_regp1.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 638s - loss: 0.2659 - acc: 0.9677 - val_loss: 0.0496 - val_acc: 0.9810
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [42]:
#Don't fine tune last layer first, retrain penultimate, and fit last with regularization
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches, c = 0.1)
layers = vgg.model.layers
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]
for layer in layers[dense_idx:]: 
    layer.trainable = True

vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_Retrain_NoPre_regp1.h5')
genSubmissions(vgg, test_path, 'dog_cats_Retrain_NoPre_regp1.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 635s - loss: 0.2618 - acc: 0.9680 - val_loss: 0.0383 - val_acc: 0.9845
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:21: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [7]:
#Don't fine tune last layer first, retrain penultimate, and fit last with regularization
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches, c = 1)
layers = vgg.model.layers
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]
for layer in layers[dense_idx:]: 
    layer.trainable = True

vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_Retrain_NoPre_reg1.h5')
genSubmissions(vgg, test_path, 'dog_cats_Retrain_NoPre_reg1.csv')


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Epoch 1/1
23000/23000 [==============================] - 626s - loss: 0.4175 - acc: 0.9674 - val_loss: 0.0551 - val_acc: 0.9825
Found 12500 images belonging to 1 classes.
/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:23: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)

In [18]:
for i, l in enumerate(vgg.model.layers):
    print('{} {} {}'.format(type(l),l.trainable, i))


<class 'keras.layers.core.Lambda'> False 0
<class 'keras.layers.convolutional.ZeroPadding2D'> False 1
<class 'keras.layers.convolutional.Convolution2D'> False 2
<class 'keras.layers.convolutional.ZeroPadding2D'> False 3
<class 'keras.layers.convolutional.Convolution2D'> False 4
<class 'keras.layers.pooling.MaxPooling2D'> False 5
<class 'keras.layers.convolutional.ZeroPadding2D'> False 6
<class 'keras.layers.convolutional.Convolution2D'> False 7
<class 'keras.layers.convolutional.ZeroPadding2D'> False 8
<class 'keras.layers.convolutional.Convolution2D'> False 9
<class 'keras.layers.pooling.MaxPooling2D'> False 10
<class 'keras.layers.convolutional.ZeroPadding2D'> False 11
<class 'keras.layers.convolutional.Convolution2D'> False 12
<class 'keras.layers.convolutional.ZeroPadding2D'> False 13
<class 'keras.layers.convolutional.Convolution2D'> False 14
<class 'keras.layers.convolutional.ZeroPadding2D'> False 15
<class 'keras.layers.convolutional.Convolution2D'> False 16
<class 'keras.layers.pooling.MaxPooling2D'> False 17
<class 'keras.layers.convolutional.ZeroPadding2D'> False 18
<class 'keras.layers.convolutional.Convolution2D'> False 19
<class 'keras.layers.convolutional.ZeroPadding2D'> False 20
<class 'keras.layers.convolutional.Convolution2D'> False 21
<class 'keras.layers.convolutional.ZeroPadding2D'> False 22
<class 'keras.layers.convolutional.Convolution2D'> False 23
<class 'keras.layers.pooling.MaxPooling2D'> False 24
<class 'keras.layers.convolutional.ZeroPadding2D'> False 25
<class 'keras.layers.convolutional.Convolution2D'> False 26
<class 'keras.layers.convolutional.ZeroPadding2D'> False 27
<class 'keras.layers.convolutional.Convolution2D'> False 28
<class 'keras.layers.convolutional.ZeroPadding2D'> False 29
<class 'keras.layers.convolutional.Convolution2D'> False 30
<class 'keras.layers.pooling.MaxPooling2D'> False 31
<class 'keras.layers.core.Flatten'> False 32
<class 'keras.layers.core.Dense'> True 33
<class 'keras.layers.core.Dropout'> True 34
<class 'keras.layers.core.Dense'> True 35
<class 'keras.layers.core.Dropout'> True 36
<class 'keras.layers.core.Dense'> True 37

In [53]:
#Don't fine tune last layer first, retrain last conv block as well
vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
vgg.finetune_reg(batches, c = 0.1)
layers = vgg.model.layers
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]
for layer in layers[25:]: 
    layer.trainable = True

vgg.fit(batches, val_batches, nb_epoch = 1)
vgg.model.save_weights(res_path + 'dog_cats_RetrainConv1_NoPre_regp1.h5')
genSubmissions(vgg, test_path, 'dog_cats_RetrainConv1_NoPre_regp1.csv')


---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-53-9564821232b3> in <module>()
      1 #Don't fine tune last layer first, retrain last conv block as well
----> 2 vgg = Vgg16()
      3 batches = vgg.get_batches(path + '/train', batch_size = batch_size)
      4 val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)
      5 vgg.finetune_reg(batches, c = 0.1)

/home/ubuntu/courses/deeplearning1/nbs/utils/vgg16.pyc in __init__(self)
     32     def __init__(self):
     33         self.FILE_PATH = 'http://www.platform.ai/models/'
---> 34         self.create()
     35         self.get_classes()
     36 

/home/ubuntu/courses/deeplearning1/nbs/utils/vgg16.pyc in create(self)
     74 
     75         self.ConvBlock(2, 64)
---> 76         self.ConvBlock(2, 128)
     77         self.ConvBlock(3, 256)
     78         self.ConvBlock(3, 512)

/home/ubuntu/courses/deeplearning1/nbs/utils/vgg16.pyc in ConvBlock(self, layers, filters)
     55         for i in range(layers):
     56             model.add(ZeroPadding2D((1, 1)))
---> 57             model.add(Convolution2D(filters, 3, 3, activation='relu'))
     58         model.add(MaxPooling2D((2, 2), strides=(2, 2)))
     59 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/models.pyc in add(self, layer)
    306                  output_shapes=[self.outputs[0]._keras_shape])
    307         else:
--> 308             output_tensor = layer(self.outputs[0])
    309             if type(output_tensor) is list:
    310                 raise Exception('All layers in a Sequential model '

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
    485                                     '`layer.build(batch_input_shape)`')
    486             if len(input_shapes) == 1:
--> 487                 self.build(input_shapes[0])
    488             else:
    489                 self.build(input_shapes)

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/layers/convolutional.pyc in build(self, input_shape)
    408         else:
    409             raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
--> 410         self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
    411         if self.bias:
    412             self.b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/initializations.pyc in glorot_uniform(shape, name, dim_ordering)
     57     fan_in, fan_out = get_fans(shape, dim_ordering=dim_ordering)
     58     s = np.sqrt(6. / (fan_in + fan_out))
---> 59     return uniform(shape, s, name=name)
     60 
     61 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/initializations.pyc in uniform(shape, scale, name)
     30 
     31 def uniform(shape, scale=0.05, name=None):
---> 32     return K.random_uniform_variable(shape, -scale, scale, name=name)
     33 
     34 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in random_uniform_variable(shape, low, high, dtype, name)
    139 def random_uniform_variable(shape, low, high, dtype=_FLOATX, name=None):
    140     return variable(np.random.uniform(low=low, high=high, size=shape),
--> 141                     dtype=dtype, name=name)
    142 
    143 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in variable(value, dtype, name)
     64     else:
     65         value = np.asarray(value, dtype=dtype)
---> 66         return theano.shared(value=value, name=name, strict=False)
     67 
     68 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/theano/compile/sharedvalue.pyc in shared(value, name, strict, allow_downcast, **kwargs)
    245             try:
    246                 var = ctor(value, name=name, strict=strict,
--> 247                            allow_downcast=allow_downcast, **kwargs)
    248                 utils.add_tag_trace(var)
    249                 return var

/home/ubuntu/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/var.pyc in float32_shared_constructor(value, name, strict, allow_downcast, borrow, broadcastable, target)
    238         # type.broadcastable is guaranteed to be a tuple, which this next
    239         # function requires
--> 240         deviceval = type_support_filter(value, type.broadcastable, False, None)
    241 
    242     try:

MemoryError: ('Error allocating 589824 bytes of device memory (out of memory).', "you might consider using 'theano.shared(..., borrow=True)'")

In [21]:
1+1


Out[21]:
2

In [59]:
#Add regularization to all of the dense layers
import vgg16; reload(vgg16)
from vgg16 import Vgg16
from keras.regularizers import l2, l1
from keras.layers.core import Flatten, Dense, Dropout, Lambda

vgg = Vgg16()
batches = vgg.get_batches(path + '/train', batch_size = batch_size)
val_batches = vgg.get_batches(path + '/valid', batch_size = batch_size * 2)

c = 0.1

vgg.model.pop()

for layer in vgg.model.layers: 
    layer.trainable=False
    
layers = vgg.model.layers
dense_idx = [index for index,layer in enumerate(layers) if type(layer) is Dense][0]
for layer in layers[dense_idx:]: 
    layer.trainable = True


vgg.model.add(Dense(batches.nb_class, activation='softmax', W_regularizer = l2(c)))

vgg.compile()
for layer in vgg.model.layers: 
    print(layer)

vgg.fit(batches, val_batches, nb_epoch = 1)


Found 23000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
<keras.layers.core.Lambda object at 0x7f5e6a9b5f10>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e3646a9d0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e643c5390>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e62937bd0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e62937790>
<keras.layers.pooling.MaxPooling2D object at 0x7f5e3646aa50>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e6a9ac250>
<keras.layers.convolutional.Convolution2D object at 0x7f5e363b6990>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e6a9bf110>
<keras.layers.convolutional.Convolution2D object at 0x7f5e6acfa790>
<keras.layers.pooling.MaxPooling2D object at 0x7f5e6a9acb50>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e68796950>
<keras.layers.convolutional.Convolution2D object at 0x7f5e721d2150>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e364d8d90>
<keras.layers.convolutional.Convolution2D object at 0x7f5e3649b2d0>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e35c795d0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e35ca5390>
<keras.layers.pooling.MaxPooling2D object at 0x7f5e6a9bf6d0>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e6a0d42d0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e6bc56510>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e69d27b50>
<keras.layers.convolutional.Convolution2D object at 0x7f5e6b467c50>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e830b65d0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e830b6b90>
<keras.layers.pooling.MaxPooling2D object at 0x7f5e6a0d4a10>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e7d33f2d0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e68b52ed0>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e35896fd0>
<keras.layers.convolutional.Convolution2D object at 0x7f5e6770efd0>
<keras.layers.convolutional.ZeroPadding2D object at 0x7f5e627ec610>
<keras.layers.convolutional.Convolution2D object at 0x7f5e6b804750>
<keras.layers.pooling.MaxPooling2D object at 0x7f5e7d33f6d0>
<keras.layers.core.Flatten object at 0x7f5e69599090>
<keras.layers.core.Dense object at 0x7f5e64b22050>
<keras.layers.core.Dropout object at 0x7f5e64b22c10>
<keras.layers.core.Dense object at 0x7f5e69599f90>
<keras.layers.core.Dropout object at 0x7f5e6ba0b590>
<keras.layers.core.Dense object at 0x7f5e63ec7050>
Epoch 1/1
 1344/23000 [>.............................] - ETA: 581s - loss: 3.6722 - acc: 0.7768
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-59-4041f6512442> in <module>()
     28     print(layer)
     29 
---> 30 vgg.fit(batches, val_batches, nb_epoch = 1)

/home/ubuntu/courses/deeplearning1/nbs/utils/vgg16.pyc in fit(self, batches, val_batches, nb_epoch)
    144     def fit(self, batches, val_batches, nb_epoch=1):
    145         self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch,
--> 146                 validation_data=val_batches, nb_val_samples=val_batches.nb_sample)
    147 
    148 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/models.pyc in fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe, **kwargs)
    872                                         max_q_size=max_q_size,
    873                                         nb_worker=nb_worker,
--> 874                                         pickle_safe=pickle_safe)
    875 
    876     def evaluate_generator(self, generator, val_samples, max_q_size=10, nb_worker=1, pickle_safe=False, **kwargs):

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe)
   1441                     outs = self.train_on_batch(x, y,
   1442                                                sample_weight=sample_weight,
-> 1443                                                class_weight=class_weight)
   1444                 except:
   1445                     _stop.set()

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in train_on_batch(self, x, y, sample_weight, class_weight)
   1219             ins = x + y + sample_weights
   1220         self._make_train_function()
-> 1221         outputs = self.train_function(ins)
   1222         if len(outputs) == 1:
   1223             return outputs[0]

/home/ubuntu/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in __call__(self, inputs)
    715     def __call__(self, inputs):
    716         assert type(inputs) in {list, tuple}
--> 717         return self.function(*inputs)
    718 
    719 

/home/ubuntu/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    857         t0_fn = time.time()
    858         try:
--> 859             outputs = self.fn()
    860         except Exception:
    861             if hasattr(self.fn, 'position_of_error'):

KeyboardInterrupt: 

In [36]:
vgg.model.save_weights(res_path + 'dog_cats_RetrainConv1_NoPre_regp1.h5')
genSubmissions(vgg, test_path, 'dog_cats_RetrainConv1_NoPre_regp1.csv')


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-36-8cb0acaa3b0a> in <module>()
----> 1 vgg.model.pop().pop().pop().pop().pop()

AttributeError: 'NoneType' object has no attribute 'pop'

In [54]:
%reset


Once deleted, variables cannot be recovered. Proceed (y/[n])? y

In [ ]: