def test_model(): test_features, test_labels = pickle.load(open('preprocess_test.p', mode='rb')) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph, config=config) as sess: loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') test_batch_acc_total = 0 test_batch_count = 0 for test_feature_batch, test_label_batch in helper.batch_features_labels(test_features, test_labels, batch_size): test_batch_acc_total += sess.run( loaded_acc, feed_dict = { loaded_x: test_feature_batch, loaded_y: test_label_batch, loaded_keep_prob: 1.0 }) test_batch_count += 1 print('Testing Accuracy: {}\n'.format(test_batch_acc_total / test_batch_count)) random_test_features, random_test_labels = tuple(zip(*random.sample(list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run( tf.nn.top_k(tf.nn.softmax(loaded_logits), top_n_predictions), feed_dict = { loaded_x: random_test_features, loaded_y: random_test_labels, loaded_keep_prob: 1.0 }) helper.display_image_predictions(random_test_features, random_test_labels, random_test_predictions)
def print_stats(session, feature_batch, label_batch, batch_size, cost, accuracy, x, y, keep_prob, train_phase): """ Print information about loss and validation accuracy : session: Current TensorFlow session : feature_batch: Batch of Numpy image data : label_batch: Batch of Numpy label data : cost: TensorFlow cost function : accuracy: TensorFlow accuracy function """ # Get accuracy in batches for memory limitations. If batch_size is not big, like when checking # the training stats, the for loop below will run just once. But when the validation stats are # required the batch_size is big and must be checked in parts. test_batch_acc_total = 0 test_batch_cost_total = 0 test_batch_count = 0 for feature_batch, label_batch in helper.batch_features_labels(feature_batch, label_batch, batch_size): feed_dict_fwd = {x: feature_batch, y: label_batch, keep_prob: 1.0, train_phase: False} test_batch_cost_total += session.run( cost,feed_dict = feed_dict_fwd) test_batch_acc_total += session.run( accuracy,feed_dict = feed_dict_fwd) test_batch_count += 1 acc = test_batch_acc_total / test_batch_count cost = test_batch_cost_total / test_batch_count return 'Cost: {:05.4}, Acc: {:.1%}'.format(cost, acc)
def test_model(plotCanvas): """ Test the saved model against the test dataset """ try: if batch_size: pass except NameError: batch_size = 64 save_model_path = './image_classification' n_samples = 4 top_n_predictions = 3 test_features, test_labels = pickle.load( open('preprocess_training.p', mode='rb')) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph) as sess: # Load model loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for train_feature_batch, train_label_batch in helper.batch_features_labels( test_features, test_labels, batch_size): test_batch_acc_total += sess.run(loaded_acc, feed_dict={ loaded_x: train_feature_batch, loaded_y: train_label_batch, loaded_keep_prob: 1.0 }) test_batch_count += 1 print('Testing Accuracy: {}\n'.format(test_batch_acc_total / test_batch_count)) # Print Random Samples random_test_features, random_test_labels = tuple( zip(*random.sample(list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run(tf.nn.top_k( tf.nn.softmax(loaded_logits), top_n_predictions), feed_dict={ loaded_x: random_test_features, loaded_y: random_test_labels, loaded_keep_prob: 1.0 }) helper.display_image_predictions(random_test_features, random_test_labels, random_test_predictions, plotCanvas)
def test_model(): """ Test the saved model against the test dataset """ test_features, test_labels = pickle.load(open('preprocess_training.p', mode='rb')) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph) as sess: # Load model loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for train_feature_batch, train_label_batch in helper.batch_features_labels(test_features, test_labels, batch_size): test_batch_acc_total += sess.run( loaded_acc, feed_dict={loaded_x: train_feature_batch, loaded_y: train_label_batch, loaded_keep_prob: 1.0}) test_batch_count += 1 print('Testing Accuracy: {}\n'.format(test_batch_acc_total/test_batch_count)) # Print Random Samples """
def test_model(self): """ Test the trained model on test data. Report its accuracy. """ save_model_path = './savedModel/cnn-model' n_samples = 4 top_n_predictions = 3 test_features, test_labels = pickle.load( open('./ProcessedData/preprocess_test.p', mode='rb')) tf.reset_default_graph() with tf.Session(graph=tf.get_default_graph()) as sess: try: graph = self.__load_graph(sess, save_model_path) # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for test_feature_batch, test_label_batch in helper.batch_features_labels( test_features, test_labels, NeuralNetwork.batch_size): test_batch_acc_total += sess.run(graph['accuracy'], feed_dict={ graph['x']: test_feature_batch, graph['y']: test_label_batch, graph['keep_prob']: 1.0 }) test_batch_count += 1 logger.info('Testing Accuracy: {}\n'.format( test_batch_acc_total / test_batch_count)) # Print Random Samples random_test_features, random_test_labels = tuple( zip(*random.sample(list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run( tf.nn.top_k(tf.nn.softmax(graph['logits']), top_n_predictions), feed_dict={ graph['x']: random_test_features, graph['y']: random_test_labels, graph['keep_prob']: 1.0 }) helper.display_image_predictions(random_test_features, random_test_labels, random_test_predictions) except Exception as e: logger.error( "Something is missing from the previous saved graph, remove it and regenerate graph" ) exit()
def test_model(learning_rate, use_two_conv, use_two_fully, log_filename): """ Test the saved model against the test dataset """ with gzip.open('preprocess_test.p.gz', mode='rb') as file: test_features, test_labels = pickle.load(file) build_network(learning_rate, use_two_conv, use_two_fully) save_model_path = os.path.join(SAVE_MODEL, 'image_classification_' + log_filename) #save_model_path = os.path.join(SAVE_MODEL, 'image_classification') loaded_graph = tf.Graph() config = tf.ConfigProto(device_count = {'GPU': 0}) with tf.Session(config=config, graph=loaded_graph) as sess: # Load model loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy/accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for test_feature_batch, test_label_batch in helper.batch_features_labels(test_features, test_labels, batch_size): test_batch_acc_total += sess.run( loaded_acc, feed_dict={loaded_x: test_feature_batch, loaded_y: test_label_batch, loaded_keep_prob: 1.0}) test_batch_count += 1 print('Testing Accuracy: {}\n'.format(test_batch_acc_total/test_batch_count)) # Print Random Samples random_test_features, random_test_labels = tuple(zip(*random.sample(list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run( tf.nn.top_k(tf.nn.softmax(loaded_logits), top_n_predictions), feed_dict={loaded_x: random_test_features, loaded_y: random_test_labels, loaded_keep_prob: 1.0}) helper.display_image_predictions(random_test_features, random_test_labels, random_test_predictions)
def test_model(): """ Test the saved model against the test dataset """ test_features, test_labels = pickle.load(open('preprocess_training.p', mode='rb')) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph) as sess: # Load model loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for train_feature_batch, train_label_batch in helper.batch_features_labels(test_features, test_labels, batch_size): test_batch_acc_total += sess.run( loaded_acc, feed_dict={loaded_x: train_feature_batch, loaded_y: train_label_batch, loaded_keep_prob: 1.0}) test_batch_count += 1 print('Testing Accuracy: {}\n'.format(test_batch_acc_total/test_batch_count)) # Print Random Samples random_test_features, random_test_labels = tuple(zip(*random.sample(list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run( tf.nn.top_k(tf.nn.softmax(loaded_logits), top_n_predictions), feed_dict={loaded_x: random_test_features, loaded_y: random_test_labels, loaded_keep_prob: 1.0}) helper.display_image_predictions(random_test_features, random_test_labels, random_test_predictions)
def test(batch_size): print('\nTesting...') save_model_path = './image_classification' n_samples = 4 top_n_predictions = 3 test_features, test_labels = pickle.load( open('preprocess_test.p', mode='rb')) loaded_graph = tf.Graph() with tf.Session(graph=loaded_graph) as sess: # Load model loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for test_feature_batch, test_label_batch in ( helper.batch_features_labels( test_features, test_labels, batch_size)): test_batch_acc_total += sess.run( loaded_acc, feed_dict={loaded_x: test_feature_batch, loaded_y: test_label_batch, loaded_keep_prob: 1.0}) test_batch_count += 1 print('Testing Accuracy: {}\n'.format( test_batch_acc_total/test_batch_count))
loader = tf.train.import_meta_graph(save_model_path + '.meta') loader.restore(sess, save_model_path) # Get Tensors from loaded model loaded_x = loaded_graph.get_tensor_by_name('x:0') loaded_y = loaded_graph.get_tensor_by_name('y:0') loaded_keep_prob = loaded_graph.get_tensor_by_name('keep_prob:0') loaded_logits = loaded_graph.get_tensor_by_name('logits:0') loaded_acc = loaded_graph.get_tensor_by_name('accuracy:0') # Get accuracy in batches for memory limitations test_batch_acc_total = 0 test_batch_count = 0 for train_feature_batch, train_label_batch in \ helper.batch_features_labels(test_features, test_labels, batch_size): test_batch_acc_total += sess.run(loaded_acc, feed_dict={loaded_x: train_feature_batch, \ loaded_y: train_label_batch, loaded_keep_prob: 1.0}) test_batch_count += 1 print('Testing Accuracy: {}\n'.\ format(test_batch_acc_total/test_batch_count)) # Print Random Samples random_test_features, random_test_labels = tuple(zip(*random.sample\ (list(zip(test_features, test_labels)), n_samples))) random_test_predictions = sess.run( tf.nn.top_k(tf.nn.softmax(loaded_logits), top_n_predictions), feed_dict={loaded_x: random_test_features, loaded_y: \