<jupyter_start><jupyter_text>IntroductionIn this notebook we will test the tuned implementation of Alexnet on the 101 Objects dataset on nine randomly selected images from the test pool. For the author's original discussion of tuning this CNN, see [here](https://kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html).To run this notebook you have to download the `bvlc_alexnet.npy` file from [here](http://www.cs.toronto.edu/~guerzhoy/tf_alexnet/), which stores the pretrained weights for AlexNet. Please be aware that checkpoint data is *required* for this demo to run properly; if checkpoint data is not present, please run ob_tuner.py to generate it.This notebook was originally written by <NAME>, and has been modified by <NAME>. If errors are encountered in the last cell, please restart the kernel and try again.<jupyter_code># Test of 101 Objects Data Set
#some basic imports and setups
import os
import cv2
import random
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

%matplotlib inline

# Mean of imagenet dataset in BGR
imagenet_mean = np.array([104., 117., 124.], dtype=np.float32)

# Open image list
te_text = open('data/101_obj/@te_list.txt')

# Format image list
te_list = te_text.readlines()
te_list = [x.replace('\n', '') for x in te_list]
te_selection = []

# Randomly select images
for i in range(9):
    te_selection.append(random.choice(te_list))
    
# Load all images
imgs = []
for image in te_selection:
    imgs.append(cv2.imread(image))
    
# Plot images
fig = plt.figure(figsize=(15,6))
for i, img in enumerate(imgs):
    fig.add_subplot(3,3,i+1)
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.axis('off')<jupyter_output><empty_output><jupyter_text>First we will create placeholder for the dropout rate and the inputs and create an AlexNet object. Then we will link the activations from the last layer to the variable `score` and define an op to calculate the softmax values.<jupyter_code>import sys
sys.path.append('code')
from alexnet import AlexNet
from ob_classes import class_names

#placeholder for input and dropout rate
x = tf.placeholder(tf.float32, [1, 256, 256, 3])
keep_prob = tf.placeholder(tf.float32)

#create model with default config ( == no skip_layer and 1000 units in the last layer)
model = AlexNet(x, keep_prob, 101, [])

#define activation of last layer as score
score = model.fc8

#create op to calculate softmax 
softmax = tf.nn.softmax(score)<jupyter_output><empty_output><jupyter_text>Now we will start a TensorFlow session and load pretrained weights into the layer weights. Then we will loop over all images and calculate the class probability for each image and plot the image again, together with the predicted class and the corresponding class probability.<jupyter_code>with tf.Session() as sess:
    
    # Initialize all variables
    sess.run(tf.global_variables_initializer())
    
    # Load tuned weights into the model
    saver = tf.train.Saver()
    saver.restore(sess, "data/ob_checkpoints/model_epoch10.ckpt")
    
    # Create figure handle
    fig2 = plt.figure(figsize=(15,6))
    
    # Loop over all images
    for i, image in enumerate(imgs):
        
        # Convert image to float32 and resize to (256x256)
        img = cv2.resize(image.astype(np.float32), (256,256))
        
        # Subtract the ImageNet mean
        img -= imagenet_mean
        
        # Reshape as needed to feed into model
        img = img.reshape((1,256,256,3))
        
        # Run the session and calculate the class probability
        probs = sess.run(softmax, feed_dict={x: img, keep_prob: 1})
        
        # Get the class name of the class with the highest probability
        class_name = class_names[np.argmax(probs)]
        
        # Plot image with class name and prob in the title
        fig2.add_subplot(3,3,i+1)
        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.title("Class: " + class_name + ", probability: %.4f" %probs[0,np.argmax(probs)])
        plt.axis('off')<jupyter_output>INFO:tensorflow:Restoring parameters from data/ob_checkpoints/model_epoch10.ckpt
{'fraction_non_alphanumeric': 0.08850630455868089, 'fraction_numerical': 0.024975751697381185, 'mean_word_length': 3.8529411764705883, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '162307', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1262, 'n_words': 444, 'path': 'ob_demo.ipynb', 'repo_name': 'audreymgu/tune-alexnet', 'size': '4138'}