【问题标题】:Attempting to reset tensorflow graph when using keras, failing尝试在使用 keras 时重置张量流图,失败
【发布时间】:2017-07-12 16:56:05
【问题描述】:

我正在开发一个带有 gunicorn 的 Python 3 API,它使用 keras 计算图像的向量,非常简单。

如何为每个请求重置存储在内存中的数据?随着时间的推移,请求的响应时间会慢慢增加。我已经运行了一个分析器,特别是 tensorflow 中的这一行(每个进程的内存使用量也会随着时间的推移而缓慢上升):

#tensorflow/python/framework/ops.py:2317:_as_graph_def
graph.node.extend([op.node_def])

节点中的数据越多,所需的时间就越长。这是我执行的代码:

# We have 11439MiB of GPU memory, lets only use 2GB of it:
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.22
sess = tf.Session(config=config)
set_session(sess)
sess.graph.as_default()

# Get the vector for the image
img_size = (224,224)
vgg = VGG16(include_top=False, weights='imagenet')
img = kimage.load_img(tmpfile.name, target_size=img_size)
x = kimage.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = vgg.predict(x)
vectors = pred.ravel().tolist()

我认为as_default() 会有所帮助,但事实并非如此。我也尝试在获得向量列表后关闭会话,但失败了。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:
    from keras import backend as K
    K.clear_session()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多