Keras variable() memory leak











up vote
0
down vote

favorite












I am new to Keras, and tensorflow in general, and have a problem. I am using some of the loss functions (binary_crossentropy and mean_squared_error mainly) to calculate the loss after prediction. Since Keras only accepts it's own variable type, I am creating one and supply it as an argument. This scenario is executed in a loop (with sleep) as such:



Get appropriate data -> predict -> calculate the lost -> return it.



Since I have multiple models that follow this pattern I created tensorflow graphs and sessions to prevent collision (also when exporting the models' weights I had problem with single graph and session so I had to create distinct ones for every single model).



However, now the memory is rising uncontrollably, from couple of MiB to 700MiB in couple of iterations. I am aware of Keras's clear_session() and gc.collect(), and I use them at the end of every iteration, but the problem is still present. Here I provide a code snippet, which is not the actual code, from the project. I've created separate script in order to isolate the problem:



import tensorflow as tf

from keras import backend as K
from keras.losses import binary_crossentropy, mean_squared_error

from time import time, sleep
import gc
from numpy.random import rand

from os import getpid
from psutil import Process

from csv import DictWriter
from keras import backend as K

this_process = Process(getpid())

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

cnt = 0
max_c = 500

with open('/home/quark/Desktop/python-test/leak-7.csv', 'a') as file:
writer = DictWriter(file, fieldnames=['time', 'mem'])
writer.writeheader()

while cnt < max_c:
with graph.as_default(), sess.as_default():
y_true = K.variable(rand(36, 6))
y_pred = K.variable(rand(36, 6))

rec_loss = K.eval(binary_crossentropy(y_true, y_pred))
val_loss = K.eval(mean_squared_error(y_true, y_pred))

writer.writerow({
'time': int(time()),
'mem': this_process.memory_info().rss
})

K.clear_session()
gc.collect()

cnt += 1
print(max_c - cnt)
sleep(0.1)


Additionally, I've added the plot of the memory usage:
Keras memory leak



Any help is appreciated.










share|improve this question
























  • Could you add the requred imports? I believe you are mixing tf and keras commands.
    – Daniel GL
    Nov 8 at 10:11










  • Yes, a full example that we can run would be nice.
    – Matias Valdenegro
    Nov 8 at 10:13










  • I've updated the code.
    – mladenk
    Nov 8 at 10:43















up vote
0
down vote

favorite












I am new to Keras, and tensorflow in general, and have a problem. I am using some of the loss functions (binary_crossentropy and mean_squared_error mainly) to calculate the loss after prediction. Since Keras only accepts it's own variable type, I am creating one and supply it as an argument. This scenario is executed in a loop (with sleep) as such:



Get appropriate data -> predict -> calculate the lost -> return it.



Since I have multiple models that follow this pattern I created tensorflow graphs and sessions to prevent collision (also when exporting the models' weights I had problem with single graph and session so I had to create distinct ones for every single model).



However, now the memory is rising uncontrollably, from couple of MiB to 700MiB in couple of iterations. I am aware of Keras's clear_session() and gc.collect(), and I use them at the end of every iteration, but the problem is still present. Here I provide a code snippet, which is not the actual code, from the project. I've created separate script in order to isolate the problem:



import tensorflow as tf

from keras import backend as K
from keras.losses import binary_crossentropy, mean_squared_error

from time import time, sleep
import gc
from numpy.random import rand

from os import getpid
from psutil import Process

from csv import DictWriter
from keras import backend as K

this_process = Process(getpid())

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

cnt = 0
max_c = 500

with open('/home/quark/Desktop/python-test/leak-7.csv', 'a') as file:
writer = DictWriter(file, fieldnames=['time', 'mem'])
writer.writeheader()

while cnt < max_c:
with graph.as_default(), sess.as_default():
y_true = K.variable(rand(36, 6))
y_pred = K.variable(rand(36, 6))

rec_loss = K.eval(binary_crossentropy(y_true, y_pred))
val_loss = K.eval(mean_squared_error(y_true, y_pred))

writer.writerow({
'time': int(time()),
'mem': this_process.memory_info().rss
})

K.clear_session()
gc.collect()

cnt += 1
print(max_c - cnt)
sleep(0.1)


Additionally, I've added the plot of the memory usage:
Keras memory leak



Any help is appreciated.










share|improve this question
























  • Could you add the requred imports? I believe you are mixing tf and keras commands.
    – Daniel GL
    Nov 8 at 10:11










  • Yes, a full example that we can run would be nice.
    – Matias Valdenegro
    Nov 8 at 10:13










  • I've updated the code.
    – mladenk
    Nov 8 at 10:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am new to Keras, and tensorflow in general, and have a problem. I am using some of the loss functions (binary_crossentropy and mean_squared_error mainly) to calculate the loss after prediction. Since Keras only accepts it's own variable type, I am creating one and supply it as an argument. This scenario is executed in a loop (with sleep) as such:



Get appropriate data -> predict -> calculate the lost -> return it.



Since I have multiple models that follow this pattern I created tensorflow graphs and sessions to prevent collision (also when exporting the models' weights I had problem with single graph and session so I had to create distinct ones for every single model).



However, now the memory is rising uncontrollably, from couple of MiB to 700MiB in couple of iterations. I am aware of Keras's clear_session() and gc.collect(), and I use them at the end of every iteration, but the problem is still present. Here I provide a code snippet, which is not the actual code, from the project. I've created separate script in order to isolate the problem:



import tensorflow as tf

from keras import backend as K
from keras.losses import binary_crossentropy, mean_squared_error

from time import time, sleep
import gc
from numpy.random import rand

from os import getpid
from psutil import Process

from csv import DictWriter
from keras import backend as K

this_process = Process(getpid())

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

cnt = 0
max_c = 500

with open('/home/quark/Desktop/python-test/leak-7.csv', 'a') as file:
writer = DictWriter(file, fieldnames=['time', 'mem'])
writer.writeheader()

while cnt < max_c:
with graph.as_default(), sess.as_default():
y_true = K.variable(rand(36, 6))
y_pred = K.variable(rand(36, 6))

rec_loss = K.eval(binary_crossentropy(y_true, y_pred))
val_loss = K.eval(mean_squared_error(y_true, y_pred))

writer.writerow({
'time': int(time()),
'mem': this_process.memory_info().rss
})

K.clear_session()
gc.collect()

cnt += 1
print(max_c - cnt)
sleep(0.1)


Additionally, I've added the plot of the memory usage:
Keras memory leak



Any help is appreciated.










share|improve this question















I am new to Keras, and tensorflow in general, and have a problem. I am using some of the loss functions (binary_crossentropy and mean_squared_error mainly) to calculate the loss after prediction. Since Keras only accepts it's own variable type, I am creating one and supply it as an argument. This scenario is executed in a loop (with sleep) as such:



Get appropriate data -> predict -> calculate the lost -> return it.



Since I have multiple models that follow this pattern I created tensorflow graphs and sessions to prevent collision (also when exporting the models' weights I had problem with single graph and session so I had to create distinct ones for every single model).



However, now the memory is rising uncontrollably, from couple of MiB to 700MiB in couple of iterations. I am aware of Keras's clear_session() and gc.collect(), and I use them at the end of every iteration, but the problem is still present. Here I provide a code snippet, which is not the actual code, from the project. I've created separate script in order to isolate the problem:



import tensorflow as tf

from keras import backend as K
from keras.losses import binary_crossentropy, mean_squared_error

from time import time, sleep
import gc
from numpy.random import rand

from os import getpid
from psutil import Process

from csv import DictWriter
from keras import backend as K

this_process = Process(getpid())

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

cnt = 0
max_c = 500

with open('/home/quark/Desktop/python-test/leak-7.csv', 'a') as file:
writer = DictWriter(file, fieldnames=['time', 'mem'])
writer.writeheader()

while cnt < max_c:
with graph.as_default(), sess.as_default():
y_true = K.variable(rand(36, 6))
y_pred = K.variable(rand(36, 6))

rec_loss = K.eval(binary_crossentropy(y_true, y_pred))
val_loss = K.eval(mean_squared_error(y_true, y_pred))

writer.writerow({
'time': int(time()),
'mem': this_process.memory_info().rss
})

K.clear_session()
gc.collect()

cnt += 1
print(max_c - cnt)
sleep(0.1)


Additionally, I've added the plot of the memory usage:
Keras memory leak



Any help is appreciated.







python tensorflow memory-leaks keras






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 10:43

























asked Nov 8 at 9:23









mladenk

12




12












  • Could you add the requred imports? I believe you are mixing tf and keras commands.
    – Daniel GL
    Nov 8 at 10:11










  • Yes, a full example that we can run would be nice.
    – Matias Valdenegro
    Nov 8 at 10:13










  • I've updated the code.
    – mladenk
    Nov 8 at 10:43


















  • Could you add the requred imports? I believe you are mixing tf and keras commands.
    – Daniel GL
    Nov 8 at 10:11










  • Yes, a full example that we can run would be nice.
    – Matias Valdenegro
    Nov 8 at 10:13










  • I've updated the code.
    – mladenk
    Nov 8 at 10:43
















Could you add the requred imports? I believe you are mixing tf and keras commands.
– Daniel GL
Nov 8 at 10:11




Could you add the requred imports? I believe you are mixing tf and keras commands.
– Daniel GL
Nov 8 at 10:11












Yes, a full example that we can run would be nice.
– Matias Valdenegro
Nov 8 at 10:13




Yes, a full example that we can run would be nice.
– Matias Valdenegro
Nov 8 at 10:13












I've updated the code.
– mladenk
Nov 8 at 10:43




I've updated the code.
– mladenk
Nov 8 at 10:43












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I just removed the with statement (probably some tf code), and I don't see any leak. I believe there is a difference between the keras session and the tf default session. So you were not clearing the correct session with K.clear_session(). Probably using tf.reset_default_graph() could work too.



while True: 
y_true = K.variable(rand(36, 6))
y_pred = K.variable(rand(36, 6))

val_loss = K.eval(binary_crossentropy(y_true, y_pred))
rec_loss = K.eval(mean_squared_error(y_true, y_pred))

K.clear_session()
gc.collect()

sleep(0.1)





share|improve this answer





















  • I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
    – mladenk
    Nov 8 at 10:41










  • Then you just have to reset the appropriate graph, not just the default one
    – Daniel GL
    Nov 8 at 10:48




















up vote
0
down vote













At the end, what I did was remove the K.variable() code out of the where statement. In that way, the variables are part of the default graph which is later cleared by K.clear_session().






share|improve this answer





















    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',
    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%2f53204747%2fkeras-variable-memory-leak%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    I just removed the with statement (probably some tf code), and I don't see any leak. I believe there is a difference between the keras session and the tf default session. So you were not clearing the correct session with K.clear_session(). Probably using tf.reset_default_graph() could work too.



    while True: 
    y_true = K.variable(rand(36, 6))
    y_pred = K.variable(rand(36, 6))

    val_loss = K.eval(binary_crossentropy(y_true, y_pred))
    rec_loss = K.eval(mean_squared_error(y_true, y_pred))

    K.clear_session()
    gc.collect()

    sleep(0.1)





    share|improve this answer





















    • I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
      – mladenk
      Nov 8 at 10:41










    • Then you just have to reset the appropriate graph, not just the default one
      – Daniel GL
      Nov 8 at 10:48

















    up vote
    1
    down vote













    I just removed the with statement (probably some tf code), and I don't see any leak. I believe there is a difference between the keras session and the tf default session. So you were not clearing the correct session with K.clear_session(). Probably using tf.reset_default_graph() could work too.



    while True: 
    y_true = K.variable(rand(36, 6))
    y_pred = K.variable(rand(36, 6))

    val_loss = K.eval(binary_crossentropy(y_true, y_pred))
    rec_loss = K.eval(mean_squared_error(y_true, y_pred))

    K.clear_session()
    gc.collect()

    sleep(0.1)





    share|improve this answer





















    • I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
      – mladenk
      Nov 8 at 10:41










    • Then you just have to reset the appropriate graph, not just the default one
      – Daniel GL
      Nov 8 at 10:48















    up vote
    1
    down vote










    up vote
    1
    down vote









    I just removed the with statement (probably some tf code), and I don't see any leak. I believe there is a difference between the keras session and the tf default session. So you were not clearing the correct session with K.clear_session(). Probably using tf.reset_default_graph() could work too.



    while True: 
    y_true = K.variable(rand(36, 6))
    y_pred = K.variable(rand(36, 6))

    val_loss = K.eval(binary_crossentropy(y_true, y_pred))
    rec_loss = K.eval(mean_squared_error(y_true, y_pred))

    K.clear_session()
    gc.collect()

    sleep(0.1)





    share|improve this answer












    I just removed the with statement (probably some tf code), and I don't see any leak. I believe there is a difference between the keras session and the tf default session. So you were not clearing the correct session with K.clear_session(). Probably using tf.reset_default_graph() could work too.



    while True: 
    y_true = K.variable(rand(36, 6))
    y_pred = K.variable(rand(36, 6))

    val_loss = K.eval(binary_crossentropy(y_true, y_pred))
    rec_loss = K.eval(mean_squared_error(y_true, y_pred))

    K.clear_session()
    gc.collect()

    sleep(0.1)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 10:25









    Daniel GL

    698316




    698316












    • I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
      – mladenk
      Nov 8 at 10:41










    • Then you just have to reset the appropriate graph, not just the default one
      – Daniel GL
      Nov 8 at 10:48




















    • I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
      – mladenk
      Nov 8 at 10:41










    • Then you just have to reset the appropriate graph, not just the default one
      – Daniel GL
      Nov 8 at 10:48


















    I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
    – mladenk
    Nov 8 at 10:41




    I know that removing the with statement solves the problem but I am afraid I cannot omit it because of the different graphs I have. Regardless, thanks and I'll try with tf.reset_default_graph().
    – mladenk
    Nov 8 at 10:41












    Then you just have to reset the appropriate graph, not just the default one
    – Daniel GL
    Nov 8 at 10:48






    Then you just have to reset the appropriate graph, not just the default one
    – Daniel GL
    Nov 8 at 10:48














    up vote
    0
    down vote













    At the end, what I did was remove the K.variable() code out of the where statement. In that way, the variables are part of the default graph which is later cleared by K.clear_session().






    share|improve this answer

























      up vote
      0
      down vote













      At the end, what I did was remove the K.variable() code out of the where statement. In that way, the variables are part of the default graph which is later cleared by K.clear_session().






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        At the end, what I did was remove the K.variable() code out of the where statement. In that way, the variables are part of the default graph which is later cleared by K.clear_session().






        share|improve this answer












        At the end, what I did was remove the K.variable() code out of the where statement. In that way, the variables are part of the default graph which is later cleared by K.clear_session().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 11:18









        mladenk

        12




        12






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204747%2fkeras-variable-memory-leak%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)