How to run tensorflow session inside a default session?

Viewed 1848

How to use the prediction of default tensorflow session as input to new tensorflow session. I have a detection model, the detected objects should be passed as input to new model for classification, when i am trying to do I am getting resource exhaust error. sample code:

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        while True:
            sess.run([boxes, scores, classes, num_detections] )

            """ I want to use the predicted values to another tensorflow session for classification"""
           i.e
           with tf.Session() as sess:
               "Classification model"
               "Pseudo code????"

Thanks

2 Answers

After this with the session will close because it falls out of scope, so yes, it would be created each iteration of the while loop:

   with tf.Session() as sess:
       "Classification model"
       "Pseudo code????"

I suspect you'd want to re-arrange to something like this:

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        sess2 = tf.Session(graph=detection_graph)
        while True:
            sess.run([boxes, scores, classes, num_detections] )

            """ I want to use the predicted values to another tensorflow session for classification"""
            # use sess2 here
            "Classification model"
            "Pseudo code????"

After trying all different methods I was able to fix it, using class init method.

def __init__(self):
    """Tensorflow detector
    """
    self.detection_graph = tf.Graph()
    with self.detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(frozen_inference.pb, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')


    with self.detection_graph.as_default():
        config = tf.ConfigProto()
        # config.gpu_options.allow_growth = True
        self.detection_sess = tf.Session(graph=self.detection_graph, config=config)
        self.windowNotSet = True
Related