@@ -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
101102void 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
134136void 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 }
0 commit comments