评估单个数据点上的Tensorflow

时间:2017-02-02 04:17:16

标签: python tensorflow deep-learning

下面的代码似乎工作正常:它加载预先训练的NN的权重,然后评估其训练和测试准确性。由此产生的精度与节省时间相同。

from __future__ import print_function
from keras.datasets import cifar10
from keras.utils import np_utils
import tensorflow as tf
import numpy as np
from random import shuffle
#import custom_activations_new as ca
from tensorflow.python.framework import ops

# input image dimensions
img_rows, img_cols = 32, 32
# the CIFAR10 images are RGB
img_channels = 3
batch_size = 100
nb_classes = 10
nb_epoch = 20

#seed = 7
#np.random.seed(seed)

def avg(l):
    return sum(l)/len(l)

def chunker(seq, size):
    return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))

num_train_examples=50000
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print('X_train shape:', X_train.shape[1:])
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
print('Y_train shape:', Y_train.shape)
Y_test = np_utils.to_categorical(y_test, nb_classes)

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

def maxpool2d(x):
    #                        size of window         movement of window
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

# Define network
# TF graph
img = tf.placeholder(tf.float32, shape=(None,img_rows, img_cols,img_channels))
y = tf.placeholder(tf.float32, [None, nb_classes])
#labels = tf.placeholder(tf.float32, shape=(None, nb_classes))

# Read weights back
f=open('W0.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((3,3,3,32))
W0=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((32))
b0=tf.Variable(weights)
conv0 = conv2d(img, W0) + b0
conv0 = tf.nn.relu(conv0)

f=open('W1.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((3,3,32,32))
W1=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((32))
b1=tf.Variable(weights)
conv1 = conv2d(conv0, W1) + b1
conv1 = tf.nn.relu(conv1)
conv1 = maxpool2d(conv1)
#conv1 = tf.nn.dropout(conv1,0.5)

f=open('W2.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((3,3,32,64))
W2=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((64))
b2=tf.Variable(weights)
conv2 = conv2d(conv1, W2) + b2
conv2 = tf.nn.relu(conv2)

f=open('W3.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((3,3,64,64))
W3=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((64))
b3=tf.Variable(weights)
conv3 = conv2d(conv2, W3) + b3
conv3 = tf.nn.relu(conv3)
conv3 = maxpool2d(conv3)
#conv3 = tf.nn.dropout(conv3,0.5)

f=open('W4.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((4096,512))
Wfc=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((512))
bfc=tf.Variable(weights)
fc = tf.reshape(conv3,[-1, 8*8*64])
fc = tf.matmul(fc, Wfc) + bfc
fc = tf.nn.relu(fc)
#fc = tf.nn.dropout(fc,0.5)

f=open('W5.txt','r')
N=int(f.readline())
B=int(f.readline())
weights = np.zeros(N,dtype=np.float32)
for i in range(N):
    weights[i]=float(f.readline())
weights=weights.reshape((512,10))
Wout=tf.Variable(weights)
weights = np.zeros(B,dtype=np.float32)
for i in range(B):
    weights[i]=float(f.readline())
f.closed
weights=weights.reshape((10))
bout=tf.Variable(weights)
output = tf.matmul(fc, Wout) + bout

# Delete from here

prediction= tf.equal(tf.argmax(output, 1), tf.argmax(y, 1))
accuracy  = tf.reduce_mean(tf.cast(prediction, tf.float32))

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())

    train_indices   = list(xrange(0,len(X_train)))
    test_indices    = list(xrange(0,len(X_test)))
    train_epoch_acc  = []
    test_epoch_acc   = []

    # Test 
    for ind in chunker(train_indices, batch_size):
        train_epoch_acc.append(accuracy.eval({img: X_train[ind], y: Y_train[ind]}))
    for ind in chunker(test_indices, batch_size):
        test_epoch_acc.append(accuracy.eval({img: X_test[ind], y: Y_test[ind]}))

    print("Result:", 
                "Training accuracy=", "{:.4f}".format(avg(train_epoch_acc)), 
                "Test accuracy=", "{:.4f}".format(avg(test_epoch_acc)))

现在,我想计算单个图像的NN输出。 所以,我从评论中删除了“从这里删除”的所有内容,并尝试首先添加:

with tf.Session() as sess:
    print(sess.run([output],feed_dict={img:X_train[0]}))

由于形状错误导致失败,我明白了。但接下来:

with tf.Session() as sess:
    print(sess.run([output],feed_dict={img:[X_train[0]]}))

而且:

tmp=np.empty(shape=(1,32,32,3),dtype='float32')
tmp[0]=X_train[0]

with tf.Session() as sess:
    print(sess.run([output],feed_dict={img:tmp}))

所有失败都是Tensorflow典型的一系列用户不友好,多线,难以理解的错误。我在单个点上看到了一些关于TF评估的其他帖子,比如here,但我也无法让它工作。任何帮助非常感谢。感谢。

1 个答案:

答案 0 :(得分:0)

如果添加初始化,则最后一个有效:

sess.run(tf.initialize_all_variables())

所以,那已经解决了。