【问题标题】:Session graph is empty although graph is set as default会话图为空,尽管图设置为默认值
【发布时间】:2019-08-13 20:18:29
【问题描述】:

我收到会话图为空的错误消息。我在 stackoverflow 页面上查看了不同的问题,但对我没有任何帮助。

import os
import sys
import scipy.io
import scipy.misc
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from PIL import Image
from nst_utils import *
import numpy as np
import tensorflow as tf

model = load_vgg_model("imagenet-vgg-verydeep-19.mat")

tf.reset_default_graph()

# Start interactive session
sess = tf.InteractiveSession()

content_image = scipy.misc.imresize(arr = scipy.misc.imread("content.jpeg"), size=(300,400,3))
content_image = reshape_and_normalize_image(content_image)

style_image = scipy.misc.imresize(arr = scipy.misc.imread("monet.jpg"), size=(300,400,3))
style_image = reshape_and_normalize_image(style_image)

generated_image = generate_noise_image(content_image)
imshow(generated_image[0])

# Assign the content image to be the input of the VGG model.  
with tf.Session() as sess:
    sess.run(model['input'].assign(content_image))

我收到“RuntimeError:会话图为空。在调用 run() 之前向图中添加操作。”感谢我能得到的每一个建议:)

【问题讨论】:

  • 请发布一个可重现的示例,以便有人可以帮助您。
  • 嗨,我更新了代码示例

标签: tensorflow session


【解决方案1】:

几个错误:

  1. 您正在实例化模型 model = load_vgg_model("imagenet-vgg-verydeep-19.mat"),然后重置默认图形 tf.reset_default_graph() → 应该是相反的。
  2. 您已经启动了一个 tf.InteractiveSession(),并且您正在创建另一个 tf.Session() → 您不需要第一个。

这应该适合你:

导入操作系统 导入系统 导入 scipy.io 导入 scipy.misc 将 matplotlib.pyplot 导入为 plt 从 matplotlib.pyplot 导入 imshow 从 PIL 导入图像 从 nst_utils 导入 * 将 numpy 导入为 np 将张量流导入为 tf

tf.reset_default_graph()
model = load_vgg_model("imagenet-vgg-verydeep-19.mat")

content_image = scipy.misc.imresize(arr = scipy.misc.imread("content.jpeg"), size=(300,400,3))
content_image = reshape_and_normalize_image(content_image)

style_image = scipy.misc.imresize(arr = scipy.misc.imread("monet.jpg"), size=(300,400,3))
style_image = reshape_and_normalize_image(style_image)

generated_image = generate_noise_image(content_image)
imshow(generated_image[0])

# Assign the content image to be the input of the VGG model.  
sess = tf.InteractiveSession()
sess.run(model['input'].assign(content_image))

【讨论】:

  • 嗨!感谢所有的建议,非常感谢! :) 如果我更喜欢在开头使用 sess = tf.InteractiveSession(),那么我就不需要 'with tf.Session() as sess:" 语句,对吧?
  • 我根据您的反馈更新了我的答案。是的,你不需要它。假设您的其他功能正常,这应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多