Error while allocating memory - Keras/TF

Hello. I have pre-trained VGG16 net with 7 classes. When I was using tensorflow without GPU I was achieving about 3s per one image classification. Now I am trying to boost it with GPU, but I found a small problem. I’m getting errors:

“W tensorflow/core/common_runtime/bfc_allocator.cc:219] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.79GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.”

I tought TX1 has about 4GB memory. When I am looking into Tensorflow informations at startup:

“totalMemory: 3.89GiB freeMemory: 2.24GiB”

Why is the freeMemory so low?

My script is:

from socket import AF_INET, SOCK_DGRAM, socket
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
from keras import optimizers
from keras import backend as K
from keras.backend.tensorflow_backend import set_session
from keras.backend.tensorflow_backend import clear_session
from keras.backend.tensorflow_backend import get_session
import tensorflow
import gc

# Reset Keras Session
def reset_keras():
    sess = get_session()
    clear_session()
    sess.close()
    sess = get_session()

    try:
        del model # this is from global space - change this as you need
    except:
        pass

    print(gc.collect()) # if it's done something you should see a number being outputted

    # use the same config as you used to create the session
    config = tensorflow.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 1
    config.gpu_options.visible_device_list = "0"
    set_session(tensorflow.Session(config=config))

reset_keras()
K.clear_session()

# dimensions of our images
img_width, img_height = 454, 227

# load the model we saved
model = load_model('Models/vgg16_9.h5')
model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.Adam(lr=1e-4),
              metrics=['acc'])

print(model.summary())

# predicting images
test_image = image.load_img('Photos/test.png', target_size = (227, 454))
test_image = image.img_to_array(test_image)
test_image = test_image/255
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image, batch_size=1, verbose=1)
y_pred = np.argmax(result, axis = 1)
test_image = image.load_img('Photos/test.png', target_size = (227, 454))
test_image = image.img_to_array(test_image)
test_image = test_image/255
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image, batch_size=1, verbose=1)
y_pred = np.argmax(result, axis = 1)
test_image = image.load_img('Photos/test.png', target_size = (227, 454))
test_image = image.img_to_array(test_image)
test_image = test_image/255
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image, batch_size=1, verbose=1)
y_pred = np.argmax(result, axis = 1)

Is there a way I can achieve better times?

Hi,

Could you try to enable the ‘allow_growth’ flag of TensorFlow first?

config = tf.ConfigProto()
config.gpu_options.allow_growth = True

session = tf.Session(config=config, ...)

Thanks.

Sorry for late response. The allow_growth didn’t help, still got allocation run out of memory. It even displayed 4 warnings instead of 2 if that matters.

Hi,

You may really run out of memory.

Try to check the physical memory usage with tegrastats first:

sudo ./tegrastats

Thanks.