uber-research / jpeg2dct

Other
258 stars 45 forks source link

Memory leakage #4

Closed ALEXKIRNAS closed 5 years ago

ALEXKIRNAS commented 5 years ago

Hi! I have tried to use your lib to train network on DCT inputs and bumped into memory leakage issue. I use Tensorflow operation to read DCT image from jpeg bytes. Here some useful info and dummy sample to reproduce the issue:

System information

Packages information

Code to reproduce

from io import BytesIO

import PIL.Image as Image
import numpy as np
import tensorflow as tf
from jpeg2dct.tensorflow import decode

def img_to_bytes(image):
    image_pil = Image.fromarray(np.uint8(image))
    output = BytesIO()
    image_pil.save(output, format='JPEG')

    return output.getvalue()

def _rgb_to_dct_image(jpegbytes):
    dct_y, dct_cb, dct_cr = decode(jpegbytes)
    return dct_y, tf.concat([dct_cb, dct_cr], axis=-1)

def batch_rgb_to_dct_image(inputs):
    dct_y_batch, dct_cb_cr_batch = tf.map_fn(
        fn=_rgb_to_dct_image,
        elems=inputs,
        dtype=(tf.int16, tf.int16)
    )

    return dct_y_batch, dct_cb_cr_batch

images = tf.placeholder(shape=(32,), dtype=tf.string)
dct_y_batch, dct_cb_cr_batch = batch_rgb_to_dct_image(images)

real_images = np.random.uniform(size=(32, 1080, 1920, 3), low=0, high=255).astype(np.uint8)
images_strings = list(map(img_to_bytes, real_images))

sess = tf.Session()
while True:
    _, _ = sess.run([dct_y_batch, dct_cb_cr_batch], feed_dict={images: images_strings})

After several minutes script will be killed by OS due to extremely large memory usage.

gueguenster commented 5 years ago

what version of Pillow are you using? Pillow==5.0.0 has a memory leak issue (https://github.com/python-pillow/Pillow/issues/2972)

gueguenster commented 5 years ago

There is a memory leak indeed in the tensorflow section. Thanks for pointing that out. It does not seem to appear in the numpy section, as replacing the "while True" with:

from jpeg2dct.numpy import loads
while True:
    loads(images_strings[0])

seems to be allright

gueguenster commented 5 years ago

a new version has been released 0.2.4. I did not notice any memory leak after that modification. Let us know if you still face the same problem with that new version.

ALEXKIRNAS commented 5 years ago

Seems that the issue is solved! Thanks for quick response.