models icon indicating copy to clipboard operation
models copied to clipboard

VRAM/GPU memory allocation for tensors is larger than expected.

Open maxter2323 opened this issue 6 years ago • 1 comments

Dear Tensorflow Team,

I have an issue with VRAM/GPU memory allocation. I tried to google this problem but I didnt find anything.

I was making a model which takes 128x128x1 images (greyscale pngs) as input. I was training this model on 20 000 dataset. However when I decided to try 100 000 dataset I encountered OOM(out of memory exception).

While debugging this issue I found out that the problem was occuring on a data loading step.(this is why the model code is not included below).

I am not an expert in Python and Tensorflow but I see two problems here:

  1. I believe that VRAM allocations are enormously huge for the data I am using, as 100000x128x128x1 bytes should take around 1.638 GB of VRAM, but instead it takes way more that that. I was using GPU-Z tool for profiling and it seems like it needs way more than 4 GB. And I not using any training or model code, I am just creating tensors here.
  2. It seems that when I try to load images from disk and transform them into tensors for training, tensors are allocated both on VRAM and RAM. Is this a correct behaviour?

Specs:

Python: 3.6.7 CUDA: 10.1 Tensorflow: 2.3.0 GPU: NVIDIA 2070 RTX SUPER 8 GB OS: Windows 10

Code(change data_dir variable to any valid image folder):

import tensorflow as tf
import pathlib

def allowGrowth():
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            for gpu in gpus:
                print(gpu)
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
        except RuntimeError as e:
            print(e)

def get_images():
    data_dir = "dirToPngs"
    data_dir = pathlib.Path(data_dir)
    pathList = list(data_dir.glob('*.png'))
    listOfImages = []
    for p in pathList:

        img = tf.io.read_file(str(p))
        img = tf.image.decode_png(img, channels=1)
        img = tf.image.resize(img, [128, 128])
        img = tf.cast(img, tf.float32) / 255.0

        listOfImages.append(img)

    tensor = tf.convert_to_tensor(listOfImages, dtype=tf.float32)
    return tensor

allowGrowth()
get_images()

maxter2323 avatar Sep 21 '20 10:09 maxter2323

@maxter2323 Could you please raise the issue in Tensorflow repo since its not related to models.Thanks!

saikumarchalla avatar Sep 23 '20 03:09 saikumarchalla