Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempting to reset tensorflow graph when using keras, failing

I'm spinning up a Python 3 API w/gunicorn that uses keras to calculate vectors for an image, pretty straightforward.

How can I reset the data stored in memory for each request? Slowly over time the requests increase in the time it takes to respond. I've run a profiler and it's specifically this line in tensorflow (also memory usage goes up slowly over time per process):

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

It takes longer as more data is in the node. Here is the code I execute:

# 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()

I thought as_default() would help, but it doesn't. I also tried closing the session after I get the list of vectors, and that fails.

like image 623
Geesu Avatar asked Jul 12 '17 16:07

Geesu


1 Answers

from keras import backend as K
K.clear_session()
like image 121
Michael Guterl Avatar answered Oct 21 '22 17:10

Michael Guterl