[英]No Opkernel was registered to support Op 'SparseSoftmaxCrossEntropyWithLogits'
I want use Tensorflow on Android by using pb file trained on python with GPU.我想通过使用在带有 GPU 的 python 上训练的 pb 文件在 Android 上使用 Tensorflow。
Official example of Tensorflow-demo on Android use pre-trained 'tensorflow_inception_graph.pb'. Android 上 Tensorflow-demo 的官方示例使用预训练的“tensorflow_inception_graph.pb”。 Like above, I want use mine '*.pb' on Android.
像上面一样,我想在 Android 上使用我的“*.pb”。
First, I have trained simple Tensorflow code which similar to mnist-example on python like below.首先,我训练了简单的 Tensorflow 代码,类似于下面的 python 上的 mnist-example。
python code蟒蛇代码
def deepnn(x):
with tf.name_scope('reshape'):
x_image = tf.reshape(x, [-1, 30, 6, 1])
with tf.name_scope('conv1'):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('conv2'):
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
with tf.name_scope('pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
W_fc1 = weight_variable([15 * 3 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 15 * 3 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
with tf.name_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
with tf.name_scope('fc2'):
W_fc2 = weight_variable([1024, 28])
b_fc2 = bias_variable([28])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
return y_conv, keep_prob
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
x = tf.placeholder(tf.float32, [None, 180], name='input')
y_ = tf.placeholder(tf.int64, [None])
y_conv, keep_prob = deepnn(x)
y_conv = tf.identity(y_conv, 'output')
with tf.name_scope('loss'):
cross_entropy = tf.losses.sparse_softmax_cross_entropy( labels=y_, logits=y_conv )
cross_entropy = tf.reduce_mean(cross_entropy, name='loss')
y_argout = tf.argmax(input=y_conv, axis=1, name="y_argout")
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)
graph_location = tempfile.mkdtemp()
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
...
...
tf.train.write_graph(sess.graph_def, './', 'mlp.pb', as_text=False)
x = tf.placeholder(tf.float32, [None, 180], name='input')
and y_conv = tf.identity(y_conv, 'output')
naming is to use on Android Studio. x = tf.placeholder(tf.float32, [None, 180], name='input')
和y_conv = tf.identity(y_conv, 'output')
命名用于 Android Studio。
Then, I used outputted 'mlp.pb' on Android Studio like below.然后,我在 Android Studio 上使用了输出的“mlp.pb”,如下所示。
java code on android studio android studio上的java代码
inferenceInterface = new TensorFlowInferenceInterface(getAssets(), 'mlp.pb');
...
inferenceInterface.feed('input', floatValues, 180);
inferenceInterface.run(new String[] {'output'}); // an error will occur here.
inferenceInterface.fetch('output', outputs);
Occurred Errors发生的错误
06-08 09:33:59.500 22971-22971/com.example.tkt.tf_test E/TensorFlowInferenceInterface: Failed to run TensorFlow inference with inputs:[input], outputs:[output]
06-08 09:33:59.501 22971-22971/com.example.tkt.tf_test D/AndroidRuntime: Shutting down VM
06-08 09:33:59.584 22971-22971/com.example.tkt.tf_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tkt.tf_test, PID: 22971
java.lang.IllegalArgumentException: No OpKernel was registered to support Op 'SparseSoftmaxCrossEntropyWithLogits' with these attrs. Registered devices: [CPU], Registered kernels:
<no registered kernels>
[[Node: loss/sparse_softmax_cross_entropy_loss/xentropy/xentropy = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64](output, Placeholder)]]
Questions问题
I would really appreciate for your reply.我真的很感激你的回复。
Question 1:问题 1:
tf.losses.sparse_softmax_cross_entropy
Question 2:问题2:
Registered kernel means what op kernel could be supported in device.注册内核意味着设备可以支持什么操作内核。
Question3:问题3:
In android, there is only a subset of kernels to keep base library small.在 android 中,只有一个内核子集来保持基本库很小。 If you want to support more custom TensorFlow operators, you should build the libraries yourself following this link
如果您想支持更多自定义 TensorFlow 运算符,您应该按照此链接自行构建库
To support SparseSoftmaxCrossEntropyWithLogits in android, you could add sparse_xent_op.cc and sparse_xent_op.h in BUILD file like xent_op.cc and xent_op.h in source file.要在 android 中支持 SparseSoftmaxCrossEntropyWithLogits,您可以在BUILD文件中添加 sparse_xent_op.cc 和 sparse_xent_op.h,如源文件中的 xent_op.cc 和 xent_op.h。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.