-
Notifications
You must be signed in to change notification settings - Fork 0
/
nisaar_dreambooth_custom_Stable_Diffusion.py
176 lines (139 loc) · 6.67 KB
/
nisaar_dreambooth_custom_Stable_Diffusion.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
# -*- coding: utf-8 -*-
"""Nisaar_DreamBooth_Custom_dreambooth_colab_joepenna.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1nZ0MGaW9RxuMvgCkYB4S3YlUDTabrXZb
"""
# Commented out IPython magic to ensure Python compatibility.
#@title Load repo (if needed)
!git clone https://github.com/JoePenna/Dreambooth-Stable-Diffusion
# %cd Dreambooth-Stable-Diffusion
!nvidia-smi
from google.colab import drive
drive.mount('/content/drive')
#@title BUILD ENV
!pip install omegaconf
!pip install einops
!pip install pytorch-lightning==1.6.5
!pip install test-tube
!pip install transformers
!pip install kornia
!pip install -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
!pip install -e git+https://github.com/openai/CLIP.git@main#egg=clip
!pip install setuptools==59.5.0
!pip install pillow==9.0.1
!pip install torchmetrics==0.6.0
!pip install -e .
!pip install protobuf==3.20.1
!pip install gdown
!pip install pydrive
!pip install -qq diffusers["training"]==0.3.0 transformers ftfy
!pip install -qq "ipywidgets>=7,<8"
!pip install huggingface_hub
!pip install ipywidgets==7.7.1
!pip install captionizer==1.0.1
import os
os._exit(00)
# Commented out IPython magic to ensure Python compatibility.
#@title # Required - Navigate back to the directory.
# %cd Dreambooth-Stable-Diffusion
#@markdown Hugging Face Login
from huggingface_hub import notebook_login
notebook_login()
#hugging_face_token = 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
#@markdown Download the 1.4 sd model
from IPython.display import clear_output
from huggingface_hub import hf_hub_download
downloaded_model_path = hf_hub_download(
repo_id="CompVis/stable-diffusion-v-1-4-original",
filename="sd-v1-4.ckpt",
use_auth_token=True
)
# Move the sd-v1-4.ckpt to the root of this directory as "model.ckpt"
actual_locations_of_model_blob = !readlink -f {downloaded_model_path}
!mv {actual_locations_of_model_blob[-1]} model.ckpt
clear_output()
print("✅ model.ckpt successfully downloaded")
#@title # Download Regularization Images
#@markdown We’ve created the following image sets
#@markdown - `man_euler` - provided by Niko Pueringer (Corridor Digital) - euler @ 40 steps, CFG 7.5
#@markdown - `man_unsplash` - pictures from various photographers
#@markdown - `person_ddim`
#@markdown - `woman_ddim` - provided by David Bielejeski - ddim @ 50 steps, CFG 10.0 <br />
#@markdown - `blonde_woman` - provided by David Bielejeski - ddim @ 50 steps, CFG 10.0 <br />
dataset="man_euler" #@param ["man_euler", "man_unsplash", "person_ddim", "woman_ddim", "blonde_woman"]
!git clone https://github.com/djbielejeski/Stable-Diffusion-Regularization-Images-{dataset}.git
!mkdir -p regularization_images/{dataset}
!mv -v Stable-Diffusion-Regularization-Images-{dataset}/{dataset}/*.* regularization_images/{dataset}
# Add this code above "t = t.to('cpu')" at /content/Dreambooth-Stable-Diffusion/ldm/models/diffusion/ddpm.py Line 1096
# Run this Command on Terminal
# mkdir /content/Dreambooth-Stable-Diffusion/training_images
# cp /content/drive/MyDrive/JOE_TRAINING_DATASET/pratikbehera/* /content/Dreambooth-Stable-Diffusion/training_images
# cp /content/drive/MyDrive/Ruhaan/train_data/* /content/Dreambooth-Stable-Diffusion/training_images
# cp /content/drive/MyDrive/Asgarali/* /content/Dreambooth-Stable-Diffusion/training_images
# cp /content/drive/MyDrive/JOE_TRAINING_DATASET/Soumyakantsahoo/images/* /content/Dreambooth-Stable-Diffusion/training_images
# COMMAND TO TRNSFR IMAGES TO training_Images
# mkdir /content/Dreambooth-Stable-Diffusion/training_images
# cp /content/drive/MyDrive/Nisaar_data/Cleandata/* /content/Dreambooth-Stable-Diffusion/training_images
#@title # Training Images
#@markdown ## Upload your training images
#@markdown WARNING: Be sure to upload an even amount of images, otherwise the training inexplicably stops at 1500 steps. <br />
#@markdown - 2-3 full body
#@markdown - 3-5 upper body
#@markdown - 5-12 close-up on face <br /> <br />
#@markdown The images should be as close as possible to the kind of images you’re trying to make (most of the time, that means no selfies).
from google.colab import files
from IPython.display import clear_output
# Create the directory
!rm -rf training_images
!mkdir -p training_images
# Upload the files
uploaded = files.upload()
for filename in uploaded.keys():
updated_file_name = filename.replace(" ", "_")
!mv "{filename}" "training_images/{updated_file_name}"
clear_output()
# Tell the user what is going on
training_images_file_paths = !find training_images/*
if len(training_images_file_paths) == 0:
print("❌ no training images found. Please upload images to training_images")
else:
print("✅ " + str(len(training_images_file_paths)) + " training images found")
#@title # Training
#@markdown This isn't used for training, just to help you remember what your trained into the model.
project_name = "NisaarAgharia_Custom_model" #@param {type:"string"}
# MAX STEPS
#@markdown How many steps do you want to train for?
max_training_steps = 2000 #@param {type:"integer"}
#@markdown Match class_word to the category of the regularization images you chose above.
class_word = "person" #@param ["man", "person", "woman"] {allow-input: true}
#@markdown This is the unique token you are incorporating into the stable diffusion model.
token = "nisaaragharia" #@param {type:"string"}
reg_data_root = "/content/Dreambooth-Stable-Diffusion/regularization_images/" + dataset
!rm -rf training_images/.ipynb_checkpoints
!python "main.py" \
--base configs/stable-diffusion/v1-finetune_unfrozen.yaml \
-t \
--actual_resume "model.ckpt" \
--reg_data_root "{reg_data_root}" \
-n "{project_name}" \
--gpus 0, \
--data_root "/content/Dreambooth-Stable-Diffusion/training_images" \
--max_training_steps {max_training_steps} \
--class_word "{class_word}" \
--token "{token}" \
--no-test
#@title # Copy and name the checkpoint file
directory_paths = !ls -d logs/*
last_checkpoint_file = directory_paths[-1] + "/checkpoints/last.ckpt"
training_images = !find training_images/*
date_string = !date +"%Y-%m-%dT%H-%M-%S"
file_name = date_string[-1] + "_" + project_name + "_" + str(len(training_images)) + "_training_images_" + str(max_training_steps) + "_max_training_steps_" + token + "_token_" + class_word + "_class_word.ckpt"
file_name = file_name.replace(" ", "_")
!mkdir -p trained_models
!mv "{last_checkpoint_file}" "trained_models/{file_name}"
print("Download your trained model file from trained_models/" + file_name + " and use in your favorite Stable Diffusion repo!")
#@title Save model in google drive
#from google.colab import drive
#drive.mount('/content/drive')
! cp trained_models/{file_name} /content/drive/MyDrive/JOE_TRAINING_DATASET/{file_name}