如何在tensorflow lite中使用numpy对张量进行运算

时间:2018-07-02 15:29:50

标签: tensorflow tensorflow-lite

我有一个张量流图,试图将图像分成三个单通道图像。

input_image = tf.placeholder(name="input_image", dtype=tf.float32, shape=[512 * 512 *3])
feed_dict ={input_image:resized_image_data}

channel_image = tf.reshape(input_image, (512, 512, 3))

我对张量进行切片,计算模糊,然后在“评估”张量上使用numpy的dstack。 这在tensorflow中有效,但tensorflow lite失败,并显示``无法分配内存''错误。

r,g,b = channel_image[:,:,0],channel_image[:,:,1],channel_image[:,:,2]

rtensor = tf.convert_to_tensor(r)
gtensor = tf.convert_to_tensor(g)
btensor = tf.convert_to_tensor(b)

rbatch = tf.expand_dims(tf.expand_dims(rtensor, axis=2), axis=0)
gbatch = tf.expand_dims(tf.expand_dims(gtensor, axis=2), axis=0)
bbatch = tf.expand_dims(tf.expand_dims(btensor, axis=2), axis=0)

rblur = tf.squeeze(blur(one_chan_kernel, strides, rbatch, 2), name="rblur")
gblur = tf.squeeze(blur(one_chan_kernel, strides, gbatch, 2), name="gblur")
bblur = tf.squeeze(blur(one_chan_kernel, strides, bbatch, 2), name="bblur")

result_np = np.dstack((rblur.eval(feed_dict=feed_dict), gblur.eval(feed_dict=feed_dict), bblur.eval(feed_dict=feed_dict)))
result = tf.expand_dims(tf.convert_to_tensor(result_np, name="result_tensor"), axis=0)

如何使用numpy对张量结果进行运算? 我不能使用tf.unstack和tf.stack,因为它们当前未在tensorflow中实现。精简版

logcat是:

                                                                            --------- beginning of crash
     

07-02 11:20:36.486 6553-6553 / camerafragment E / AndroidRuntime:FATAL EXCEPTION:main                                                                                   工艺:摄像头碎片,PID:6553                                                                                   java.lang.RuntimeException:无法恢复活动{camerafragment / camerafragment.MainActivity}:java.lang.NullPointerException:无法为解释器分配内存                                                                                       在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3773)                                                                                       在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3805)                                                                                       在android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)                                                                                       在android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)                                                                                       在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)                                                                                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1797)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:106)                                                                                       在android.os.Looper.loop(Looper.java:193)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6642)                                                                                       在java.lang.reflect.Method.invoke(本机方法)                                                                                       在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)                                                                                    原因:java.lang.NullPointerException:无法为解释器分配内存                                                                                       在org.tensorflow.lite.NativeInterpreterWrapper.createInterpreter(本机方法)                                                                                       在org.tensorflow.lite.NativeInterpreterWrapper。(NativeInterpreterWrapper.java:63)                                                                                       在org.tensorflow.lite.NativeInterpreterWrapper。(NativeInterpreterWrapper.java:51)                                                                                       在org.tensorflow.lite.Interpreter。(Interpreter.java:90)                                                                                       在camerafragment.MainActivity.initializeInference(MainActivity.java:230)                                                                                       在camerafragment.MainActivity.onResume(MainActivity.java:91)                                                                                       在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1412)                                                                                       在android.app.Activity.performResume(Activity.java:7287)                                                                                       在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3765)                                                                                       在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3805)                                                                                       在android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)                                                                                       在android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)                                                                                       在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)                                                                                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1797)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:106)                                                                                       在android.os.Looper.loop(Looper.java:193)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6642)                                                                                       在java.lang.reflect.Method.invoke(本机方法)                                                                                       在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)                                                                                       com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

TensorFlow Lite不支持直到最近的堆栈和堆栈。 “无法分配内存”错误很可能是由于转换失败这一事实。如果您每晚使用tensorflow重试一次,则可能会起作用。