简体   繁体   English

没有注册 Opkernel 来支持 Op 'SparseSoftmaxCrossEntropyWithLogits'

[英]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问题

  1. I just want to get variable named 'output' but why does 'SparseSoftmaxCrossEntropyWithLogits' involved?我只想获得名为“输出”的变量,但为什么涉及“SparseSoftmaxCrossEntropyWithLogits”?
  2. What does Registered kernels means.注册内核是什么意思。
  3. What should I do?我该怎么办?

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.

相关问题 没有注册OpKernel来支持在Android上使用这些功能的Op&#39;Add&#39; - No OpKernel was registered to support Op 'Add' with these attrs on Android 没有注册 OpKernel 来支持 Android 上的 Op“Pow” - No OpKernel was registered to support Op 'Pow' on Android 在Android上运行推理时,没有注册任何OpKernel支持Op&#39;Cos&#39; - No OpKernel was registered to support Op 'Cos' when running inference on Android 推断期间出错:无效的参数:没有使用这些attrs注册任何OpKernel支持Op&#39;DecodeJpeg&#39; - Error during inference: Invalid argument: No OpKernel was registered to support Op 'DecodeJpeg' with these attrs Android Studio:错误:程序类型已经存在:android.support.v4.app.BackStackRecord$Op - Android Studio: Error: Program type already present: android.support.v4.app.BackStackRecord$Op 程序类型已经存在:android.support.v4.app.BackStackRecord $ Op - Program type already present: android.support.v4.app.BackStackRecord$Op FireBase Unity-多个dex文件定义了Landroid / support / v4 / app / BackStackRecord $ Op; - FireBase Unity- Multiple dex files define Landroid/support/v4/app/BackStackRecord$Op; 程序类型已经存在:我的应用程序中为android.support.v4.app.BackStackRecord $ Op - Program type already present: android.support.v4.app.BackStackRecord$Op in my application 没有OpKernel Tensorflow Mobile Android。 如何调试? - No OpKernel Tensorflow Mobile Android. How to debug? Android Studio app.gradle错误:程序类型已存在:android.support.v4.app.BackStackRecord $ Op - Android Studio app.gradle Error: Program type already present: android.support.v4.app.BackStackRecord$Op
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM