Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace placeholder with a constant in an existing graph?

I have a frozen graph of a trained model, it has one tf.placeholder which I always feed the same value to.

I was wondering if it's possible to replace it with tf.constant instead. If it is somehow - any examples would be appreciated!

EDIT: Here is how it looks with code, to help visualize the question

I am using a pre-trained (by other people) model to run inference. The model is stored locally as a frozen graph file with .pb extension.

the code looks like this:

# load graph
graph = load_graph('frozen.pb')
session = tf.Session(graph=graph)

# Get input and output tensors
images_placeholder = graph.get_tensor_by_name("input:0")
output = graph.get_tensor_by_name("output:0")
phase_train_placeholder = graph.get_tensor_by_name("phase_train:0")

feed_dict = {images_placeholder: images, phase_train_placeholder: False}

result = session.run(output, feed_dict=feed_dict)

The problem is that I always feed phase_train_placeholder: False for my purposes, so I was wondering if it's possible to eliminate that placeholder and replace it with something like tf.constant(False, dtype=bool, shape=[])

like image 466
CrowbarKZ Avatar asked Apr 10 '17 20:04

CrowbarKZ


2 Answers

I've recently had to rewrite the answer above.

import tensorflow as tf
import sys
from tensorflow.core.framework import graph_pb2
import copy


INPUT_GRAPH_DEF_FILE = sys.argv[1]
OUTPUT_GRAPH_DEF_FILE = sys.argv[2]

# load our graph
def load_graph(filename):
    graph_def = tf.GraphDef()
    with tf.gfile.FastGFile(filename, 'rb') as f:
        graph_def.ParseFromString(f.read())
    return graph_def
graph_def = load_graph(INPUT_GRAPH_DEF_FILE)

target_node_name = sys.argv[3]
c = tf.constant(False, dtype=bool, shape=[], name=target_node_name)

# Create new graph, and rebuild it from original one
# replacing phase train node def with constant
new_graph_def = graph_pb2.GraphDef()
for node in graph_def.node:
    if node.name == target_node_name:
        new_graph_def.node.extend([c.op.node_def])
    else:
        new_graph_def.node.extend([copy.deepcopy(node)])

# save new graph
with tf.gfile.GFile(OUTPUT_GRAPH_DEF_FILE, "wb") as f:
    f.write(new_graph_def.SerializeToString())
like image 121
bantmen Avatar answered Oct 20 '22 00:10

bantmen


So I didn't manage to find any proper way, but managed to do it in a hacky way, by rebuilding the graph def and substituting the node I needed to substitute. Inspired by this code.

Here is the code (super hacky, use at your own risk):

INPUT_GRAPH_DEF_FILE = 'path/to/file'
OUTPUT_GRAPH_DEF_FILE = 'another/one'

# Get NodeDef of a constant tensor we want to put in place of 
# the placeholder. 
# (There is probably a better way to do this)
example_graph = tf.Graph()
with tf.Session(graph=example_graph):
    c = tf.constant(False, dtype=bool, shape=[], name='phase_train')
    for node in example_graph.as_graph_def().node:
        if node.name == 'phase_train':
            c_def = node

# load our graph
graph = load_graph(INPUT_GRAPH_DEF_FILE)
graph_def = graph.as_graph_def()

# Create new graph, and rebuild it from original one
# replacing phase train node def with constant
new_graph_def = graph_pb2.GraphDef()
for node in graph_def.node:
    if node.name == 'phase_train':
        new_graph_def.node.extend([c_def])
    else:
        new_graph_def.node.extend([copy.deepcopy(node)])

# save new graph
with tf.gfile.GFile(OUTPUT_GRAPH_DEF_FILE, "wb") as f:
    f.write(new_graph_def.SerializeToString())
like image 30
CrowbarKZ Avatar answered Oct 19 '22 22:10

CrowbarKZ