2017-11-06 22 views
0


私はTensorFlowInferenceInterfaceでフィード関数とフェッチ関数を使用したいと思いますが、フィードを理解してargsをフェッチできません。
TensorFlowInferencefaceでフィード関数とフェッチ関数を使用するにはどうすればよいですか?

public void feed(String inputName, float[] src, long... dims) 
public void fetch(String outputName, float[] dst) 

ここTensorflowInferenceInterfaceです。↓ https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/contrib/android/java/org/tensorflow/contrib/android/TensorFlowInferenceInterface.java

、私は、Android-Studioを使用してMNISTを使用してプログラムをインポートしたいです。
プロトコルバッファを作るプログラムです。

import tensorflow as tf 
import shutil 
import os.path 

if os.path.exists("./tmp/beginner-export"): 
    shutil.rmtree("./tmp/beginner-export") 

# Import data 
from tensorflow.examples.tutorials.mnist import input_data 

mnist = input_data.read_data_sets("./tmp/data/", one_hot=True) 

g = tf.Graph() 

with g.as_default(): 
    # Create the model 
    x = tf.placeholder("float", [None, 784]) 
    W = tf.Variable(tf.zeros([784, 10]), name="vaiable_W") 
    b = tf.Variable(tf.zeros([10]), name="variable_b") 
    y = tf.nn.softmax(tf.matmul(x, W) + b) 

    # Define loss and optimizer 
    y_ = tf.placeholder("float", [None, 10]) 
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) 
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 

    sess = tf.Session() 

    # Train 
    init = tf.initialize_all_variables() 
    sess.run(init) 

    for i in range(1000): 
     batch_xs, batch_ys = mnist.train.next_batch(100) 
     train_step.run({x: batch_xs, y_: batch_ys}, sess) 

    # Test trained model 
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 

    print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}, sess)) 

# Store variable 
_W = W.eval(sess) 
_b = b.eval(sess) 

sess.close() 

# Create new graph for exporting 
g_2 = tf.Graph() 
with g_2.as_default(): 
    # Reconstruct graph 
    x_2 = tf.placeholder("float", [None, 784], name="input") 
    W_2 = tf.constant(_W, name="constant_W") 
    b_2 = tf.constant(_b, name="constant_b") 
    y_2 = tf.nn.softmax(tf.matmul(x_2, W_2) + b_2, name="output") 

    sess_2 = tf.Session() 

    init_2 = tf.initialize_all_variables(); 
    sess_2.run(init_2) 

    graph_def = g_2.as_graph_def() 

    tf.train.write_graph(graph_def, './tmp/beginner-export', 
         'beginner-graph.pb', as_text=False) 

    # Test trained model 
    y__2 = tf.placeholder("float", [None, 10]) 
    correct_prediction_2 = tf.equal(tf.argmax(y_2, 1), tf.argmax(y__2, 1)) 
    accuracy_2 = tf.reduce_mean(tf.cast(correct_prediction_2, "float")) 
    print(accuracy_2.eval({x_2: mnist.test.images, y__2: mnist.test.labels}, sess_2)) 

入力のプレースホルダ名が「入力」です。
出力のプレースホルダ名は「出力」です。

フィードの使用方法を教えてください。

答えて

0

私はコメントとともにサンプルコードを与えました。あなたが理解することを願って。

private static final String INPUT_NODE = "input:0"; // input tensor name 
    private static final String OUTPUT_NODE = "output:0"; // output tensor name 
    private static final String[] OUTPUT_NODES = {"output:0"}; 
    private static final int OUTPUT_SIZE = 10; // number of classes 
    private static final int INPUT_SIZE = 784; // size of the input 
    INPUT_IMAGE //MNIST Image 
    float[] result = new float[OUTPUT_SIZE]; // get the output probabilities for each class 

    inferenceInterface.feed(INPUT_NODE, INPUT_IMAGE, 1, INPUT_SIZE); //1-D input (1,INPUT_SIZE) 
    inferenceInterface.run(OUTPUT_NODES); 
    inferenceInterface.fetch(OUTPUT_NODE, result); 

私が使用しているAndroid Tensorflowライブラリのバージョンでは、1次元入力をする必要があります。したがって、Tensorflowのコードはそれに応じて変更する必要があります。

x_2 = tf.placeholder("float", [None, 1, 784], name="input") //1-D input 
x_2 = tf.reshape(x_2,[-1, 784]) // reshape according to the model requirements 

希望します。

+0

私に教えていただきありがとうございます!しかし、私はさらに質問があります。フィード関数argsのinputNameとdimsを理解していますが、srcを理解できません。 srcに784個の淡色(たとえば[1、33、34、2、84、....])の扁平な数字を入力する必要がありますか? srcはサンプルコードのINPUT_IMAGEです。 – maguro

+0

ドキュメントでは、 "シェイプとコンテンツのソース配列が与えられていると仮定します。"したがって、srcはモデルに応じた入力イメージでなければなりません。さらに、1次元配列でなければなりません(あなたの場合はfloat [] src)。 –

+0

ありがとう! :)私は質問を解決する! – maguro

関連する問題