Skip to content

Commit 193fb62

Browse files
sean-baileyleejet
andauthored
feat: add capability to repeatedly run the upscaler in a row (#174)
* Add in upscale repeater logic --------- Co-authored-by: leejet <[email protected]>
1 parent b636886 commit 193fb62

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ cmake --build . --config Release
148148
### Run
149149
150150
```
151-
usage: ./bin/sd [arguments]
151+
usage: ./build/bin/sd [arguments]
152152

153153
arguments:
154154
-h, --help show this help message and exit
@@ -161,6 +161,7 @@ arguments:
161161
--control-net [CONTROL_PATH] path to control net model
162162
--embd-dir [EMBEDDING_PATH] path to embeddings.
163163
--upscale-model [ESRGAN_PATH] path to esrgan model. Upscale images after generate, just RealESRGAN_x4plus_anime_6B supported by now.
164+
--upscale-repeats Run the ESRGAN upscaler this many times (default 1)
164165
--type [TYPE] weight type (f32, f16, q4_0, q4_1, q5_0, q5_1, q8_0)
165166
If not specified, the default is the type of the weight file.
166167
--lora-model-dir [DIR] lora model directory
@@ -186,6 +187,7 @@ arguments:
186187
<= 0 represents unspecified, will be 1 for SD1.x, 2 for SD2.x
187188
--vae-tiling process vae in tiles to reduce memory usage
188189
--control-net-cpu keep controlnet in cpu (for low vram)
190+
--canny apply canny preprocessor (edge detection)
189191
-v, --verbose print extra info
190192
```
191193

examples/cli/main.cpp

+24-7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct SDParams {
9696
bool vae_tiling = false;
9797
bool control_net_cpu = false;
9898
bool canny_preprocess = false;
99+
int upscale_repeats = 1;
99100
};
100101

101102
void print_params(SDParams params) {
@@ -129,6 +130,7 @@ void print_params(SDParams params) {
129130
printf(" seed: %ld\n", params.seed);
130131
printf(" batch_count: %d\n", params.batch_count);
131132
printf(" vae_tiling: %s\n", params.vae_tiling ? "true" : "false");
133+
printf(" upscale_repeats: %d\n", params.upscale_repeats);
132134
}
133135

134136
void print_usage(int argc, const char* argv[]) {
@@ -145,6 +147,7 @@ void print_usage(int argc, const char* argv[]) {
145147
printf(" --control-net [CONTROL_PATH] path to control net model\n");
146148
printf(" --embd-dir [EMBEDDING_PATH] path to embeddings.\n");
147149
printf(" --upscale-model [ESRGAN_PATH] path to esrgan model. Upscale images after generate, just RealESRGAN_x4plus_anime_6B supported by now.\n");
150+
printf(" --upscale-repeats Run the ESRGAN upscaler this many times (default 1)\n");
148151
printf(" --type [TYPE] weight type (f32, f16, q4_0, q4_1, q5_0, q5_1, q8_0)\n");
149152
printf(" If not specified, the default is the type of the weight file.\n");
150153
printf(" --lora-model-dir [DIR] lora model directory\n");
@@ -296,6 +299,16 @@ void parse_args(int argc, const char** argv, SDParams& params) {
296299
break;
297300
}
298301
params.prompt = argv[i];
302+
} else if (arg == "--upscale-repeats") {
303+
if (++i >= argc) {
304+
invalid_arg = true;
305+
break;
306+
}
307+
params.upscale_repeats = std::stoi(argv[i]);
308+
if (params.upscale_repeats < 1) {
309+
fprintf(stderr, "error: upscale multiplier must be at least 1\n");
310+
exit(1);
311+
}
299312
} else if (arg == "-n" || arg == "--negative-prompt") {
300313
if (++i >= argc) {
301314
invalid_arg = true;
@@ -700,7 +713,7 @@ int main(int argc, const char* argv[]) {
700713
}
701714

702715
int upscale_factor = 4; // unused for RealESRGAN_x4plus_anime_6B.pth
703-
if (params.esrgan_path.size() > 0) {
716+
if (params.esrgan_path.size() > 0 && params.upscale_repeats > 0) {
704717
upscaler_ctx_t* upscaler_ctx = new_upscaler_ctx(params.esrgan_path.c_str(),
705718
params.n_threads,
706719
params.wtype);
@@ -712,13 +725,17 @@ int main(int argc, const char* argv[]) {
712725
if (results[i].data == NULL) {
713726
continue;
714727
}
715-
sd_image_t upscaled_image = upscale(upscaler_ctx, results[i], upscale_factor);
716-
if (upscaled_image.data == NULL) {
717-
printf("upscale failed\n");
718-
continue;
728+
sd_image_t current_image = results[i];
729+
for (int u = 0; u < params.upscale_repeats; ++u) {
730+
sd_image_t upscaled_image = upscale(upscaler_ctx, current_image, upscale_factor);
731+
if (upscaled_image.data == NULL) {
732+
printf("upscale failed\n");
733+
break;
734+
}
735+
free(current_image.data);
736+
current_image = upscaled_image;
719737
}
720-
free(results[i].data);
721-
results[i] = upscaled_image;
738+
results[i] = current_image; // Set the final upscaled image as the result
722739
}
723740
}
724741
}

stable-diffusion.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ class StableDiffusionGGML {
173173
if (version == VERSION_XL) {
174174
scale_factor = 0.13025f;
175175
if (vae_path.size() == 0 && taesd_path.size() == 0) {
176-
LOG_WARN("!!!It looks like you are using SDXL model. "
177-
"If you find that the generated images are completely black, "
178-
"try specifying SDXL VAE FP16 Fix with the --vae parameter. "
179-
"You can find it here: https://huggingface.co/madebyollin/sdxl-vae-fp16-fix/blob/main/sdxl_vae.safetensors");
176+
LOG_WARN(
177+
"!!!It looks like you are using SDXL model. "
178+
"If you find that the generated images are completely black, "
179+
"try specifying SDXL VAE FP16 Fix with the --vae parameter. "
180+
"You can find it here: https://huggingface.co/madebyollin/sdxl-vae-fp16-fix/blob/main/sdxl_vae.safetensors");
180181
}
181182
}
182183

0 commit comments

Comments
 (0)