如何在Tesorflow代码中传输数据类型

时间:2017-12-11 08:33:01

标签: tensorflow

假设已在tensorflow中建立了两个模型,则model1后跟model2。

条件是输出的model1类型是“张量”和 model2的输入类型在创建图形模型的结构时需要“ndarray”。(数据不流动图形)如果我们没有构建两个或更多的Session,我们如何将model1与model2结合起来。

(实际上,需要输入类型的库函数是“ndarray”可以在model2中调用。我不想编写这个过程)

样本正在关注

import tensorflow as tf
import cv2

img = cv2.read("star_sky.jpg")#assumpting  shape of image is (256,256,3)


x_input = tf.placeholder(shape=(1,256,256,3),dtype=tf.float32)

W = tf.Variable(tf.random_normal([3,3,3,3]),dtype = tf.float32)

x_output_temp = tf.nn.conv2d(x_input,W,[1,1,1,1],padding="SAME")

#the other model want to use x_output to get Canny edge of image

x_output_ = x_output_temp[0]
x_output = cv2.Canny(x_output_,100,200)#number is parameter of threshold

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    img = [img]
    x_output.eval({x_input:img})

1 个答案:

答案 0 :(得分:0)

如果你想使用cv2来处理张量流Tensor,你需要在tf.py_func中做这个(它会在图形执行时将张量转换为ndarray并运行你在该数组上传递的python代码)