Skip to main content

ImStack

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()

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()

Loading images into an ImStack

You don’t need to start from scratch - pass in a PIL image or a filename 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

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()

Examples

Text-to-image with ImStack+CLIP

Very fast text-to-image, using CLIP to calculate a loss that measures how well the image matches a text prompt. In this example, the prompt was ‘A watercolor painting of an underwater submarine’:

Image.open('clip_eg.png')

colab link

and a CLOOB version

Style Transfer

Simple style transfer, with an ImStack being optimized such that content loss to one image and style loss to another are minimized.

Image.open('style_tf_eg.png')

colab link

Release files for imstack 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imstack 0.1.8
File Size Uploaded
imstack-0.1.8.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for imstack 0.1.8
File Interpreter ABI Platform
imstack-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 21.7 kB

Release files / imstack-0.1.8.tar.gz

Download URL imstack-0.1.8.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fec6d8665e6a825a527b5e516ea6625bd81c8ef8f2d21d9219a146417cf24ff1
BLAKE2b-256 checksum
How to use checksums
2df7280c654d0fc4a9315c2e5763840dfd39f90f3344c89f91d77d9f5b94f10b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / imstack-0.1.8-py3-none-any.whl

Download URL imstack-0.1.8-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
107fc34aa7556586a38792a9a2f89910e6ff762aa01486e966f34e3415eb3c01
BLAKE2b-256 checksum
How to use checksums
078e05847fa0ab300b649d603c90d370ab712273f1327a16c269b81ceffb1e70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page