ml4a / ml4a

A python library and collection of notebooks for making art with machine learning.

Home Page:https://ml4a.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adjustments to torch-dreams example

genekogan opened this issue · comments

For torch-dreams example.

Some adjustments that would be nice:

  1. make possible to pass image directly (via ml4a.image.load_image) into config or into the dreamer function, instead of having to save it to disk first. Like in the deepdream and neura_style examples.

  2. put make_custom_func into the wrapper (models/torch-dreams.py). Best to abstract the low-level stuff.

  3. use ml4a native stuff to save/display images to keep things consistent between repos. I've already started that in the example, although i noticed some noisy artifacts that didn't go away with np.clip, maybe you can figure out what's going on there @Mayukhdeb.

  1. This seems like a good idea for consistency, I'll first have to figure out how ml4a.image.load_image is different from load_image that's being used in torch_dreams.

  2. make_custom_func works only for selecting individual channels or layers (one at a time,and not blending/combining them, which is also possible if the user wants). Anyways I'll move it into torch_dreams.utils for easy access.

  3. Will take a closer look on this first

Feel free to assign me to this issue 👍

  1. It just outputs a PIL.Image unless to_numpy is set to True, in which case it's converted to a numpy array.

    ml4a/ml4a/image.py

    Lines 29 to 55 in 0fed58b

    def load_image(img, image_size=None, to_numpy=False, normalize=False, autocrop=False):
    if isinstance(img, str):
    if is_url(img):
    img = url_to_image(img)
    if img is None:
    print("Error: no image returned")
    return None
    elif os.path.exists(img):
    img = Image.open(img).convert('RGB')
    else:
    raise ValueError('no image found at %s'%img)
    elif isinstance(img, np.ndarray):
    img = Image.fromarray(img.astype(np.uint8)).convert('RGB')
    if image_size is not None and isinstance(image_size, tuple):
    if autocrop:
    aspect = float(image_size[0])/image_size[1]
    img = crop_to_aspect_ratio(img, aspect)
    img = resize(img, image_size)
    elif image_size is not None and not isinstance(image_size, tuple):
    aspect = get_aspect_ratio(img)
    image_size = (int(aspect * image_size), image_size)
    img = resize(img, image_size)
    if to_numpy:
    img = np.array(img)
    if normalize:
    img = img / 255.0
    return img
  2. for ml4a.models.deepdream, the user puts the desired objective (whether one or multiple channels or layers) into the config, and the model does the rest internally. This is for the sake of abstraction, to make it a higher-level API. The best of both worlds is to allow this high-level abstraction, but to also make sure that the API allows you access to the low-level stuff as well, like you have, as a "power user" might want to use them in ways that we can't predict.
  1. make possible to pass image directly (via ml4a.image.load_image) into config or into the dreamer function, instead of having to save it to disk first.

Quick update:
I made some more changes to torch-dreams v1.1.0 to load make it possible to load images either from the path or as a numpy.ndarray.

It works on the config as:

config = {
    "image": your_numpy_array,       # could also be: np.array(your_PIL_image)
    "layers": [layer1, layer2],
    # others params 
}

I'm planning to make a universal load_image function that works on urls, image paths, numpy arrays and PIL images (similar to the one you made).