Session graph empty - multiple graphs












0















I am trying to use a Policy Network and CNN within the same Python file. I am only training the PN and using the CNN as a feed-forward feature extractor. I am getting an issue where the Train-PC isn't "building" the imported Keras graph into the session, and thus when I try to run a feed-forward function on the CNN (feature_vector = self.extract_feature_vector([X_img_norm])[0]), I get the error posted below.



The only difference between the files on the Home-PC I am developing on and the Train-PC is the way I import the model which I believe may be causing this (possibly it's not recognised). I posted previously about this error here.



Since the Train-PC is not my own, I cannot change the version of the packages. So I am looking for a solution which does not involve downgrading versions, as the issue with importing wasn't solved through that also.



Home-PC (Mac):



tensorflow==1.11.0
keras 2.2.4


Train-PC (Ubuntu):



tensorflow-gpu==1.10.1
keras 2.1.2


Code of CNN:



class ResNetCNN:

def __init__(self):
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)

with self.sess.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

def feed_forward(self, img):
"""Extract the feature vector of the passed image using the pre-trained CNN."""

# Resize observable region into input volume
X_img = np.array(img, dtype=np.float32).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

# Normalise feature data
X_img_norm = X_img / 255.0

# Extract feature vector from the pre-trained CNN (1, 4096)
feature_vector = self.extract_feature_vector([X_img_norm])[0]

# Reshape tensor to (4096, )
feature_vector = np.array(feature_vector).reshape(4096, )

# Confirm that the correct output layer was used
assert feature_vector.shape == (4096, ), "Incorrect CNN output layer: shape = {}".format(feature_vector.shape)

return feature_vector


Error:



Traceback (most recent call last):
File "keras_pn.py", line 856, in <module>
s, a, r, d_r, n = rollout(epsilon, RENDER, PolicyNetwork, ResNetCNN)
File "keras_pn.py", line 682, in rollout
feature_vector = ResNetCNN.feed_forward(observable_region)
File "keras_pn.py", line 87, in feed_forward
feature_vector = self.extract_feature_vector([X_img_norm])[0]
File "/home/name/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2357, in __call__
**self.session_kwargs)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 877, in run
run_metadata_ptr)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1025, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().


Edit:



def __init__(self):
self.graph = tf.Graph()

with self.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

self.sess = tf.Session(graph=self.graph)









share|improve this question

























  • You need to build the graph before starting the session.

    – Dinari
    Nov 25 '18 at 7:12











  • @OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

    – Joshua
    Nov 25 '18 at 7:25
















0















I am trying to use a Policy Network and CNN within the same Python file. I am only training the PN and using the CNN as a feed-forward feature extractor. I am getting an issue where the Train-PC isn't "building" the imported Keras graph into the session, and thus when I try to run a feed-forward function on the CNN (feature_vector = self.extract_feature_vector([X_img_norm])[0]), I get the error posted below.



The only difference between the files on the Home-PC I am developing on and the Train-PC is the way I import the model which I believe may be causing this (possibly it's not recognised). I posted previously about this error here.



Since the Train-PC is not my own, I cannot change the version of the packages. So I am looking for a solution which does not involve downgrading versions, as the issue with importing wasn't solved through that also.



Home-PC (Mac):



tensorflow==1.11.0
keras 2.2.4


Train-PC (Ubuntu):



tensorflow-gpu==1.10.1
keras 2.1.2


Code of CNN:



class ResNetCNN:

def __init__(self):
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)

with self.sess.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

def feed_forward(self, img):
"""Extract the feature vector of the passed image using the pre-trained CNN."""

# Resize observable region into input volume
X_img = np.array(img, dtype=np.float32).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

# Normalise feature data
X_img_norm = X_img / 255.0

# Extract feature vector from the pre-trained CNN (1, 4096)
feature_vector = self.extract_feature_vector([X_img_norm])[0]

# Reshape tensor to (4096, )
feature_vector = np.array(feature_vector).reshape(4096, )

# Confirm that the correct output layer was used
assert feature_vector.shape == (4096, ), "Incorrect CNN output layer: shape = {}".format(feature_vector.shape)

return feature_vector


Error:



Traceback (most recent call last):
File "keras_pn.py", line 856, in <module>
s, a, r, d_r, n = rollout(epsilon, RENDER, PolicyNetwork, ResNetCNN)
File "keras_pn.py", line 682, in rollout
feature_vector = ResNetCNN.feed_forward(observable_region)
File "keras_pn.py", line 87, in feed_forward
feature_vector = self.extract_feature_vector([X_img_norm])[0]
File "/home/name/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2357, in __call__
**self.session_kwargs)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 877, in run
run_metadata_ptr)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1025, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().


Edit:



def __init__(self):
self.graph = tf.Graph()

with self.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

self.sess = tf.Session(graph=self.graph)









share|improve this question

























  • You need to build the graph before starting the session.

    – Dinari
    Nov 25 '18 at 7:12











  • @OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

    – Joshua
    Nov 25 '18 at 7:25














0












0








0








I am trying to use a Policy Network and CNN within the same Python file. I am only training the PN and using the CNN as a feed-forward feature extractor. I am getting an issue where the Train-PC isn't "building" the imported Keras graph into the session, and thus when I try to run a feed-forward function on the CNN (feature_vector = self.extract_feature_vector([X_img_norm])[0]), I get the error posted below.



The only difference between the files on the Home-PC I am developing on and the Train-PC is the way I import the model which I believe may be causing this (possibly it's not recognised). I posted previously about this error here.



Since the Train-PC is not my own, I cannot change the version of the packages. So I am looking for a solution which does not involve downgrading versions, as the issue with importing wasn't solved through that also.



Home-PC (Mac):



tensorflow==1.11.0
keras 2.2.4


Train-PC (Ubuntu):



tensorflow-gpu==1.10.1
keras 2.1.2


Code of CNN:



class ResNetCNN:

def __init__(self):
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)

with self.sess.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

def feed_forward(self, img):
"""Extract the feature vector of the passed image using the pre-trained CNN."""

# Resize observable region into input volume
X_img = np.array(img, dtype=np.float32).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

# Normalise feature data
X_img_norm = X_img / 255.0

# Extract feature vector from the pre-trained CNN (1, 4096)
feature_vector = self.extract_feature_vector([X_img_norm])[0]

# Reshape tensor to (4096, )
feature_vector = np.array(feature_vector).reshape(4096, )

# Confirm that the correct output layer was used
assert feature_vector.shape == (4096, ), "Incorrect CNN output layer: shape = {}".format(feature_vector.shape)

return feature_vector


Error:



Traceback (most recent call last):
File "keras_pn.py", line 856, in <module>
s, a, r, d_r, n = rollout(epsilon, RENDER, PolicyNetwork, ResNetCNN)
File "keras_pn.py", line 682, in rollout
feature_vector = ResNetCNN.feed_forward(observable_region)
File "keras_pn.py", line 87, in feed_forward
feature_vector = self.extract_feature_vector([X_img_norm])[0]
File "/home/name/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2357, in __call__
**self.session_kwargs)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 877, in run
run_metadata_ptr)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1025, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().


Edit:



def __init__(self):
self.graph = tf.Graph()

with self.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

self.sess = tf.Session(graph=self.graph)









share|improve this question
















I am trying to use a Policy Network and CNN within the same Python file. I am only training the PN and using the CNN as a feed-forward feature extractor. I am getting an issue where the Train-PC isn't "building" the imported Keras graph into the session, and thus when I try to run a feed-forward function on the CNN (feature_vector = self.extract_feature_vector([X_img_norm])[0]), I get the error posted below.



The only difference between the files on the Home-PC I am developing on and the Train-PC is the way I import the model which I believe may be causing this (possibly it's not recognised). I posted previously about this error here.



Since the Train-PC is not my own, I cannot change the version of the packages. So I am looking for a solution which does not involve downgrading versions, as the issue with importing wasn't solved through that also.



Home-PC (Mac):



tensorflow==1.11.0
keras 2.2.4


Train-PC (Ubuntu):



tensorflow-gpu==1.10.1
keras 2.1.2


Code of CNN:



class ResNetCNN:

def __init__(self):
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)

with self.sess.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

def feed_forward(self, img):
"""Extract the feature vector of the passed image using the pre-trained CNN."""

# Resize observable region into input volume
X_img = np.array(img, dtype=np.float32).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

# Normalise feature data
X_img_norm = X_img / 255.0

# Extract feature vector from the pre-trained CNN (1, 4096)
feature_vector = self.extract_feature_vector([X_img_norm])[0]

# Reshape tensor to (4096, )
feature_vector = np.array(feature_vector).reshape(4096, )

# Confirm that the correct output layer was used
assert feature_vector.shape == (4096, ), "Incorrect CNN output layer: shape = {}".format(feature_vector.shape)

return feature_vector


Error:



Traceback (most recent call last):
File "keras_pn.py", line 856, in <module>
s, a, r, d_r, n = rollout(epsilon, RENDER, PolicyNetwork, ResNetCNN)
File "keras_pn.py", line 682, in rollout
feature_vector = ResNetCNN.feed_forward(observable_region)
File "keras_pn.py", line 87, in feed_forward
feature_vector = self.extract_feature_vector([X_img_norm])[0]
File "/home/name/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2357, in __call__
**self.session_kwargs)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 877, in run
run_metadata_ptr)
File "/home/name/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1025, in _run
raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().


Edit:



def __init__(self):
self.graph = tf.Graph()

with self.graph.as_default():
# Load pre-trained model from file
self.CNN_ResNet_Pascal = models.load_model(CNN_MODEL_DIR)
# self.CNN_ResNet_Pascal = tf.keras.models.load_model(CNN_MODEL_DIR)

self.extract_feature_vector = K.function([self.CNN_ResNet_Pascal.layers[0].input],
[self.CNN_ResNet_Pascal.layers[-4].output])

self.sess = tf.Session(graph=self.graph)






python tensorflow keras






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:25







Joshua

















asked Nov 25 '18 at 7:02









JoshuaJoshua

296




296













  • You need to build the graph before starting the session.

    – Dinari
    Nov 25 '18 at 7:12











  • @OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

    – Joshua
    Nov 25 '18 at 7:25



















  • You need to build the graph before starting the session.

    – Dinari
    Nov 25 '18 at 7:12











  • @OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

    – Joshua
    Nov 25 '18 at 7:25

















You need to build the graph before starting the session.

– Dinari
Nov 25 '18 at 7:12





You need to build the graph before starting the session.

– Dinari
Nov 25 '18 at 7:12













@OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

– Joshua
Nov 25 '18 at 7:25





@OrDinari I built the graph prior to starting the session yet I still get the error. I have edited the init function and edited the post showing it.

– Joshua
Nov 25 '18 at 7:25












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465376%2fsession-graph-empty-multiple-graphs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465376%2fsession-graph-empty-multiple-graphs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)