Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use gpu limit #970

Open
mickey-kim opened this issue Nov 7, 2019 · 1 comment
Open

how to use gpu limit #970

mickey-kim opened this issue Nov 7, 2019 · 1 comment

Comments

@mickey-kim
Copy link

hello

i use face_recognition and i have some question
can i limit gpu usage??

@alessiosavi
Copy link

The problem is related to the high dimension of the image.

A simple and basic approach, can be resize the photo.
Instead of use the builtin method load_image_file, you can use mine:
https://github.com/alessiosavi/PyRecognizer/blob/50b9fe1e4676cb85aa9f88c15fbe988a258eec1d/utils/util.py#L286

It will simply resize the photo. You can tweak for you purpouse.

def load_image_file(file, mode='RGB',):
    """
    Loads an image file (.jpg, .png, etc) into a numpy array
    :param file: image file name or file object to load
    :param mode: format to convert the image to. Only 'RGB' (8-bit RGB, 3 channels) and 'L' (black and white) are supported.
    :return: image contents as numpy array
    """

    im = PIL.Image.open(file)
    width, height = im.size
    w, h = width, height
    log = logging.getLogger()

    ratio = -1
    # Ratio for resize the image
    log.debug("load_image_file | Image dimension: ({}:{})".format(w, h))
    # Resize in case of to bigger dimension
    if 1200 <= width <= 1600 or 1200 <= height <= 1600:
        ratio = 1/2
    elif 1600 <= width <= 3600 or 1600 <= height <= 3600:
        ratio = 1/3
    elif width > 3600 or height > 3600:
        if width > height:
            ratio = width/800
        else:
            ratio = height/800
        log.debug("Dimension: w: {} | h: {}".format(w, h))
        log.debug("new ratio -> {}".format(ratio))

    if 0 < ratio < 1:
        # Scale image in case of width > 1600
        w = width * ratio
        h = height * ratio
    elif ratio > 1:
        # Scale image in case of width > 3600
        w = width / ratio
        h = height / ratio
    if w != width:
        # Check if scaling was applied
        maxsize = (w, h)
        log.debug(
            "Image have to high dimension, avoiding memory error. Resizing to {}".format(maxsize))
        im.thumbnail(maxsize, PIL.Image.ANTIALIAS)

    if mode:
        im = im.convert(mode)
    return np.array(im), ratio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants