We have prepared all necessary files after implementation of part 1 and part 2 works. Lets start procedure of learning with tensorflow. TF model part taken at this link (Jiovanny Claudio). Jiovanny gave very good explanation of Tensorflow model which have used for deep learning. I have made some corrections in model for correct implementation of Tensorflow 1.2.0. Also I have added names of tensors for next use of trained model in other projects (espessially with android devices and embedded platforms).
This is example of testing trained model – very good result for this dataset quality – ~94% of proper recognition!
Ok. First start with learning of model.
We have pickled files with training, validation and test set and also prepared for learning pickled files train_p_ru.p, test_p_ru.p, valid_p_ru.p
""" tf part from this project https://github.com/jokla/CarND-Traffic-Sign-Classifier-Project/blob/master/Traffic_Sign_Classifier.ipynb other part (traffic_ru images and preparation) from original project traffic_ru_aug.py program load pickled data from pickled files (training and test) made by make_pickled_from_image.py where all images saved in [features ] and all labels (classes of images) savel to [labels] Images saved in an numpy array 32x32x3 (RGB) color profile labels is integer value from 0 till 66 in traffic_ru example """ ######################################################################################################## import os import pickle import pandas as pd import numpy as np import tensorflow as tf from tensorflow.contrib.layers import flatten from sklearn import metrics from sklearn.utils import shuffle from skimage import exposure import cv2 import matplotlib.pyplot as plt import random ######################################################################################################## def LeNet(x, weights, biases, apply_dropout): if apply_dropout is not None: print ("Training phase -> perform Dropout") else: print ("Evalutation phase -> not performing Dropout") layer = 0 # Layer 1: Convolutional. Input = 32x32x1. Output = 28x28x12. conv1 = tf.nn.conv2d(x, weights[layer], strides=[1, 1, 1, 1], padding='VALID') + biases[layer] layer += 1 # Activation. conv1 = tf.nn.relu(conv1, name = 'act1') # Pooling. Input = 28x28x12. Output = 14x14x12. conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') # Dropout conv1 = tf.cond(apply_dropout, lambda: tf.nn.dropout(conv1, keep_prob = 0.8), lambda: conv1) # Layer 2: Convolutional. Output = 10x10x24. conv2 = tf.nn.conv2d(conv1, weights[layer], strides=[1, 1, 1, 1], padding='VALID') + biases[layer] layer += 1 # Activation. conv2 = tf.nn.relu(conv2, name = 'act2') # Pooling. Input = 10x10x24. Output = 5x5x24. conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') # Dropout conv2 = tf.cond(apply_dropout, lambda: tf.nn.dropout(conv2, keep_prob = 0.7), lambda: conv2) # Input = 14x14x12. Output = 7x7x12 = 588 conv1_1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') shape = conv1_1.get_shape().as_list() conv1_1 = tf.reshape(conv1_1, [-1, shape[1] * shape[2] * shape[3]]) # Flatten conv2 Input = 5x5x24. Output = 600 shape = conv2.get_shape().as_list() conv2 = tf.reshape(conv2, [-1, shape[1] * shape[2] * shape[3]]) #fc0 = tf.concat(1, [conv1_1, conv2]) #old version before tf 1.0.0 fc0 = tf.concat([conv1_1, conv2],1) #new version # Layer 3: Fully Connected. Input = 588+600 = 1188. Output = 320. fc1 = tf.matmul(fc0, weights[layer]) + biases[layer] layer += 1 # Activation. fc1 = tf.nn.relu(fc1) # Dropout fc1 = tf.cond(apply_dropout, lambda: tf.nn.dropout(fc1, keep_prob = 0.6), lambda: fc1) logits = tf.matmul(fc1, weights[layer], name = "logits") + biases[layer] return logits # Layer 4: Fully Connected. Input = 512. Output = 700. #fc2_W = tf.Variable(tf.truncated_normal(shape=(128, 700), mean = mu, stddev = sigma)) #fc2_b = tf.Variable(tf.zeros(700)) #fc2 = tf.matmul(fc1, fc2_W) + fc2_b # Activation. #fc2 = tf.nn.relu(fc2) # Dropout #fc2 = tf.cond(apply_dropout, lambda: tf.nn.dropout(fc2, keep_prob = 0.5), lambda: fc2) # Layer 5: Fully Connected. Input = 84. Output = n_classes. #fc3_W = tf.Variable(tf.truncated_normal(shape=(320, n_classes), mean = mu, stddev = sigma)) #fc3_b = tf.Variable(tf.zeros(n_classes)) ######################################################################################################### #Evaluate how well the loss and accuracy of the model for a given dataset. ######################################################################################################### def evaluate(X_data, y_data): num_examples = len(X_data) total_accuracy = 0 sess = tf.get_default_session() for offset in range(0, num_examples, BATCH_SIZE): batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE] accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y, apply_dropout: False}) total_accuracy += (accuracy * len(batch_x)) return total_accuracy / num_examples ######################################################################################################### ######################################################################################################### # load pickled files training_file = "./pickle_data/train_ru.p" validation_file = "./pickle_data/valid_ru.p" testing_file = "./pickle_data/test_ru.p" with open(training_file, mode='rb') as f1: train = pickle.load(f1) with open(validation_file, mode='rb') as f2: valid = pickle.load(f2) with open(testing_file, mode='rb') as f3: test = pickle.load(f3) X_train, y_train = train['features'], train['labels'] X_valid, y_valid = valid['features'], valid['labels'] X_test, y_test = test['features'], test['labels'] #load preprocessed pickled files training_p_file = "./pickle_data/train_ru_p.p" validation_p_file = "./pickle_data/valid_ru_p.p" testing_p_file = "./pickle_data/test_ru_p.p" with open(training_p_file, mode='rb') as f4: train_p = pickle.load(f4) with open(validation_p_file, mode='rb') as f5: valid_p = pickle.load(f5) with open(testing_p_file, mode='rb') as f6: test_p = pickle.load(f6) X_train_p, y_train_p = train_p['features'], train_p['labels'] X_valid_p, y_valid_p = valid_p['features'], valid_p['labels'] X_test_p, y_test_p = test_p['features'], test_p['labels'] # Read sign names sign_names = pd.read_csv("numbers_to_classes.csv").values[:, 1] #for sign_name in sign_names: #print sign_name # Number of training examples n_train = len(X_train) # Number of validation examples n_valid = len(X_valid) # Number of testing examples. n_test = len(X_test) # What's the shape of an traffic sign image? image_shape = X_train[0].shape # How many unique classes/labels there are in the dataset. n_classes = len(np.unique(y_train)) sign_classes, class_indices, class_counts = np.unique(y_train, return_index = True, return_counts = True) n_classes = len(class_counts) sign_classes_test, class_indices_test, class_counts_test = np.unique(y_test, return_index = True, return_counts = True) print("Number of training examples =", n_train) print("Number of validation examples =", n_valid) print("Number of testing examples =", n_test) print("Image data shape =", image_shape) print("Number of train classes =", n_classes) print("Number of test classes =", len(class_counts_test)) print("Number of preprocessed training examples =", len(X_train_p)) print("Number of preprocessed validation examples =", len(X_valid_p)) print("Number of preprocessed testing examples =", len(X_test_p)) print("Preprocessed image data shape =", X_train_p[0].shape) """ # show preprocessed result col_width = max(len(name) for name in sign_names) for c, c_index, c_count in zip(sign_classes, class_indices, class_counts): print("Class %i: %-*s %s samples" % (c, col_width, sign_names[c], str(c_count))) fig = plt.figure(figsize = (6, 1)) fig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 1, hspace = 0.05, wspace = 0.05) random_ind = random.sample(range(c_index, c_index + c_count), 1) axis = fig.add_subplot(1, 4, 1, xticks=[], yticks=[]) axis.imshow(X_train[random_ind].squeeze()) axis = fig.add_subplot(1, 4, 2, xticks=[], yticks=[]) axis.imshow(X_train_p[random_ind].squeeze(), cmap='gray') plt.show() print("----------------------------------------\n") """ #creating placeholders x = tf.placeholder(tf.float32, (None, 32, 32, 1), name = "images") # image(s) y = tf.placeholder(tf.int32, (None), name = "labels") # label(s) one_hot_y = tf.one_hot(y, n_classes) apply_dropout = tf.placeholder(tf.bool, name = "apply_dropout") #creating model architecture EPOCHS = 30 BATCH_SIZE = 128 ### Calculate and report the accuracy on the training and validation set. ### Once a final model architecture is selected, ### the accuracy on the test set should be calculated and reported as well. ### Feel free to use as many code cells as needed. rate = 0.001 # Arguments used for tf.truncated_normal, randomly defines variables for the weights # and biases for each layer mu = 0 sigma = 0.1 beta = 0.001 weights = [ tf.Variable(tf.truncated_normal(shape=(5, 5, 1, 12), mean = mu, stddev = sigma)), tf.Variable(tf.truncated_normal(shape=(5, 5, 12, 24), mean = mu, stddev = sigma)), tf.Variable(tf.truncated_normal(shape=(1188, 320), mean = mu, stddev = sigma)), tf.Variable(tf.truncated_normal(shape=(320, n_classes), mean = mu, stddev = sigma)) ] biases = [ tf.Variable(tf.zeros(12)), tf.Variable(tf.zeros(24)), tf.Variable(tf.zeros(320)), tf.Variable(tf.zeros(n_classes)) ] logits = LeNet(x, weights, biases, apply_dropout) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=one_hot_y) # changed to tf 1.2.0 format loss_operation = tf.reduce_mean(cross_entropy) regularizer = tf.reduce_sum([tf.nn.l2_loss(w) for w in weights]) loss = tf.reduce_mean(loss_operation + beta * regularizer) optimizer = tf.train.AdamOptimizer(learning_rate = rate) training_operation = optimizer.minimize(loss) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1), name = "prediction") accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name = "accuracy") saver = tf.train.Saver() # Train the Model # Run the training data through the training pipeline to train the model. # Before each epoch, shuffle the training set. # After each epoch, measure the loss and accuracy of the validation set. # Save the model after training. global best_validation_accuracy best_validation_accuracy = 0.0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #saver.restore(sess, tf.train.latest_checkpoint('.')) #saver.restore(sess, './graph_lenet_saves') num_examples = len(X_train_p) print("Training...") print() for i in range(EPOCHS): X_train_p, y_train_p = shuffle(X_train_p, y_train_p) for offset in range(0, num_examples, BATCH_SIZE): end = offset + BATCH_SIZE batch_x, batch_y = X_train_p[offset:end], y_train_p[offset:end] sess.run(training_operation, feed_dict={x: batch_x, y: batch_y, apply_dropout: True }) validation_accuracy = evaluate(X_valid_p, y_valid_p) training_accuracy = evaluate(X_train_p, y_train_p) print("EPOCH {} ...".format(i+1)) print("Training Accuracy = {:.3f}".format(training_accuracy)) print("Validation Accuracy = {:.3f}".format(validation_accuracy)) print() if (validation_accuracy > best_validation_accuracy): best_validation_accuracy = validation_accuracy saver.save(sess, './graph_lenet_saves/traffic_ru_v1.ckpt') print("Model saved") #compute accuracy with tf.Session() as sess: #saver.restore(sess, tf.train.latest_checkpoint('.')) saver.restore(sess, './graph_lenet_saves/traffic_ru_v1.ckpt') training_accuracy = evaluate(X_train_p, y_train_p) print("Training Accuracy = {:.3f}".format(training_accuracy)) validation_accuracy = evaluate(X_valid_p, y_valid_p) print("Validation Accuracy = {:.3f}".format(validation_accuracy)) test_accuracy = evaluate(X_test_p, y_test_p) print("Test Accuracy = {:.3f}".format(test_accuracy)) #metrics y_p = tf.argmax(logits, 1) y_pred = sess.run( y_p, feed_dict={x: X_test_p, y: y_test_p, apply_dropout: False}) # Compute Precision, recall and confusion matrix print ("test accuracy:", test_accuracy) y_true = y_test_p print ("Precision", metrics.precision_score(y_true, y_pred, average='macro')) print ("Recall", metrics.recall_score(y_true, y_pred, average='micro')) print ("f1_score", metrics.f1_score(y_true, y_pred, average='weighted')) print ("Confusion_matrix") cm = metrics.confusion_matrix(y_true, y_pred) print (cm) # Save the confusion matrix in a csv file: np.savetxt("cm.csv", cm, delimiter=",")
This program in 30 epoches train model with very good result – about 98% with trained dataset, about 97% of validation dataset, and 94% with new traffic sign files from pictures.
This program saves trained model to ckpt files which we will use to check with other images.
Program to check files with trained model
""" program to check images from ./Testing1 directory with Tensorflow graph prepared by traffic_ru_aug_v1.py program """ import os import random import pickle import cv2 import skimage.data import skimage.transform import skimage.exposure import matplotlib import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import numpy as np import tensorflow as tf import pandas as pd def load_data(data_dir): """ Loads a data set and returns two lists: images: a list of Numpy arrays, each representing an image. labels: a list of numbers that represent the images labels. """ # Get all subdirectories of data_dir. Each represents a label. directories = [d for d in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, d))] # Loop through the label directories and collect the data in # two lists, labels and images. labels = [] images = [] for d in directories: label_dir = os.path.join(data_dir, d) file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if (f.endswith(".jpg") or f.endswith(".jpeg"))] # For each label, load it's images and add them to the images list. # And add the label number (i.e. directory name) to the labels list. for f in file_names: #image = cv2.imread(f, cv2.COLOR_BGRA2RGB) image = skimage.data.imread(f) image = cv2.resize(image, (32, 32)) #images.append(skimage.data.imread(f)) images.append(image) labels.append(int(d)) print d images_np = np.array(images) return images_np, labels ########################################################################################################## # preprocessing images ########################################################################################################## def pre_processing_single_img (img): img_y = cv2.cvtColor(img, (cv2.COLOR_BGR2YUV))[:,:,0] img_y = (img_y / 255.).astype(np.float32) #img_y = exposure.adjust_log(img_y) img_y = (skimage.exposure.equalize_adapthist(img_y,) - 0.5) img_y = img_y.reshape(img_y.shape + (1,)) return img_y def pre_processing(X): print(X.shape) X_out = np.empty((X.shape[0],X.shape[1],X.shape[2],1)).astype(np.float32) print(X_out.shape) i=0 for idx, img in enumerate(X): X_out[idx] = pre_processing_single_img(img) i+=1 if i%1000 == 0: print i return X_out ######################################################################################################### #Evaluate how well the loss and accuracy of the model for a given dataset. ######################################################################################################### def evaluate(X_data, y_data): num_examples = len(X_data) total_accuracy = 0 sess = tf.get_default_session() for offset in range(0, num_examples, BATCH_SIZE): batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE] accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y, apply_dropout: False}) total_accuracy += (accuracy * len(batch_x)) return total_accuracy / num_examples ######################################################################################################### # Load the images and plot them here. modelFullPath = '/home/tensorflow/python_prog/traffic_ru_aug/graph_lenet_saves/traffic_ru_v1.ckpt.meta' #modell path modelFullPathData = '/home/tensorflow/python_prog/traffic_ru_aug/graph_lenet_saves/.' #modell path BATCH_SIZE = 128 training_file = "./pickle_data/train_ru.p" labels = {} path_to_jpg = './Testing1' X_new, y_new = load_data(path_to_jpg) print(len(X_new), ' images of shape: ' , X_new[0].shape) X_train_new_p = pre_processing(X_new) with open(training_file, mode='rb') as f1: train = pickle.load(f1) X_train, y_train = train['features'], train['labels'] sign_classes, class_indices, class_counts = np.unique(y_train, return_index = True, return_counts = True) # Read sign names sign_names = pd.read_csv("numbers_to_classes.csv").values[:, 1] """ # plot test pic for img in X_train_new_p: plt.figure(figsize=(1,1)) plt.imshow(img.squeeze(), cmap="gray") plt.show() """ with tf.Session() as sess: #First let's load meta graph and restore weights os trained model saver = tf.train.import_meta_graph(modelFullPath) saver.restore(sess,tf.train.latest_checkpoint(modelFullPathData)) x = sess.graph.get_tensor_by_name('images:0') y = sess.graph.get_tensor_by_name('labels:0') apply_dropout = sess.graph.get_tensor_by_name('apply_dropout:0') accuracy_operation = sess.graph.get_tensor_by_name('accuracy:0') test_accuracy = evaluate(X_train_new_p, y_new) print("Test Accuracy = {:.3f}".format(test_accuracy)) # Output Top 5 Softmax Probabilities For Each Image with tf.Session() as sess: #First let's load meta graph and restore weights os trained model saver = tf.train.import_meta_graph(modelFullPath) saver.restore(sess,tf.train.latest_checkpoint(modelFullPathData)) x = sess.graph.get_tensor_by_name('images:0') y = sess.graph.get_tensor_by_name('labels:0') logits = sess.graph.get_tensor_by_name('logits:0') apply_dropout = sess.graph.get_tensor_by_name('apply_dropout:0') feed_dict_new = feed_dict={x: X_train_new_p, y: y_new, apply_dropout: False} predictions = sess.run(logits,feed_dict = feed_dict_new) top5_pred = sess.run([logits, tf.nn.top_k(logits, 5)], feed_dict=feed_dict_new) for i in range(len(X_new)): plt.figure(figsize = (6,3)) gs = gridspec.GridSpec(1, 3) plt.subplot(gs[0]) plt.axis('off') plt.imshow(X_new[i].squeeze()) plt.subplot(gs[1]) plt.imshow(X_train[class_indices[top5_pred[1][1][i][0]]].squeeze()) plt.axis('off') plt.subplot(gs[2]) plt.barh(6-np.arange(5),top5_pred[1][0][i], align='center') for i_label in range(5): plt.text(top5_pred[1][0][i][i_label]+.02,6-i_label-.15, sign_names[top5_pred[1][1][i][i_label]]) plt.axis('off'); plt.show();
After this program execution we have recieved pictures like at the beginning of this article.
If we want to use model with android or embedded devices better prepare protobuf file with all trained information.
Program to prepare (.pb) file
""" program to prepare from standart saved model protobuf file (with augmented files) working with files prepared by traffic_ru_aug_v1.py model graph_lenet_saves """ from tensorflow.python.framework import graph_util import tensorflow as tf model_folder = '/home/tensorflow/python_prog/traffic_ru_aug/graph_lenet_saves' # We retrieve our checkpoint fullpath checkpoint = tf.train.get_checkpoint_state(model_folder) input_checkpoint = checkpoint.model_checkpoint_path # We precise the file fullname of our freezed graph absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1]) output_graph = absolute_model_folder + "/traffic_ru_v1.pb" # Before exporting our graph, we need to precise what is our output node # This is how TF decides what part of the Graph he has to keep and what part it can dump # NOTE: this variable is plural, because you can have multiple output nodes #output_node_names = "Placeholder,Placeholder_1,predicted" output_node_names = "images,labels,apply_dropout,prediction,accuracy,logits" # We clear devices to allow TensorFlow to control on which device it will load operations clear_devices = True # We import the meta graph and retrieve a Saver saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices) # We retrieve the protobuf graph definition graph = tf.get_default_graph() input_graph_def = graph.as_graph_def() # We start a session and restore the graph weights with tf.Session() as sess: saver.restore(sess, input_checkpoint) # We use a built-in TF helper to export variables to constants output_graph_def = graph_util.convert_variables_to_constants( sess, # The session is used to retrieve the weights input_graph_def, # The graph_def is used to retrieve the nodes output_node_names.split(",") # The output node names are used to select the usefull nodes ) # Finally we serialize and dump the output graph to the filesystem with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString()) print("%d ops in the final graph." % len(output_graph_def.node))
We have only one traffic_ru_v1.pb with all information.
Now we can check all images with this python program:
""" program to check images from ./Testing1 directory with Tensorflow graph prepared by traffic_ru_aug_v1.py program and saved like protobuf file (.pb) with traffic_ru_make_pb_aug.py """ import os import random import pickle import cv2 import skimage.data import skimage.transform import skimage.exposure import matplotlib import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import numpy as np import tensorflow as tf import pandas as pd def load_data(data_dir): """ Loads a data set and returns two lists: images: a list of Numpy arrays, each representing an image. labels: a list of numbers that represent the images labels. """ # Get all subdirectories of data_dir. Each represents a label. directories = [d for d in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, d))] # Loop through the label directories and collect the data in # two lists, labels and images. labels = [] images = [] for d in directories: label_dir = os.path.join(data_dir, d) file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if (f.endswith(".jpg") or f.endswith(".jpeg"))] # For each label, load it's images and add them to the images list. # And add the label number (i.e. directory name) to the labels list. for f in file_names: #image = cv2.imread(f, cv2.COLOR_BGRA2RGB) image = skimage.data.imread(f) image = cv2.resize(image, (32, 32)) #images.append(skimage.data.imread(f)) images.append(image) labels.append(int(d)) print d images_np = np.array(images) return images_np, labels ########################################################################################################## # preprocessing images ########################################################################################################## def pre_processing_single_img (img): img_y = cv2.cvtColor(img, (cv2.COLOR_BGR2YUV))[:,:,0] img_y = (img_y / 255.).astype(np.float32) #img_y = exposure.adjust_log(img_y) img_y = (skimage.exposure.equalize_adapthist(img_y,) - 0.5) img_y = img_y.reshape(img_y.shape + (1,)) return img_y def pre_processing(X): print(X.shape) X_out = np.empty((X.shape[0],X.shape[1],X.shape[2],1)).astype(np.float32) print(X_out.shape) i=0 for idx, img in enumerate(X): X_out[idx] = pre_processing_single_img(img) i+=1 if i%1000 == 0: print i return X_out ######################################################################################################### #Evaluate how well the loss and accuracy of the model for a given dataset. ######################################################################################################### def evaluate(X_data, y_data): num_examples = len(X_data) total_accuracy = 0 sess = tf.get_default_session() for offset in range(0, num_examples, BATCH_SIZE): batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE] accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y, apply_dropout: False}) total_accuracy += (accuracy * len(batch_x)) return total_accuracy / num_examples ######################################################################################################### # Load the images and plot them here. modelFullPath = '/home/tensorflow/python_prog/traffic_ru_aug/graph_lenet_saves/traffic_ru_v1.pb' #modell path BATCH_SIZE = 128 training_file = "./pickle_data/train_ru.p" labels = {} path_to_jpg = './Testing1' X_new, y_new = load_data(path_to_jpg) print(len(X_new), ' images of shape: ' , X_new[0].shape) X_train_new_p = pre_processing(X_new) with open(training_file, mode='rb') as f1: train = pickle.load(f1) X_train, y_train = train['features'], train['labels'] sign_classes, class_indices, class_counts = np.unique(y_train, return_index = True, return_counts = True) # Read sign names sign_names = pd.read_csv("numbers_to_classes.csv").values[:, 1] """ # plot test pic for img in X_train_new_p: plt.figure(figsize=(1,1)) plt.imshow(img.squeeze(), cmap="gray") plt.show() """ # Creates graph from saved *.pb. with tf.gfile.FastGFile(modelFullPath, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session() as sess: x = sess.graph.get_tensor_by_name('images:0') y = sess.graph.get_tensor_by_name('labels:0') apply_dropout = sess.graph.get_tensor_by_name('apply_dropout:0') accuracy_operation = sess.graph.get_tensor_by_name('accuracy:0') test_accuracy = evaluate(X_train_new_p, y_new) print("Test Accuracy = {:.3f}".format(test_accuracy)) # Output Top 5 Softmax Probabilities For Each Image with tf.Session() as sess: x = sess.graph.get_tensor_by_name('images:0') y = sess.graph.get_tensor_by_name('labels:0') logits = sess.graph.get_tensor_by_name('logits:0') apply_dropout = sess.graph.get_tensor_by_name('apply_dropout:0') feed_dict_new = feed_dict={x: X_train_new_p, y: y_new, apply_dropout: False} predictions = sess.run(logits,feed_dict = feed_dict_new) top5_pred = sess.run([logits, tf.nn.top_k(logits, 5)], feed_dict=feed_dict_new) for i in range(len(X_new)): plt.figure(figsize = (6,3)) gs = gridspec.GridSpec(1, 3) plt.subplot(gs[0]) plt.axis('off') plt.imshow(X_new[i].squeeze()) plt.subplot(gs[1]) plt.imshow(X_train[class_indices[top5_pred[1][1][i][0]]].squeeze()) plt.axis('off') plt.subplot(gs[2]) plt.barh(6-np.arange(5),top5_pred[1][0][i], align='center') for i_label in range(5): plt.text(top5_pred[1][0][i][i_label]+.02,6-i_label-.15, sign_names[top5_pred[1][1][i][i_label]]) plt.axis('off'); plt.show();