Commit
•
1fd69b2
1
Parent(s):
b28a5fd
Upload sdxl_thumbsup.py
Browse files- sdxl_thumbsup.py +182 -0
sdxl_thumbsup.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""SDXL-Thumbsup.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1T0tqXsscUsDxLSt6MIiqEmNVYEuCejGL
|
8 |
+
|
9 |
+
# Training DreamBooth LoRA with Stable Diffusion XL on Trump Thumbs Up Images:
|
10 |
+
|
11 |
+
## Linking Drive
|
12 |
+
"""
|
13 |
+
|
14 |
+
from google.colab import drive
|
15 |
+
drive.mount('/content/drive')
|
16 |
+
|
17 |
+
import warnings
|
18 |
+
warnings.filterwarnings("ignore")
|
19 |
+
|
20 |
+
"""## Installing & Login to Hugging Face
|
21 |
+
|
22 |
+
"""
|
23 |
+
|
24 |
+
!pip install huggingface-hub
|
25 |
+
|
26 |
+
!git config --global credential.helper store
|
27 |
+
|
28 |
+
!huggingface-cli login
|
29 |
+
|
30 |
+
"""##Cloning Hugging Face/diffusers - [Repo](https://github.com/huggingface/diffusers)
|
31 |
+
|
32 |
+
"""
|
33 |
+
|
34 |
+
import os
|
35 |
+
import subprocess
|
36 |
+
|
37 |
+
|
38 |
+
subprocess.run(["git", "clone", "https://github.com/huggingface/diffusers"])
|
39 |
+
os.chdir("diffusers")
|
40 |
+
subprocess.run(["pip", "install", "-e", "."])
|
41 |
+
|
42 |
+
"""## Installing Requirements - Dreambooth SDXL"""
|
43 |
+
|
44 |
+
os.chdir("examples/dreambooth")
|
45 |
+
|
46 |
+
!pip install -r requirements_sdxl.txt
|
47 |
+
|
48 |
+
"""## Write Basic Configuration for Accelerate"""
|
49 |
+
|
50 |
+
from accelerate.utils import write_basic_config
|
51 |
+
write_basic_config()
|
52 |
+
|
53 |
+
"""## Load and Display Images from Drive"""
|
54 |
+
|
55 |
+
import os
|
56 |
+
import cv2
|
57 |
+
from matplotlib import pyplot as plt
|
58 |
+
|
59 |
+
dir = '/content/drive/MyDrive/SDXL/Images/thumbsup'
|
60 |
+
count = 0
|
61 |
+
max_images = 10
|
62 |
+
|
63 |
+
for img_name in os.listdir(dir):
|
64 |
+
img_path = os.path.join(dir, img_name)
|
65 |
+
|
66 |
+
if img_path.lower().endswith(('.png', '.jpg', '.jpeg')):
|
67 |
+
img = cv2.imread(img_path)
|
68 |
+
|
69 |
+
if img is None:
|
70 |
+
print(f"Failed to load image: {img_path}")
|
71 |
+
continue
|
72 |
+
|
73 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
74 |
+
|
75 |
+
plt.imshow(img)
|
76 |
+
plt.axis('off')
|
77 |
+
plt.show()
|
78 |
+
|
79 |
+
count += 1
|
80 |
+
if count >= max_images:
|
81 |
+
break
|
82 |
+
|
83 |
+
"""## Installing Required Libraries"""
|
84 |
+
|
85 |
+
!pip install tensorrt bitsandbytes xformers wandb
|
86 |
+
|
87 |
+
pip install --upgrade diffusers accelerate
|
88 |
+
|
89 |
+
"""## Logging into Weights and Biases"""
|
90 |
+
|
91 |
+
!wandb login
|
92 |
+
|
93 |
+
"""## Train DreamBooth LoRA Model with Stable Diffusion XL"""
|
94 |
+
|
95 |
+
!accelerate launch train_dreambooth_lora_sdxl.py \
|
96 |
+
--pretrained_model_name_or_path="stabilityai/stable-diffusion-xl-base-1.0" \
|
97 |
+
--instance_data_dir="/content/drive/MyDrive/SDXL/Images/thumbsup" \
|
98 |
+
--pretrained_vae_model_name_or_path="stabilityai/sdxl-vae" \
|
99 |
+
--output_dir="/content/drive/MyDrive/SDXL/Output-Complex" \
|
100 |
+
--mixed_precision="fp16" \
|
101 |
+
--instance_prompt="a high-quality photo of Trump showing thumbs up" \
|
102 |
+
--resolution=1024 \
|
103 |
+
--train_batch_size=1 \
|
104 |
+
--gradient_accumulation_steps=2 \
|
105 |
+
--learning_rate=2e-4 \
|
106 |
+
--lr_scheduler="constant_with_warmup" \
|
107 |
+
--lr_warmup_steps=0 \
|
108 |
+
--max_train_steps=500 \
|
109 |
+
--validation_prompt="A high-quality photo of Trump showing thumbs up in a taco restaurant, detailed, sharp focus" \
|
110 |
+
--validation_epochs=15 \
|
111 |
+
--seed="42" \
|
112 |
+
--push_to_hub \
|
113 |
+
--gradient_checkpointing \
|
114 |
+
--checkpointing_steps=100 \
|
115 |
+
--use_8bit_adam \
|
116 |
+
--prior_loss_weight=0.8 \
|
117 |
+
--num_class_images=10 \
|
118 |
+
--report_to="wandb"
|
119 |
+
|
120 |
+
# Commented out IPython magic to ensure Python compatibility.
|
121 |
+
# %cd ..
|
122 |
+
|
123 |
+
!pip uninstall diffusers
|
124 |
+
!pip install -e ./diffusers
|
125 |
+
|
126 |
+
"""## Load LoRA Weights and Generate Images
|
127 |
+
|
128 |
+
"""
|
129 |
+
|
130 |
+
from huggingface_hub.repocard import RepoCard
|
131 |
+
from diffusers import DiffusionPipeline
|
132 |
+
import torch
|
133 |
+
|
134 |
+
pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
|
135 |
+
pipeline = pipeline.to("cuda")
|
136 |
+
pipeline.load_lora_weights("/content/diffusers/examples/dreambooth/pytorch_lora_weights.safetensors")
|
137 |
+
|
138 |
+
image = pipeline("A high quality picture of Trump showing the thumbs up in Paris", num_inference_steps=50).images[0]
|
139 |
+
|
140 |
+
image_path = "/content/drive/MyDrive/SDXL/Output-Complex/Trump1.png"
|
141 |
+
image.save(image_path)
|
142 |
+
|
143 |
+
print("Image saved at:", image_path)
|
144 |
+
|
145 |
+
image = pipeline("A picture of Trump showing the thumbs up as a Anime character, detailed, sharp focus", num_inference_steps=50).images[0]
|
146 |
+
|
147 |
+
image_path = "/content/drive/MyDrive/SDXL/Output-Complex/Trump2.png"
|
148 |
+
image.save(image_path)
|
149 |
+
|
150 |
+
print("Image saved at:", image_path)
|
151 |
+
|
152 |
+
image = pipeline("A picture of Trump showing thumbsup in whitehouse", num_inference_steps=50).images[0]
|
153 |
+
|
154 |
+
image_path = "/content/drive/MyDrive/SDXL/Output-Complex/Trump3.png"
|
155 |
+
image.save(image_path)
|
156 |
+
|
157 |
+
print("Image saved at:", image_path)
|
158 |
+
|
159 |
+
image = pipeline("A high quality picture of Trump showing the thumbs up as The Statue of Liberty", num_inference_steps=50).images[0]
|
160 |
+
|
161 |
+
image_path = "/content/drive/MyDrive/SDXL/Output-Complex/Trump4.png"
|
162 |
+
image.save(image_path)
|
163 |
+
|
164 |
+
print("Image saved at:", image_path)
|
165 |
+
|
166 |
+
image = pipeline("A high quality picture of Trump showing thumbs up in a lake with a laptop", num_inference_steps=50).images[0]
|
167 |
+
|
168 |
+
image_path = "/content/drive/MyDrive/SDXL/Output-Complex/Trump5.png"
|
169 |
+
image.save(image_path)
|
170 |
+
|
171 |
+
print("Image saved at:", image_path)
|
172 |
+
|
173 |
+
"""## Push to Hugging Face Hub"""
|
174 |
+
|
175 |
+
from huggingface_hub import HfApi
|
176 |
+
|
177 |
+
api = HfApi()
|
178 |
+
|
179 |
+
username = "Paresh1879"
|
180 |
+
repo_name = "stable-diffusion-xl-thumbsup-extend"
|
181 |
+
|
182 |
+
api.upload_folder(repo_id=f"{username}/{repo_name}", folder_path="/content/drive/MyDrive/SDXL")
|