Skip to content

Commit d6c5aa3

Browse files
author
Daniel Müller
committed
small changes & fixes to imagen.sh
1 parent ef21ace commit d6c5aa3

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

imagen.sh

100644100755
Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#This version offers to reuse last prompt
22
#this version can evaluate the contents of /home/deck/stable-diffusion.cpp/Checkpoints and prompt the user to select a checkpoint from that dir. logic to differentiate SD vs SDXL to be added
33
#This version saves output files as . To change back to Output_#.png, set line 11 to base_filename="output" and 15 to while [[ -e "$output_dir/${base_filename}_${i}.png" ]]; do
4-
#This version will query the user for which LORA, if any, they want to use and the strength of the LORA. The script assumes LORAS are at /home/deck/stable-diffusion.cpp/LORAs/
4+
#This version will query the user for which LoRA, if any, to use and the strength of the LoRA. The script assumes LoRAs are at /home/deck/stable-diffusion.cpp/loras/
55
#This version has the capability to specify a neg prompt and will ask if you want to reuse the previous one
66
#This version will verify SD vs SDXL prompt/parameter compliance with the selected model. If noncompliant, will offer to attempt to automatically comply the prompt/parameters with the SDXL model
77
#!/bin/bash
88

9-
#!/bin/bash
10-
119
# Directory to save images
1210
output_dir="output_images"
1311
mkdir -p "$output_dir"
1412

13+
checkpoint_dir="/home/deck/stable-diffusion.cpp/checkpoints"
14+
lora_dir="/home/deck/stable-diffusion.cpp/loras"
15+
1516
# Base filename
1617
base_filename="output"
1718

@@ -48,19 +49,21 @@ list_checkpoints() {
4849
list_loras() {
4950
local dir=$1
5051
local files=("$dir"/*)
52+
echo "${files}"
5153
echo "LoRAs found in $dir:"
5254
for idx in "${!files[@]}"; do
5355
echo "$((idx + 1))) ${files[$idx]##*/}"
5456
done
55-
read -p "Please input the number corresponding to the LORA to be used in this generation: " choice
57+
read -p "Please input the number corresponding to the LoRA to be used in this generation: " choice
5658
if [[ $choice -gt 0 && $choice -le ${#files[@]} ]]; then
5759
selected_lora="${files[$((choice - 1))]##*/}"
5860
echo "You've chosen: $choice) $selected_lora"
5961

60-
# Prompt the user to indicate the strength of the LORA
61-
read -p "Enter the strength of the LORA (e.g., 1): " lora_strength
62+
# Prompt the user to indicate the strength of the LoRA
63+
read -p "Enter the strength of the LoRA (default 1): " lora_strength
64+
lora_strength=${lora_strength:-1}
6265

63-
# Amend the user's prompt to include the selected LORA and its strength
66+
# Amend the user's prompt to include the selected LoRA and its strength
6467
prompt="$prompt <lora:${selected_lora%.safetensors}:$lora_strength>"
6568
else
6669
echo "Invalid choice. Exiting."
@@ -69,21 +72,14 @@ list_loras() {
6972
}
7073

7174
# List checkpoints and prompt the user to choose one
72-
checkpoint_dir="/home/deck/stable-diffusion.cpp/Checkpoints"
7375
list_checkpoints "$checkpoint_dir"
7476

75-
# Change directory to Checkpoints to look for the model
76-
cd /home/deck/stable-diffusion.cpp/Checkpoints
77-
7877
# Check if the selected checkpoint exists in the Checkpoints directory
79-
if [[ ! -f "$selected_checkpoint" ]]; then
80-
echo "Model $selected_checkpoint not found in /home/deck/stable-diffusion.cpp/Checkpoints"
78+
if [[ ! -f "$checkpoint_dir/$selected_checkpoint" ]]; then
79+
echo "Model $selected_checkpoint not found in $checkpoint_dir"
8180
exit 1
8281
fi
8382

84-
# Return to the base working directory
85-
cd /home/deck/stable-diffusion.cpp/
86-
8783
# Prompt the user for a new prompt or reuse the previous one
8884
if [[ -f "last_prompt.txt" ]]; then
8985
echo "Do you want to reuse the previous prompt? (y/n)"
@@ -116,13 +112,12 @@ else
116112
echo "$negative_prompt" > last_negative_prompt.txt
117113
fi
118114

119-
# Prompt the user if they want to use a LORA for generating this image
120-
echo "Would you like to use a LORA for generating this image? (y/n)"
115+
# Prompt the user whether to use a LoRA for generating this image
116+
echo "Would you like to use a LoRA for generating this image? (y/n)"
121117
read use_lora
122118

123119
if [[ "$use_lora" == "y" ]]; then
124120
# List LoRAs and prompt the user to choose one
125-
lora_dir="/home/deck/stable-diffusion.cpp/LORAs"
126121
list_loras "$lora_dir"
127122
fi
128123

@@ -163,16 +158,68 @@ case $resolution_choice in
163158
;;
164159
esac
165160

161+
read -p "Enter the CFG scale (default is 1): " cfg_scale
162+
cfg_scale=${cfg_scale:-1}
163+
164+
echo "Choose a sampling method:"
165+
echo "1) euler"
166+
echo "2) euler_a"
167+
echo "3) heun"
168+
echo "4) dpm2"
169+
echo "5) dpm++2s_a"
170+
echo "6) dpm++2m"
171+
echo "7) dpm++2mv2"
172+
echo "8) ipndm"
173+
echo "9) ipndm_v"
174+
echo "10) lcm"
175+
read -p "Enter the number corresponding to your choice (default is lcm): " sampling_method_choice
176+
177+
case $sampling_method_choice in
178+
1)
179+
sampling_method=euler
180+
;;
181+
2)
182+
sampling_method=euler_a
183+
;;
184+
3)
185+
sampling_method=heun
186+
;;
187+
4)
188+
sampling_method=dpm2
189+
;;
190+
5)
191+
sampling_method=dpm++2s_a
192+
;;
193+
6)
194+
sampling_method=dpm++2m
195+
;;
196+
7)
197+
sampling_method=dpm++2mv2
198+
;;
199+
8)
200+
sampling_method=ipndm
201+
;;
202+
9)
203+
sampling_method=ipndm_v
204+
;;
205+
10)
206+
sampling_method=lcm
207+
;;
208+
*)
209+
sampling_method=lcm
210+
;;
211+
esac
212+
166213
# Prompt the user to specify the number of steps (default to 8)
167214
read -p "Enter the number of steps (default is 8): " steps
168215
steps=${steps:-8}
169216

170217
# Prompt the user to enter the number of pictures to generate
171218
read -p "Enter the number of pictures to generate: " num_pictures
172219

173-
# Check if model is SDXL and warn user if true, ask if they want automatic adjustment for SDXL compliance
174-
if [[ "$selected_checkpoint" == *"SDXL"* ]]; then
175-
echo "WARNING: The model being used is SDXL."
220+
# Check if model is SDXL and warn user if true, ask if automatic adjustment for SDXL compliance wanted
221+
if [[ "$(stat -L -c %s "$checkpoint_dir/$selected_checkpoint")" > 2500000000 ]]; then
222+
echo "WARNING: You seem to be using an SDXL model."
176223
read -p "Would you like your prompt automatically adjusted for SDXL compliance? (y/n): " adjust_sdxl
177224
if [[ "$adjust_sdxl" == "y" ]]; then
178225
sdxl_compliance="--vae-on-cpu"
@@ -194,7 +241,7 @@ for ((j=1; j<=num_pictures; j++)); do
194241
output_filename="${safe_prompt}-${safe_checkpoint}_${i}.png"
195242

196243
# Run the stable-diffusion command with the new filename and random seed, using the selected checkpoint from Checkpoints directory
197-
./sd -m "/home/deck/stable-diffusion.cpp/Checkpoints/$selected_checkpoint" -H "$height" -W "$width" --vae-on-cpu --sampling-method lcm --steps "$steps" --cfg-scale 1 --seed "$seed" --prompt "$prompt" --negative-prompt "$negative_prompt" $sdxl_compliance -o "$output_dir/$output_filename"
244+
./sd -m "$checkpoint_dir/$selected_checkpoint" -H "$height" -W "$width" --sampling-method lcm --steps "$steps" --cfg-scale "$cfg_scale" --seed "$seed" --prompt "$prompt" --negative-prompt "$negative_prompt" $sdxl_compliance -o "$output_dir/$output_filename" | tee output.log
198245

199246
# Check for memory allocation error
200247
if grep -q "ErrorOutOfDeviceMemory" <<< "$(tail -n 10 output.log)"; then
@@ -210,7 +257,7 @@ done
210257

211258
# create dir with
212259
#cd /home/deck/stable-diffusion.cpp/
213-
#chmod +x imgen.sh
260+
#chmod +x imagen.sh
214261

215262
#run this script to generate multiple images in 1 command
216-
#./imgen
263+
#./imagen.sh

0 commit comments

Comments
 (0)