Fisheries competition

In this notebook we're going to investigate a range of different architectures for the Kaggle fisheries competition. The video states that vgg.py and vgg_ft() from utils.py have been updated to include VGG with batch normalization, but this is not the case. We've instead created a new file vgg_bn.py and an additional method vgg_ft_bn() (which is already in utils.py) which we use in this notebook.


In [1]:
from __future__ import division, print_function
%matplotlib inline
from importlib import reload  # Python 3
import utils; reload(utils)
from utils import *


Using cuDNN version 5105 on context None
Mapped name None to device cuda0: GeForce GTX TITAN X (0000:04:00.0)
Using Theano backend.

Setup dirs

We create the validation and sample sets in the usual way.


In [2]:
%cd data/fish
%cd train
%mkdir ../valid


/home/roebius/pj/fastai/nbs/data/fish
/home/roebius/pj/fastai/nbs/data/fish/train

In [3]:
g = glob('*')
for d in g: os.mkdir('../valid/'+d)

g = glob('*/*.jpg')
shuf = np.random.permutation(g)
for i in range(500): os.rename(shuf[i], '../valid/' + shuf[i])

In [4]:
%mkdir ../sample
%mkdir ../sample/train
%mkdir ../sample/valid

In [5]:
from shutil import copyfile

g = glob('*')
for d in g: 
    os.mkdir('../sample/train/'+d)
    os.mkdir('../sample/valid/'+d)

In [6]:
g = glob('*/*.jpg')
shuf = np.random.permutation(g)
for i in range(400): copyfile(shuf[i], '../sample/train/' + shuf[i])

%cd ../valid

g = glob('*/*.jpg')
shuf = np.random.permutation(g)
for i in range(200): copyfile(shuf[i], '../sample/valid/' + shuf[i])

%cd ..


/home/roebius/pj/fastai/nbs/data/fish/valid
/home/roebius/pj/fastai/nbs/data/fish

In [7]:
%mkdir results
%mkdir sample/results
%cd ../..


/home/roebius/pj/fastai/nbs

In [8]:
path = "data/fish/"
#path = "data/fish/sample/"
model_path = path + 'models/'
if not os.path.exists(model_path): os.mkdir(model_path)
#batch_size=64
batch_size=4

In [9]:
batches = get_batches(path+'train', batch_size=batch_size)
val_batches = get_batches(path+'valid', batch_size=batch_size*2, shuffle=False)

(val_classes, trn_classes, val_labels, trn_labels, 
    val_filenames, filenames, test_filenames) = get_classes(path)


Found 3277 images belonging to 8 classes.
Found 500 images belonging to 8 classes.
Found 3277 images belonging to 8 classes.
Found 500 images belonging to 8 classes.
Found 1000 images belonging to 1 classes.

Sometimes it's helpful to have just the filenames, without the path.


In [10]:
raw_filenames = [f.split('/')[-1] for f in filenames]
raw_test_filenames = [f.split('/')[-1] for f in test_filenames]
raw_val_filenames = [f.split('/')[-1] for f in val_filenames]

Basic VGG

We start with our usual VGG approach. We will be using VGG with batch normalization. We explained how to add batch normalization to VGG in the imagenet_batchnorm notebook. VGG with batch normalization is implemented in vgg_bn.py, and there is a version of vgg_ft (our fine tuning function) with batch norm called vgg_ft_bn in utils.py.

Initial model

First we create a simple fine-tuned VGG model to be our starting point.


In [152]:
from vgg16bn import Vgg16BN
model = vgg_ft_bn(8)

In [153]:
trn = get_data(path+'train')
val = get_data(path+'valid')


Found 3277 images belonging to 8 classes.
Found 500 images belonging to 8 classes.

In [154]:
test = get_data(path+'test')


Found 1000 images belonging to 1 classes.

In [155]:
save_array(path+'results/trn.dat', trn)
save_array(path+'results/val.dat', val)

In [156]:
save_array(path+'results/test.dat', test)

In [157]:
trn = load_array(path+'results/trn.dat')
val = load_array(path+'results/val.dat')

In [158]:
test = load_array(path+'results/test.dat')

In [159]:
gen = image.ImageDataGenerator()

In [160]:
model.compile(optimizer=Adam(1e-3),
       loss='categorical_crossentropy', metrics=['accuracy'])

In [161]:
model.fit(trn, trn_labels, batch_size=batch_size, epochs=3, validation_data=(val, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/3
3277/3277 [==============================] - 39s - loss: 3.7058 - acc: 0.4736 - val_loss: 1.9251 - val_acc: 0.7460
Epoch 2/3
3277/3277 [==============================] - 40s - loss: 3.5664 - acc: 0.5548 - val_loss: 2.3395 - val_acc: 0.7400
Epoch 3/3
3277/3277 [==============================] - 41s - loss: 3.6398 - acc: 0.5746 - val_loss: 2.3794 - val_acc: 0.7680
Out[161]:
<keras.callbacks.History at 0x7f09ebbbe048>

In [162]:
model.save_weights(path+'results/ft1.h5')

Precompute convolutional output

We pre-compute the output of the last convolution layer of VGG, since we're unlikely to need to fine-tune those layers. (All following analysis will be done on just the pre-computed convolutional features.)


In [163]:
model.load_weights(path+'results/ft1.h5')

In [164]:
conv_layers,fc_layers = split_at(model, Conv2D)

In [165]:
conv_model = Sequential(conv_layers)

In [166]:
conv_feat = conv_model.predict(trn)
conv_val_feat = conv_model.predict(val)

In [167]:
conv_test_feat = conv_model.predict(test)

In [168]:
save_array(path+'results/conv_val_feat.dat', conv_val_feat)
save_array(path+'results/conv_feat.dat', conv_feat)

In [169]:
save_array(path+'results/conv_test_feat.dat', conv_test_feat)

In [170]:
conv_feat = load_array(path+'results/conv_feat.dat')
conv_val_feat = load_array(path+'results/conv_val_feat.dat')

In [171]:
conv_test_feat = load_array(path+'results/conv_test_feat.dat')

In [172]:
conv_val_feat.shape


Out[172]:
(500, 512, 14, 14)

Train model

We can now create our first baseline model - a simple 3-layer FC net.


In [173]:
def get_bn_layers(p):
    return [
        MaxPooling2D(input_shape=conv_layers[-1].output_shape[1:]),
        BatchNormalization(axis=1),
        Dropout(p/4),
        Flatten(),
        Dense(512, activation='relu'),
        BatchNormalization(),
        Dropout(p),
        Dense(512, activation='relu'),
        BatchNormalization(),
        Dropout(p/2),
        Dense(8, activation='softmax')
    ]

In [174]:
p=0.6

In [175]:
bn_model = Sequential(get_bn_layers(p))
bn_model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

In [176]:
bn_model.fit(conv_feat, trn_labels, batch_size=batch_size, epochs=3, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/3
3277/3277 [==============================] - 7s - loss: 1.5678 - acc: 0.5233 - val_loss: 0.7572 - val_acc: 0.7720
Epoch 2/3
3277/3277 [==============================] - 7s - loss: 0.9998 - acc: 0.6665 - val_loss: 0.3161 - val_acc: 0.9020
Epoch 3/3
3277/3277 [==============================] - 7s - loss: 0.7647 - acc: 0.7501 - val_loss: 0.3276 - val_acc: 0.8840
Out[176]:
<keras.callbacks.History at 0x7f09ea3f68d0>

In [177]:
bn_model.optimizer.lr = 1e-4

In [178]:
bn_model.fit(conv_feat, trn_labels, batch_size=batch_size, epochs=7, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/7
3277/3277 [==============================] - 7s - loss: 0.6448 - acc: 0.7839 - val_loss: 0.2559 - val_acc: 0.9320
Epoch 2/7
3277/3277 [==============================] - 7s - loss: 0.5588 - acc: 0.8145 - val_loss: 0.1866 - val_acc: 0.9400
Epoch 3/7
3277/3277 [==============================] - 7s - loss: 0.5256 - acc: 0.8279 - val_loss: 0.4144 - val_acc: 0.9000
Epoch 4/7
3277/3277 [==============================] - 7s - loss: 0.4657 - acc: 0.8456 - val_loss: 0.2483 - val_acc: 0.9340
Epoch 5/7
3277/3277 [==============================] - 7s - loss: 0.4360 - acc: 0.8529 - val_loss: 0.2196 - val_acc: 0.9180
Epoch 6/7
3277/3277 [==============================] - 7s - loss: 0.4196 - acc: 0.8612 - val_loss: 0.1393 - val_acc: 0.9600
Epoch 7/7
3277/3277 [==============================] - 7s - loss: 0.3840 - acc: 0.8694 - val_loss: 0.1445 - val_acc: 0.9620
Out[178]:
<keras.callbacks.History at 0x7f09ea3f6f60>

In [179]:
bn_model.save_weights(path+'models/conv_512_6.h5')

In [180]:
bn_model.evaluate(conv_val_feat, val_labels)


416/500 [=======================>......] - ETA: 0s
Out[180]:
[0.14449646301567554, 0.96199999999999997]

In [181]:
bn_model.load_weights(path+'models/conv_512_6.h5')

Multi-input

The images are of different sizes, which are likely to represent the boat they came from (since different boats will use different cameras). Perhaps this creates some data leakage that we can take advantage of to get a better Kaggle leaderboard position? To find out, first we create arrays of the file sizes for each image:


In [182]:
sizes = [PIL.Image.open(path+'train/'+f).size for f in filenames]
id2size = list(set(sizes))
size2id = {o:i for i,o in enumerate(id2size)}

In [183]:
import collections
collections.Counter(sizes)


Out[183]:
Counter({(1192, 670): 179,
         (1244, 700): 24,
         (1276, 718): 187,
         (1280, 720): 1900,
         (1280, 750): 509,
         (1280, 924): 49,
         (1280, 974): 333,
         (1334, 750): 29,
         (1518, 854): 35,
         (1732, 974): 32})

Then we one-hot encode them (since we want to treat them as categorical) and normalize the data.


In [184]:
trn_sizes_orig = to_categorical([size2id[o] for o in sizes], len(id2size))

In [185]:
raw_val_sizes = [PIL.Image.open(path+'valid/'+f).size for f in val_filenames]
val_sizes = to_categorical([size2id[o] for o in raw_val_sizes], len(id2size))

In [186]:
trn_sizes = trn_sizes_orig-trn_sizes_orig.mean(axis=0)/trn_sizes_orig.std(axis=0)
val_sizes = val_sizes-trn_sizes_orig.mean(axis=0)/trn_sizes_orig.std(axis=0)

To use this additional "meta-data", we create a model with multiple input layers - sz_inp will be our input for the size information.


In [187]:
p=0.6

In [188]:
inp = Input(conv_layers[-1].output_shape[1:])
sz_inp = Input((len(id2size),))
bn_inp = BatchNormalization()(sz_inp)

x = MaxPooling2D()(inp)
x = BatchNormalization(axis=1)(x)
x = Dropout(p/4)(x)
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(p)(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(p/2)(x)
x = concatenate([x,bn_inp], axis=-1)
x = Dense(8, activation='softmax')(x)

When we compile the model, we have to specify all the input layers in an array.


In [189]:
model = Model([inp, sz_inp], x)
model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

And when we train the model, we have to provide all the input layers' data in an array.


In [190]:
model.fit([conv_feat, trn_sizes], trn_labels, batch_size=batch_size, epochs=3, 
             validation_data=([conv_val_feat, val_sizes], val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/3
3277/3277 [==============================] - 7s - loss: 1.5947 - acc: 0.5078 - val_loss: 0.6043 - val_acc: 0.8240
Epoch 2/3
3277/3277 [==============================] - 7s - loss: 1.0065 - acc: 0.6802 - val_loss: 0.4912 - val_acc: 0.8580
Epoch 3/3
3277/3277 [==============================] - 7s - loss: 0.7840 - acc: 0.7385 - val_loss: 0.3533 - val_acc: 0.9160
Out[190]:
<keras.callbacks.History at 0x7f09f21cc048>

In [191]:
bn_model.optimizer.lr = 1e-4

In [192]:
bn_model.fit(conv_feat, trn_labels, batch_size=batch_size, epochs=8, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/8
3277/3277 [==============================] - 7s - loss: 0.3744 - acc: 0.8727 - val_loss: 0.1622 - val_acc: 0.9580
Epoch 2/8
3277/3277 [==============================] - 7s - loss: 0.3236 - acc: 0.8959 - val_loss: 0.1642 - val_acc: 0.9620
Epoch 3/8
3277/3277 [==============================] - 7s - loss: 0.3033 - acc: 0.8972 - val_loss: 0.1507 - val_acc: 0.9620
Epoch 4/8
3277/3277 [==============================] - 7s - loss: 0.3040 - acc: 0.9023 - val_loss: 0.1575 - val_acc: 0.9580
Epoch 5/8
3277/3277 [==============================] - 7s - loss: 0.2980 - acc: 0.9075 - val_loss: 0.1532 - val_acc: 0.9520
Epoch 6/8
3277/3277 [==============================] - 7s - loss: 0.2876 - acc: 0.9005 - val_loss: 0.2188 - val_acc: 0.9400
Epoch 7/8
3277/3277 [==============================] - 7s - loss: 0.2883 - acc: 0.9088 - val_loss: 0.2305 - val_acc: 0.9520
Epoch 8/8
3277/3277 [==============================] - 7s - loss: 0.2761 - acc: 0.9069 - val_loss: 0.1795 - val_acc: 0.9560
Out[192]:
<keras.callbacks.History at 0x7f09f21e6e48>

The model did not show an improvement by using the leakage, other than in the early epochs. This is most likely because the information about what boat the picture came from is readily identified from the image itself, so the meta-data turned out not to add any additional information.

Bounding boxes & multi output

Import / view bounding boxes

A kaggle user has created bounding box annotations for each fish in each training set image. You can download them from here. We will see if we can utilize this additional information. First, we'll load in the data, and keep just the largest bounding box for each image.


In [193]:
import ujson as json

In [194]:
anno_classes = ['alb', 'bet', 'dol', 'lag', 'other', 'shark', 'yft']

In [195]:
bb_json = {}
for c in anno_classes:
    j = json.load(open('{}annos/{}_labels.json'.format(path, c), 'r'))
    for l in j:
        if 'annotations' in l.keys() and len(l['annotations'])>0:
            bb_json[l['filename'].split('/')[-1]] = sorted(
                l['annotations'], key=lambda x: x['height']*x['width'])[-1]

In [196]:
bb_json['img_04908.jpg']


Out[196]:
{'class': 'rect',
 'height': 246.75000000000074,
 'width': 432.8700000000013,
 'x': 465.3000000000014,
 'y': 496.32000000000147}

In [197]:
file2idx = {o:i for i,o in enumerate(raw_filenames)}
val_file2idx = {o:i for i,o in enumerate(raw_val_filenames)}

For any images that have no annotations, we'll create an empty bounding box.


In [198]:
empty_bbox = {'height': 0., 'width': 0., 'x': 0., 'y': 0.}

In [199]:
for f in raw_filenames:
    if not f in bb_json.keys(): bb_json[f] = empty_bbox
for f in raw_val_filenames:
    if not f in bb_json.keys(): bb_json[f] = empty_bbox

Finally, we convert the dictionary into an array, and convert the coordinates to our resized 224x224 images.


In [200]:
bb_params = ['height', 'width', 'x', 'y']
def convert_bb(bb, size):
    bb = [bb[p] for p in bb_params]
    conv_x = (224. / size[0])
    conv_y = (224. / size[1])
    bb[0] = bb[0]*conv_y
    bb[1] = bb[1]*conv_x
    bb[2] = max(bb[2]*conv_x, 0)
    bb[3] = max(bb[3]*conv_y, 0)
    return bb

In [201]:
trn_bbox = np.stack([convert_bb(bb_json[f], s) for f,s in zip(raw_filenames, sizes)], 
                   ).astype(np.float32)
val_bbox = np.stack([convert_bb(bb_json[f], s) 
                   for f,s in zip(raw_val_filenames, raw_val_sizes)]).astype(np.float32)

Now we can check our work by drawing one of the annotations.


In [202]:
def create_rect(bb, color='red'):
    return plt.Rectangle((bb[2], bb[3]), bb[1], bb[0], color=color, fill=False, lw=3)

def show_bb(i):
    bb = val_bbox[i]
    plot(val[i])
    plt.gca().add_patch(create_rect(bb))

In [203]:
show_bb(0)


Create & train model

Since we're not allowed (by the kaggle rules) to manually annotate the test set, we'll need to create a model that predicts the locations of the bounding box on each image. To do so, we create a model with multiple outputs: it will predict both the type of fish (the 'class'), and the 4 bounding box coordinates. We prefer this approach to only predicting the bounding box coordinates, since we hope that giving the model more context about what it's looking for will help it with both tasks.


In [204]:
p=0.6

In [205]:
inp = Input(conv_layers[-1].output_shape[1:])
x = MaxPooling2D()(inp)
x = BatchNormalization(axis=1)(x)
x = Dropout(p/4)(x)
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(p)(x)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(p/2)(x)
x_bb = Dense(4, name='bb')(x)
x_class = Dense(8, activation='softmax', name='class')(x)

Since we have multiple outputs, we need to provide them to the model constructor in an array, and we also need to say what loss function to use for each. We also weight the bounding box loss function down by 1000x since the scale of the cross-entropy loss and the MSE is very different.


In [206]:
model = Model([inp], [x_bb, x_class])
model.compile(Adam(lr=0.001), loss=['mse', 'categorical_crossentropy'], metrics=['accuracy'],
             loss_weights=[.001, 1.])

In [207]:
model.fit(conv_feat, [trn_bbox, trn_labels], batch_size=batch_size, epochs=3, 
             validation_data=(conv_val_feat, [val_bbox, val_labels]))


Train on 3277 samples, validate on 500 samples
Epoch 1/3
3277/3277 [==============================] - 7s - loss: 5.7031 - bb_loss: 4095.0932 - class_loss: 1.6080 - bb_acc: 0.4944 - class_acc: 0.5050 - val_loss: 2.7169 - val_bb_loss: 1842.0082 - val_class_loss: 0.8749 - val_bb_acc: 0.5960 - val_class_acc: 0.7820
Epoch 2/3
3277/3277 [==============================] - 7s - loss: 2.2164 - bb_loss: 1184.6388 - class_loss: 1.0318 - bb_acc: 0.6045 - class_acc: 0.6631 - val_loss: 1.0637 - val_bb_loss: 661.0160 - val_class_loss: 0.4026 - val_bb_acc: 0.7140 - val_class_acc: 0.8900
Epoch 3/3
3277/3277 [==============================] - 7s - loss: 1.6226 - bb_loss: 860.6624 - class_loss: 0.7620 - bb_acc: 0.6280 - class_acc: 0.7522 - val_loss: 0.9923 - val_bb_loss: 510.3292 - val_class_loss: 0.4820 - val_bb_acc: 0.7260 - val_class_acc: 0.8760
Out[207]:
<keras.callbacks.History at 0x7f09e4e03e10>

In [208]:
model.optimizer.lr = 1e-5

In [209]:
model.fit(conv_feat, [trn_bbox, trn_labels], batch_size=batch_size, epochs=10, 
             validation_data=(conv_val_feat, [val_bbox, val_labels]))


Train on 3277 samples, validate on 500 samples
Epoch 1/10
3277/3277 [==============================] - 7s - loss: 1.4401 - bb_loss: 794.0527 - class_loss: 0.6461 - bb_acc: 0.6298 - class_acc: 0.7849 - val_loss: 0.7974 - val_bb_loss: 554.3266 - val_class_loss: 0.2431 - val_bb_acc: 0.7280 - val_class_acc: 0.9400
Epoch 2/10
3277/3277 [==============================] - 7s - loss: 1.3212 - bb_loss: 747.8805 - class_loss: 0.5733 - bb_acc: 0.6341 - class_acc: 0.8068 - val_loss: 0.8879 - val_bb_loss: 597.8120 - val_class_loss: 0.2901 - val_bb_acc: 0.7020 - val_class_acc: 0.9300
Epoch 3/10
3277/3277 [==============================] - 7s - loss: 1.2893 - bb_loss: 759.3997 - class_loss: 0.5299 - bb_acc: 0.6387 - class_acc: 0.8245 - val_loss: 1.0489 - val_bb_loss: 650.9776 - val_class_loss: 0.3979 - val_bb_acc: 0.7360 - val_class_acc: 0.9220
Epoch 4/10
3277/3277 [==============================] - 7s - loss: 1.1480 - bb_loss: 717.9670 - class_loss: 0.4300 - bb_acc: 0.6478 - class_acc: 0.8605 - val_loss: 0.8125 - val_bb_loss: 573.7786 - val_class_loss: 0.2387 - val_bb_acc: 0.7120 - val_class_acc: 0.9400
Epoch 5/10
3277/3277 [==============================] - 7s - loss: 1.1442 - bb_loss: 724.5437 - class_loss: 0.4197 - bb_acc: 0.6405 - class_acc: 0.8532 - val_loss: 0.9154 - val_bb_loss: 621.4960 - val_class_loss: 0.2939 - val_bb_acc: 0.7220 - val_class_acc: 0.9440
Epoch 6/10
3277/3277 [==============================] - 7s - loss: 1.1204 - bb_loss: 695.2761 - class_loss: 0.4251 - bb_acc: 0.6552 - class_acc: 0.8593 - val_loss: 0.9294 - val_bb_loss: 517.6843 - val_class_loss: 0.4118 - val_bb_acc: 0.7480 - val_class_acc: 0.9180
Epoch 7/10
3277/3277 [==============================] - 7s - loss: 1.0516 - bb_loss: 667.9968 - class_loss: 0.3836 - bb_acc: 0.6576 - class_acc: 0.8792 - val_loss: 0.8381 - val_bb_loss: 538.6997 - val_class_loss: 0.2994 - val_bb_acc: 0.7040 - val_class_acc: 0.9560
Epoch 8/10
3277/3277 [==============================] - 7s - loss: 1.0705 - bb_loss: 682.1615 - class_loss: 0.3883 - bb_acc: 0.6594 - class_acc: 0.8770 - val_loss: 0.8090 - val_bb_loss: 557.9911 - val_class_loss: 0.2510 - val_bb_acc: 0.7200 - val_class_acc: 0.9540
Epoch 9/10
3277/3277 [==============================] - 7s - loss: 1.0095 - bb_loss: 657.6072 - class_loss: 0.3519 - bb_acc: 0.6582 - class_acc: 0.8868 - val_loss: 0.7072 - val_bb_loss: 561.7349 - val_class_loss: 0.1454 - val_bb_acc: 0.7480 - val_class_acc: 0.9620
Epoch 10/10
3277/3277 [==============================] - 7s - loss: 0.9998 - bb_loss: 664.6141 - class_loss: 0.3352 - bb_acc: 0.6652 - class_acc: 0.8935 - val_loss: 0.7462 - val_bb_loss: 444.1151 - val_class_loss: 0.3021 - val_bb_acc: 0.7340 - val_class_acc: 0.9340
Out[209]:
<keras.callbacks.History at 0x7f09e4d9e550>

Excitingly, it turned out that the classification model is much improved by giving it this additional task. Let's see how well the bounding box model did by taking a look at its output.


In [210]:
pred = model.predict(conv_val_feat[0:10])

In [211]:
def show_bb_pred(i):
    bb = val_bbox[i]
    bb_pred = pred[0][i]
    plt.figure(figsize=(6,6))
    plot(val[i])
    ax=plt.gca()
    ax.add_patch(create_rect(bb_pred, 'yellow'))
    ax.add_patch(create_rect(bb))

The image shows that it can find fish that are tricky for us to see!


In [212]:
show_bb_pred(6)



In [213]:
model.evaluate(conv_val_feat, [val_bbox, val_labels])


384/500 [======================>.......] - ETA: 0s
Out[213]:
[0.7462120842933655,
 444.11510913085937,
 0.30209694766998291,
 0.73399999904632574,
 0.93399999952316282]

In [214]:
model.save_weights(path+'models/bn_anno.h5')

In [215]:
model.load_weights(path+'models/bn_anno.h5')

Larger size

Set up data

Let's see if we get better results if we use larger images. We'll use 640x360, since it's the same shape as the most common size we saw earlier (1280x720), without being too big.


In [216]:
trn = get_data(path+'train', (360,640))
val = get_data(path+'valid', (360,640))


Found 3277 images belonging to 8 classes.
Found 500 images belonging to 8 classes.

The image shows that things are much clearer at this size.


In [217]:
plot(trn[0])



In [218]:
test = get_data(path+'test', (360,640))


Found 1000 images belonging to 1 classes.

In [219]:
save_array(path+'results/trn_640.dat', trn)
save_array(path+'results/val_640.dat', val)

In [220]:
save_array(path+'results/test_640.dat', test)

In [221]:
trn = load_array(path+'results/trn_640.dat')
val = load_array(path+'results/val_640.dat')

We can now create our VGG model - we'll need to tell it we're not using the normal 224x224 images, which also means it won't include the fully connected layers (since they don't make sense for non-default sizes). We will also remove the last max pooling layer, since we don't want to throw away information yet.


In [222]:
vgg640 = Vgg16BN((360, 640)).model
vgg640.pop()
vgg640.input_shape, vgg640.output_shape
vgg640.compile(Adam(), 'categorical_crossentropy', metrics=['accuracy'])

We can now pre-compute the output of the convolutional part of VGG.


In [223]:
conv_val_feat = vgg640.predict(val, batch_size=batch_size, verbose=1)
conv_trn_feat = vgg640.predict(trn, batch_size=batch_size, verbose=1)


500/500 [==============================] - 17s    
3276/3277 [============================>.] - ETA: 0s

In [224]:
save_array(path+'results/conv_val_640.dat', conv_val_feat)
save_array(path+'results/conv_trn_640.dat', conv_trn_feat)

In [225]:
conv_test_feat = vgg640.predict(test, batch_size=batch_size, verbose=1)


1000/1000 [==============================] - 36s    

In [226]:
save_array(path+'results/conv_test_640.dat', conv_test_feat)

In [227]:
conv_val_feat = load_array(path+'results/conv_val_640.dat')
conv_trn_feat = load_array(path+'results/conv_trn_640.dat')

In [228]:
conv_test_feat = load_array(path+'results/conv_test_640.dat')

Fully convolutional net (FCN)

Since we're using a larger input, the output of the final convolutional layer is also larger. So we probably don't want to put a dense layer there - that would be a lot of parameters! Instead, let's use a fully convolutional net (FCN); this also has the benefit that they tend to generalize well, and also seems like a good fit for our problem (since the fish are a small part of the image).


In [229]:
conv_layers,_ = split_at(vgg640, Convolution2D)

I'm not using any dropout, since I found I got better results without it.


In [230]:
nf=128; p=0.

In [231]:
def get_lrg_layers():
    return [
        BatchNormalization(axis=1, input_shape=conv_layers[-1].output_shape[1:]),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        MaxPooling2D(),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        MaxPooling2D(),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        MaxPooling2D((1,2)),
        Conv2D(8,(3,3), padding='same'),
        Dropout(p),
        GlobalAveragePooling2D(),
        Activation('softmax')
    ]

In [232]:
lrg_model = Sequential(get_lrg_layers())

In [233]:
lrg_model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
batch_normalization_65 (Batc (None, 512, 22, 40)       2048      
_________________________________________________________________
conv2d_122 (Conv2D)          (None, 128, 22, 40)       589952    
_________________________________________________________________
batch_normalization_66 (Batc (None, 128, 22, 40)       512       
_________________________________________________________________
max_pooling2d_46 (MaxPooling (None, 128, 11, 20)       0         
_________________________________________________________________
conv2d_123 (Conv2D)          (None, 128, 11, 20)       147584    
_________________________________________________________________
batch_normalization_67 (Batc (None, 128, 11, 20)       512       
_________________________________________________________________
max_pooling2d_47 (MaxPooling (None, 128, 5, 10)        0         
_________________________________________________________________
conv2d_124 (Conv2D)          (None, 128, 5, 10)        147584    
_________________________________________________________________
batch_normalization_68 (Batc (None, 128, 5, 10)        512       
_________________________________________________________________
max_pooling2d_48 (MaxPooling (None, 128, 5, 5)         0         
_________________________________________________________________
conv2d_125 (Conv2D)          (None, 8, 5, 5)           9224      
_________________________________________________________________
dropout_34 (Dropout)         (None, 8, 5, 5)           0         
_________________________________________________________________
global_average_pooling2d_4 ( (None, 8)                 0         
_________________________________________________________________
activation_4 (Activation)    (None, 8)                 0         
=================================================================
Total params: 897,928
Trainable params: 896,136
Non-trainable params: 1,792
_________________________________________________________________

In [234]:
lrg_model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

In [235]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=2, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/2
3277/3277 [==============================] - 7s - loss: 0.8299 - acc: 0.7348 - val_loss: 0.4927 - val_acc: 0.8300
Epoch 2/2
3277/3277 [==============================] - 7s - loss: 0.2877 - acc: 0.9112 - val_loss: 0.2231 - val_acc: 0.9280
Out[235]:
<keras.callbacks.History at 0x7f0682ee81d0>

In [236]:
lrg_model.optimizer.lr=1e-5

In [237]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=6, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/6
3277/3277 [==============================] - 7s - loss: 0.1128 - acc: 0.9692 - val_loss: 0.4489 - val_acc: 0.8960
Epoch 2/6
3277/3277 [==============================] - 7s - loss: 0.1292 - acc: 0.9591 - val_loss: 0.1664 - val_acc: 0.9460
Epoch 3/6
3277/3277 [==============================] - 7s - loss: 0.0611 - acc: 0.9814 - val_loss: 0.1634 - val_acc: 0.9580
Epoch 4/6
3277/3277 [==============================] - 7s - loss: 0.0571 - acc: 0.9814 - val_loss: 0.3401 - val_acc: 0.9200
Epoch 5/6
3277/3277 [==============================] - 7s - loss: 0.0499 - acc: 0.9829 - val_loss: 0.1846 - val_acc: 0.9500
Epoch 6/6
3277/3277 [==============================] - 7s - loss: 0.0422 - acc: 0.9893 - val_loss: 0.2470 - val_acc: 0.9460
Out[237]:
<keras.callbacks.History at 0x7f0682ee87f0>

When I submitted the results of this model to Kaggle, I got the best single model results of any shown here (ranked 22nd on the leaderboard as at Dec-6-2016.)


In [238]:
lrg_model.save_weights(path+'models/lrg_nmp.h5')

In [239]:
lrg_model.load_weights(path+'models/lrg_nmp.h5')

In [240]:
lrg_model.evaluate(conv_val_feat, val_labels)


448/500 [=========================>....] - ETA: 0s
Out[240]:
[0.24704774374514818, 0.94599999999999995]

Another benefit of this kind of model is that the last convolutional layer has to learn to classify each part of the image (since there's only an average pooling layer after). Let's create a function that grabs the output of this layer (which is the 4th-last layer of our model).


In [241]:
l = lrg_model.layers
conv_fn = K.function([l[0].input, K.learning_phase()], l[-4].output)

In [242]:
def get_cm(imp, label):
    conv = conv_fn([inp,0])[0, label]
    return scipy.misc.imresize(conv, (360,640), interp='nearest')

We have to add an extra dimension to our input since the CNN expects a 'batch' (even if it's just a batch of one).


In [243]:
inp = np.expand_dims(conv_val_feat[0], 0)
np.round(lrg_model.predict(inp)[0],2)


Out[243]:
array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.], dtype=float32)

In [244]:
plt.imshow(to_plot(val[0]))


Out[244]:
<matplotlib.image.AxesImage at 0x7f09aade7160>

In [245]:
cm = get_cm(inp, 0)

The heatmap shows that (at very low resolution) the model is finding the fish!


In [246]:
plt.imshow(cm, cmap="cool")


Out[246]:
<matplotlib.image.AxesImage at 0x7f09aaf3b400>

All convolutional net heatmap

To create a higher resolution heatmap, we'll remove all the max pooling layers, and repeat the previous steps.


In [247]:
def get_lrg_layers():
    return [
        BatchNormalization(axis=1, input_shape=conv_layers[-1].output_shape[1:]),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        Conv2D(nf,(3,3), activation='relu', padding='same'),
        BatchNormalization(axis=1),
        Conv2D(8,(3,3), padding='same'),
        GlobalAveragePooling2D(),
        Activation('softmax')
    ]

In [248]:
lrg_model = Sequential(get_lrg_layers())

In [249]:
lrg_model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
batch_normalization_69 (Batc (None, 512, 22, 40)       2048      
_________________________________________________________________
conv2d_126 (Conv2D)          (None, 128, 22, 40)       589952    
_________________________________________________________________
batch_normalization_70 (Batc (None, 128, 22, 40)       512       
_________________________________________________________________
conv2d_127 (Conv2D)          (None, 128, 22, 40)       147584    
_________________________________________________________________
batch_normalization_71 (Batc (None, 128, 22, 40)       512       
_________________________________________________________________
conv2d_128 (Conv2D)          (None, 128, 22, 40)       147584    
_________________________________________________________________
batch_normalization_72 (Batc (None, 128, 22, 40)       512       
_________________________________________________________________
conv2d_129 (Conv2D)          (None, 8, 22, 40)         9224      
_________________________________________________________________
global_average_pooling2d_5 ( (None, 8)                 0         
_________________________________________________________________
activation_5 (Activation)    (None, 8)                 0         
=================================================================
Total params: 897,928
Trainable params: 896,136
Non-trainable params: 1,792
_________________________________________________________________

In [250]:
lrg_model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

In [251]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=2, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/2
3277/3277 [==============================] - 9s - loss: 1.1163 - acc: 0.6298 - val_loss: 0.6116 - val_acc: 0.8060
Epoch 2/2
3277/3277 [==============================] - 9s - loss: 0.5462 - acc: 0.8267 - val_loss: 0.3347 - val_acc: 0.8880
Out[251]:
<keras.callbacks.History at 0x7f0682225cf8>

In [252]:
lrg_model.optimizer.lr=1e-5

In [253]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=6, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/6
3277/3277 [==============================] - 9s - loss: 0.3647 - acc: 0.8929 - val_loss: 0.1832 - val_acc: 0.9400
Epoch 2/6
3277/3277 [==============================] - 9s - loss: 0.2524 - acc: 0.9234 - val_loss: 0.1365 - val_acc: 0.9560
Epoch 3/6
3277/3277 [==============================] - 9s - loss: 0.2192 - acc: 0.9316 - val_loss: 0.1388 - val_acc: 0.9620
Epoch 4/6
3277/3277 [==============================] - 9s - loss: 0.1490 - acc: 0.9530 - val_loss: 0.2306 - val_acc: 0.9380
Epoch 5/6
3277/3277 [==============================] - 9s - loss: 0.1565 - acc: 0.9530 - val_loss: 0.3508 - val_acc: 0.9100
Epoch 6/6
3277/3277 [==============================] - 9s - loss: 0.1303 - acc: 0.9612 - val_loss: 0.1702 - val_acc: 0.9500
Out[253]:
<keras.callbacks.History at 0x7f068222a400>

In [254]:
lrg_model.save_weights(path+'models/lrg_0mp.h5')

In [255]:
lrg_model.load_weights(path+'models/lrg_0mp.h5')

Create heatmap


In [256]:
l = lrg_model.layers
conv_fn = K.function([l[0].input, K.learning_phase()], l[-3].output)

In [257]:
def get_cm2(imp, label):
    conv = conv_fn([inp,0])[0, label]
    return scipy.misc.imresize(conv, (360,640))

In [258]:
inp = np.expand_dims(conv_val_feat[0], 0)

In [259]:
plt.imshow(to_plot(val[0]))


Out[259]:
<matplotlib.image.AxesImage at 0x7f0681edb7b8>

In [260]:
cm = get_cm2(inp, 0)

In [261]:
cm = get_cm2(inp, 4)

In [262]:
plt.imshow(cm, cmap="cool")


Out[262]:
<matplotlib.image.AxesImage at 0x7f0681e350b8>

In [263]:
plt.figure(figsize=(10,10))
plot(val[0])
plt.imshow(cm, cmap="cool", alpha=0.5)


Out[263]:
<matplotlib.image.AxesImage at 0x7f0682847a90>

Inception mini-net

Here's an example of how to create and use "inception blocks" - as you see, they use multiple different convolution filter sizes and concatenate the results together. We'll talk more about these next year.


In [264]:
def conv2d_bn(x, nb_filter, nb_row, nb_col, subsample=(1, 1)):
    x = Conv2D(nb_filter, (nb_row, nb_col),
                      activation='relu', padding='same', strides=subsample)(x)
    return BatchNormalization(axis=1)(x)

In [265]:
def incep_block(x):
    branch1x1 = conv2d_bn(x, 32, 1, 1, subsample=(2, 2))
    branch5x5 = conv2d_bn(x, 24, 1, 1)
    branch5x5 = conv2d_bn(branch5x5, 32, 5, 5, subsample=(2, 2))

    branch3x3dbl = conv2d_bn(x, 32, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 48, 3, 3)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 48, 3, 3, subsample=(2, 2))

    branch_pool = AveragePooling2D(
        (3, 3), strides=(2, 2), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 16, 1, 1)
    return concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=1)

In [266]:
inp = Input(vgg640.layers[-1].output_shape[1:]) 
x = BatchNormalization(axis=1)(inp)
x = incep_block(x)
x = incep_block(x)
x = incep_block(x)
x = Dropout(0.75)(x)
x = Conv2D(8,(3,3), padding='same')(x)
x = GlobalAveragePooling2D()(x)
outp = Activation('softmax')(x)

In [267]:
lrg_model = Model([inp], outp)

In [268]:
lrg_model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

In [269]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=2, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/2
3277/3277 [==============================] - 14s - loss: 1.1496 - acc: 0.6115 - val_loss: 0.4986 - val_acc: 0.8540
Epoch 2/2
3277/3277 [==============================] - 14s - loss: 0.5391 - acc: 0.8291 - val_loss: 0.2653 - val_acc: 0.9000
Out[269]:
<keras.callbacks.History at 0x7f067ccee8d0>

In [270]:
lrg_model.optimizer.lr=1e-5

In [271]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=6, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/6
3277/3277 [==============================] - 14s - loss: 0.2979 - acc: 0.9011 - val_loss: 0.2314 - val_acc: 0.9320
Epoch 2/6
3277/3277 [==============================] - 14s - loss: 0.2343 - acc: 0.9219 - val_loss: 0.2150 - val_acc: 0.9420
Epoch 3/6
3277/3277 [==============================] - 13s - loss: 0.1558 - acc: 0.9500 - val_loss: 0.1714 - val_acc: 0.9640
Epoch 4/6
3277/3277 [==============================] - 13s - loss: 0.1070 - acc: 0.9683 - val_loss: 0.2046 - val_acc: 0.9440
Epoch 5/6
3277/3277 [==============================] - 13s - loss: 0.1335 - acc: 0.9573 - val_loss: 0.1968 - val_acc: 0.9540
Epoch 6/6
3277/3277 [==============================] - 13s - loss: 0.0997 - acc: 0.9713 - val_loss: 0.1896 - val_acc: 0.9580
Out[271]:
<keras.callbacks.History at 0x7f067e326c88>

In [272]:
lrg_model.fit(conv_trn_feat, trn_labels, batch_size=batch_size, epochs=10, 
             validation_data=(conv_val_feat, val_labels))


Train on 3277 samples, validate on 500 samples
Epoch 1/10
3277/3277 [==============================] - 13s - loss: 0.0644 - acc: 0.9774 - val_loss: 0.1589 - val_acc: 0.9640
Epoch 2/10
3277/3277 [==============================] - 13s - loss: 0.0842 - acc: 0.9744 - val_loss: 0.2271 - val_acc: 0.9500
Epoch 3/10
3277/3277 [==============================] - 13s - loss: 0.0704 - acc: 0.9771 - val_loss: 0.2468 - val_acc: 0.9500
Epoch 4/10
3277/3277 [==============================] - 13s - loss: 0.0695 - acc: 0.9762 - val_loss: 0.2631 - val_acc: 0.9380
Epoch 5/10
3277/3277 [==============================] - 13s - loss: 0.0677 - acc: 0.9777 - val_loss: 0.1893 - val_acc: 0.9520
Epoch 6/10
3277/3277 [==============================] - 13s - loss: 0.0322 - acc: 0.9893 - val_loss: 0.2033 - val_acc: 0.9600
Epoch 7/10
3277/3277 [==============================] - 13s - loss: 0.0716 - acc: 0.9768 - val_loss: 0.3706 - val_acc: 0.9320
Epoch 8/10
3277/3277 [==============================] - 14s - loss: 0.0737 - acc: 0.9792 - val_loss: 0.2376 - val_acc: 0.9520
Epoch 9/10
3277/3277 [==============================] - 14s - loss: 0.0404 - acc: 0.9872 - val_loss: 0.2307 - val_acc: 0.9500
Epoch 10/10
3277/3277 [==============================] - 14s - loss: 0.0327 - acc: 0.9890 - val_loss: 0.2277 - val_acc: 0.9540
Out[272]:
<keras.callbacks.History at 0x7f067e326eb8>

In [273]:
lrg_model.save_weights(path+'models/lrg_nmp.h5')

In [274]:
lrg_model.load_weights(path+'models/lrg_nmp.h5')

Pseudo-labeling


In [279]:
# This is the original code, whose data preparation would require some completion steps
#
# preds = model.predict([conv_test_feat, test_sizes], batch_size=batch_size*2)
# gen = image.ImageDataGenerator()
# test_batches = gen.flow(conv_test_feat, preds, batch_size=16)
# val_batches = gen.flow(conv_val_feat, val_labels, batch_size=4)
# batches = gen.flow(conv_feat, trn_labels, batch_size=44)
# mi = MixIterator([batches, test_batches, val_batches)
# bn_model.fit_generator(mi, mi.N, epochs=8, validation_data=(conv_val_feat, val_labels))

In [315]:
# The following is a replacement code attempting to simplify the pseudo-labeling example (hope so...) by
# fitting the whole dataset in memory instead of using batches (which wouldn't work when directly
# passing to Keras' gen.flow a data array that is not images.

In [316]:
# we continue from the last lrg_model
preds = lrg_model.predict(conv_test_feat, batch_size=batch_size*2)

In [317]:
# create cumulative array of features and labels+pseudo-labels, then shuffle
all_feat = np.vstack((conv_trn_feat, conv_val_feat, conv_test_feat))
all_labels = np.vstack((trn_labels, val_labels, preds))
shuffix = np.arange(all_feat.shape[0])
np.random.shuffle(shuffix)
mix_feat = all_feat[shuffix]
mix_labels = all_labels[shuffix]

In [319]:
lrg_model.fit(mix_feat, mix_labels, batch_size=batch_size, epochs=8, 
             validation_data=(conv_val_feat, val_labels))


Train on 4777 samples, validate on 500 samples
Epoch 1/8
4777/4777 [==============================] - 19s - loss: 0.2455 - acc: 0.9408 - val_loss: 0.0729 - val_acc: 0.9840
Epoch 2/8
4777/4777 [==============================] - 19s - loss: 0.1587 - acc: 0.9650 - val_loss: 0.0135 - val_acc: 0.9960
Epoch 3/8
4777/4777 [==============================] - 19s - loss: 0.1255 - acc: 0.9713 - val_loss: 0.0188 - val_acc: 0.9960
Epoch 4/8
4777/4777 [==============================] - 18s - loss: 0.1097 - acc: 0.9772 - val_loss: 0.0077 - val_acc: 1.0000
Epoch 5/8
4777/4777 [==============================] - 19s - loss: 0.1154 - acc: 0.9784 - val_loss: 0.0265 - val_acc: 0.9940
Epoch 6/8
4777/4777 [==============================] - 19s - loss: 0.0990 - acc: 0.9807 - val_loss: 0.0068 - val_acc: 1.0000
Epoch 7/8
4777/4777 [==============================] - 19s - loss: 0.0920 - acc: 0.9826 - val_loss: 0.0114 - val_acc: 0.9980
Epoch 8/8
4777/4777 [==============================] - 19s - loss: 0.0912 - acc: 0.9814 - val_loss: 0.0126 - val_acc: 0.9960
Out[319]:
<keras.callbacks.History at 0x7f067bf35eb8>

Submit


In [352]:
def do_clip(arr, mx): return np.clip(arr, (1-mx)/7, mx)

In [377]:
lrg_model.evaluate(conv_val_feat, val_labels, batch_size*2)


480/500 [===========================>..] - ETA: 0s
Out[377]:
[0.012647391739941668, 0.996]

In [378]:
preds = lrg_model.predict(conv_test_feat, batch_size=batch_size)

In [379]:
subm = do_clip(preds,0.82)

In [380]:
subm_name = path+'results/subm_bb.gz'

In [381]:
# classes = sorted(batches.class_indices, key=batches.class_indices.get)
classes = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']

In [382]:
submission = pd.DataFrame(subm, columns=classes)
submission.insert(0, 'image', raw_test_filenames)
submission.head()


Out[382]:
image ALB BET DOL LAG NoF OTHER SHARK YFT
0 img_00833.jpg 0.820000 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714
1 img_03355.jpg 0.820000 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714
2 img_00486.jpg 0.820000 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714 0.025714
3 img_03087.jpg 0.025714 0.025714 0.025714 0.025714 0.820000 0.025714 0.025714 0.025714
4 img_06663.jpg 0.025714 0.025714 0.025714 0.820000 0.025714 0.025714 0.025714 0.025714

In [383]:
submission.to_csv(subm_name, index=False, compression='gzip')

In [384]:
FileLink(subm_name)