-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
263 lines (208 loc) · 9.51 KB
/
train_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
# #@markdown Check type of GPU and VRAM available.
# !nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader\
# 1. Install the required packages:
# !wget -q https://github.com/ShivamShrirao/diffusers/raw/main/examples/dreambooth/train_dreambooth.py
# !wget -q https://github.com/ShivamShrirao/diffusers/raw/main/scripts/convert_diffusers_to_original_stable_diffusion.py
# %pip install -qq git+https://github.com/ShivamShrirao/diffusers
# %pip install -q -U --pre triton
# %pip install -q accelerate transformers ftfy bitsandbytes==0.35.0 gradio natsort safetensors xformers
# 2. Login to HuggingFace
# #@title Login to HuggingFace 🤗
#
# #@markdown You need to accept the model license before downloading or using the Stable Diffusion weights. Please, visit the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5), read the license and tick the checkbox if you agree. You have to be a registered user in 🤗 Hugging Face Hub, and you'll also need to use an access token for the code to work.
# # https://huggingface.co/settings/tokens
# !mkdir -p ~/.huggingface
# HUGGINGFACE_TOKEN = "" #@param {type:"string"}
# !echo -n "{HUGGINGFACE_TOKEN}" > ~/.huggingface/token
# 3. Settings and run
# #@markdown Name/Path of the initial model.
MODEL_NAME = "runwayml/stable-diffusion-v1-5" #@param {type:"string"}
#
# #@markdown Enter the directory name to save model at.
OUTPUT_DIR = "/content/stable_diffusion_weights/zwx" #@param {type:"string"}
print(f"[*] Weights will be saved at {OUTPUT_DIR}")
# !mkdir -p $OUTPUT_DIR
# You can also add multiple concepts here. Try tweaking `--max_train_steps` accordingly.
concepts_list = [
{
"instance_prompt": "photo of zwx dog",
"class_prompt": "photo of a dog",
"instance_data_dir": "/content/data/zwx",
"class_data_dir": "/content/data/dog"
},
# {
# "instance_prompt": "photo of ukj person",
# "class_prompt": "photo of a person",
# "instance_data_dir": "/content/data/ukj",
# "class_data_dir": "/content/data/person"
# }
]
# `class_data_dir` contains regularization images
import json
import os
for c in concepts_list:
os.makedirs(c["instance_data_dir"], exist_ok=True)
with open("concepts_list.json", "w") as f:
json.dump(concepts_list, f, indent=4)
#@markdown Upload your images by running this cell.
#@markdown OR
#@markdown You can use the file manager on the left panel to upload (drag and drop) to each `instance_data_dir` (it uploads faster). You can also upload your own class images in `class_data_dir` if u don't wanna generate with SD.
import os
import shutil
for c in concepts_list:
print(f"Uploading instance images for `{c['instance_prompt']}`")
uploaded = files.upload()
for filename in uploaded.keys():
dst_path = os.path.join(c['instance_data_dir'], filename)
shutil.move(filename, dst_path)
# !python3 train_dreambooth.py \
# --pretrained_model_name_or_path=$MODEL_NAME \
# --pretrained_vae_name_or_path="stabilityai/sd-vae-ft-mse" \
# --output_dir=$OUTPUT_DIR \
# --revision="fp16" \
# --with_prior_preservation --prior_loss_weight=1.0 \
# --seed=1337 \
# --resolution=512 \
# --train_batch_size=1 \
# --train_text_encoder \
# --mixed_precision="fp16" \
# --use_8bit_adam \
# --gradient_accumulation_steps=1 \
# --learning_rate=1e-6 \
# --lr_scheduler="constant" \
# --lr_warmup_steps=0 \
# --num_class_images=50 \
# --sample_batch_size=4 \
# --max_train_steps=800 \
# --save_interval=10000 \
# --save_sample_prompt="photo of zwx dog" \
# --concepts_list="concepts_list.json"
# Reduce the `--save_interval` to lower than `--max_train_steps` to save weights from intermediate steps.
# `--save_sample_prompt` can be same as `--instance_prompt` to generate intermediate samples (saved along with weights in samples directory).
#@markdown Specify the weights directory to use (leave blank for latest)
WEIGHTS_DIR = "" #@param {type:"string"}
if WEIGHTS_DIR == "":
from natsort import natsorted
from glob import glob
import os
WEIGHTS_DIR = natsorted(glob(OUTPUT_DIR + os.sep + "*"))[-1]
print(f"[*] WEIGHTS_DIR={WEIGHTS_DIR}")
# @markdown Run to generate a grid of preview images from the last saved weights.
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
weights_folder = OUTPUT_DIR
folders = sorted([f for f in os.listdir(weights_folder) if f != "0"], key=lambda x: int(x))
row = len(folders)
col = len(os.listdir(os.path.join(weights_folder, folders[0], "samples")))
scale = 4
fig, axes = plt.subplots(row, col, figsize=(col * scale, row * scale), gridspec_kw={'hspace': 0, 'wspace': 0})
for i, folder in enumerate(folders):
folder_path = os.path.join(weights_folder, folder)
image_folder = os.path.join(folder_path, "samples")
images = [f for f in os.listdir(image_folder)]
for j, image in enumerate(images):
if row == 1:
currAxes = axes[j]
else:
currAxes = axes[i, j]
if i == 0:
currAxes.set_title(f"Image {j}")
if j == 0:
currAxes.text(-0.1, 0.5, folder, rotation=0, va='center', ha='center', transform=currAxes.transAxes)
image_path = os.path.join(image_folder, image)
img = mpimg.imread(image_path)
currAxes.imshow(img, cmap='gray')
currAxes.axis('off')
plt.tight_layout()
plt.savefig('grid.png', dpi=72)
#@markdown Run conversion.
ckpt_path = WEIGHTS_DIR + "/model.ckpt"
half_arg = ""
#@markdown Whether to convert to fp16, takes half the space (2GB).
fp16 = True #@param {type: "boolean"}
if fp16:
half_arg = "--half"
#!python convert_diffusers_to_original_stable_diffusion.py --model_path $WEIGHTS_DIR --checkpoint_path $ckpt_path $half_arg
print(f"[*] Converted ckpt saved at {ckpt_path}")
# 7 . Inference
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline, DDIMScheduler
from IPython.display import display
model_path = WEIGHTS_DIR # If you want to use previously trained model saved in gdrive, replace this with the full path of model in gdrive
pipe = StableDiffusionPipeline.from_pretrained(model_path, safety_checker=None, torch_dtype=torch.float16).to("cuda")
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe.enable_xformers_memory_efficient_attention()
g_cuda = None
#@markdown Can set random seed here for reproducibility.
g_cuda = torch.Generator(device='cuda')
seed = 52362 #@param {type:"number"}
g_cuda.manual_seed(seed)
#@title Run for generating images.
prompt = "photo of zwx dog in a bucket" #@param {type:"string"}
negative_prompt = "" #@param {type:"string"}
num_samples = 4 #@param {type:"number"}
guidance_scale = 7.5 #@param {type:"number"}
num_inference_steps = 24 #@param {type:"number"}
height = 512 #@param {type:"number"}
width = 512 #@param {type:"number"}
with autocast("cuda"), torch.inference_mode():
images = pipe(
prompt,
height=height,
width=width,
negative_prompt=negative_prompt,
num_images_per_prompt=num_samples,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=g_cuda
).images
for img in images:
display(img)
#@markdown Run Gradio UI for generating images.
import gradio as gr
def inference(prompt, negative_prompt, num_samples, height=512, width=512, num_inference_steps=50, guidance_scale=7.5):
with torch.autocast("cuda"), torch.inference_mode():
return pipe(
prompt, height=int(height), width=int(width),
negative_prompt=negative_prompt,
num_images_per_prompt=int(num_samples),
num_inference_steps=int(num_inference_steps), guidance_scale=guidance_scale,
generator=g_cuda
).images
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Prompt", value="photo of zwx dog in a bucket")
negative_prompt = gr.Textbox(label="Negative Prompt", value="")
run = gr.Button(value="Generate")
with gr.Row():
num_samples = gr.Number(label="Number of Samples", value=4)
guidance_scale = gr.Number(label="Guidance Scale", value=7.5)
with gr.Row():
height = gr.Number(label="Height", value=512)
width = gr.Number(label="Width", value=512)
num_inference_steps = gr.Slider(label="Steps", value=24)
with gr.Column():
gallery = gr.Gallery()
run.click(inference, inputs=[prompt, negative_prompt, num_samples, height, width, num_inference_steps, guidance_scale], outputs=gallery)
demo.launch(debug=True)
#@title (Optional) Delete diffuser and old weights and only keep the ckpt to free up drive space.
#@markdown [ ! ] Caution, Only execute if you are sure u want to delete the diffuser format weights and only use the ckpt.
import shutil
from glob import glob
import os
for f in glob(OUTPUT_DIR+os.sep+"*"):
if f != WEIGHTS_DIR:
shutil.rmtree(f)
print("Deleted", f)
for f in glob(WEIGHTS_DIR+"/*"):
if not f.endswith(".ckpt") or not f.endswith(".json"):
try:
shutil.rmtree(f)
except NotADirectoryError:
continue
print("Deleted", f)
#@title Free runtime memory
exit()