-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel.py
271 lines (233 loc) · 8.7 KB
/
model.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import torch
import textwrap
from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler
from transformers import CLIPTokenizer, CLIPTextModel
from huggingface_hub import hf_hub_download
from config_sd import BUFFER_SIZE, PRETRAINED_MODEL_NAME_OR_PATH
from utils import NUM_BUCKETS
from huggingface_hub import upload_folder
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card
from safetensors.torch import save_file, load_file
import json
def get_ft_vae_decoder() -> AutoencoderKL:
"""
Based on the original GameNGen code, the vae decoder is finetuned on images from the
training set to improve the quality of the images.
"""
return AutoencoderKL.from_pretrained("arnaudstiegler/game-n-gen-vae-finetuned")
def get_model(
action_embedding_dim: int, skip_image_conditioning: bool = False
) -> tuple[
UNet2DConditionModel,
AutoencoderKL,
torch.nn.Embedding,
DDIMScheduler,
CLIPTokenizer,
CLIPTextModel,
]:
"""
Args:
action_embedding_dim: the dimension of the action embedding, i.e the number of possible actions + 1 (do nothing action)
skip_image_conditioning: whether to skip image conditioning
"""
# This will be used to encode the actions
action_embedding = torch.nn.Embedding(
num_embeddings=action_embedding_dim + 1, embedding_dim=768
)
torch.nn.init.normal_(action_embedding.weight, mean=0.0, std=0.02)
# DDIM scheduler allows for v-prediction and less sampling steps
noise_scheduler = DDIMScheduler.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="scheduler"
)
# This is what the paper uses
noise_scheduler.register_to_config(prediction_type="v_prediction")
vae = get_ft_vae_decoder()
unet = UNet2DConditionModel.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="unet"
)
# There are 10 noise buckets total
unet.register_to_config(num_class_embeds=NUM_BUCKETS)
# We do not use .add_module() because the class_embedding is already initialized as None
unet.class_embedding = torch.nn.Embedding(
NUM_BUCKETS, unet.time_embedding.linear_2.out_features
)
tokenizer = CLIPTokenizer.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="tokenizer"
)
text_encoder = CLIPTextModel.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="text_encoder"
)
if not skip_image_conditioning:
# This is to accomodate concatenating previous frames in the channels dimension
new_in_channels = 4 * (BUFFER_SIZE + 1)
new_conv_in = torch.nn.Conv2d(
new_in_channels, 320, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)
)
torch.nn.init.xavier_uniform_(new_conv_in.weight)
torch.nn.init.zeros_(new_conv_in.bias)
# Replace the conv_in layer
unet.conv_in = new_conv_in
# Have to account for BUFFER SIZE conditioning frames + 1 for the noise
unet.config["in_channels"] = new_in_channels
unet.requires_grad_(True)
vae.requires_grad_(False)
text_encoder.requires_grad_(False)
return unet, vae, action_embedding, noise_scheduler, tokenizer, text_encoder
def load_embedding_info_dict(model_folder: str) -> dict:
if os.path.exists(model_folder):
with open(os.path.join(model_folder, "embedding_info.json"), "r") as f:
embedding_info = json.load(f)
else:
file_path = hf_hub_download(
repo_id=model_folder, filename="embedding_info.json", repo_type="model"
)
with open(file_path, "r") as f:
embedding_info = json.load(f)
return embedding_info
def load_action_embedding(
model_folder: str, action_num_embeddings: int
) -> torch.nn.Embedding:
action_embedding = torch.nn.Embedding(
num_embeddings=action_num_embeddings, embedding_dim=768
)
if os.path.exists(model_folder):
action_embedding.load_state_dict(
load_file(os.path.join(model_folder, "action_embedding_model.safetensors"))
)
else:
file_path = hf_hub_download(
repo_id=model_folder,
filename="action_embedding_model.safetensors",
repo_type="model",
)
action_embedding.load_state_dict(load_file(file_path))
return action_embedding
def load_model(
model_folder: str, device: torch.device | None = None
) -> tuple[
UNet2DConditionModel,
AutoencoderKL,
torch.nn.Embedding,
DDIMScheduler,
CLIPTokenizer,
CLIPTextModel,
]:
"""
Load a model from the hub
Args:
model_folder: the folder to load the model from, can be a model id or a local folder
"""
embedding_info = load_embedding_info_dict(model_folder)
action_embedding = load_action_embedding(
model_folder=model_folder,
action_num_embeddings=embedding_info["num_embeddings"],
)
noise_scheduler = DDIMScheduler.from_pretrained(
model_folder, subfolder="noise_scheduler"
)
vae = get_ft_vae_decoder()
unet = UNet2DConditionModel.from_pretrained(model_folder, subfolder="unet")
assert (
noise_scheduler.config.prediction_type == "v_prediction"
), "Noise scheduler prediction type should be 'v_prediction'"
assert (
unet.config.num_class_embeds == NUM_BUCKETS
), f"UNet num_class_embeds should be {NUM_BUCKETS}"
# Unaltered
tokenizer = CLIPTokenizer.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="tokenizer"
)
text_encoder = CLIPTextModel.from_pretrained(
PRETRAINED_MODEL_NAME_OR_PATH, subfolder="text_encoder"
)
if device:
unet = unet.to(device)
vae = vae.to(device)
action_embedding = action_embedding.to(device)
text_encoder = text_encoder.to(device)
return unet, vae, action_embedding, noise_scheduler, tokenizer, text_encoder
def save_model(
output_dir: str,
unet: UNet2DConditionModel,
vae: AutoencoderKL,
noise_scheduler: DDIMScheduler,
action_embedding: torch.nn.Embedding,
) -> None:
unet.save_pretrained(os.path.join(output_dir, "unet"))
vae.save_pretrained(os.path.join(output_dir, "vae"))
noise_scheduler.save_pretrained(os.path.join(output_dir, "noise_scheduler"))
save_file(
action_embedding.state_dict(),
os.path.join(output_dir, "action_embedding_model.safetensors"),
)
# Save embedding dimensions
embedding_info = {
"num_embeddings": action_embedding.num_embeddings,
"embedding_dim": action_embedding.embedding_dim,
}
with open(os.path.join(output_dir, "embedding_info.json"), "w") as f:
json.dump(embedding_info, f)
def save_and_maybe_upload_to_hub(
repo_id: str,
output_dir: str,
unet: UNet2DConditionModel,
vae: AutoencoderKL,
noise_scheduler: DDIMScheduler,
action_embedding: torch.nn.Embedding,
should_upload_to_hub: bool = True,
images: list = None,
dataset_name: str = None,
) -> None:
save_model(output_dir, unet, vae, noise_scheduler, action_embedding)
if should_upload_to_hub:
try:
upload_folder(
repo_id=repo_id,
folder_path=output_dir,
commit_message="End of training",
ignore_patterns=["step_*", "epoch_*"],
)
save_model_card(
repo_id=repo_id,
images=images,
base_model=PRETRAINED_MODEL_NAME_OR_PATH,
dataset_name=dataset_name,
repo_folder=output_dir,
)
except Exception as e:
print(f"Error uploading to hub: {e}")
def save_model_card(
repo_id: str,
images: list = None,
base_model: str = None,
dataset_name: str = None,
repo_folder: str = None,
):
img_str = ""
if images is not None:
for i, image in enumerate(images):
image.save(os.path.join(repo_folder, f"image_{i}.png"))
img_str += f"![img_{i}](./image_{i}.png)\n"
model_description = textwrap.dedent(f"""
# GameNgen fine-tuning - {repo_id}
Full finetune of {base_model}. The weights were fine-tuned on the {dataset_name} dataset. You can find some example images in the following. \n
{img_str}
""")
model_card = load_or_create_model_card(
repo_id_or_path=repo_id,
from_training=True,
license="creativeml-openrail-m",
base_model=base_model,
model_description=model_description,
inference=True,
)
tags = [
"stable-diffusion",
"stable-diffusion-diffusers",
"text-to-image",
"diffusers",
"diffusers-training",
]
model_card = populate_model_card(model_card, tags=tags)
model_card.save(os.path.join(repo_folder, "README.md"))