Download generate.py from StarpowerTechnology/WVY-Image-Composition-101: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/StarpowerTechnology/WVY-Image-Composition-101/resolve/main/generate.py
- Command line
-
hf download hf://StarpowerTechnology/WVY-Image-Composition-101/generate.py
-
curl -L -o generate.py https://huggingface.co/StarpowerTechnology/WVY-Image-Composition-101/resolve/main/generate.py
1.52 kB
| """ | |
| Generate an image from a prompt with WVY-Image-Composition-101. | |
| python3 generate.py "a cat on top of a dog on a green background" | |
| python3 generate.py "a fish next to a pig on a purple background" -o fish_pig.png | |
| Prompts must follow one of the two sentence shapes it was trained on: | |
| a <animal> on top of a <animal> on a <color> background | |
| a <animal> next to a <animal> on a <color> background | |
| animals: dog cat bird fish pig rabbit | |
| colors: white black red orange yellow green blue purple | |
| Needs only numpy + Pillow. | |
| """ | |
| import sys, json, argparse | |
| import numpy as np | |
| from PIL import Image | |
| import model as M | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("prompt") | |
| ap.add_argument("-o", "--out", default="out.png") | |
| ap.add_argument("--scale", type=int, default=8, help="pixel upscale for viewing (model draws 48x48)") | |
| a = ap.parse_args() | |
| vocab = json.load(open("train_log.json"))["vocab"] | |
| w2i = {w: i for i, w in enumerate(vocab)} | |
| words = a.prompt.lower().strip().split() | |
| unknown = [w for w in words if w not in w2i] | |
| if unknown or len(words) > M.L: | |
| sys.exit(f"Can't read that prompt. Unknown words: {unknown}. Use: a <animal> on top of / next to a <animal> on a <color> background") | |
| T = np.zeros((1, M.L), dtype=np.int64); T[0, :len(words)] = [w2i[w] for w in words] | |
| p = dict(np.load("wvy_compose_weights.npz")) | |
| y = M.forward(p, T)[0].reshape(48, 48, 3) | |
| Image.fromarray((np.clip(y, 0, 1) * 255).astype("uint8")).resize((48 * a.scale, 48 * a.scale), Image.NEAREST).save(a.out) | |
| print("saved", a.out) | |