forked from contrebande-labs/charred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.py
155 lines (125 loc) · 3.79 KB
/
validation.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import jax
from flax.training.common_utils import shard
import jax.numpy as jnp
from monitoring import wandb_init, wandb_log_validation, wandb_close
from diffusers import (
FlaxAutoencoderKL,
FlaxDPMSolverMultistepScheduler,
FlaxStableDiffusionPipeline,
FlaxUNet2DConditionModel,
)
from transformers import ByT5Tokenizer, FlaxT5ForConditionalGeneration
def predict(
pipeline: FlaxStableDiffusionPipeline,
tokenizer,
rng,
validation_prompts: list[str],
num_inference_steps: int,
):
output_images = []
for validation_prompt in validation_prompts:
text_input_ids = tokenizer(
text=validation_prompt,
max_length=1024,
padding="max_length",
truncation=True,
return_tensors="np",
).input_ids[0]
output_images.append(
pipeline.numpy_to_pil(
pipeline(
params={},
prompt_ids=text_input_ids,
prng_seed=rng,
num_inference_steps=num_inference_steps,
jit=True,
).images.reshape(
(output_images.shape[0] * output_images.shape[1],)
+ output_images.shape[-3:]
)
)
)
return output_images
def log_validate(
pipeline: FlaxStableDiffusionPipeline,
tokenizer,
rng,
validation_prompts: list[str],
validation_images: list,
num_inference_steps: int,
):
predicted_images = predict(
pipeline,
tokenizer,
rng,
validation_prompts,
num_inference_steps,
)
image_logs = []
for i, predicted_image in enumerate(predicted_images):
image_logs.append(
{
"validation_image": validation_images[i]
if validation_images is not None and i < len(validation_images)
else None,
"images": predicted_image,
"validation_prompt": validation_prompts[i],
}
)
wandb_log_validation(image_logs)
def get_inference_log_validate_lambda(pretrained_unet_path, seed):
tokenizer = ByT5Tokenizer()
language_model = FlaxT5ForConditionalGeneration.from_pretrained(
"google/byt5-base",
dtype=jnp.bfloat16,
)
vae, _ = FlaxAutoencoderKL.from_pretrained(
"flax/stable-diffusion-2-1",
subfolder="vae",
dtype=jnp.bfloat16,
)
unet, _ = FlaxUNet2DConditionModel.from_pretrained(
pretrained_unet_path,
dtype=jnp.bfloat16,
)
scheduler = FlaxDPMSolverMultistepScheduler.from_config(
config={
"_diffusers_version": "0.16.0",
"beta_end": 0.012,
"beta_schedule": "scaled_linear",
"beta_start": 0.00085,
"clip_sample": False,
"num_train_timesteps": 1000,
"prediction_type": "v_prediction",
"set_alpha_to_one": False,
"skip_prk_steps": True,
"steps_offset": 1,
"trained_betas": None,
}
)
pipeline = FlaxStableDiffusionPipeline(
tokenizer=tokenizer,
text_encoder=language_model.encode,
vae=vae,
unet=unet,
scheduler=scheduler,
feature_extractor=None,
safety_checker=None,
)
rng = jax.random.PRNGKey(seed)
return (
lambda validation_prompts, validation_images, num_inference_steps: log_validate(
pipeline,
tokenizer,
rng,
validation_prompts,
validation_images,
num_inference_steps,
)
)
if __name__ == "__main__":
wandb_init(None)
get_inference_log_validate_lambda("character-aware-diffusion/charred", 87)(
["a running shoe"], None, 20
)
wandb_close()