Skip to content

Commit 90ef5f8

Browse files
authored
feat: add auto-resize support for reference images (was Qwen-Image-Edit only) (#898)
1 parent db6f479 commit 90ef5f8

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

examples/cli/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ arguments:
3535
-r, --ref-image [PATH] reference image for Flux Kontext models (can be used multiple times)
3636
--control-video [PATH] path to control video frames, It must be a directory path.
3737
The video frames inside should be stored as images in lexicographical (character) order
38-
For example, if the control video path is `frames`, the directory contain images such as 00.png, 01.png, 鈥?etc.
38+
For example, if the control video path is `frames`, the directory contain images such as 00.png, 01.png, ... etc.
3939
--increase-ref-index automatically increase the indices of references images based on the order they are listed (starting with 1).
40+
--disable-auto-resize-ref-image disable auto resize of ref images
4041
-o, --output OUTPUT path to write result image to (default: ./output.png)
4142
-p, --prompt [PROMPT] the prompt to render
4243
-n, --negative-prompt PROMPT the negative prompt (default: "")

examples/cli/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ struct SDParams {
8080
std::string control_image_path;
8181
std::vector<std::string> ref_image_paths;
8282
std::string control_video_path;
83-
bool increase_ref_index = false;
83+
bool auto_resize_ref_image = true;
84+
bool increase_ref_index = false;
8485

8586
std::string prompt;
8687
std::string negative_prompt;
@@ -175,6 +176,7 @@ void print_params(SDParams params) {
175176
printf(" %s\n", path.c_str());
176177
};
177178
printf(" control_video_path: %s\n", params.control_video_path.c_str());
179+
printf(" auto_resize_ref_image: %s\n", params.auto_resize_ref_image ? "true" : "false");
178180
printf(" increase_ref_index: %s\n", params.increase_ref_index ? "true" : "false");
179181
printf(" offload_params_to_cpu: %s\n", params.offload_params_to_cpu ? "true" : "false");
180182
printf(" clip_on_cpu: %s\n", params.clip_on_cpu ? "true" : "false");
@@ -244,9 +246,10 @@ void print_usage(int argc, const char* argv[]) {
244246
printf(" -i, --end-img [IMAGE] path to the end image, required by flf2v\n");
245247
printf(" --control-image [IMAGE] path to image condition, control net\n");
246248
printf(" -r, --ref-image [PATH] reference image for Flux Kontext models (can be used multiple times) \n");
249+
printf(" --disable-auto-resize-ref-image disable auto resize of ref images\n");
247250
printf(" --control-video [PATH] path to control video frames, It must be a directory path.\n");
248251
printf(" The video frames inside should be stored as images in lexicographical (character) order\n");
249-
printf(" For example, if the control video path is `frames`, the directory contain images such as 00.png, 01.png, etc.\n");
252+
printf(" For example, if the control video path is `frames`, the directory contain images such as 00.png, 01.png, ... etc.\n");
250253
printf(" --increase-ref-index automatically increase the indices of references images based on the order they are listed (starting with 1).\n");
251254
printf(" -o, --output OUTPUT path to write result image to (default: ./output.png)\n");
252255
printf(" -p, --prompt [PROMPT] the prompt to render\n");
@@ -579,6 +582,7 @@ void parse_args(int argc, const char** argv, SDParams& params) {
579582
{"", "--chroma-disable-dit-mask", "", false, &params.chroma_use_dit_mask},
580583
{"", "--chroma-enable-t5-mask", "", true, &params.chroma_use_t5_mask},
581584
{"", "--increase-ref-index", "", true, &params.increase_ref_index},
585+
{"", "--disable-auto-resize-ref-image", "", false, &params.auto_resize_ref_image},
582586
};
583587

584588
auto on_mode_arg = [&](int argc, const char** argv, int index) {
@@ -1428,6 +1432,7 @@ int main(int argc, const char* argv[]) {
14281432
init_image,
14291433
ref_images.data(),
14301434
(int)ref_images.size(),
1435+
params.auto_resize_ref_image,
14311436
params.increase_ref_index,
14321437
mask_image,
14331438
params.width,

stable-diffusion.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,7 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
19701970
"seed: %" PRId64
19711971
"batch_count: %d\n"
19721972
"ref_images_count: %d\n"
1973+
"auto_resize_ref_image: %s\n"
19731974
"increase_ref_index: %s\n"
19741975
"control_strength: %.2f\n"
19751976
"photo maker: {style_strength = %.2f, id_images_count = %d, id_embed_path = %s}\n"
@@ -1984,6 +1985,7 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
19841985
sd_img_gen_params->seed,
19851986
sd_img_gen_params->batch_count,
19861987
sd_img_gen_params->ref_images_count,
1988+
BOOL_STR(sd_img_gen_params->auto_resize_ref_image),
19871989
BOOL_STR(sd_img_gen_params->increase_ref_index),
19881990
sd_img_gen_params->control_strength,
19891991
sd_img_gen_params->pm_params.style_strength,
@@ -2624,14 +2626,20 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
26242626
std::vector<ggml_tensor*> ref_latents;
26252627
for (int i = 0; i < ref_images.size(); i++) {
26262628
ggml_tensor* img;
2627-
if (sd_version_is_qwen_image(sd_ctx->sd->version)) {
2629+
if (sd_img_gen_params->auto_resize_ref_image) {
2630+
LOG_DEBUG("auto resize ref images");
26282631
sd_image_f32_t ref_image = sd_image_t_to_sd_image_f32_t(*ref_images[i]);
26292632
int VAE_IMAGE_SIZE = std::min(1024 * 1024, width * height);
26302633
double vae_width = sqrt(VAE_IMAGE_SIZE * ref_image.width / ref_image.height);
26312634
double vae_height = vae_width * ref_image.height / ref_image.width;
26322635

2633-
vae_height = round(vae_height / 32) * 32;
2634-
vae_width = round(vae_width / 32) * 32;
2636+
int factor = 16;
2637+
if (sd_version_is_qwen_image(sd_ctx->sd->version)) {
2638+
factor = 32;
2639+
}
2640+
2641+
vae_height = round(vae_height / factor) * factor;
2642+
vae_width = round(vae_width / factor) * factor;
26352643

26362644
sd_image_f32_t resized_image = resize_sd_image_f32_t(ref_image, static_cast<int>(vae_width), static_cast<int>(vae_height));
26372645
free(ref_image.data);

stable-diffusion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ typedef struct {
216216
sd_image_t init_image;
217217
sd_image_t* ref_images;
218218
int ref_images_count;
219+
bool auto_resize_ref_image;
219220
bool increase_ref_index;
220221
sd_image_t mask_image;
221222
int width;

0 commit comments

Comments
 (0)