question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pipeline: obtain per-image reproducibility seeds when generating multiple images

See original GitHub issue

People are interested in the following workflow: generate a few images from a prompt, select the one you like and tweak the prompt to steer it towards your goal. There are many examples in Twitter and the Discord bot, see context section below.

I managed to replicate this workflow in a demo Space generating one image at a time, and manually setting the seed before each generation. This is sub-optimal, especially when considering high-load services that need to batch requests from several users in order to run them in parallel.

Given that the pipeline is already designed to receive a Generator, I’d like to explore if we can (optionally) return per-image seeds as part of the generation process.

Describe the solution you’d like I’d like the following to work:

ldm = DiffusionPipeline.from_pretrained(MODEL_ID)
rng = torch.manual_seed(1337)
prompts = ["Labrador in the style of Vermeer"] * 6
preds = ldm(prompts, generator=rng, return_seeds=True)
images, seeds = preds["sample"], preds["seed"]

rng.manual_seed(seeds[3])
tweaked_image = ldm("Labrador in the style of Hokusai", generator=rng)

The same thing should work when batching prompts from several users.

Perhaps something like this is already possible and I haven’t looked enough.

One idea is to iterate here instead of creating the latents all at once. It would have a performance penalty, but I presume it should be negligible compared to the rest of the process. A drawback of this approach is that we’d need to do it for every pipeline, or try to create a helper.

Describe alternatives you’ve considered

  • Do nothing, and use the low-level API instead of the pipeline to achieve this goal (I think it should be possible, I plan to test it later). Implementing it in the pipeline would just make it easier for some users to experiment on their own.
  • Iterate to obtain just a single image per generation. This wastes GPU memory and is slower in most cases. But it might not be a problem for users that just want to experiment.

Additional context

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pcuencacommented, Aug 25, 2022

Fixed in #247.

1reaction
spychiatristcommented, Aug 18, 2022

Coming here from the HuggingFace comments. I’ve implemented a simple workaround for the non-determinism: sequential seeding (for n_iter) now with sub-seed offset (for n_samples). One new parameter --seed_offset allows you to deterministically shift which sample-subseed is selected.

Here’s an example of deterministically shifting down and right in the matrix (previously impossible):

--n_samples 3 --n_iter 3 --seed 42 --seed_offset 0 --n_samples 2 --n_iter 2 --seed 43 --seed_offset 1

See this commit (and priors) on my fork of the main repo

Other relevant commit w/ simple sequential seeding

Read more comments on GitHub >

github_iconTop Results From Across the Web

Latents / seeds are a mess. Make it easier to replicate ... - GitHub
This solution would do two things: make all generated images in a batch directly replicable (across all pipes and all future pipes), and;...
Read more >
Random Seeds and Reproducibility - Towards Data Science
A sequence of states and corresponding generated numbers for seed #42. Image by author. You can retrieve (and set) that state if you...
Read more >
Properly Setting the Random Seed in ML Experiments. Not as ...
In this post, we explore areas where randomness appears in machine learning and how to achieve reproducible, deterministic, and more ...
Read more >
Training instance segmentation neural network with synthetic ...
Our attempt is based on the concept of domain randomization, where a large amount of image is generated by randomly orienting the seed...
Read more >
A novel image-based approach for soybean seed ... - bioRxiv
house image processing pipeline, which enabled a full ... Through images of seeds obtained by different optical sensors, it is possible to.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found