Testing the pre-trained model

We have already created a VGG16 neural network. In this section, we will try to use the pre-trained model to perform the classifications of cars, cats, and dogs to check whether the model has been loaded successfully.

In the nets.py file, we need to replace the current __main__ code with the following code:

    import os 
    from utils import debug_print 
    from scipy.misc import imread, imresize 
 
 
    if __name__ == "__main__": 
    SAMPLES_FOLDER = "samples_data" 
    with open('%s/imagenet-classes.txt' % SAMPLES_FOLDER, 'rb') as   
    infile: 
     class_labels = map(str.strip, infile.readlines()) 
 
    inputs = tf.placeholder(tf.float32, [None, 224, 224, 3],   
    name="inputs") 
    outputs = inference(inputs) 
 
    debug_print.print_variables(tf.global_variables()) 
    debug_print.print_variables([inputs, outputs]) 
 
    with tf.Session() as sess: 
     load_caffe_weights("data/VGG16.npz", sess,   
    ignore_missing=False) 
 
        files = os.listdir(SAMPLES_FOLDER) 
        for file_name in files: 
            if not file_name.endswith(".jpg"): 
                continue 
            print("=== Predict %s ==== " % file_name) 
            img = imread(os.path.join(SAMPLES_FOLDER, file_name),  
            mode="RGB") 
            img = imresize(img, (224, 224)) 
 
            prob = sess.run(outputs, feed_dict={inputs: [img]})[0] 
            preds = (np.argsort(prob)[::-1])[0:3] 
 
            for p in preds: 
                print class_labels[p], prob[p]

In the preceding code, there are several things that you should note:

  • We use the debug_print.print_variables helper method to visualize all the variables by printing the variable names and shapes.
  • We define a placeholder named inputs with the shape [None, 224, 224, 3], which is the required input size of the VGG16 model:
      We get the model graph with outputs = inference(inputs). 
  • In tf.Session(), we call the load_caffe_weights method with ignore_missing=False to ensure that we can load all the weights and biases of the pre-trained model.
  • The image is loaded and resized with the imread and imresize methods from scipy. Then, we use the sess.run method with the feed_dict dictionary and receive the predictions.
  • The following results are the predictions for car.jpg, cat.jpg, and dog.jpg in the samples_data that we provided at the beginning of the chapter:
    == Predict car.jpg ==== 
    racer, race car, racing car 0.666172
    sports car, sport car 0.315847
    car wheel 0.0117961
    === Predict cat.jpg ==== 
    Persian cat 0.762223
    tabby, tabby cat 0.0647032
    lynx, catamount 0.0371023
    === Predict dog.jpg ==== 
    Border collie 0.562288
    collie 0.239735
    Appenzeller 0.0186233

The preceding results are the exact labels of these images. This means that we have successfully loaded the pre-trained VGG16 model in TensorFlow. In the next section, we will show you how to fine-tune the model on our dataset.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.239.214