Transfer Learning

Most of the time you won't want to train a whole convolutional network yourself. Modern ConvNets training on huge datasets like ImageNet take weeks on multiple GPUs. Instead, most people use a pretrained network either as a fixed feature extractor, or as an initial network to fine tune. In this notebook, you'll be using VGGNet trained on the ImageNet dataset as a feature extractor. Below is a diagram of the VGGNet architecture.

VGGNet is great because it's simple and has great performance, coming in second in the ImageNet competition. The idea here is that we keep all the convolutional layers, but replace the final fully connected layers with our own classifier. This way we can use VGGNet as a feature extractor for our images then easily train a simple classifier on top of that. What we'll do is take the first fully connected layer with 4096 units, including thresholding with ReLUs. We can use those values as a code for each image, then build a classifier on top of those codes.

You can read more about transfer learning from the CS231n course notes.

Pretrained VGGNet

We'll be using a pretrained network from https://github.com/machrisaa/tensorflow-vgg. Make sure to clone this repository to the directory you're working from. You'll also want to rename it so it has an underscore instead of a dash.

git clone https://github.com/machrisaa/tensorflow-vgg.git tensorflow_vgg

This is a really nice implementation of VGGNet, quite easy to work with. The network has already been trained and the parameters are available from this link. You'll need to clone the repo into the folder containing this notebook. Then download the parameter file using the next cell.


In [1]:
from urllib.request import urlretrieve
from os.path import isfile, isdir
from tqdm import tqdm

vgg_dir = 'tensorflow_vgg/'
# Make sure vgg exists
if not isdir(vgg_dir):
    raise Exception("VGG directory doesn't exist!")

class DLProgress(tqdm):
    last_block = 0

    def hook(self, block_num=1, block_size=1, total_size=None):
        self.total = total_size
        self.update((block_num - self.last_block) * block_size)
        self.last_block = block_num

if not isfile(vgg_dir + "vgg16.npy"):
    with DLProgress(unit='B', unit_scale=True, miniters=1, desc='VGG16 Parameters') as pbar:
        urlretrieve(
            'https://s3.amazonaws.com/content.udacity-data.com/nd101/vgg16.npy',
            vgg_dir + 'vgg16.npy',
            pbar.hook)
else:
    print("Parameter file already exists!")


Parameter file already exists!

Flower power

Here we'll be using VGGNet to classify images of flowers. To get the flower dataset, run the cell below. This dataset comes from the TensorFlow inception tutorial.


In [2]:
import tarfile

dataset_folder_path = 'flower_photos'

class DLProgress(tqdm):
    last_block = 0

    def hook(self, block_num=1, block_size=1, total_size=None):
        self.total = total_size
        self.update((block_num - self.last_block) * block_size)
        self.last_block = block_num

if not isfile('flower_photos.tar.gz'):
    with DLProgress(unit='B', unit_scale=True, miniters=1, desc='Flowers Dataset') as pbar:
        urlretrieve(
            'http://download.tensorflow.org/example_images/flower_photos.tgz',
            'flower_photos.tar.gz',
            pbar.hook)

if not isdir(dataset_folder_path):
    with tarfile.open('flower_photos.tar.gz') as tar:
        tar.extractall()
        tar.close()

ConvNet Codes

Below, we'll run through all the images in our dataset and get codes for each of them. That is, we'll run the images through the VGGNet convolutional layers and record the values of the first fully connected layer. We can then write these to a file for later when we build our own classifier.

Here we're using the vgg16 module from tensorflow_vgg. The network takes images of size $224 \times 224 \times 3$ as input. Then it has 5 sets of convolutional layers. The network implemented here has this structure (copied from the source code):

self.conv1_1 = self.conv_layer(bgr, "conv1_1")
self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
self.pool1 = self.max_pool(self.conv1_2, 'pool1')

self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
self.pool2 = self.max_pool(self.conv2_2, 'pool2')

self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
self.pool3 = self.max_pool(self.conv3_3, 'pool3')

self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
self.pool4 = self.max_pool(self.conv4_3, 'pool4')

self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
self.pool5 = self.max_pool(self.conv5_3, 'pool5')

self.fc6 = self.fc_layer(self.pool5, "fc6")
self.relu6 = tf.nn.relu(self.fc6)

So what we want are the values of the first fully connected layer, after being ReLUd (self.relu6). To build the network, we use

with tf.Session() as sess:
    vgg = vgg16.Vgg16()
    input_ = tf.placeholder(tf.float32, [None, 224, 224, 3])
    with tf.name_scope("content_vgg"):
        vgg.build(input_)

This creates the vgg object, then builds the graph with vgg.build(input_). Then to get the values from the layer,

feed_dict = {input_: images}
codes = sess.run(vgg.relu6, feed_dict=feed_dict)

In [3]:
import numpy as np

In [4]:
import os

import numpy as np
import tensorflow as tf

from tensorflow_vgg import vgg16
from tensorflow_vgg import utils

In [5]:
data_dir = 'flower_photos/'
contents = os.listdir(data_dir)
classes = [each for each in contents if os.path.isdir(data_dir + each)]

Below I'm running images through the VGG network in batches.

Exercise: Below, build the VGG network. Also get the codes from the first fully connected layer (make sure you get the ReLUd values).


In [6]:
# Set the batch size higher if you can fit in in your GPU memory
batch_size = 100
codes_list = []
labels = []
batch = []

codes = None

with tf.Session() as sess:
    
    # TODO: Build the vgg network here
    vgg = vgg16.Vgg16()
    input_ = tf.placeholder(tf.float32, [None, 224, 224, 3])
    with tf.name_scope("content_vgg"):
        vgg.build(input_)

    for each in classes:
        print("Starting {} images".format(each))
        class_path = data_dir + each
        files = os.listdir(class_path)
        for ii, file in enumerate(files, 1):
            # Add images to the current batch
            # utils.load_image crops the input images for us, from the center
            img = utils.load_image(os.path.join(class_path, file))
            batch.append(img.reshape((1, 224, 224, 3)))
            labels.append(each)
            
            # Running the batch through the network to get the codes
            if ii % batch_size == 0 or ii == len(files):
                
                # Image batch to pass to VGG network
                images = np.concatenate(batch)
                
                # TODO: Get the values from the relu6 layer of the VGG network
                feed_dict = {input_: images}
                codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict)

                # Here I'm building an array of the codes
                if codes is None:
                    codes = codes_batch
                else:
                    codes = np.concatenate((codes, codes_batch))

                # Reset to start building the next batch
                batch = []
                print('{} images processed'.format(ii))


C:\Users\davef\Desktop\src\deep-learning\transfer-learning\tensorflow_vgg\vgg16.npy
npy file loaded
build model started
build model finished: 0s
Starting daisy images
100 images processed
200 images processed
300 images processed
400 images processed
500 images processed
600 images processed
633 images processed
Starting dandelion images
100 images processed
200 images processed
300 images processed
400 images processed
500 images processed
600 images processed
700 images processed
800 images processed
898 images processed
Starting roses images
100 images processed
200 images processed
300 images processed
400 images processed
500 images processed
600 images processed
641 images processed
Starting sunflowers images
100 images processed
200 images processed
300 images processed
400 images processed
500 images processed
600 images processed
699 images processed
Starting tulips images
100 images processed
200 images processed
300 images processed
400 images processed
500 images processed
600 images processed
700 images processed
799 images processed

In [12]:
# write codes to file
with open('codes', 'w') as f:
    codes.tofile(f)
    
# write labels to file
import csv
with open('labels', 'w') as f:
    writer = csv.writer(f, delimiter='\n')
    writer.writerow(labels)

Building the Classifier

Now that we have codes for all the images, we can build a simple classifier on top of them. The codes behave just like normal input into a simple neural network. Below I'm going to have you do most of the work.


In [13]:
# read codes and labels from file
import csv

with open('labels') as f:
    reader = csv.reader(f, delimiter='\n')
    labels = np.array([each for each in reader if len(each) > 0]).squeeze()
with open('codes') as f:
    codes = np.fromfile(f, dtype=np.float32)
    codes = codes.reshape((len(labels), -1))

In [7]:
from sklearn import preprocessing
binarizer = preprocessing.LabelBinarizer()
binarizer.fit(labels)
one_hot_encoded = binarizer.fit_transform(labels)

print(binarizer.classes_) # <-- should be 'daisy' 'dandelion' 'roses' 'sunflowers' 'tulips'
print(set(labels))
print(labels[0]) # <-- should be 'tulips'
print(one_hot_encoded[0]) # <-- should be 0 0 0 0 1


['daisy' 'dandelion' 'roses' 'sunflowers' 'tulips']
{'roses', 'tulips', 'daisy', 'sunflowers', 'dandelion'}
daisy
[1 0 0 0 0]

Data prep

As usual, now we need to one-hot encode our labels and create validation/test sets. First up, creating our labels!

Exercise: From scikit-learn, use LabelBinarizer to create one-hot encoded vectors from the labels.


In [12]:
labels_vecs = one_hot_encoded

Now you'll want to create your training, validation, and test sets. An important thing to note here is that our labels and data aren't randomized yet. We'll want to shuffle our data so the validation and test sets contain data from all classes. Otherwise, you could end up with testing sets that are all one class. Typically, you'll also want to make sure that each smaller set has the same the distribution of classes as it is for the whole data set. The easiest way to accomplish both these goals is to use StratifiedShuffleSplit from scikit-learn.

You can create the splitter like so:

ss = StratifiedShuffleSplit(n_splits=1, test_size=0.2)

Then split the data with

splitter = ss.split(x, y)

ss.split returns a generator of indices. You can pass the indices into the arrays to get the split sets. The fact that it's a generator means you either need to iterate over it, or use next(splitter) to get the indices. Be sure to read the documentation and the user guide.

Exercise: Use StratifiedShuffleSplit to split the codes and labels into training, validation, and test sets.


In [8]:
from sklearn.model_selection import StratifiedShuffleSplit
ss = StratifiedShuffleSplit(n_splits=1, test_size=0.2)
splitter = ss.split(codes, one_hot_encoded)
# only need to call next once since we only have one split
train_index, test_index = next(splitter)
#train_index, test_index in ss.split(codes, one_hot_encoded)
print("TRAIN:", len(train_index), "TEST:", len(test_index))


TRAIN: 2936 TEST: 734

In [9]:
train_x, train_y = codes[train_index], one_hot_encoded[train_index]
tmp_val_x, tmp_val_y = codes[test_index], one_hot_encoded[test_index]
val_x, val_y = tmp_val_x[0:len(test_index)//2], tmp_val_y[0:len(test_index)//2]
test_x, test_y = tmp_val_x[(len(test_index)//2):len(test_index)], tmp_val_y[(len(test_index)//2):len(test_index)]

In [10]:
print("Train shapes (x, y):", train_x.shape, train_y.shape)
print("Validation shapes (x, y):", val_x.shape, val_y.shape)
print("Test shapes (x, y):", test_x.shape, test_y.shape)


Train shapes (x, y): (2936, 4096) (2936, 5)
Validation shapes (x, y): (367, 4096) (367, 5)
Test shapes (x, y): (367, 4096) (367, 5)

If you did it right, you should see these sizes for the training sets:

Train shapes (x, y): (2936, 4096) (2936, 5)
Validation shapes (x, y): (367, 4096) (367, 5)
Test shapes (x, y): (367, 4096) (367, 5)

Classifier layers

Once you have the convolutional codes, you just need to build a classfier from some fully connected layers. You use the codes as the inputs and the image labels as targets. Otherwise the classifier is a typical neural network.

Exercise: With the codes and labels loaded, build the classifier. Consider the codes as your inputs, each of them are 4096D vectors. You'll want to use a hidden layer and an output layer as your classifier. Remember that the output layer needs to have one unit for each class and a softmax activation function. Use the cross entropy to calculate the cost.


In [35]:
inputs_ = tf.placeholder(tf.float32, shape=[None, codes.shape[1]])
labels_ = tf.placeholder(tf.int64, shape=[None, labels_vecs.shape[1]])

# TODO: Classifier layers and operations
fc = tf.contrib.layers.fully_connected(inputs_, 256)
# output layer logits
logits = tf.contrib.layers.fully_connected(fc, labels_vecs.shape[1], activation_fn=None)
# cross entropy loss
cross_ent = tf.nn.softmax_cross_entropy_with_logits(labels=labels_, logits=logits)
cost = tf.reduce_mean(cross_ent)

# training optimizer
optimizer = tf.train.AdamOptimizer().minimize(cost)

# Operations for validation/test accuracy
predicted = tf.nn.softmax(logits)
correct_pred = tf.equal(tf.argmax(predicted, 1), tf.argmax(labels_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

Batches!

Here is just a simple way to do batches. I've written it so that it includes all the data. Sometimes you'll throw out some data at the end to make sure you have full batches. Here I just extend the last batch to include the remaining data.


In [14]:
def get_batches(x, y, n_batches=10):
    """ Return a generator that yields batches from arrays x and y. """
    batch_size = len(x)//n_batches
    
    for ii in range(0, n_batches*batch_size, batch_size):
        # If we're not on the last batch, grab data with size batch_size
        if ii != (n_batches-1)*batch_size:
            X, Y = x[ii: ii+batch_size], y[ii: ii+batch_size] 
        # On the last batch, grab the rest of the data
        else:
            X, Y = x[ii:], y[ii:]
        # I love generators
        yield X, Y

Training

Here, we'll train the network.

Exercise: So far we've been providing the training code for you. Here, I'm going to give you a bit more of a challenge and have you write the code to train the network. Of course, you'll be able to see my solution if you need help. Use the get_batches function I wrote before to get your batches like for x, y in get_batches(train_x, train_y). Or write your own!


In [33]:
import time
epochs = 20
iteration = 0
print_per_k = 5

In [36]:
saver = tf.train.Saver()
with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    # TODO: Your training code here
    for epoch in range(epochs):
        for x, y in get_batches(train_x, train_y):
            feed_dict = { inputs_: x, labels_: y }
            start = time.time()
            loss, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
            end = time.time()
            print('Epoch: {}/{} | Iteration: {} | Training Loss: {:.5f} | Time: {} sec'.format(
                epoch, epochs, iteration, loss, (end-start)))
            iteration += 1

            if iteration % print_per_k:
                feed_dict = { inputs_ : val_x, labels_: val_y }
                start = time.time()
                validation_accuracy = sess.run(accuracy, feed_dict=feed_dict)
                end = time.time()
                print('Epoch: {}/{} | Iteration: {} | Validation Accuracy: {:.4f} | Time: {} sec'.format(
                    epoch, epochs, iteration, validation_accuracy, end-start))
    
    saver.save(sess, "checkpoints/flowers.ckpt")


Epoch: 0/20 | Iteration: 200 | Training Loss: 6.52867 | Time: 0.01751708984375 sec
Epoch: 0/20 | Iteration: 201 | Validation Accuracy: 0.3569 | Time: 0.012011528015136719 sec
Epoch: 0/20 | Iteration: 201 | Training Loss: 15.27522 | Time: 0.0065059661865234375 sec
Epoch: 0/20 | Iteration: 202 | Validation Accuracy: 0.5286 | Time: 0.00750732421875 sec
Epoch: 0/20 | Iteration: 202 | Training Loss: 15.88501 | Time: 0.00850820541381836 sec
Epoch: 0/20 | Iteration: 203 | Validation Accuracy: 0.5368 | Time: 0.00750732421875 sec
Epoch: 0/20 | Iteration: 203 | Training Loss: 9.68733 | Time: 0.008008003234863281 sec
Epoch: 0/20 | Iteration: 204 | Validation Accuracy: 0.6676 | Time: 0.0070073604583740234 sec
Epoch: 0/20 | Iteration: 204 | Training Loss: 4.69021 | Time: 0.008008480072021484 sec
Epoch: 0/20 | Iteration: 205 | Training Loss: 2.35238 | Time: 0.006005525588989258 sec
Epoch: 0/20 | Iteration: 206 | Validation Accuracy: 0.7384 | Time: 0.008007049560546875 sec
Epoch: 0/20 | Iteration: 206 | Training Loss: 2.61132 | Time: 0.007506132125854492 sec
Epoch: 0/20 | Iteration: 207 | Validation Accuracy: 0.7820 | Time: 0.0065059661865234375 sec
Epoch: 0/20 | Iteration: 207 | Training Loss: 1.53544 | Time: 0.00650787353515625 sec
Epoch: 0/20 | Iteration: 208 | Validation Accuracy: 0.7738 | Time: 0.005505084991455078 sec
Epoch: 0/20 | Iteration: 208 | Training Loss: 1.69241 | Time: 0.006006002426147461 sec
Epoch: 0/20 | Iteration: 209 | Validation Accuracy: 0.7738 | Time: 0.006506204605102539 sec
Epoch: 0/20 | Iteration: 209 | Training Loss: 1.30718 | Time: 0.00900888442993164 sec
Epoch: 1/20 | Iteration: 210 | Training Loss: 0.93869 | Time: 0.006506443023681641 sec
Epoch: 1/20 | Iteration: 211 | Validation Accuracy: 0.7956 | Time: 0.007007122039794922 sec
Epoch: 1/20 | Iteration: 211 | Training Loss: 1.03103 | Time: 0.007006645202636719 sec
Epoch: 1/20 | Iteration: 212 | Validation Accuracy: 0.8120 | Time: 0.006005764007568359 sec
Epoch: 1/20 | Iteration: 212 | Training Loss: 0.69993 | Time: 0.005004405975341797 sec
Epoch: 1/20 | Iteration: 213 | Validation Accuracy: 0.8392 | Time: 0.00550532341003418 sec
Epoch: 1/20 | Iteration: 213 | Training Loss: 1.11821 | Time: 0.00550532341003418 sec
Epoch: 1/20 | Iteration: 214 | Validation Accuracy: 0.8501 | Time: 0.006005525588989258 sec
Epoch: 1/20 | Iteration: 214 | Training Loss: 0.80137 | Time: 0.00700688362121582 sec
Epoch: 1/20 | Iteration: 215 | Training Loss: 0.58506 | Time: 0.007006168365478516 sec
Epoch: 1/20 | Iteration: 216 | Validation Accuracy: 0.8556 | Time: 0.005505084991455078 sec
Epoch: 1/20 | Iteration: 216 | Training Loss: 0.45315 | Time: 0.005505084991455078 sec
Epoch: 1/20 | Iteration: 217 | Validation Accuracy: 0.8610 | Time: 0.006506443023681641 sec
Epoch: 1/20 | Iteration: 217 | Training Loss: 0.42905 | Time: 0.006005764007568359 sec
Epoch: 1/20 | Iteration: 218 | Validation Accuracy: 0.8665 | Time: 0.006006002426147461 sec
Epoch: 1/20 | Iteration: 218 | Training Loss: 0.39065 | Time: 0.006005764007568359 sec
Epoch: 1/20 | Iteration: 219 | Validation Accuracy: 0.8692 | Time: 0.005505800247192383 sec
Epoch: 1/20 | Iteration: 219 | Training Loss: 0.44445 | Time: 0.006505727767944336 sec
Epoch: 2/20 | Iteration: 220 | Training Loss: 0.23576 | Time: 0.00550532341003418 sec
Epoch: 2/20 | Iteration: 221 | Validation Accuracy: 0.8638 | Time: 0.005505561828613281 sec
Epoch: 2/20 | Iteration: 221 | Training Loss: 0.33519 | Time: 0.006006002426147461 sec
Epoch: 2/20 | Iteration: 222 | Validation Accuracy: 0.8692 | Time: 0.005505084991455078 sec
Epoch: 2/20 | Iteration: 222 | Training Loss: 0.35267 | Time: 0.006506204605102539 sec
Epoch: 2/20 | Iteration: 223 | Validation Accuracy: 0.8747 | Time: 0.005505561828613281 sec
Epoch: 2/20 | Iteration: 223 | Training Loss: 0.46187 | Time: 0.006006002426147461 sec
Epoch: 2/20 | Iteration: 224 | Validation Accuracy: 0.8719 | Time: 0.006005287170410156 sec
Epoch: 2/20 | Iteration: 224 | Training Loss: 0.32021 | Time: 0.006005287170410156 sec
Epoch: 2/20 | Iteration: 225 | Training Loss: 0.29722 | Time: 0.007005929946899414 sec
Epoch: 2/20 | Iteration: 226 | Validation Accuracy: 0.9046 | Time: 0.006006002426147461 sec
Epoch: 2/20 | Iteration: 226 | Training Loss: 0.30634 | Time: 0.006005287170410156 sec
Epoch: 2/20 | Iteration: 227 | Validation Accuracy: 0.8937 | Time: 0.006506204605102539 sec
Epoch: 2/20 | Iteration: 227 | Training Loss: 0.25484 | Time: 0.005505084991455078 sec
Epoch: 2/20 | Iteration: 228 | Validation Accuracy: 0.8910 | Time: 0.006506443023681641 sec
Epoch: 2/20 | Iteration: 228 | Training Loss: 0.24789 | Time: 0.007006645202636719 sec
Epoch: 2/20 | Iteration: 229 | Validation Accuracy: 0.8992 | Time: 0.005004405975341797 sec
Epoch: 2/20 | Iteration: 229 | Training Loss: 0.22543 | Time: 0.007007122039794922 sec
Epoch: 3/20 | Iteration: 230 | Training Loss: 0.17590 | Time: 0.006005525588989258 sec
Epoch: 3/20 | Iteration: 231 | Validation Accuracy: 0.9019 | Time: 0.005507230758666992 sec
Epoch: 3/20 | Iteration: 231 | Training Loss: 0.15002 | Time: 0.006504058837890625 sec
Epoch: 3/20 | Iteration: 232 | Validation Accuracy: 0.9074 | Time: 0.006006002426147461 sec
Epoch: 3/20 | Iteration: 232 | Training Loss: 0.12444 | Time: 0.006506919860839844 sec
Epoch: 3/20 | Iteration: 233 | Validation Accuracy: 0.9101 | Time: 0.006006479263305664 sec
Epoch: 3/20 | Iteration: 233 | Training Loss: 0.25776 | Time: 0.006505489349365234 sec
Epoch: 3/20 | Iteration: 234 | Validation Accuracy: 0.9101 | Time: 0.005505561828613281 sec
Epoch: 3/20 | Iteration: 234 | Training Loss: 0.21789 | Time: 0.0055048465728759766 sec
Epoch: 3/20 | Iteration: 235 | Training Loss: 0.18523 | Time: 0.006506443023681641 sec
Epoch: 3/20 | Iteration: 236 | Validation Accuracy: 0.9292 | Time: 0.005504608154296875 sec
Epoch: 3/20 | Iteration: 236 | Training Loss: 0.17048 | Time: 0.005505084991455078 sec
Epoch: 3/20 | Iteration: 237 | Validation Accuracy: 0.9401 | Time: 0.006005764007568359 sec
Epoch: 3/20 | Iteration: 237 | Training Loss: 0.14706 | Time: 0.006506443023681641 sec
Epoch: 3/20 | Iteration: 238 | Validation Accuracy: 0.9455 | Time: 0.006005764007568359 sec
Epoch: 3/20 | Iteration: 238 | Training Loss: 0.13952 | Time: 0.005505084991455078 sec
Epoch: 3/20 | Iteration: 239 | Validation Accuracy: 0.9482 | Time: 0.006006002426147461 sec
Epoch: 3/20 | Iteration: 239 | Training Loss: 0.16688 | Time: 0.006005525588989258 sec
Epoch: 4/20 | Iteration: 240 | Training Loss: 0.11099 | Time: 0.00550532341003418 sec
Epoch: 4/20 | Iteration: 241 | Validation Accuracy: 0.9373 | Time: 0.005505084991455078 sec
Epoch: 4/20 | Iteration: 241 | Training Loss: 0.10292 | Time: 0.006005525588989258 sec
Epoch: 4/20 | Iteration: 242 | Validation Accuracy: 0.9428 | Time: 0.00550532341003418 sec
Epoch: 4/20 | Iteration: 242 | Training Loss: 0.09482 | Time: 0.006005525588989258 sec
Epoch: 4/20 | Iteration: 243 | Validation Accuracy: 0.9455 | Time: 0.005505084991455078 sec
Epoch: 4/20 | Iteration: 243 | Training Loss: 0.17577 | Time: 0.006005287170410156 sec
Epoch: 4/20 | Iteration: 244 | Validation Accuracy: 0.9373 | Time: 0.00550532341003418 sec
Epoch: 4/20 | Iteration: 244 | Training Loss: 0.11483 | Time: 0.006008625030517578 sec
Epoch: 4/20 | Iteration: 245 | Training Loss: 0.12002 | Time: 0.005004405975341797 sec
Epoch: 4/20 | Iteration: 246 | Validation Accuracy: 0.9401 | Time: 0.005004167556762695 sec
Epoch: 4/20 | Iteration: 246 | Training Loss: 0.11811 | Time: 0.006506204605102539 sec
Epoch: 4/20 | Iteration: 247 | Validation Accuracy: 0.9373 | Time: 0.006507396697998047 sec
Epoch: 4/20 | Iteration: 247 | Training Loss: 0.08934 | Time: 0.005004405975341797 sec
Epoch: 4/20 | Iteration: 248 | Validation Accuracy: 0.9346 | Time: 0.0060062408447265625 sec
Epoch: 4/20 | Iteration: 248 | Training Loss: 0.10583 | Time: 0.00550532341003418 sec
Epoch: 4/20 | Iteration: 249 | Validation Accuracy: 0.9373 | Time: 0.006006717681884766 sec
Epoch: 4/20 | Iteration: 249 | Training Loss: 0.09668 | Time: 0.00550532341003418 sec
Epoch: 5/20 | Iteration: 250 | Training Loss: 0.07239 | Time: 0.005006551742553711 sec
Epoch: 5/20 | Iteration: 251 | Validation Accuracy: 0.9401 | Time: 0.005005359649658203 sec
Epoch: 5/20 | Iteration: 251 | Training Loss: 0.06506 | Time: 0.006005525588989258 sec
Epoch: 5/20 | Iteration: 252 | Validation Accuracy: 0.9346 | Time: 0.005505561828613281 sec
Epoch: 5/20 | Iteration: 252 | Training Loss: 0.06610 | Time: 0.005003690719604492 sec
Epoch: 5/20 | Iteration: 253 | Validation Accuracy: 0.9346 | Time: 0.0055048465728759766 sec
Epoch: 5/20 | Iteration: 253 | Training Loss: 0.11119 | Time: 0.005505084991455078 sec
Epoch: 5/20 | Iteration: 254 | Validation Accuracy: 0.9373 | Time: 0.005505084991455078 sec
Epoch: 5/20 | Iteration: 254 | Training Loss: 0.07769 | Time: 0.005505084991455078 sec
Epoch: 5/20 | Iteration: 255 | Training Loss: 0.08655 | Time: 0.005005836486816406 sec
Epoch: 5/20 | Iteration: 256 | Validation Accuracy: 0.9401 | Time: 0.0050046443939208984 sec
Epoch: 5/20 | Iteration: 256 | Training Loss: 0.05884 | Time: 0.00550532341003418 sec
Epoch: 5/20 | Iteration: 257 | Validation Accuracy: 0.9401 | Time: 0.005005359649658203 sec
Epoch: 5/20 | Iteration: 257 | Training Loss: 0.05164 | Time: 0.0050046443939208984 sec
Epoch: 5/20 | Iteration: 258 | Validation Accuracy: 0.9401 | Time: 0.0050051212310791016 sec
Epoch: 5/20 | Iteration: 258 | Training Loss: 0.07381 | Time: 0.005506277084350586 sec
Epoch: 5/20 | Iteration: 259 | Validation Accuracy: 0.9428 | Time: 0.00550532341003418 sec
Epoch: 5/20 | Iteration: 259 | Training Loss: 0.06082 | Time: 0.00550532341003418 sec
Epoch: 6/20 | Iteration: 260 | Training Loss: 0.05826 | Time: 0.0050046443939208984 sec
Epoch: 6/20 | Iteration: 261 | Validation Accuracy: 0.9455 | Time: 0.004504203796386719 sec
Epoch: 6/20 | Iteration: 261 | Training Loss: 0.04795 | Time: 0.006006717681884766 sec
Epoch: 6/20 | Iteration: 262 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 6/20 | Iteration: 262 | Training Loss: 0.03584 | Time: 0.005505561828613281 sec
Epoch: 6/20 | Iteration: 263 | Validation Accuracy: 0.9455 | Time: 0.006005525588989258 sec
Epoch: 6/20 | Iteration: 263 | Training Loss: 0.05395 | Time: 0.0050051212310791016 sec
Epoch: 6/20 | Iteration: 264 | Validation Accuracy: 0.9428 | Time: 0.0055048465728759766 sec
Epoch: 6/20 | Iteration: 264 | Training Loss: 0.05020 | Time: 0.006506204605102539 sec
Epoch: 6/20 | Iteration: 265 | Training Loss: 0.05290 | Time: 0.005004405975341797 sec
Epoch: 6/20 | Iteration: 266 | Validation Accuracy: 0.9401 | Time: 0.00550532341003418 sec
Epoch: 6/20 | Iteration: 266 | Training Loss: 0.03668 | Time: 0.005505084991455078 sec
Epoch: 6/20 | Iteration: 267 | Validation Accuracy: 0.9401 | Time: 0.005004405975341797 sec
Epoch: 6/20 | Iteration: 267 | Training Loss: 0.03442 | Time: 0.005506038665771484 sec
Epoch: 6/20 | Iteration: 268 | Validation Accuracy: 0.9401 | Time: 0.0050046443939208984 sec
Epoch: 6/20 | Iteration: 268 | Training Loss: 0.05208 | Time: 0.005505084991455078 sec
Epoch: 6/20 | Iteration: 269 | Validation Accuracy: 0.9401 | Time: 0.0050046443939208984 sec
Epoch: 6/20 | Iteration: 269 | Training Loss: 0.04284 | Time: 0.0050051212310791016 sec
Epoch: 7/20 | Iteration: 270 | Training Loss: 0.03933 | Time: 0.006506204605102539 sec
Epoch: 7/20 | Iteration: 271 | Validation Accuracy: 0.9428 | Time: 0.005004167556762695 sec
Epoch: 7/20 | Iteration: 271 | Training Loss: 0.04048 | Time: 0.005004405975341797 sec
Epoch: 7/20 | Iteration: 272 | Validation Accuracy: 0.9455 | Time: 0.004504203796386719 sec
Epoch: 7/20 | Iteration: 272 | Training Loss: 0.02875 | Time: 0.006005764007568359 sec
Epoch: 7/20 | Iteration: 273 | Validation Accuracy: 0.9482 | Time: 0.005505084991455078 sec
Epoch: 7/20 | Iteration: 273 | Training Loss: 0.02768 | Time: 0.0050048828125 sec
Epoch: 7/20 | Iteration: 274 | Validation Accuracy: 0.9482 | Time: 0.005505084991455078 sec
Epoch: 7/20 | Iteration: 274 | Training Loss: 0.03879 | Time: 0.006005764007568359 sec
Epoch: 7/20 | Iteration: 275 | Training Loss: 0.04480 | Time: 0.0050046443939208984 sec
Epoch: 7/20 | Iteration: 276 | Validation Accuracy: 0.9455 | Time: 0.006005287170410156 sec
Epoch: 7/20 | Iteration: 276 | Training Loss: 0.02910 | Time: 0.0050046443939208984 sec
Epoch: 7/20 | Iteration: 277 | Validation Accuracy: 0.9428 | Time: 0.0050046443939208984 sec
Epoch: 7/20 | Iteration: 277 | Training Loss: 0.02094 | Time: 0.006005764007568359 sec
Epoch: 7/20 | Iteration: 278 | Validation Accuracy: 0.9428 | Time: 0.0050046443939208984 sec
Epoch: 7/20 | Iteration: 278 | Training Loss: 0.03742 | Time: 0.0050051212310791016 sec
Epoch: 7/20 | Iteration: 279 | Validation Accuracy: 0.9455 | Time: 0.00550532341003418 sec
Epoch: 7/20 | Iteration: 279 | Training Loss: 0.02579 | Time: 0.005004405975341797 sec
Epoch: 8/20 | Iteration: 280 | Training Loss: 0.02984 | Time: 0.0050048828125 sec
Epoch: 8/20 | Iteration: 281 | Validation Accuracy: 0.9455 | Time: 0.00550532341003418 sec
Epoch: 8/20 | Iteration: 281 | Training Loss: 0.02658 | Time: 0.005004167556762695 sec
Epoch: 8/20 | Iteration: 282 | Validation Accuracy: 0.9455 | Time: 0.0050051212310791016 sec
Epoch: 8/20 | Iteration: 282 | Training Loss: 0.02149 | Time: 0.005005359649658203 sec
Epoch: 8/20 | Iteration: 283 | Validation Accuracy: 0.9455 | Time: 0.004504203796386719 sec
Epoch: 8/20 | Iteration: 283 | Training Loss: 0.02142 | Time: 0.005004405975341797 sec
Epoch: 8/20 | Iteration: 284 | Validation Accuracy: 0.9455 | Time: 0.006006002426147461 sec
Epoch: 8/20 | Iteration: 284 | Training Loss: 0.02889 | Time: 0.005505561828613281 sec
Epoch: 8/20 | Iteration: 285 | Training Loss: 0.03110 | Time: 0.004503965377807617 sec
Epoch: 8/20 | Iteration: 286 | Validation Accuracy: 0.9428 | Time: 0.006005525588989258 sec
Epoch: 8/20 | Iteration: 286 | Training Loss: 0.01924 | Time: 0.0050048828125 sec
Epoch: 8/20 | Iteration: 287 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 8/20 | Iteration: 287 | Training Loss: 0.01682 | Time: 0.0055048465728759766 sec
Epoch: 8/20 | Iteration: 288 | Validation Accuracy: 0.9455 | Time: 0.0050051212310791016 sec
Epoch: 8/20 | Iteration: 288 | Training Loss: 0.02867 | Time: 0.0050051212310791016 sec
Epoch: 8/20 | Iteration: 289 | Validation Accuracy: 0.9455 | Time: 0.006005048751831055 sec
Epoch: 8/20 | Iteration: 289 | Training Loss: 0.01878 | Time: 0.00550532341003418 sec
Epoch: 9/20 | Iteration: 290 | Training Loss: 0.02075 | Time: 0.005004405975341797 sec
Epoch: 9/20 | Iteration: 291 | Validation Accuracy: 0.9428 | Time: 0.005505084991455078 sec
Epoch: 9/20 | Iteration: 291 | Training Loss: 0.02100 | Time: 0.005004405975341797 sec
Epoch: 9/20 | Iteration: 292 | Validation Accuracy: 0.9428 | Time: 0.00550532341003418 sec
Epoch: 9/20 | Iteration: 292 | Training Loss: 0.01428 | Time: 0.005504608154296875 sec
Epoch: 9/20 | Iteration: 293 | Validation Accuracy: 0.9428 | Time: 0.0050046443939208984 sec
Epoch: 9/20 | Iteration: 293 | Training Loss: 0.01659 | Time: 0.0050051212310791016 sec
Epoch: 9/20 | Iteration: 294 | Validation Accuracy: 0.9455 | Time: 0.004504203796386719 sec
Epoch: 9/20 | Iteration: 294 | Training Loss: 0.02428 | Time: 0.0050048828125 sec
Epoch: 9/20 | Iteration: 295 | Training Loss: 0.02513 | Time: 0.005506038665771484 sec
Epoch: 9/20 | Iteration: 296 | Validation Accuracy: 0.9428 | Time: 0.005005598068237305 sec
Epoch: 9/20 | Iteration: 296 | Training Loss: 0.01606 | Time: 0.004504203796386719 sec
Epoch: 9/20 | Iteration: 297 | Validation Accuracy: 0.9428 | Time: 0.00450444221496582 sec
Epoch: 9/20 | Iteration: 297 | Training Loss: 0.01280 | Time: 0.0050048828125 sec
Epoch: 9/20 | Iteration: 298 | Validation Accuracy: 0.9428 | Time: 0.006005525588989258 sec
Epoch: 9/20 | Iteration: 298 | Training Loss: 0.02070 | Time: 0.006005764007568359 sec
Epoch: 9/20 | Iteration: 299 | Validation Accuracy: 0.9455 | Time: 0.00550532341003418 sec
Epoch: 9/20 | Iteration: 299 | Training Loss: 0.01415 | Time: 0.005004405975341797 sec
Epoch: 10/20 | Iteration: 300 | Training Loss: 0.01459 | Time: 0.006005764007568359 sec
Epoch: 10/20 | Iteration: 301 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 10/20 | Iteration: 301 | Training Loss: 0.01664 | Time: 0.004504203796386719 sec
Epoch: 10/20 | Iteration: 302 | Validation Accuracy: 0.9455 | Time: 0.0050046443939208984 sec
Epoch: 10/20 | Iteration: 302 | Training Loss: 0.01155 | Time: 0.0050048828125 sec
Epoch: 10/20 | Iteration: 303 | Validation Accuracy: 0.9428 | Time: 0.005506038665771484 sec
Epoch: 10/20 | Iteration: 303 | Training Loss: 0.01200 | Time: 0.0055048465728759766 sec
Epoch: 10/20 | Iteration: 304 | Validation Accuracy: 0.9428 | Time: 0.0050048828125 sec
Epoch: 10/20 | Iteration: 304 | Training Loss: 0.01831 | Time: 0.005004405975341797 sec
Epoch: 10/20 | Iteration: 305 | Training Loss: 0.01767 | Time: 0.006005764007568359 sec
Epoch: 10/20 | Iteration: 306 | Validation Accuracy: 0.9428 | Time: 0.005505561828613281 sec
Epoch: 10/20 | Iteration: 306 | Training Loss: 0.01349 | Time: 0.0050051212310791016 sec
Epoch: 10/20 | Iteration: 307 | Validation Accuracy: 0.9428 | Time: 0.005506277084350586 sec
Epoch: 10/20 | Iteration: 307 | Training Loss: 0.01049 | Time: 0.006005764007568359 sec
Epoch: 10/20 | Iteration: 308 | Validation Accuracy: 0.9428 | Time: 0.00450444221496582 sec
Epoch: 10/20 | Iteration: 308 | Training Loss: 0.01539 | Time: 0.0050051212310791016 sec
Epoch: 10/20 | Iteration: 309 | Validation Accuracy: 0.9428 | Time: 0.0050046443939208984 sec
Epoch: 10/20 | Iteration: 309 | Training Loss: 0.01077 | Time: 0.005004167556762695 sec
Epoch: 11/20 | Iteration: 310 | Training Loss: 0.01089 | Time: 0.0050046443939208984 sec
Epoch: 11/20 | Iteration: 311 | Validation Accuracy: 0.9428 | Time: 0.005505561828613281 sec
Epoch: 11/20 | Iteration: 311 | Training Loss: 0.01076 | Time: 0.005505800247192383 sec
Epoch: 11/20 | Iteration: 312 | Validation Accuracy: 0.9428 | Time: 0.0050048828125 sec
Epoch: 11/20 | Iteration: 312 | Training Loss: 0.00864 | Time: 0.0050048828125 sec
Epoch: 11/20 | Iteration: 313 | Validation Accuracy: 0.9428 | Time: 0.005505561828613281 sec
Epoch: 11/20 | Iteration: 313 | Training Loss: 0.01005 | Time: 0.00550532341003418 sec
Epoch: 11/20 | Iteration: 314 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 11/20 | Iteration: 314 | Training Loss: 0.01471 | Time: 0.005004405975341797 sec
Epoch: 11/20 | Iteration: 315 | Training Loss: 0.01314 | Time: 0.006506204605102539 sec
Epoch: 11/20 | Iteration: 316 | Validation Accuracy: 0.9428 | Time: 0.0050046443939208984 sec
Epoch: 11/20 | Iteration: 316 | Training Loss: 0.00980 | Time: 0.0050046443939208984 sec
Epoch: 11/20 | Iteration: 317 | Validation Accuracy: 0.9428 | Time: 0.005505561828613281 sec
Epoch: 11/20 | Iteration: 317 | Training Loss: 0.00785 | Time: 0.0055048465728759766 sec
Epoch: 11/20 | Iteration: 318 | Validation Accuracy: 0.9428 | Time: 0.004504203796386719 sec
Epoch: 11/20 | Iteration: 318 | Training Loss: 0.01134 | Time: 0.005005359649658203 sec
Epoch: 11/20 | Iteration: 319 | Validation Accuracy: 0.9428 | Time: 0.006005525588989258 sec
Epoch: 11/20 | Iteration: 319 | Training Loss: 0.00872 | Time: 0.0050046443939208984 sec
Epoch: 12/20 | Iteration: 320 | Training Loss: 0.00914 | Time: 0.00550532341003418 sec
Epoch: 12/20 | Iteration: 321 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 12/20 | Iteration: 321 | Training Loss: 0.00738 | Time: 0.0050046443939208984 sec
Epoch: 12/20 | Iteration: 322 | Validation Accuracy: 0.9455 | Time: 0.005004405975341797 sec
Epoch: 12/20 | Iteration: 322 | Training Loss: 0.00685 | Time: 0.005004405975341797 sec
Epoch: 12/20 | Iteration: 323 | Validation Accuracy: 0.9455 | Time: 0.005505561828613281 sec
Epoch: 12/20 | Iteration: 323 | Training Loss: 0.00838 | Time: 0.004503726959228516 sec
Epoch: 12/20 | Iteration: 324 | Validation Accuracy: 0.9482 | Time: 0.005505084991455078 sec
Epoch: 12/20 | Iteration: 324 | Training Loss: 0.01152 | Time: 0.00550532341003418 sec
Epoch: 12/20 | Iteration: 325 | Training Loss: 0.01058 | Time: 0.006005764007568359 sec
Epoch: 12/20 | Iteration: 326 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 12/20 | Iteration: 326 | Training Loss: 0.00807 | Time: 0.0050048828125 sec
Epoch: 12/20 | Iteration: 327 | Validation Accuracy: 0.9455 | Time: 0.005005359649658203 sec
Epoch: 12/20 | Iteration: 327 | Training Loss: 0.00646 | Time: 0.005504608154296875 sec
Epoch: 12/20 | Iteration: 328 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 12/20 | Iteration: 328 | Training Loss: 0.00963 | Time: 0.00550532341003418 sec
Epoch: 12/20 | Iteration: 329 | Validation Accuracy: 0.9455 | Time: 0.0050046443939208984 sec
Epoch: 12/20 | Iteration: 329 | Training Loss: 0.00696 | Time: 0.005003929138183594 sec
Epoch: 13/20 | Iteration: 330 | Training Loss: 0.00678 | Time: 0.004503965377807617 sec
Epoch: 13/20 | Iteration: 331 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 13/20 | Iteration: 331 | Training Loss: 0.00590 | Time: 0.005505800247192383 sec
Epoch: 13/20 | Iteration: 332 | Validation Accuracy: 0.9482 | Time: 0.0050048828125 sec
Epoch: 13/20 | Iteration: 332 | Training Loss: 0.00571 | Time: 0.005004405975341797 sec
Epoch: 13/20 | Iteration: 333 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 13/20 | Iteration: 333 | Training Loss: 0.00724 | Time: 0.005003929138183594 sec
Epoch: 13/20 | Iteration: 334 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 13/20 | Iteration: 334 | Training Loss: 0.00997 | Time: 0.0050046443939208984 sec
Epoch: 13/20 | Iteration: 335 | Training Loss: 0.00887 | Time: 0.006005525588989258 sec
Epoch: 13/20 | Iteration: 336 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 13/20 | Iteration: 336 | Training Loss: 0.00693 | Time: 0.0050048828125 sec
Epoch: 13/20 | Iteration: 337 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 13/20 | Iteration: 337 | Training Loss: 0.00517 | Time: 0.005004167556762695 sec
Epoch: 13/20 | Iteration: 338 | Validation Accuracy: 0.9455 | Time: 0.004504203796386719 sec
Epoch: 13/20 | Iteration: 338 | Training Loss: 0.00749 | Time: 0.005003929138183594 sec
Epoch: 13/20 | Iteration: 339 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 13/20 | Iteration: 339 | Training Loss: 0.00603 | Time: 0.004503965377807617 sec
Epoch: 14/20 | Iteration: 340 | Training Loss: 0.00525 | Time: 0.0050051212310791016 sec
Epoch: 14/20 | Iteration: 341 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 14/20 | Iteration: 341 | Training Loss: 0.00517 | Time: 0.0050051212310791016 sec
Epoch: 14/20 | Iteration: 342 | Validation Accuracy: 0.9482 | Time: 0.005506753921508789 sec
Epoch: 14/20 | Iteration: 342 | Training Loss: 0.00479 | Time: 0.005004405975341797 sec
Epoch: 14/20 | Iteration: 343 | Validation Accuracy: 0.9482 | Time: 0.007006406784057617 sec
Epoch: 14/20 | Iteration: 343 | Training Loss: 0.00616 | Time: 0.006005048751831055 sec
Epoch: 14/20 | Iteration: 344 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 14/20 | Iteration: 344 | Training Loss: 0.00826 | Time: 0.005505084991455078 sec
Epoch: 14/20 | Iteration: 345 | Training Loss: 0.00718 | Time: 0.005505084991455078 sec
Epoch: 14/20 | Iteration: 346 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 14/20 | Iteration: 346 | Training Loss: 0.00583 | Time: 0.005005359649658203 sec
Epoch: 14/20 | Iteration: 347 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 14/20 | Iteration: 347 | Training Loss: 0.00439 | Time: 0.0050048828125 sec
Epoch: 14/20 | Iteration: 348 | Validation Accuracy: 0.9455 | Time: 0.00450444221496582 sec
Epoch: 14/20 | Iteration: 348 | Training Loss: 0.00612 | Time: 0.005005359649658203 sec
Epoch: 14/20 | Iteration: 349 | Validation Accuracy: 0.9455 | Time: 0.005004405975341797 sec
Epoch: 14/20 | Iteration: 349 | Training Loss: 0.00511 | Time: 0.006506681442260742 sec
Epoch: 15/20 | Iteration: 350 | Training Loss: 0.00435 | Time: 0.00550532341003418 sec
Epoch: 15/20 | Iteration: 351 | Validation Accuracy: 0.9455 | Time: 0.0050051212310791016 sec
Epoch: 15/20 | Iteration: 351 | Training Loss: 0.00416 | Time: 0.0050046443939208984 sec
Epoch: 15/20 | Iteration: 352 | Validation Accuracy: 0.9455 | Time: 0.006006479263305664 sec
Epoch: 15/20 | Iteration: 352 | Training Loss: 0.00403 | Time: 0.005003213882446289 sec
Epoch: 15/20 | Iteration: 353 | Validation Accuracy: 0.9455 | Time: 0.0050051212310791016 sec
Epoch: 15/20 | Iteration: 353 | Training Loss: 0.00538 | Time: 0.006506204605102539 sec
Epoch: 15/20 | Iteration: 354 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 15/20 | Iteration: 354 | Training Loss: 0.00709 | Time: 0.006005287170410156 sec
Epoch: 15/20 | Iteration: 355 | Training Loss: 0.00600 | Time: 0.005505561828613281 sec
Epoch: 15/20 | Iteration: 356 | Validation Accuracy: 0.9455 | Time: 0.005005598068237305 sec
Epoch: 15/20 | Iteration: 356 | Training Loss: 0.00501 | Time: 0.0050046443939208984 sec
Epoch: 15/20 | Iteration: 357 | Validation Accuracy: 0.9455 | Time: 0.0050046443939208984 sec
Epoch: 15/20 | Iteration: 357 | Training Loss: 0.00373 | Time: 0.005505084991455078 sec
Epoch: 15/20 | Iteration: 358 | Validation Accuracy: 0.9455 | Time: 0.0055048465728759766 sec
Epoch: 15/20 | Iteration: 358 | Training Loss: 0.00536 | Time: 0.006506919860839844 sec
Epoch: 15/20 | Iteration: 359 | Validation Accuracy: 0.9455 | Time: 0.0050051212310791016 sec
Epoch: 15/20 | Iteration: 359 | Training Loss: 0.00441 | Time: 0.005004405975341797 sec
Epoch: 16/20 | Iteration: 360 | Training Loss: 0.00370 | Time: 0.0050046443939208984 sec
Epoch: 16/20 | Iteration: 361 | Validation Accuracy: 0.9455 | Time: 0.0050046443939208984 sec
Epoch: 16/20 | Iteration: 361 | Training Loss: 0.00359 | Time: 0.0055048465728759766 sec
Epoch: 16/20 | Iteration: 362 | Validation Accuracy: 0.9455 | Time: 0.005004405975341797 sec
Epoch: 16/20 | Iteration: 362 | Training Loss: 0.00352 | Time: 0.005505084991455078 sec
Epoch: 16/20 | Iteration: 363 | Validation Accuracy: 0.9455 | Time: 0.005505084991455078 sec
Epoch: 16/20 | Iteration: 363 | Training Loss: 0.00468 | Time: 0.006005525588989258 sec
Epoch: 16/20 | Iteration: 364 | Validation Accuracy: 0.9455 | Time: 0.0050046443939208984 sec
Epoch: 16/20 | Iteration: 364 | Training Loss: 0.00621 | Time: 0.0050048828125 sec
Epoch: 16/20 | Iteration: 365 | Training Loss: 0.00511 | Time: 0.0050046443939208984 sec
Epoch: 16/20 | Iteration: 366 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 16/20 | Iteration: 366 | Training Loss: 0.00441 | Time: 0.006505727767944336 sec
Epoch: 16/20 | Iteration: 367 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 16/20 | Iteration: 367 | Training Loss: 0.00325 | Time: 0.006005764007568359 sec
Epoch: 16/20 | Iteration: 368 | Validation Accuracy: 0.9455 | Time: 0.00450444221496582 sec
Epoch: 16/20 | Iteration: 368 | Training Loss: 0.00464 | Time: 0.006506443023681641 sec
Epoch: 16/20 | Iteration: 369 | Validation Accuracy: 0.9455 | Time: 0.0050048828125 sec
Epoch: 16/20 | Iteration: 369 | Training Loss: 0.00384 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 370 | Training Loss: 0.00312 | Time: 0.0050051212310791016 sec
Epoch: 17/20 | Iteration: 371 | Validation Accuracy: 0.9455 | Time: 0.004503965377807617 sec
Epoch: 17/20 | Iteration: 371 | Training Loss: 0.00314 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 372 | Validation Accuracy: 0.9482 | Time: 0.00550532341003418 sec
Epoch: 17/20 | Iteration: 372 | Training Loss: 0.00306 | Time: 0.0050048828125 sec
Epoch: 17/20 | Iteration: 373 | Validation Accuracy: 0.9510 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 373 | Training Loss: 0.00414 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 374 | Validation Accuracy: 0.9510 | Time: 0.005004405975341797 sec
Epoch: 17/20 | Iteration: 374 | Training Loss: 0.00543 | Time: 0.006005287170410156 sec
Epoch: 17/20 | Iteration: 375 | Training Loss: 0.00444 | Time: 0.0050048828125 sec
Epoch: 17/20 | Iteration: 376 | Validation Accuracy: 0.9510 | Time: 0.005505561828613281 sec
Epoch: 17/20 | Iteration: 376 | Training Loss: 0.00389 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 377 | Validation Accuracy: 0.9510 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 377 | Training Loss: 0.00290 | Time: 0.005505800247192383 sec
Epoch: 17/20 | Iteration: 378 | Validation Accuracy: 0.9510 | Time: 0.0050051212310791016 sec
Epoch: 17/20 | Iteration: 378 | Training Loss: 0.00404 | Time: 0.0050046443939208984 sec
Epoch: 17/20 | Iteration: 379 | Validation Accuracy: 0.9482 | Time: 0.0050051212310791016 sec
Epoch: 17/20 | Iteration: 379 | Training Loss: 0.00345 | Time: 0.0050048828125 sec
Epoch: 18/20 | Iteration: 380 | Training Loss: 0.00276 | Time: 0.0050048828125 sec
Epoch: 18/20 | Iteration: 381 | Validation Accuracy: 0.9482 | Time: 0.0050046443939208984 sec
Epoch: 18/20 | Iteration: 381 | Training Loss: 0.00278 | Time: 0.0050048828125 sec
Epoch: 18/20 | Iteration: 382 | Validation Accuracy: 0.9482 | Time: 0.0050051212310791016 sec
Epoch: 18/20 | Iteration: 382 | Training Loss: 0.00273 | Time: 0.0055048465728759766 sec
Epoch: 18/20 | Iteration: 383 | Validation Accuracy: 0.9510 | Time: 0.0050046443939208984 sec
Epoch: 18/20 | Iteration: 383 | Training Loss: 0.00370 | Time: 0.0050051212310791016 sec
Epoch: 18/20 | Iteration: 384 | Validation Accuracy: 0.9510 | Time: 0.0050046443939208984 sec
Epoch: 18/20 | Iteration: 384 | Training Loss: 0.00478 | Time: 0.0050046443939208984 sec
Epoch: 18/20 | Iteration: 385 | Training Loss: 0.00392 | Time: 0.005004405975341797 sec
Epoch: 18/20 | Iteration: 386 | Validation Accuracy: 0.9510 | Time: 0.005505800247192383 sec
Epoch: 18/20 | Iteration: 386 | Training Loss: 0.00347 | Time: 0.0050048828125 sec
Epoch: 18/20 | Iteration: 387 | Validation Accuracy: 0.9510 | Time: 0.004503965377807617 sec
Epoch: 18/20 | Iteration: 387 | Training Loss: 0.00248 | Time: 0.005004405975341797 sec
Epoch: 18/20 | Iteration: 388 | Validation Accuracy: 0.9510 | Time: 0.0050048828125 sec
Epoch: 18/20 | Iteration: 388 | Training Loss: 0.00356 | Time: 0.005004405975341797 sec
Epoch: 18/20 | Iteration: 389 | Validation Accuracy: 0.9510 | Time: 0.00450444221496582 sec
Epoch: 18/20 | Iteration: 389 | Training Loss: 0.00306 | Time: 0.005004405975341797 sec
Epoch: 19/20 | Iteration: 390 | Training Loss: 0.00245 | Time: 0.0060045719146728516 sec
Epoch: 19/20 | Iteration: 391 | Validation Accuracy: 0.9510 | Time: 0.0050046443939208984 sec
Epoch: 19/20 | Iteration: 391 | Training Loss: 0.00245 | Time: 0.006005525588989258 sec
Epoch: 19/20 | Iteration: 392 | Validation Accuracy: 0.9510 | Time: 0.0050048828125 sec
Epoch: 19/20 | Iteration: 392 | Training Loss: 0.00239 | Time: 0.00550532341003418 sec
Epoch: 19/20 | Iteration: 393 | Validation Accuracy: 0.9510 | Time: 0.005004405975341797 sec
Epoch: 19/20 | Iteration: 393 | Training Loss: 0.00333 | Time: 0.006506204605102539 sec
Epoch: 19/20 | Iteration: 394 | Validation Accuracy: 0.9510 | Time: 0.005005598068237305 sec
Epoch: 19/20 | Iteration: 394 | Training Loss: 0.00411 | Time: 0.004504680633544922 sec
Epoch: 19/20 | Iteration: 395 | Training Loss: 0.00347 | Time: 0.00550532341003418 sec
Epoch: 19/20 | Iteration: 396 | Validation Accuracy: 0.9510 | Time: 0.0050048828125 sec
Epoch: 19/20 | Iteration: 396 | Training Loss: 0.00312 | Time: 0.006006479263305664 sec
Epoch: 19/20 | Iteration: 397 | Validation Accuracy: 0.9510 | Time: 0.005004167556762695 sec
Epoch: 19/20 | Iteration: 397 | Training Loss: 0.00222 | Time: 0.005506277084350586 sec
Epoch: 19/20 | Iteration: 398 | Validation Accuracy: 0.9510 | Time: 0.005005359649658203 sec
Epoch: 19/20 | Iteration: 398 | Training Loss: 0.00319 | Time: 0.005005598068237305 sec
Epoch: 19/20 | Iteration: 399 | Validation Accuracy: 0.9510 | Time: 0.0050048828125 sec
Epoch: 19/20 | Iteration: 399 | Training Loss: 0.00271 | Time: 0.0050046443939208984 sec

Testing

Below you see the test accuracy. You can also see the predictions returned for images.


In [37]:
with tf.Session() as sess:
    saver.restore(sess, tf.train.latest_checkpoint('checkpoints'))
    
    feed = {inputs_: test_x,
            labels_: test_y}
    test_acc = sess.run(accuracy, feed_dict=feed)
    print("Test accuracy: {:.4f}".format(test_acc))


Test accuracy: 0.9564

In [38]:
%matplotlib inline

import matplotlib.pyplot as plt
from scipy.ndimage import imread

Below, feel free to choose images and see how the trained classifier predicts the flowers in them.


In [39]:
test_img_path = 'flower_photos/roses/10894627425_ec76bbc757_n.jpg'
test_img = imread(test_img_path)
plt.imshow(test_img)


Out[39]:
<matplotlib.image.AxesImage at 0x18ed8f63748>

In [40]:
# Run this cell if you don't have a vgg graph built
if 'vgg' in globals():
    print('"vgg" object already exists.  Will not create again.')
else:
    #create vgg
    with tf.Session() as sess:
        input_ = tf.placeholder(tf.float32, [None, 224, 224, 3])
        vgg = vgg16.Vgg16()
        vgg.build(input_)


C:\Users\davef\Desktop\src\deep-learning\transfer-learning\tensorflow_vgg\vgg16.npy
npy file loaded
build model started
build model finished: 0s

In [41]:
with tf.Session() as sess:
    img = utils.load_image(test_img_path)
    img = img.reshape((1, 224, 224, 3))

    feed_dict = {input_: img}
    code = sess.run(vgg.relu6, feed_dict=feed_dict)
        
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, tf.train.latest_checkpoint('checkpoints'))
    
    feed = {inputs_: code}
    prediction = sess.run(predicted, feed_dict=feed).squeeze()

In [42]:
plt.imshow(test_img)


Out[42]:
<matplotlib.image.AxesImage at 0x18ed9d2d0f0>

In [44]:
plt.barh(np.arange(5), prediction)
_ = plt.yticks(np.arange(5), binarizer.classes_)