Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow:如何将图像解码器节点添加到图形中?_Python_Tensorflow_Image Processing - Fatal编程技术网

Python TensorFlow:如何将图像解码器节点添加到图形中?

Python TensorFlow:如何将图像解码器节点添加到图形中?,python,tensorflow,image-processing,Python,Tensorflow,Image Processing,我有一个作为冻结图的tensorflow模型,它接受图像张量作为输入。但是,我想在这个图中添加一个新的输入图像解码器节点,以便模型也接受jpg图像的编码字节字符串,并最终自行解码图像。到目前为止,我已经尝试过这种方法: model = './frozen_graph.pb' with tf.gfile.FastGFile(model, 'rb') as f: # read graph graph_def = tf.GraphDef() graph_def.ParseF

我有一个作为冻结图的tensorflow模型,它接受图像张量作为输入。但是,我想在这个图中添加一个新的输入图像解码器节点,以便模型也接受jpg图像的编码字节字符串,并最终自行解码图像。到目前为止,我已经尝试过这种方法:

model = './frozen_graph.pb'

with tf.gfile.FastGFile(model, 'rb') as f:

    # read graph
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name="")
    g = tf.get_default_graph()

    # fetch old input
    old_input = g.get_tensor_by_name('image_tensor:0')

    # define new input
    new_input = graph_def.node.add()
    new_input.name = 'encoded_image_string_tensor'
    new_input.op = 'Substr'
    # add new input attr
    image = tf.image.decode_image(new_input, channels=3)

    # link new input to old input
    old_input.input = 'encoded_image_string_tensor'  #  must match with the name above
上述代码返回此异常:

Expected string passed to parameter 'input' of op 'Substr', got name: "encoded_image_string_tensor" op: "Substr"  of type 'NodeDef' instead.
我不太确定是否可以在图形中使用
tf.image.decode\u image
,因此可能有另一种方法来解决这个问题。有人得到提示吗?

多亏了,他给了我一个很好的提示,我才解决了这个问题。使用
input\u map
参数,我成功地将一个新的图形(仅解码jpg图像)映射到原始图形的输入(此处:
node.name='image\u tensor:0'
)。只需确保重命名解码器图的
名称\u范围
(此处:
解码器
)。 之后,可以使用tensorflow SavedModelBuilder保存新的连接图

这里是对象检测网络的示例:

将tensorflow导入为tf
从tensorflow.python.saved_model导入签名_常量
从tensorflow.python.saved_model导入标记_常量
#导出路径包含模型的名称和版本
model='path/to/model.pb'
导出路径='。/output/dir/'
sigs={}
将tf.gfile.FastGFile(型号“rb”)作为f:
使用tf.name_作用域(“解码器”):
image\u str\u tensor=tf.placeholder(tf.string,shape=[None],name='encoded\u image\u string\u tensor')
#CloudML预测API始终使用
#动态批量大小,例如(?,)。仅解码jpeg处理标量
#字符串,因为它不能保证一批图像
#相同的输出大小。我们使用tf.map\u fn给decode\u jpeg一个标量
#来自动态批处理的字符串。
def解码和调整大小(图像张力张量):
“”“解码jpeg字符串,调整其大小并返回uint8张量。”“”
image=tf.image.decode\u jpeg(image\u str\u张量,通道=3)
#在此处执行其他图像操作(如调整大小等)
image=tf.cast(image,dtype=tf.uint8)
返回图像
image=tf.map\fn(解码并调整大小,image\u str\u张量,back\u prop=False,dtype=tf.uint8)
使用tf.name_范围('net'):
#加载.pb文件
graph_def=tf.GraphDef()
graph_def.ParseFromString(f.read())
#连接解码器图和原始图
tf.import_graph_def(graph_def,name=”“,input_map={'image_张量:0':image})
g=tf.get_default_graph()
使用tf.Session()作为sess:
#将图形加载到会话并保存到新的.pb文件
#定义模型输入
inp=g.get\u tensor\u by\u name('解码器/编码的\u图像\u字符串\u tensor:0')
#定义模型输出
num_detections=g.get_tensor_by_name('num_detections:0'))
检测分数=g.通过名称获取张量('检测分数:0')
detection\u Box=g.get\u tensor\u by\u name('detection\u Box:0'))
out={'num_detections':num_detections,'detection_scores':detection_scores,'detection_box':detection_box}
builder=tf.saved\u model.builder.SavedModelBuilder(导出路径)
张量信息输入={
“输入”:tf.saved\u model.utils.build\u tensor\u info(inp)}
张量信息输出={}
对于k,v in out.items():
张量信息输出[k]=tf.saved\u model.utils.build\u张量信息(v)
#为tensorflow服务分配检测签名
检测\u签名=(
tf.saved_model.signature_def_utils.build_signature_def(
输入=张量信息输入,
输出=张量信息输出,
方法\名称=签名\常数。预测\方法\名称)
#“构建”图
builder.add_meta_graph_和_变量(
sess,[tf.saved_model.tag_constants.service],
签名定义图={
“检测/签名”:
检测和签名,
签名\常量。默认值\服务\签名\定义\密钥:
检测和签名,
},
main_op=tf.tables_初始值设定项()
)
#保存图形
builder.save()
我用来找到正确解决方案的来源:

此外: 如果难以找到正确的输入和输出节点,可以运行此命令以显示图形:

graph\u op=g.get\u操作()
对于图_op中的i:
打印(i.node_def)

我不确定我是否完全理解您想要做什么,但有一个
input\u map
参数,您可以将图形中的现有节点映射到图形定义中的节点。这将允许您更改输入数据进入图形的方式。