2017-02-02 5 views
1

以下のコードは、あらかじめ訓練されたNNの重みを読み込み、トレーニングとテストの精度を評価します。結果として得られる精度は、保存時と同じです。今単一のデータポイントでTensorflowを評価する

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の典型的なユーザーがよそよそしい、マルチ並んで、不可解なエラーのカスケードで失敗しました。私は、hereのような一点でのTF評価についてのいくつかの他の投稿を見ましたが、それをうまく動作させることができませんでした。どんな助けでも大歓迎です。ありがとう。

sess.run(tf.initialize_all_variables()) 

だから、それが解決です:1が初期化を追加する場合

+0

最後の試みでは、あなたが正しい軌道に乗っているように見えました(余分なサイズ-1のバッチ次元を持つように入力を再形成する必要があります--- numpy.reshapeはあなたが望むツールでしょう)。最後の試行はどのような誤りでしたか? –

答えて

0

最後のものは動作します。

関連する問題