Тестирование изображения на модели Tensorflow Mnist с использованием контрольных точек

Я новичок в TensorFlow. У меня есть образец поезда mnist, и я хочу протестировать изображение, создав контрольные точки. Я сослался на документацию Tensorflow, создал контрольные точки и попытался протестировать образец изображения, обратившись к слою softmax. Но с учетом изображения number-9softmax дает мне недопустимый массив с горячим кодированием, например 'array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)', когда я пытался для доступа к softmax с помощью

softmax = graph.get_tensor_by_name('SOFTMAX:0').

Я пробовал тестировать с разными изображениями, это не дало должного результата ни для одного из них.

1. Я предположил, что softmax даст мне набор вероятностей. Я прав?

2.Правильно ли я сохраняю модель?

3. Я получаю доступ к правильному слою для тестирования ввода?

4. Есть ли что-нибудь еще, что нужно добавить в мой тестовый/тренировочный код?

Извините, что все сюда выкладываю.

Это мой код поезда:

from __future__ import division, print_function, unicode_literals
import tensorflow as tf
from time import time
import numpy as np
import os
import scipy.ndimage as ndimage
from scipy import misc

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

logs_train_dir = '/home/test/Logs'

def weight_variable(shape,name):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial,name=name+'_weight')

def bias_variable(shape,name):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial,name=name+'_bias')

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x,name):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME',name=name+'_max_pool')

# correct labels

y_ = tf.placeholder(tf.float32, [None, 10])

# reshape the input data to image dimensions

x = tf.placeholder(tf.float32, [None, 784],name='X')#Input Tensor
x_image = tf.reshape(x, [-1, 28, 28, 1],name='X_Image')

# build the network

W_conv1 = weight_variable([5, 5, 1, 32],'W_conv1')
b_conv1 = bias_variable([32],'b_conv1')
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1,name='h_conv1')
h_pool1 = max_pool_2x2(h_conv1,'h_pool1')
W_conv2 = weight_variable([5, 5, 32, 64],'W_conv2')
b_conv2 = bias_variable([64],'b_conv2')
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2,name='h_conv2')
h_pool2 = max_pool_2x2(h_conv2,'W_conv2')
W_fc1 = weight_variable([7 * 7 * 64, 1024],name='wc1')
b_fc1 = bias_variable([1024],name='b_fc1')
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32,name='KEEP_PROB')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10],name='w_fc2')
b_fc2 = bias_variable([10],name='b_fc2')
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2,name='SOFTMAX')#Softmax Tensor

# define the loss function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]),name='CROSS_ENTROPY')
loss_summary = tf.summary.scalar('loss_sc',cross_entropy)

# define training step and accuracy

train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1),name='CORRECT_PRED')
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32),name='ACCURACY')
accuracy_summary = tf.summary.scalar('accuracy_sc', accuracy)

# create a saver
saver = tf.train.Saver()

# initialize the graph
init = tf.global_variables_initializer()
summary_op = tf.summary.merge_all()

sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
sess.run(init)

# train

print("Startin Burn-In...")
for i in range(500):
    input_images, correct_predictions = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = sess.run(accuracy, feed_dict={x: input_images, y_: correct_predictions, keep_prob: 1.0})
        print("step %d, training accuracy_a %g" % (i, train_accuracy))

    sess.run(train_step, feed_dict={x: input_images, y_: correct_predictions, keep_prob: 0.5})

print("Starting the training...")
start_time = time()
for i in range(20000):
    input_images, correct_predictions = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = sess.run(accuracy, feed_dict={x: input_images, y_: correct_predictions, keep_prob: 1.0})
        print("step %d, training accuracy_b %g" % (i, train_accuracy))
    sess.run(train_step, feed_dict={x: input_images, y_: correct_predictions, keep_prob: 0.5})

    summary_str = sess.run(summary_op,feed_dict={x: input_images, y_: correct_predictions, keep_prob: 0.5})
    train_writer.add_summary(summary_str, i)

    print('SAVING CHECKPOINTS......i is ',i)

    if i % 1000 == 0 or (i+1) == 20000:
        checkpoint_path = os.path.join(logs_train_dir,'cnn_new_model.ckpt')
        print('checkpoint_path is ',checkpoint_path)
        saver.save(sess,checkpoint_path,global_step=i)

print("The training took %.4f seconds." % (time() - start_time))
# validate
print("test accuracy %g" % sess.run(accuracy, feed_dict={
x: mnist.test.images,
y_: mnist.test.labels,
keep_prob: 1.0}))

Точность составила 0,97.

Это мой тестовый код:

import numpy as np
import tensorflow as tf
import scipy.ndimage as ndimage
from scipy import misc
import cv2 as cv

def get_test_image():    
    image = cv.imread('/home/test/Downloads/9.png', 0)
    resized = cv.resize(image, (28,28), interpolation = cv.INTER_AREA)
    image = np.array(resized)
    flat = np.ndarray.flatten(image)    
    reshaped_image = np.reshape(flat,(1, 784))
return reshaped_image


def evaluate_one_image():

    image_array = get_test_image()
    image_array = image_array.astype(np.float32)
    logs_train_dir ='/home/test/Logs'    
    model_path = logs_train_dir+"/cnn_new_model.ckpt-19999"
    detection_graph = tf.Graph()

    with tf.Session(graph=detection_graph) as sess:
        # Load the graph with the trained states
        loader = tf.train.import_meta_graph(model_path+'.meta')
        loader.restore(sess, model_path)

        # Get the tensors by their variable name

        image_tensor = detection_graph.get_tensor_by_name('X:0')
        softmax = detection_graph.get_tensor_by_name('SOFTMAX:0')
        keep_prob = detection_graph.get_tensor_by_name('KEEP_PROB:0')       

        # Make prediction

        softmax = sess.run(softmax, feed_dict={image_tensor: image_array,keep_prob:0.75}) 

        print('softmax is ', cost_val,'\n\n')
        print('softmax maximum val is ', np.argmax(cost_val))

evaluate_one_image()

Поэтому, когда я тестировал изображение под номером 9, он дал мне следующий результат:

softmax равен [[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]]

максимальное значение softmax равно 1

Я понятия не имею, где я ошибаюсь. Любая помощь будет действительно полезной и очень признательна.


person george    schedule 20.08.2017    source источник


Ответы (1)


  1. Dropout не используется во время оценки/прогнозирования. Итак, вам нужно установить keep_prob=1

  2. Проверьте значения пикселей входного изображения image_array, значения пикселей должны быть в диапазоне [0, 1], иначе вам нужно нормализовать значения пикселей, вычитая среднее значение изображения и разделив его на стандартное изображение.

Для функции загрузки изображения вы можете добавить следующие строки для нормализации

def get_test_image():  
    ...  
    image = np.array(resized)
    mean = image.mean()
    std = image.std()
    image = np.subtract(image, mean)
    image = np.divide(image, std)
    image = np.clip(image, 0, 1.000001)
    ...
person Ishant Mrinal    schedule 20.08.2017
comment
привет Ишант. Спасибо за ответ. Я добавил вышеуказанные строки и продолжил эти две строки: flat = np.ndarray.flatten(image) reshape_image = np.reshape(flat,(1, 784)).Теперь 1. image_array имеет значения btw 0-1.2.softmax дает вероятности. Но прогноз неверен. - person george; 20.08.2017
comment
может быть, ваша модель не сходится, чтобы хорошо обобщать. - person Ishant Mrinal; 20.08.2017
comment
так что я должен добавить, чтобы обобщить мою модель. Должен ли я увеличить итерации? Но было показано, что точность увеличивается, а потери уменьшаются в тензорной доске с 2000 итерациями. - person george; 20.08.2017
comment
вы можете попробовать tf.nn.softmax_cross_entropy_with_logits в качестве функции потерь, здесь не используйте вывод softmax в качестве логитов. logitts=f.matmul(h_fc1_drop, W_fc2) + b_fc2) - person Ishant Mrinal; 20.08.2017
comment
вы имеете в виду pred = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 loss = tf.nn.softmax_cross_entropy_with_logits(pred) .. если да, должен ли я получить доступ к этому pred для тестирования?? - person george; 20.08.2017
comment
более подробная информация tensorflow.org/versions/r1.2/ api_docs/python/tf/nn/ - person Ishant Mrinal; 20.08.2017