Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow : how to insert custom input to existing graph?

I have downloaded a tensorflow GraphDef that implements a VGG16 ConvNet, which I use doing this :

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']})

Besides, I have image features that are homogeneous to the output of the "import/pool5/".

How can I tell my graph that don't want to use his input "images", but the tensor "import/pool5/" as input ?

Thank's !

EDIT

OK I realize I haven't been very clear. Here is the situation:

I am trying to use this implementation of ROI pooling, using a pre-trained VGG16, which I have in the GraphDef format. So here is what I do:

First of all, I load the model:

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()

Then, I create my placeholders

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")

And I define the output of the first part of the graph to be 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")

So, out_tensor is of shape [None,14,14,512]

Then, I do the ROI pooling:

[out_pool,argmax] = module.roi_pool(out_tensor,
                                    boxes,
                                    7,7,1.0/1) 

With out_pool.shape = N_Boxes_in_batch x 7 x 7 x 512, which is homogeneous to pool5. I would then like to feed out_pool as an input to the op that comes just after pool5, so it would look like

tf.import_graph_def(graph.as_graph_def(),
                    input_map={'import/pool5':out_pool})

But it doesn't work, I have this error:

---------------------------------------------------------------------------
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

Any clue ?

like image 867
HediBY Avatar asked Jul 27 '16 16:07

HediBY


1 Answers

It is usually very convenient to use tf.train.export_meta_graph to store the whole MetaGraph. Then, upon restoring you can use tf.train.import_meta_graph, because it turns out that it passes all additional arguments to the underlying import_scoped_meta_graph which has the input_map argument and utilizes it when it gets to it's own invocation of import_graph_def.

It is not documented, and took me waaaay toooo much time to find it, but it works!

like image 129
Jonan Gueorguiev Avatar answered Sep 19 '22 22:09

Jonan Gueorguiev