@@ -96,6 +96,7 @@ struct SDParams {
96
96
bool vae_tiling = false ;
97
97
bool control_net_cpu = false ;
98
98
bool canny_preprocess = false ;
99
+ int upscale_repeats = 1 ;
99
100
};
100
101
101
102
void print_params (SDParams params) {
@@ -129,6 +130,7 @@ void print_params(SDParams params) {
129
130
printf (" seed: %ld\n " , params.seed );
130
131
printf (" batch_count: %d\n " , params.batch_count );
131
132
printf (" vae_tiling: %s\n " , params.vae_tiling ? " true" : " false" );
133
+ printf (" upscale_repeats: %d\n " , params.upscale_repeats );
132
134
}
133
135
134
136
void print_usage (int argc, const char * argv[]) {
@@ -145,6 +147,7 @@ void print_usage(int argc, const char* argv[]) {
145
147
printf (" --control-net [CONTROL_PATH] path to control net model\n " );
146
148
printf (" --embd-dir [EMBEDDING_PATH] path to embeddings.\n " );
147
149
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 " );
148
151
printf (" --type [TYPE] weight type (f32, f16, q4_0, q4_1, q5_0, q5_1, q8_0)\n " );
149
152
printf (" If not specified, the default is the type of the weight file.\n " );
150
153
printf (" --lora-model-dir [DIR] lora model directory\n " );
@@ -296,6 +299,16 @@ void parse_args(int argc, const char** argv, SDParams& params) {
296
299
break ;
297
300
}
298
301
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
+ }
299
312
} else if (arg == " -n" || arg == " --negative-prompt" ) {
300
313
if (++i >= argc) {
301
314
invalid_arg = true ;
@@ -700,7 +713,7 @@ int main(int argc, const char* argv[]) {
700
713
}
701
714
702
715
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 ) {
704
717
upscaler_ctx_t * upscaler_ctx = new_upscaler_ctx (params.esrgan_path .c_str (),
705
718
params.n_threads ,
706
719
params.wtype );
@@ -712,13 +725,17 @@ int main(int argc, const char* argv[]) {
712
725
if (results[i].data == NULL ) {
713
726
continue ;
714
727
}
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;
719
737
}
720
- free (results[i].data );
721
- results[i] = upscaled_image;
738
+ results[i] = current_image; // Set the final upscaled image as the result
722
739
}
723
740
}
724
741
}
0 commit comments