-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion-script-custom-VAE.sh
executable file
·319 lines (267 loc) · 10.4 KB
/
conversion-script-custom-VAE.sh
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
VERSION=0.7.6
# Set the name of the model and its extension to be replaced
MODEL_NAME="model_name"
EXTENSION="ckpt"
# Set variables for easy updating
ROOT_DIR="/ml-stable-diffusion-main"
WORK_DIR="/ml-stable-diffusion-main/work"
MODELS_LOAD="/Volumes/External Drive/Model Archive"
COMPRESSED_DUMP="/Volumes/External Drive/Uploads"
DIFFUSERS_DUMP="${WORK_DIR}/Diffusers"
EM_PICKLES_DUMP="${WORK_DIR}/Embedded Pickles"
MODELS_LOCAL="${WORK_DIR}/${MODEL_NAME}"
MODELS_DUMP="${WORK_DIR}/Models"
VAE_LOAD="${WORK_DIR}/VAE"
# Define color variables
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
CYAN=$(tput setaf 6)
RESET=$(tput sgr 0)
echo -e "${BOLD}${CYAN}"
cat << "EOF"
▄▄· ▄▄▄ ▄▄▄ .• ▌ ▄ ·. ▄▄▌
▐█ ▌▪▪ ▀▄ █·▀▄.▀··██ ▐███▪██•
██ ▄▄ ▄█▀▄ ▐▀▀▄ ▐▀▀▪▄▐█ ▌▐▌▐█·██▪
▐███▌▐█▌.▐▌▐█•█▌▐█▄▄▌██ ██▌▐█▌▐█▌▐▌
·▀▀▀ ▀█▄▀▪.▀ ▀ ▀▀▀ ▀▀ █▪▀▀▀.▀▀▀
VAE Embedding
EOF
echo "${RESET}${GREEN}Version${RESET}: ${YELLOW}${VERSION}${RESET}"
sleep 0.3
# Print message indicating activation of environment
echo "${RED}🚀 Activating Environment...🚀${RESET}"
sleep 0.2
# Navigate to the project directory and activate the virtual environment
cd "${ROOT_DIR}"
sleep 0.2
. bin/activate
sleep 0.2
# Navigate to the work directory
cd "${WORK_DIR}"
sleep 0.1
# Print message indicating successful activation of environment
echo "${GREEN}🎉 Environment Activated!${RESET}"
sleep 0.3
#########################################################################
# SETUP
#########################################################################
# Copy workfile to working directory
echo ""
echo "${RED}Copying workfile to working directory ${RESET}"
echo ""
sleep 0.3
# Check if file exists before copying
if [ -f "${WORK_DIR}/${MODEL_NAME}.${EXTENSION}" ]; then
echo ""
echo "${YELLOW}File already exists in working directory. Skipping copy.${RESET}"
echo ""
else
start=$SECONDS
cp -v "${MODELS_LOAD}/${MODEL_NAME}.${EXTENSION}" "${WORK_DIR}"
end=$SECONDS
duration=$(( end - start ))
echo ""
echo "${GREEN}Copy took $duration seconds to complete${RESET}"
echo ""
sleep 0.3
fi
#########################################################################
# DOWNLOADS
#########################################################################
# Check for the VAE files and if not found download them
VAE_DIR="${VAE_LOAD}"
FILE_1="moistmixv2-diffusion_pytorch_model.bin"
FILE_2="orangemix-diffusion_pytorch_model.bin"
FILE_3="2.1-diffusion_pytorch_model.bin"
FILE_4="1.5-diffusion_pytorch_model.bin"
FILE_URL_1="https://huggingface.co/mdmachine/VAE/resolve/main/VAE/moistmixv2-diffusion_pytorch_model.bin"
FILE_URL_2="https://huggingface.co/mdmachine/VAE/resolve/main/VAE/orangemix-diffusion_pytorch_model.bin"
FILE_URL_3="https://huggingface.co/mdmachine/VAE/resolve/main/VAE/2.1-diffusion_pytorch_model.bin"
FILE_URL_4="https://huggingface.co/mdmachine/VAE/resolve/main/VAE/1.5-diffusion_pytorch_model.bin"
if [ ! -d "$VAE_DIR" ]; then
mkdir "$VAE_DIR"
fi
# curl method
if [ ! -f "$VAE_DIR/$FILE_1" ]; then
echo "Downloading $FILE_1"
curl -# -L "$FILE_URL_1" -o "$VAE_DIR/$FILE_1"
fi
if [ ! -f "$VAE_DIR/$FILE_2" ]; then
echo "Downloading $FILE_2"
curl -# -L "$FILE_URL_2" -o "$VAE_DIR/$FILE_2"
fi
if [ ! -f "$VAE_DIR/$FILE_3" ]; then
echo "Downloading $FILE_3"
curl -# -L "$FILE_URL_3" -o "$VAE_DIR/$FILE_3"
fi
if [ ! -f "$VAE_DIR/$FILE_4" ]; then
echo "Downloading $FILE_4"
curl -# -L "$FILE_URL_4" -o "$VAE_DIR/$FILE_4"
fi
#########################################################################
# DIFFUSERS
#########################################################################
# Change directory to Stable Diffusion project
cd "${ROOT_DIR}"
start=$SECONDS
# Function to create diffusers from a specified checkpoint path and dump path
function create_diffusers() {
local checkpoint_path="$1"
local dump_path="$2"
local count=1
local attempts=3
local from_safetensors_arg=""
local from_custom_config=""
# ask if the user wants to use a custom config file
read -t 10 -p "${RED}Use a custom config file?${RESET} (y/n) ${YELLOW}Skipping in 10 seconds.${RESET} " custom_config
# if there is no input in 10 seconds, continue on
if [ -z "$custom_config" ]; then
echo "${RED}No input received. Skipping custom config.${RESET}"
else
if [ "$custom_config" == "y" ]; then
echo "${RED}Listing available config files in /yaml:${RESET}"
find "${WORK_DIR}/yaml" -type f -name '*.yaml' | cat -n
read -p "${RED}Enter the number of the custom config file you want to use:${RESET} " config_input_number
config_input="$(find "${WORK_DIR}/yaml" -type f -name '*.yaml' | sed -n ${config_input_number}p)"
from_custom_config="--original_config_file ${config_input}"
else
echo "${RED}Skipping custom config.${RESET}"
fi
fi
if [ "$EXTENSION" = "safetensors" ]; then
from_safetensors_arg="--from_safetensors"
fi
if [ -d "${dump_path}" ]; then
echo "${YELLOW} ${dump_path} directory exists. Skipping diffuser creation. ${RESET}"
return 0
fi
# Try creating diffusers with given checkpoint path and dump path up to 3 times
for ((i=1; i<=$attempts; i++)); do
if python convert_original_stable_diffusion_to_diffusers.py --checkpoint_path "${checkpoint_path}" ${from_custom_config:+--original_config_file "$config_input"} --device cpu --extract_ema --dump_path "${dump_path}" --upcast_attention $from_safetensors_arg; then
echo "${GREEN}The conversion of the workfile into diffusers is complete${RESET}"
echo ""
return
else
echo "${CYAN}Conversion killed with exit code $?. Respawning...${RESET}" >&2
echo "${YELLOW}Retrying #$i out of $attempts...${RESET}"
sleep 0.3
fi
done
# If creation failed too many times, skip and print message
[[ $count -eq $attempts ]] && echo "${RED} Skipping after too many attempts ${RESET}"
# Print message for successful creation
echo "${GREEN} The conversion of the workfile into diffusers is complete ${RESET}"
echo ""
}
# Create diffusers from raw workfile
echo "${RED} Now creating diffusers from the raw workfile into diffusers... ${RESET}"
create_diffusers "${MODELS_LOCAL}.${EXTENSION}" "${DIFFUSERS_DUMP}/${MODEL_NAME}_raw_diffusers_model/"
# Print the duration of the diffuser creation process
end=$SECONDS
duration=$(( end - start ))
echo "${GREEN} The conversion of diffusers took $duration seconds to complete ${RESET}"
#########################################################################
# DUPLICATION
#########################################################################
# Define function to print message in green color
print_message() {
printf "${GREEN}%s${RESET}\n" "$1"
}
# Define function to print error message in red color
print_error() {
printf "${RED}%s${RESET}\n" "$1" >&2
}
# Define function to duplicate and copy diffusers
duplicate_and_copy_diffusers() {
local source_dir="$1"
local dest_dir="$2"
local vae_file="$3"
local vae_source="$4"
print_message "Duplicating..."
if cp -v -r "$source_dir/${MODEL_NAME}_raw_diffusers_model" "$dest_dir/${MODEL_NAME}_${vae_file}_diffusers_model"; then
print_message "Duplicating complete."
else
print_error "Error duplicating diffusers."
return 1
fi
print_message "Copying vae now..."
if cp -v "$VAE_LOAD/$vae_source-diffusion_pytorch_model.bin" "$dest_dir/${MODEL_NAME}_${vae_file}_diffusers_model/vae/diffusion_pytorch_model.bin"; then
print_message "Copy complete."
else
print_error "Error copying vae."
return 1
fi
}
# Duplicate and copy diffusers for 1.5 SD
start=$SECONDS
if duplicate_and_copy_diffusers "$DIFFUSERS_DUMP" "$DIFFUSERS_DUMP" "ema-vae-1.5" "1.5"; then
duration=$(( SECONDS - start ))
printf "\n%s Duplication & copy took %d seconds to complete.\n\n" "$(tput setaf 2)" "$duration"
sleep 0.3
fi
# Duplicate and copy diffusers for 2.1 SD
print_message "Duplicating raw diffusers and replace VAE with SD 2.1"
sleep 1
start=$SECONDS
if duplicate_and_copy_diffusers "$DIFFUSERS_DUMP" "$DIFFUSERS_DUMP" "ema-vae-2.1" "2.1"; then
duration=$(( SECONDS - start ))
printf "\n%s Duplication & copy took %d seconds to complete.\n\n" "$(tput setaf 2)" "$duration"
fi
# Duplicate and copy diffusers for orangemix
print_message "Duplicating raw diffusers and replace VAE with orangemix"
sleep 1
start=$SECONDS
if duplicate_and_copy_diffusers "$DIFFUSERS_DUMP" "$DIFFUSERS_DUMP" "orangemix-vae" "orangemix"; then
duration=$(( SECONDS - start ))
printf "\n%s Duplication & copy took %d seconds to complete.\n\n" "$(tput setaf 2)" "$duration"
fi
# Duplicate and copy diffusers for 2.1 SD
print_message "Duplicating raw diffusers and replace VAE with SD 2.1"
sleep 1
start=$SECONDS
if duplicate_and_copy_diffusers "$DIFFUSERS_DUMP" "$DIFFUSERS_DUMP" "moistmixv2-vae" "moistmixv2"; then
duration=$(( SECONDS - start ))
printf "\n%s Duplication & copy took %d seconds to complete.\n\n" "$(tput setaf 2)" "$duration"
fi
####################################################################
# CLEANUP
####################################################################
# Clean up message
echo ""
echo "${RED}Cleaning Up!${RESET}"
echo ""
sleep 0.3
# Navigate to the work directory
cd "${WORK_DIR}"
sleep 0.3
# Check if workfile exists
if [[ ! -f "${WORK_DIR}/${MODEL_NAME}.${EXTENSION}" ]]; then
echo "Workfile does not exist, skipping deletion."
exit 0
fi
# Ask user if they want to delete the workfile
read -rp "Do you want to delete the workfile? Otherwise it will be automatically removed after 15 seconds. [y/N]: " -t 15 userInput
if [[ -z "${userInput}" ]]; then
# User didn't respond in time
echo "Automatically removed workfile after 15 seconds."
if rm "${WORK_DIR}/${MODEL_NAME}.${EXTENSION}"; then
echo "Successfully deleted workfile"
else
echo "Error: Failed to delete workfile"
fi
elif [[ "${userInput}" =~ ^[Yy]$ ]]; then
# Delete workfile
if rm "${WORK_DIR}/${MODEL_NAME}.${EXTENSION}"; then
echo "Successfully deleted workfile"
else
echo "Error: Failed to delete workfile"
fi
else
echo "Skipping deletion of workfile."
fi
sleep 0.3
echo ""
echo "${GREEN}Done!${RESET}"
echo ""