Tensorflow:如何将自定义输入插入现有图形?

Hed*_*iBY 5 graph subgraph tensorflow

我已经下载了一个实现VGG16 ConvNet的tensorflow GraphDef,我使用它来执行以下操作:

Pl['images'] = tf.placeholder(tf.float32, 
                          [None, 448, 448, 3],
                          name="images") #batch x width x height x channels
with open("tensorflow-vgg16/vgg16.tfmodel", mode='rb') as f: 
    fileContent = f.read()

graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)
tf.import_graph_def(graph_def, input_map={"images": Pl['images']})
Run Code Online (Sandbox Code Playgroud)

此外,我具有与的输出同质的图像特征"import/pool5/"

我怎么能告诉我的图不想使用他的输入"images",而只是张量"import/pool5/"作为输入?

谢谢 !

编辑

好吧,我知道我还不太清楚。情况如下:

我正在尝试使用GraphDef格式的预训练VGG16来实现 ROI池的这种实现。所以这是我的工作:

首先,我加载模型:

tf.reset_default_graph()
with open("tensorflow-vgg16/vgg16.tfmodel",
          mode='rb') as f:
    fileContent = f.read()
graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)
graph = tf.get_default_graph()
Run Code Online (Sandbox Code Playgroud)

然后,我创建我的占位符

images = tf.placeholder(tf.float32, 
                              [None, 448, 448, 3],
                              name="images") #batch x width x height x channels
boxes = tf.placeholder(tf.float32, 
                             [None,5], # 5 = [batch_id,x1,y1,x2,y2]
                             name = "boxes")
Run Code Online (Sandbox Code Playgroud)

我将图的第一部分的输出定义为conv5_3 / Relu

tf.import_graph_def(graph_def, 
                    input_map={'images':images})
out_tensor = graph.get_tensor_by_name("import/conv5_3/Relu:0")
Run Code Online (Sandbox Code Playgroud)

所以,out_tensor是形状[None,14,14,512]

然后,我进行ROI合并:

[out_pool,argmax] = module.roi_pool(out_tensor,
                                    boxes,
                                    7,7,1.0/1) 
Run Code Online (Sandbox Code Playgroud)

out_pool.shape = N_Boxes_in_batch x 7 x 7 x 512,是同质的pool5。然后,我想将out_pool 输入作为紧接在其后的op的输入pool5,因此它看起来像

tf.import_graph_def(graph.as_graph_def(),
                    input_map={'import/pool5':out_pool})
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我有这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-89-527398d7344b> in <module>()
      5 
      6 tf.import_graph_def(graph.as_graph_def(),
----> 7                     input_map={'import/pool5':out_pool})
      8 
      9 final_out = graph.get_tensor_by_name("import/Relu_1:0")

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/importer.py in import_graph_def(graph_def, input_map, return_elements, name, op_dict)
    333       # NOTE(mrry): If the graph contains a cycle, the full shape information
    334       # may not be available for this op's inputs.
--> 335       ops.set_shapes_for_outputs(op)
    336 
    337       # Apply device functions for this op.

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op)
   1610       raise RuntimeError("No shape function registered for standard op: %s"
   1611                          % op.type)
-> 1612   shapes = shape_func(op)
   1613   if len(op.outputs) != len(shapes):
   1614     raise RuntimeError(

/home/hbenyounes/vqa/roi_pooling_op_grad.py in _roi_pool_shape(op)
     13   channels = dims_data[3]
     14   print(op.inputs[1].name, op.inputs[1].get_shape())
---> 15   dims_rois = op.inputs[1].get_shape().as_list()
     16   num_rois = dims_rois[0]
     17 

/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py in as_list(self)
    745       A list of integers or None for each dimension.
    746     """
--> 747     return [dim.value for dim in self._dims]
    748 
    749   def as_proto(self):

TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)

有什么线索吗?

jea*_*ean 2

我要做的就是沿着这些思路:

-首先检索代表 VGG16 中 pool5 之后的 3 个全连接层的权重和偏差的张量名称。
为此,我会进行检查[n.name for n in graph.as_graph_def().node]。(它们可能看起来像 import/locali/weight:0、import/locali/bias:0 等)

-将它们放入Python列表中:

weights_names=["import/local1/weight:0" ,"import/local2/weight:0" ,"import/local3/weight:0"]
biases_names=["import/local1/bias:0" ,"import/local2/bias:0" ,"import/local3/bias:0"]
Run Code Online (Sandbox Code Playgroud)

- 定义一个函数,如下所示:

def pool5_tofcX(input_tensor, layer_number=3):
  flatten=tf.reshape(input_tensor,(-1,7*7*512))
  tmp=flatten
  for i in xrange(layer_number):
    tmp=tf.matmul(tmp, graph.get_tensor_by_name(weights_name[i]))
    tmp=tf.nn.bias_add(tmp, graph.get_tensor_by_name(biases_name[i]))
    tmp=tf.nn.relu(tmp)
  return tmp
Run Code Online (Sandbox Code Playgroud)

然后使用以下函数定义张量:

wanted_output=pool5_tofcX(out_pool) 
Run Code Online (Sandbox Code Playgroud)

然后你就完成了!