神经网络对不同的输入显示相同的结果

时间:2017-12-13 13:05:14

标签: python tensorflow neural-network

我是tensorflow的新手。我正在使用cnn训练我的tensorflow模型进行数字识别。虽然,它在训练期间显示99%的准确性。但是在我的图像集上进行测试时,它会继续显示相同的结果,例所有图片有时为8,有时为所有图片2,有时为所有图片3

请帮忙!

我附上参考代码

档案trainer.py

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

def layer(input,weight_shape,bias_shape):
    w_stddev = (2.0/weight_shape[0])**0.5
    w_init = tf.random_normal_initializer(stddev = w_stddev)
    b_init = tf.constant_initializer(0)
    W = tf.get_variable('W',weight_shape,initializer = w_init)
    b = tf.get_variable('B',bias_shape,initializer = b_init)

    output = tf.matmul(input,W)+b
    return tf.nn.relu(output)

def conv2d(input,weight_shape,bias_shape):
    inp = weight_shape[0]*weight_shape[1]*weight_shape[2]
    w_init = tf.random_normal_initializer(stddev = (2.0/inp)**0.5)

    W = tf.get_variable('W',weight_shape,initializer = w_init)
    b_init = tf.constant_initializer(0)

    b = tf.get_variable('b',bias_shape,initializer = b_init)

    conv_out = tf.nn.conv2d(input,W,strides = [1,1,1,1], padding = 'SAME')

    return tf.nn.relu(tf.nn.bias_add(conv_out,b))

def max_pool(input,k=2):
    return tf.nn.max_pool(input,ksize = [1,k,k,1], strides = [1,k,k,1],padding = 'SAME')

def inference(x,keep_prob):
    x = tf.reshape(x,shape = [-1,28,28,1])
    keep_prob = keep_prob[0]
    with tf.variable_scope("conv_1"):
        conv_1 = conv2d(x,[5,5,1,32],[32])
        pool_1 = max_pool(conv_1)

    with tf.variable_scope("conv_2"):
        conv_2 = conv2d(pool_1,[5,5,32,64],[64])
        pool_2 = max_pool(conv_2)

    with tf.variable_scope("fc"):
        pool_2_flat = tf.reshape(pool_2,[-1,7*7*64])
        fc_1 = layer(pool_2_flat,[7*7*64,1024],[1024])

        fc_1_drop = tf.nn.dropout(fc_1,keep_prob)

    with tf.variable_scope("output"):
        output = layer(fc_1_drop,[1024,10],[10])

    return output

def evaluate(output,y):
    correct_prediction = tf.equal(tf.argmax(output,1),tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    return accuracy

def training(cost,global_step):
    tf.summary.scalar("cost",cost)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = optimizer.minimize(cost,global_step = global_step)
    return train_op

def loss(output,y):
    xentropy = tf.nn.softmax_cross_entropy_with_logits(labels = y,logits = output)
    loss = tf.reduce_mean(xentropy)
    return loss

learning_rate = 0.01
display_step = 1
batch_size = 100
training_epoch = 2

mnist = input_data.read_data_sets('MNIST_data/',one_hot = True)

with tf.Graph().as_default():
    x = tf.placeholder('float',[None,784],name="x")
    y = tf.placeholder('float',[None,10],name="y")
    keep_prob = tf.placeholder('float',[1],name="keep_prob")
    global_step = tf.Variable(0,name = 'global_step',trainable = False)
    output = inference(x,keep_prob)
    #print(output.name)
    cost_op = loss(output,y)
    eval_op = evaluate(output,y)
    print(eval_op.name)
    train_op = training(cost_op,global_step)

    summary_op = tf.summary.merge_all()
    saver = tf.train.Saver()
    sess = tf.Session()
    writer = tf.summary.FileWriter('logistics_logs/',graph_def = sess.graph_def)
    init_op = tf.global_variables_initializer()

    sess.run(init_op)
    for epoch in range(training_epoch):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        for i in range(total_batch):
            mbatch_x,mbatch_y = mnist.train.next_batch(batch_size)
            feed_dict = {
                x:      mbatch_x,
                y:      mbatch_y,
                keep_prob:  np.asarray([0.5])
            }
            sess.run(train_op,feed_dict = feed_dict)
            mbatch_cost = sess.run(cost_op,feed_dict = feed_dict)
            avg_cost += mbatch_cost/total_batch
        if epoch % display_step == 0:
            val_feed_dict = {
                x:      mnist.validation.images,
                y:      mnist.validation.labels,
                keep_prob:  np.asarray([1])
            }
            accuracy = sess.run(eval_op,feed_dict = val_feed_dict)
            print(epoch+1,'Validation Accuracy : ',accuracy*100,'%')
            summary_str = sess.run(summary_op,feed_dict = val_feed_dict)
            writer.add_summary(summary_str,sess.run(global_step))
            saver.save(sess,'logistics_logs/model_checkpoint/',global_step)
    print('Optimization finished')

    test_feed_dict = {
        x:      mnist.test.images,
        y:      mnist.test.labels,
        keep_prob:  np.asarray([1])
    }
    accuracy = sess.run(eval_op,feed_dict = test_feed_dict)
    print('Test Accuracy : ',accuracy*100,'%')

档案model.py

import tensorflow as tf
import numpy as np
import os
import cv2
from PIL import Image
from scipy import misc

#img = Image.open('character.bmp')
#a = np.asarray(img).flatten()
#test_data = np.subtract(a,0.,dtype=np.float32)
#test_data = np.reshape(test_data,(784))
#test_data = 255*test_data
#print(test_data)
#print(test_data.shape)

image = cv2.imread('character.jpg')
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image = cv2.resize(image,(28,28),cv2.INTER_LINEAR)
images= np.array(image,dtype=np.uint8)
images=images.astype('float32')
images=np.multiply(images,1.0/255.0)
images = np.asarray(images).flatten()
test_data=[images]

#print(test_data)


restore_path = os.path.join(os.path.abspath('./'),"logistics_logs\\model_checkpoint\\")

meta_file_location = os.path.join(restore_path,"-550.meta")

sess = tf.Session()

saver = tf.train.import_meta_graph(meta_file_location)

saver.restore(sess,tf.train.latest_checkpoint(restore_path))

graph = tf.get_default_graph()


x = graph.get_tensor_by_name("x:0")


keep_prob = graph.get_tensor_by_name("keep_prob:0")

feed_dict = {x:test_data,keep_prob:np.asarray([1])}

op = graph.get_tensor_by_name("output/Relu:0")

digit = sess.run(tf.argmax(op,1),feed_dict = feed_dict)

print(int(digit))

1 个答案:

答案 0 :(得分:0)

虽然我不能轻易地将您的代码从一个文件跟踪到另一个文件,但我很确定您的问题就在这里:

init_op = tf.global_variables_initializer()

sess.run(init_op)

我怀疑在使用以下代码加载网络后正在运行代码:

saver = tf.train.import_meta_graph(meta_file_location)

saver.restore(sess,tf.train.latest_checkpoint(restore_path))

如果是这样的话,你正在做的就是用随机初始化覆盖你的模型。从磁盘加载模型时,不应运行init操作。

无论输入如何,单个值的输出是随机初始化网络的常见结果,这是我怀疑的原因。