Skip to main content

Represent an image as a stack of different sized tensors

Project description

ImStack

A quick and easy way to represent images as a stack of optimizable layers.

Optimizing the pixel values of an image to minimize some loss is common in some applications like style transfer. But because a change to any one pixel doesn't affect much of the image, results are often noisy and slow. By representing an image as a stack of layers at different resolutions, we get parameters that affect a large part of the image (low-res layers) as well as some that can encode fine detail (the high-res layers). There are better ways to do this, but I found myself using this approach enough that I decided to turn it into a proper library.

Here's a colab notebook showing this in action, generating images to match a CLIP prompt.

Install

This package is available on pypi so install should be as easy as:

pip install imstack

How to use

We create a new image stack like so:

ims = ImStack(n_layers=3)

By default, the first layer is 32x32 pixels and each subsequent layer is 2x larger. We can visualize the layers with:

ims.plot_layers()

png

The parameters (pixels) of the layers are set to requires_grad=True, so you can pass the layers to an optimizer with something like optimizer = optim.Adam(ims.layers, lr=0.1, weight_decay=1e-4) to modify them based on some loss. Calling the forward pass (image = ims()) returns a tensor representation of the combined image, suitable for various pytorch operations.

For convenience, you can also get a PIL Image for easy viewing with:

ims.to_pil()

png

Loading images into an ImStack

You don't need to start from scratch - pass in a PIL image and the ImStack will be initialized such that the layers combine to re-create the input image as closely as possible.

from PIL import Image

# Load the input image
input_image = Image.open('demo_image.png')
input_image

png

Note how the lower layers capture broad shapes while the final layer is mostly fine detail.

# Create an image stack with init_image=input_image and plot the layers
ims_w_init = ImStack(n_layers=3, base_size=16, scale=4, out_size=256, init_image=input_image)
ims_w_init.plot_layers()

png

Examples

Text-to-image with ImStack+CLIP (colab link)

More coming soon :)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imstack-0.0.6.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imstack-0.0.6-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file imstack-0.0.6.tar.gz.

File metadata

  • Download URL: imstack-0.0.6.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for imstack-0.0.6.tar.gz
Algorithm Hash digest
SHA256 940346d440781d182f87505e190466afde4a49e2fadea1b59d70a921875cb9ba
MD5 2073262197b2a7837d43c548c3ed8b97
BLAKE2b-256 c933b0f1795d02b516672781055c8037f4eab1c773d98973d4c7882993fb0f43

See more details on using hashes here.

File details

Details for the file imstack-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: imstack-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for imstack-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 dd330df8d873e417c469353194bbc3d5e579b47882edcc4f074ba66e09e6e8f3
MD5 a575499e97248178b1014b1cf0587cb1
BLAKE2b-256 8a2cbe9bc27773e8b5525a87f0d9ae7e5abd3fa55f989addad52334e3e59fa85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page