Visual Recognition Lab

In this lab we will experiment with convolutional neural networks to process images. We will use Keras with Tensorflow. Here are installation instructions for Keras: https://keras.io/#installation, and here are installation instructions for Tensorflow: https://github.com/tensorflow/tensorflow#download-and-setup. Keras is an easy front-end for Tensorflow that allows you to use high-level layers on top of primitive operations implemented in Tensorflow.

We will take a set of training images and sentences from the MS-COCO dataset (400k sentences) and train our network to detect images that contain women vs. men, based on information from the captions.

First, let's import libraries and make sure you have everything properly installed.

In [1]:
import tensorflow as tf
import numpy as np
import random, json, string, pickle
import keras
import keras.layers
import keras.models
import keras.optimizers
import keras.callbacks
from keras.preprocessing import image
import keras.applications.vgg16 as vgg16
import keras.applications.resnet50 as resnet50
import matplotlib.pyplot as plt
from nltk import word_tokenize
%matplotlib inline
Using TensorFlow backend.

1. Loading a Pre-trained Convolutional Neural Network (CNN)

We will load here a VGG network proposed by Simonyan & Zisserman "Very Deep Convolutional Networks for Large-Scale Visual Recognition"

In [2]:
model = vgg16.VGG16(weights='imagenet')
model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 224, 224, 3)   0                                            
____________________________________________________________________________________________________
block1_conv1 (Convolution2D)     (None, 224, 224, 64)  1792        input_1[0][0]                    
____________________________________________________________________________________________________
block1_conv2 (Convolution2D)     (None, 224, 224, 64)  36928       block1_conv1[0][0]               
____________________________________________________________________________________________________
block1_pool (MaxPooling2D)       (None, 112, 112, 64)  0           block1_conv2[0][0]               
____________________________________________________________________________________________________
block2_conv1 (Convolution2D)     (None, 112, 112, 128) 73856       block1_pool[0][0]                
____________________________________________________________________________________________________
block2_conv2 (Convolution2D)     (None, 112, 112, 128) 147584      block2_conv1[0][0]               
____________________________________________________________________________________________________
block2_pool (MaxPooling2D)       (None, 56, 56, 128)   0           block2_conv2[0][0]               
____________________________________________________________________________________________________
block3_conv1 (Convolution2D)     (None, 56, 56, 256)   295168      block2_pool[0][0]                
____________________________________________________________________________________________________
block3_conv2 (Convolution2D)     (None, 56, 56, 256)   590080      block3_conv1[0][0]               
____________________________________________________________________________________________________
block3_conv3 (Convolution2D)     (None, 56, 56, 256)   590080      block3_conv2[0][0]               
____________________________________________________________________________________________________
block3_pool (MaxPooling2D)       (None, 28, 28, 256)   0           block3_conv3[0][0]               
____________________________________________________________________________________________________
block4_conv1 (Convolution2D)     (None, 28, 28, 512)   1180160     block3_pool[0][0]                
____________________________________________________________________________________________________
block4_conv2 (Convolution2D)     (None, 28, 28, 512)   2359808     block4_conv1[0][0]               
____________________________________________________________________________________________________
block4_conv3 (Convolution2D)     (None, 28, 28, 512)   2359808     block4_conv2[0][0]               
____________________________________________________________________________________________________
block4_pool (MaxPooling2D)       (None, 14, 14, 512)   0           block4_conv3[0][0]               
____________________________________________________________________________________________________
block5_conv1 (Convolution2D)     (None, 14, 14, 512)   2359808     block4_pool[0][0]                
____________________________________________________________________________________________________
block5_conv2 (Convolution2D)     (None, 14, 14, 512)   2359808     block5_conv1[0][0]               
____________________________________________________________________________________________________
block5_conv3 (Convolution2D)     (None, 14, 14, 512)   2359808     block5_conv2[0][0]               
____________________________________________________________________________________________________
block5_pool (MaxPooling2D)       (None, 7, 7, 512)     0           block5_conv3[0][0]               
____________________________________________________________________________________________________
flatten (Flatten)                (None, 25088)         0           block5_pool[0][0]                
____________________________________________________________________________________________________
fc1 (Dense)                      (None, 4096)          102764544   flatten[0][0]                    
____________________________________________________________________________________________________
fc2 (Dense)                      (None, 4096)          16781312    fc1[0][0]                        
____________________________________________________________________________________________________
predictions (Dense)              (None, 1000)          4097000     fc2[0][0]                        
====================================================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
____________________________________________________________________________________________________

2. Making predictions on a few images using this network.

The last dense layer "predictions" uses a Softmax activation, so the outputs correspond to probabilities for the 1000 classes in the Imagenet ILSVRC task. We will load an image an make some predictions with this network. I took a picture of my toaster to see what the network outputs. It did quite well.

In [3]:
img_path = 'test_image.jpg'  # This is an image I took in my kitchen.
img = image.load_img(img_path, target_size=(224, 224))
img_arr = image.img_to_array(img)
x = np.expand_dims(img_arr, axis=0)  # The model only accepts batches so we add a dummy dimension.
x = vgg16.preprocess_input(x)  # The preprocessing should be the same that was used during training.

predictions = model.predict(x)

label_predictions = vgg16.decode_predictions(predictions, top = 10)

print('Input image size:', x.shape)
print('Prediction scores: ', predictions.shape)
print('\nPredictions:')
for (i, (category_id, name, probability)) in enumerate(label_predictions[0]):
    print('%d. %s(%.3f)' % (i, name, probability))

plt.imshow(np.asarray(img));
('Input image size:', (1, 224, 224, 3))
('Prediction scores: ', (1, 1000))

Predictions:
0. waffle_iron(0.922)
1. modem(0.035)
2. vacuum(0.016)
3. mouse(0.015)
4. projector(0.004)
5. space_heater(0.002)
6. can_opener(0.001)
7. toilet_seat(0.001)
8. CD_player(0.001)
9. remote_control(0.000)

3. Using a CNN as visual features

We will use the CNN outputs at the "fc2" layer as features in our task of detecting 80 object categories from the MS-COCO dataset. Apart from image descriptions, the MS-COCO dataset contains annotations for 80 object categories with bounding boxes. In this lab we will not be using the bounding boxes so we will only use as a label for each image a vector with 80 dimensions where each entry is a binary value indicating whether an instance of a corresponding object is present.

In [3]:
mscoco_objs = json.load(open('annotations/instances_train2014.json'))
In [4]:
print(mscoco_objs.keys())
print(mscoco_objs['categories'][0])
print(mscoco_objs['annotations'][0])
[u'info', u'images', u'licenses', u'annotations', u'categories']
{u'supercategory': u'person', u'id': 1, u'name': u'person'}
{u'segmentation': [[312.29, 562.89, 402.25, 511.49, 400.96, 425.38, 398.39, 372.69, 388.11, 332.85, 318.71, 325.14, 295.58, 305.86, 269.88, 314.86, 258.31, 337.99, 217.19, 321.29, 182.49, 343.13, 141.37, 348.27, 132.37, 358.55, 159.36, 377.83, 116.95, 421.53, 167.07, 499.92, 232.61, 560.32, 300.72, 571.89]], u'area': 54652.9556, u'iscrowd': 0, u'image_id': 480023, u'bbox': [116.95, 305.86, 285.3, 266.03], u'category_id': 58, u'id': 86}
In [5]:
imageIds = list(set([entry['id'] for entry in mscoco_objs['images']]))[:50000]
imageId2Name = {entry['id']: entry['file_name'] for entry in mscoco_objs['images']}
imageId2index = {image_id: idx for (idx, image_id) in enumerate(imageIds)}
categoryId2index = {entry['id']: idx for (idx, entry) in enumerate(mscoco_objs['categories'])}

labels = np.zeros((len(imageIds), 80))

print('Computing labels'),
simageids = set(imageIds)
for (i, entry) in enumerate(mscoco_objs['annotations']):
    if entry['image_id'] not in simageids: continue
    if i % 10000 == 0: print('.'),
    image_id = entry['image_id']
    category_id = entry['category_id']
    labels[imageId2index[image_id], categoryId2index[category_id]] = 1

print('\n')
print('Labels ', labels.shape)
Computing labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 

('Labels ', (50000, 80))

4. Compute Visual Features using the CNN.

We will use the CNN outputs at the "fc2" layer as features in our task of detecting 80 object categories. So we need to remove the "predictions" linear layer, let's remove it and verify that the layer is removed by printing the model summary.

In [6]:
# Remove last Linear/Dense layer.
model = vgg16.VGG16(weights='imagenet')
model.layers.pop()
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes = []
model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_2 (InputLayer)             (None, 224, 224, 3)   0                                            
____________________________________________________________________________________________________
block1_conv1 (Convolution2D)     (None, 224, 224, 64)  1792        input_2[0][0]                    
____________________________________________________________________________________________________
block1_conv2 (Convolution2D)     (None, 224, 224, 64)  36928       block1_conv1[0][0]               
____________________________________________________________________________________________________
block1_pool (MaxPooling2D)       (None, 112, 112, 64)  0           block1_conv2[0][0]               
____________________________________________________________________________________________________
block2_conv1 (Convolution2D)     (None, 112, 112, 128) 73856       block1_pool[0][0]                
____________________________________________________________________________________________________
block2_conv2 (Convolution2D)     (None, 112, 112, 128) 147584      block2_conv1[0][0]               
____________________________________________________________________________________________________
block2_pool (MaxPooling2D)       (None, 56, 56, 128)   0           block2_conv2[0][0]               
____________________________________________________________________________________________________
block3_conv1 (Convolution2D)     (None, 56, 56, 256)   295168      block2_pool[0][0]                
____________________________________________________________________________________________________
block3_conv2 (Convolution2D)     (None, 56, 56, 256)   590080      block3_conv1[0][0]               
____________________________________________________________________________________________________
block3_conv3 (Convolution2D)     (None, 56, 56, 256)   590080      block3_conv2[0][0]               
____________________________________________________________________________________________________
block3_pool (MaxPooling2D)       (None, 28, 28, 256)   0           block3_conv3[0][0]               
____________________________________________________________________________________________________
block4_conv1 (Convolution2D)     (None, 28, 28, 512)   1180160     block3_pool[0][0]                
____________________________________________________________________________________________________
block4_conv2 (Convolution2D)     (None, 28, 28, 512)   2359808     block4_conv1[0][0]               
____________________________________________________________________________________________________
block4_conv3 (Convolution2D)     (None, 28, 28, 512)   2359808     block4_conv2[0][0]               
____________________________________________________________________________________________________
block4_pool (MaxPooling2D)       (None, 14, 14, 512)   0           block4_conv3[0][0]               
____________________________________________________________________________________________________
block5_conv1 (Convolution2D)     (None, 14, 14, 512)   2359808     block4_pool[0][0]                
____________________________________________________________________________________________________
block5_conv2 (Convolution2D)     (None, 14, 14, 512)   2359808     block5_conv1[0][0]               
____________________________________________________________________________________________________
block5_conv3 (Convolution2D)     (None, 14, 14, 512)   2359808     block5_conv2[0][0]               
____________________________________________________________________________________________________
block5_pool (MaxPooling2D)       (None, 7, 7, 512)     0           block5_conv3[0][0]               
____________________________________________________________________________________________________
flatten (Flatten)                (None, 25088)         0           block5_pool[0][0]                
____________________________________________________________________________________________________
fc1 (Dense)                      (None, 4096)          102764544   flatten[0][0]                    
____________________________________________________________________________________________________
fc2 (Dense)                      (None, 4096)          16781312    fc1[0][0]                        
====================================================================================================
Total params: 134,260,544
Trainable params: 134,260,544
Non-trainable params: 0
____________________________________________________________________________________________________

Let's compute features for all images in the training dataset. You will need to download the training images from the MS-COCO webpage. I'm including the pre-computed vgg16_features.p file so you don't really need to run this code but I encourage you to do it so you have a better idea of computation times for your project.

In [8]:
features = np.zeros((len(imageIds), 4096), dtype=np.float32)

batch_size = 500
n_batches = len(imageIds) / batch_size
index = 0
for b in range(0, n_batches):
    batch = np.zeros((batch_size, 224, 224, 3))
    print(('Computing features for batch %d of %d') % (b + 1, n_batches))
    for i in range(0, batch_size):
        img_path = '/data/data/coco/train2014/' + imageId2Name[imageIds[index]]
        img = image.load_img(img_path, target_size=(224, 224))
        img = image.img_to_array(img)
        batch[i, :, :, :] = img
        index = index + 1
    
    print(('Batch loaded for batch %d of %d') % (b + 1, n_batches))
    batch = vgg16.preprocess_input(batch)
    features[b * batch_size : (b + 1) * batch_size, :] = model.predict(batch)

pickle.dump({'features': features, 'imageIds': imageIds, 'labels': labels,
             'imageId2Name': imageId2Name, 'imageId2index': imageId2index, 'categoryId2index': categoryId2index}, 
            open('vgg16_features.p', 'w'))
Computing features for batch 1 of 100
Batch loaded for batch 1 of 100
Computing features for batch 2 of 100
Batch loaded for batch 2 of 100
Computing features for batch 3 of 100
Batch loaded for batch 3 of 100
Computing features for batch 4 of 100
Batch loaded for batch 4 of 100
Computing features for batch 5 of 100
Batch loaded for batch 5 of 100
Computing features for batch 6 of 100
Batch loaded for batch 6 of 100
Computing features for batch 7 of 100
Batch loaded for batch 7 of 100
Computing features for batch 8 of 100
Batch loaded for batch 8 of 100
Computing features for batch 9 of 100
Batch loaded for batch 9 of 100
Computing features for batch 10 of 100
Batch loaded for batch 10 of 100
Computing features for batch 11 of 100
Batch loaded for batch 11 of 100
Computing features for batch 12 of 100
Batch loaded for batch 12 of 100
Computing features for batch 13 of 100
Batch loaded for batch 13 of 100
Computing features for batch 14 of 100
Batch loaded for batch 14 of 100
Computing features for batch 15 of 100
Batch loaded for batch 15 of 100
Computing features for batch 16 of 100
Batch loaded for batch 16 of 100
Computing features for batch 17 of 100
Batch loaded for batch 17 of 100
Computing features for batch 18 of 100
Batch loaded for batch 18 of 100
Computing features for batch 19 of 100
Batch loaded for batch 19 of 100
Computing features for batch 20 of 100
Batch loaded for batch 20 of 100
Computing features for batch 21 of 100
Batch loaded for batch 21 of 100
Computing features for batch 22 of 100
Batch loaded for batch 22 of 100
Computing features for batch 23 of 100
Batch loaded for batch 23 of 100
Computing features for batch 24 of 100
Batch loaded for batch 24 of 100
Computing features for batch 25 of 100
Batch loaded for batch 25 of 100
Computing features for batch 26 of 100
Batch loaded for batch 26 of 100
Computing features for batch 27 of 100
Batch loaded for batch 27 of 100
Computing features for batch 28 of 100
Batch loaded for batch 28 of 100
Computing features for batch 29 of 100
Batch loaded for batch 29 of 100
Computing features for batch 30 of 100
Batch loaded for batch 30 of 100
Computing features for batch 31 of 100
Batch loaded for batch 31 of 100
Computing features for batch 32 of 100
Batch loaded for batch 32 of 100
Computing features for batch 33 of 100
Batch loaded for batch 33 of 100
Computing features for batch 34 of 100
Batch loaded for batch 34 of 100
Computing features for batch 35 of 100
Batch loaded for batch 35 of 100
Computing features for batch 36 of 100
Batch loaded for batch 36 of 100
Computing features for batch 37 of 100
Batch loaded for batch 37 of 100
Computing features for batch 38 of 100
Batch loaded for batch 38 of 100
Computing features for batch 39 of 100
Batch loaded for batch 39 of 100
Computing features for batch 40 of 100
Batch loaded for batch 40 of 100
Computing features for batch 41 of 100
Batch loaded for batch 41 of 100
Computing features for batch 42 of 100
Batch loaded for batch 42 of 100
Computing features for batch 43 of 100
Batch loaded for batch 43 of 100
Computing features for batch 44 of 100
Batch loaded for batch 44 of 100
Computing features for batch 45 of 100
Batch loaded for batch 45 of 100
Computing features for batch 46 of 100
Batch loaded for batch 46 of 100
Computing features for batch 47 of 100
Batch loaded for batch 47 of 100
Computing features for batch 48 of 100
Batch loaded for batch 48 of 100
Computing features for batch 49 of 100
Batch loaded for batch 49 of 100
Computing features for batch 50 of 100
Batch loaded for batch 50 of 100
Computing features for batch 51 of 100
Batch loaded for batch 51 of 100
Computing features for batch 52 of 100
Batch loaded for batch 52 of 100
Computing features for batch 53 of 100
Batch loaded for batch 53 of 100
Computing features for batch 54 of 100
Batch loaded for batch 54 of 100
Computing features for batch 55 of 100
Batch loaded for batch 55 of 100
Computing features for batch 56 of 100
Batch loaded for batch 56 of 100
Computing features for batch 57 of 100
Batch loaded for batch 57 of 100
Computing features for batch 58 of 100
Batch loaded for batch 58 of 100
Computing features for batch 59 of 100
Batch loaded for batch 59 of 100
Computing features for batch 60 of 100
Batch loaded for batch 60 of 100
Computing features for batch 61 of 100
Batch loaded for batch 61 of 100
Computing features for batch 62 of 100
Batch loaded for batch 62 of 100
Computing features for batch 63 of 100
Batch loaded for batch 63 of 100
Computing features for batch 64 of 100
Batch loaded for batch 64 of 100
Computing features for batch 65 of 100
Batch loaded for batch 65 of 100
Computing features for batch 66 of 100
Batch loaded for batch 66 of 100
Computing features for batch 67 of 100
Batch loaded for batch 67 of 100
Computing features for batch 68 of 100
Batch loaded for batch 68 of 100
Computing features for batch 69 of 100
Batch loaded for batch 69 of 100
Computing features for batch 70 of 100
Batch loaded for batch 70 of 100
Computing features for batch 71 of 100
Batch loaded for batch 71 of 100
Computing features for batch 72 of 100
Batch loaded for batch 72 of 100
Computing features for batch 73 of 100
Batch loaded for batch 73 of 100
Computing features for batch 74 of 100
Batch loaded for batch 74 of 100
Computing features for batch 75 of 100
Batch loaded for batch 75 of 100
Computing features for batch 76 of 100
Batch loaded for batch 76 of 100
Computing features for batch 77 of 100
Batch loaded for batch 77 of 100
Computing features for batch 78 of 100
Batch loaded for batch 78 of 100
Computing features for batch 79 of 100
Batch loaded for batch 79 of 100
Computing features for batch 80 of 100
Batch loaded for batch 80 of 100
Computing features for batch 81 of 100
Batch loaded for batch 81 of 100
Computing features for batch 82 of 100
Batch loaded for batch 82 of 100
Computing features for batch 83 of 100
Batch loaded for batch 83 of 100
Computing features for batch 84 of 100
Batch loaded for batch 84 of 100
Computing features for batch 85 of 100
Batch loaded for batch 85 of 100
Computing features for batch 86 of 100
Batch loaded for batch 86 of 100
Computing features for batch 87 of 100
Batch loaded for batch 87 of 100
Computing features for batch 88 of 100
Batch loaded for batch 88 of 100
Computing features for batch 89 of 100
Batch loaded for batch 89 of 100
Computing features for batch 90 of 100
Batch loaded for batch 90 of 100
Computing features for batch 91 of 100
Batch loaded for batch 91 of 100
Computing features for batch 92 of 100
Batch loaded for batch 92 of 100
Computing features for batch 93 of 100
Batch loaded for batch 93 of 100
Computing features for batch 94 of 100
Batch loaded for batch 94 of 100
Computing features for batch 95 of 100
Batch loaded for batch 95 of 100
Computing features for batch 96 of 100
Batch loaded for batch 96 of 100
Computing features for batch 97 of 100
Batch loaded for batch 97 of 100
Computing features for batch 98 of 100
Batch loaded for batch 98 of 100
Computing features for batch 99 of 100
Batch loaded for batch 99 of 100
Computing features for batch 100 of 100
Batch loaded for batch 100 of 100

5. Train the model to predict the labels on this task.

Now that we have computed features for each image in our training data. We will try to train the model to predict the labels using a binary cross entropy loss. Remember that this was one of the loss functions you implemented in the Deep Learning Lab. We define here a simple 2-layer neural network that takes input feature vectors and produces 80 probabilities for each object using the output of a sigmoid layer.

In [7]:
# Let's define the model.
# 1. The inputs are feature vectors of size 4096 corresponding to each image.
x = keras.layers.Input(batch_shape = (None, 4096))

# 2. Then we add a linear layer with a ReLU activation and Batch-normalization afterwards.
hidden = keras.layers.Dense(512, activation = 'relu')(x)
hidden = keras.layers.BatchNormalization()(hidden)
hidden = keras.layers.Dropout(0.5)(hidden)

# 3. Add another dense layer with and pass a sigmoid function afterwards.
predictions = keras.layers.Dense(80, activation = 'sigmoid')(hidden)

# 4. Define the inputs and outpus of the model and print a summary.
#    Remember that one could have multiple inputs or outputs.
mlp_model = keras.models.Model(input = [x], output = [predictions])
mlp_model.summary()

# Define the optimization method, and its parameters.
#sgd = keras.optimizers.SGD(lr = 0.01, decay = 1e-2, momentum = 0.9, nesterov = True)
sgd = keras.optimizers.Adam(lr = 0.01, decay = 1e-2)  # Maybe this works better?

# Define the loss function that will be used to compute the error between predictions and labels.
mlp_model.compile(loss='binary_crossentropy', optimizer = sgd, metrics=['accuracy'])
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_3 (InputLayer)             (None, 4096)          0                                            
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 512)           2097664     input_3[0][0]                    
____________________________________________________________________________________________________
batchnormalization_1 (BatchNorma (None, 512)           2048        dense_1[0][0]                    
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 512)           0           batchnormalization_1[0][0]       
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 80)            41040       dropout_1[0][0]                  
====================================================================================================
Total params: 2,140,752
Trainable params: 2,139,728
Non-trainable params: 1,024
____________________________________________________________________________________________________

Let's load the precomputed features.

In [8]:
# First load features from file if needed.
precomputed = pickle.load(open('vgg16_features.p'))
features = precomputed['features']
imageIds = precomputed['imageIds']
imageId2Name = precomputed['imageId2Name']
imageId2index = precomputed['imageId2index']
categoryId2index = precomputed['categoryId2index']
labels = precomputed['labels']

Now let's train the model. (Note: again I'm including the mlp_model_weights.hdf5 file in the lab).

In [9]:
train_features = features[:40000, :]
train_labels = labels[:40000, :]
train_imageIds = imageIds[:40000]

val_features = features[40000:, :]
val_labels = labels[40000:, :]
val_imageIds = imageIds[40000:]

mlp_model.fit(train_features, train_labels, 
              validation_data = (val_features, val_labels),
              nb_epoch = 20, batch_size = 128, shuffle = True);

mlp_model.save_weights('mlp_model_weights.hdf5')
Train on 40000 samples, validate on 10000 samples
Epoch 1/20
40000/40000 [==============================] - 3s - loss: 0.1132 - acc: 0.9584 - val_loss: 0.0712 - val_acc: 0.9753
Epoch 2/20
40000/40000 [==============================] - 3s - loss: 0.0737 - acc: 0.9749 - val_loss: 0.0687 - val_acc: 0.9760
Epoch 3/20
40000/40000 [==============================] - 3s - loss: 0.0706 - acc: 0.9758 - val_loss: 0.0677 - val_acc: 0.9764
Epoch 4/20
40000/40000 [==============================] - 3s - loss: 0.0685 - acc: 0.9764 - val_loss: 0.0669 - val_acc: 0.9767
Epoch 5/20
40000/40000 [==============================] - 3s - loss: 0.0671 - acc: 0.9768 - val_loss: 0.0664 - val_acc: 0.9769
Epoch 6/20
40000/40000 [==============================] - 3s - loss: 0.0660 - acc: 0.9771 - val_loss: 0.0661 - val_acc: 0.9770
Epoch 7/20
40000/40000 [==============================] - 2s - loss: 0.0649 - acc: 0.9774 - val_loss: 0.0657 - val_acc: 0.9772
Epoch 8/20
40000/40000 [==============================] - 2s - loss: 0.0637 - acc: 0.9777 - val_loss: 0.0656 - val_acc: 0.9773
Epoch 9/20
40000/40000 [==============================] - 2s - loss: 0.0628 - acc: 0.9780 - val_loss: 0.0654 - val_acc: 0.9773
Epoch 10/20
40000/40000 [==============================] - 3s - loss: 0.0618 - acc: 0.9783 - val_loss: 0.0653 - val_acc: 0.9774
Epoch 11/20
40000/40000 [==============================] - 3s - loss: 0.0611 - acc: 0.9784 - val_loss: 0.0653 - val_acc: 0.9774
Epoch 12/20
40000/40000 [==============================] - 3s - loss: 0.0604 - acc: 0.9787 - val_loss: 0.0653 - val_acc: 0.9774
Epoch 13/20
40000/40000 [==============================] - 3s - loss: 0.0595 - acc: 0.9789 - val_loss: 0.0653 - val_acc: 0.9775
Epoch 14/20
40000/40000 [==============================] - 3s - loss: 0.0589 - acc: 0.9791 - val_loss: 0.0653 - val_acc: 0.9775
Epoch 15/20
40000/40000 [==============================] - 3s - loss: 0.0581 - acc: 0.9793 - val_loss: 0.0652 - val_acc: 0.9776
Epoch 16/20
40000/40000 [==============================] - 3s - loss: 0.0574 - acc: 0.9795 - val_loss: 0.0653 - val_acc: 0.9776
Epoch 17/20
40000/40000 [==============================] - 3s - loss: 0.0569 - acc: 0.9796 - val_loss: 0.0655 - val_acc: 0.9776
Epoch 18/20
40000/40000 [==============================] - 3s - loss: 0.0563 - acc: 0.9798 - val_loss: 0.0654 - val_acc: 0.9776
Epoch 19/20
40000/40000 [==============================] - 2s - loss: 0.0558 - acc: 0.9799 - val_loss: 0.0655 - val_acc: 0.9776
Epoch 20/20
40000/40000 [==============================] - 3s - loss: 0.0552 - acc: 0.9801 - val_loss: 0.0657 - val_acc: 0.9777

6. Try the trained model on some validation data.

Now that we have trained the model we can try running it on some data that we didn't use for training.

In [10]:
predictions = mlp_model.predict(val_features)

for ind in [2, 3, 4, 39, 14]:
    img_path = '/data/data/coco/train2014/' + imageId2Name[val_imageIds[ind]]
    img = image.load_img(img_path, target_size=(224, 224))
    img_arr = np.expand_dims(image.img_to_array(img), axis = 0)

    plt.figure()
    plt.imshow(np.asarray(img))
  
    vlabels = [mscoco_objs['categories'][idx]['name'] for idx in np.nonzero(val_labels[ind, :])[0]]
    vpreds = [(mscoco_objs['categories'][idx]['name'], predictions[ind, idx]) 
              for idx in np.nonzero(predictions[ind, :] > 0.75)[0]]
    plt.title('labels:' + str(vlabels) + '\npredictions:' + str(vpreds))