Text-to-Image
Diffusers
Safetensors
English
Chinese
flow-matching
qwen-image
ascend
lora
recursive-self-improvement
Instructions to use DALabCommunity/Haidass-Image-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use DALabCommunity/Haidass-Image-Instruct with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("DALabCommunity/Haidass-Image-Base-v1_0.6B_512px", dtype=torch.bfloat16, device_map="cuda") pipe.load_lora_weights("DALabCommunity/Haidass-Image-Instruct") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Download counting_head/sampling.py from DALabCommunity/Haidass-Image-Instruct: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/DALabCommunity/Haidass-Image-Instruct/resolve/main/counting_head/sampling.py
- Command line
-
hf download hf://DALabCommunity/Haidass-Image-Instruct/counting_head/sampling.py
-
curl -L -o sampling.py https://huggingface.co/DALabCommunity/Haidass-Image-Instruct/resolve/main/counting_head/sampling.py
3.07 kB
| """Step-five feature capture and continuation for the released count head.""" | |
| from collections import defaultdict | |
| import random | |
| import numpy as np | |
| from .count_n_rank import select_index | |
| PREFIX_STEPS = 5 | |
| PREFIX_GROUPS = (1, 3, 4) | |
| class FeatureCapture: | |
| def __init__(self, layer, torch): | |
| self.layer, self.torch = layer, torch | |
| self.step, self.handle, self.values = 0, None, None | |
| def begin(self, model, contexts, spatial): | |
| self.h, self.w = spatial | |
| self.starts = [] | |
| offset = 0 | |
| for idx, text in enumerate(contexts): | |
| if idx % 2 == 0: | |
| self.starts.append(offset + len(text)) | |
| offset += len(text) + self.h * self.w | |
| self.handle = model.dit.transformer_blocks[self.layer - 1].register_forward_hook(self.capture) | |
| def capture(self, _module, _args, value): | |
| if self.step != PREFIX_STEPS: | |
| return | |
| h, w = self.h, self.w | |
| tokens = self.torch.stack([value[idx:idx + h * w, 0] for idx in self.starts]) | |
| tiles = tokens.float().reshape(len(self.starts), 4, h // 4, 4, w // 4, -1) | |
| self.values = tiles.mean((2, 4)).to(self.torch.float16).cpu().numpy() | |
| def close(self): | |
| if self.handle is not None: | |
| self.handle.remove() | |
| def candidate_seeds(seed): | |
| seeds = [seed] | |
| rng = random.Random(seed) | |
| while len(seeds) < 8: | |
| candidate = rng.randrange(2**31) | |
| if candidate not in seeds: | |
| seeds.append(candidate) | |
| return seeds | |
| def choose_and_finish(generator, caption, seeds, target, emit): | |
| head = generator.count_head | |
| cache = defaultdict(list) | |
| start = 0 | |
| for width in PREFIX_GROUPS: | |
| tracker = FeatureCapture(head.layer, generator.torch) | |
| end = start + width | |
| try: | |
| latents, contexts = generator._render( | |
| [caption] * width, seeds[start:end], False, 50, stop_step=PREFIX_STEPS, | |
| capture=tracker, progress=lambda step: emit('prefix_step', step=step, steps=PREFIX_STEPS, | |
| first_candidate=start + 1, last_candidate=end)) | |
| finally: | |
| tracker.close() | |
| cache['latents'].extend(latents) | |
| cache['contexts'].extend(contexts) | |
| cache['features'].extend(tracker.values) | |
| for index in range(start, end): | |
| emit('prefix', candidate=index + 1, candidates_total=8, steps=PREFIX_STEPS) | |
| start = end | |
| scores = head.log_prob(np.asarray(cache['features']))[:, min(int(target), 7)] | |
| index = select_index(scores, seeds[0]) | |
| emit('selected', selected=index, seed=seeds[index]) | |
| state = ([cache['latents'][index]], cache['contexts'][2 * index:2 * index + 2]) | |
| latents, _ = generator._render([caption], [seeds[index]], False, 50, | |
| start_step=PREFIX_STEPS, resume=state, | |
| progress=lambda step: emit('step', step=step, steps=50)) | |
| return latents[0], index, [float(value) if np.isfinite(value) else None for value in scores] | |