File size: 1,521 Bytes
39d8cda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
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)