From ea624b906866a9b6623ef95617d412655baad997 Mon Sep 17 00:00:00 2001 From: Konie Date: Thu, 28 Dec 2023 12:21:01 +0800 Subject: [PATCH] Optimize generation request default params --- examples/examples.py | 180 +--------------------------------------- examples/examples_v1.py | 5 +- fooocusapi/api_utils.py | 4 +- fooocusapi/img_utils.py | 4 +- fooocusapi/models.py | 2 +- fooocusapi/models_v2.py | 18 ++-- fooocusapi/worker.py | 2 +- 7 files changed, 21 insertions(+), 194 deletions(-) diff --git a/examples/examples.py b/examples/examples.py index e59001c..aa5b399 100644 --- a/examples/examples.py +++ b/examples/examples.py @@ -24,194 +24,18 @@ class Config(): cfg = Config() upscale_params = { - "prompt": "", - "negative_prompt": "", - "style_selections": [ - "Fooocus V2", - "Fooocus Enhance", - "Fooocus Sharp" - ], - "performance_selection": "Speed", - "aspect_ratios_selection": "1152*896", - "image_number": 1, - "image_seed": -1, - "sharpness": 2, - "guidance_scale": 4, - "base_model_name": "juggernautXL_version6Rundiffusion.safetensors", - "refiner_model_name": "None", - "refiner_switch": 0.5, - "loras": [ - { - "model_name": "sd_xl_offset_example-lora_1.0.safetensors", - "weight": 0.1 - } - ], - "advanced_params": { - "disable_preview": False, - "adm_scaler_positive": 1.5, - "adm_scaler_negative": 0.8, - "adm_scaler_end": 0.3, - "refiner_swap_method": "joint", - "adaptive_cfg": 7, - "sampler_name": "dpmpp_2m_sde_gpu", - "scheduler_name": "karras", - "overwrite_step": -1, - "overwrite_switch": -1, - "overwrite_width": -1, - "overwrite_height": -1, - "overwrite_vary_strength": -1, - "overwrite_upscale_strength": -1, - "mixing_image_prompt_and_vary_upscale": False, - "mixing_image_prompt_and_inpaint": False, - "debugging_cn_preprocessor": False, - "skipping_cn_preprocessor": False, - "controlnet_softness": 0.25, - "canny_low_threshold": 64, - "canny_high_threshold": 128, - "freeu_enabled": False, - "freeu_b1": 1.01, - "freeu_b2": 1.02, - "freeu_s1": 0.99, - "freeu_s2": 0.95, - "debugging_inpaint_preprocessor": False, - "inpaint_disable_initial_latent": False, - "inpaint_engine": inpaint_engine, - "inpaint_strength": 1, - "inpaint_respective_field": 1 - }, - "require_base64": False, - "async_process": False, "uov_method": "Upscale (Custom)", "upscale_value": 3, "input_image": "" } inpaint_params = { - "prompt": "", - "negative_prompt": "", - "style_selections": [ - "Fooocus V2", - "Fooocus Enhance", - "Fooocus Sharp" - ], - "performance_selection": "Speed", - "aspect_ratios_selection": "1152*896", - "image_number": 1, - "image_seed": -1, - "sharpness": 2, - "guidance_scale": 4, - "base_model_name": "juggernautXL_version6Rundiffusion.safetensors", - "refiner_model_name": "None", - "refiner_switch": 0.5, - "loras": [ - { - "model_name": "sd_xl_offset_example-lora_1.0.safetensors", - "weight": 0.1 - } - ], - "advanced_params": { - "disable_preview": False, - "adm_scaler_positive": 1.5, - "adm_scaler_negative": 0.8, - "adm_scaler_end": 0.3, - "refiner_swap_method": "joint", - "adaptive_cfg": 7, - "sampler_name": "dpmpp_2m_sde_gpu", - "scheduler_name": "karras", - "overwrite_step": -1, - "overwrite_switch": -1, - "overwrite_width": -1, - "overwrite_height": -1, - "overwrite_vary_strength": -1, - "overwrite_upscale_strength": -1, - "mixing_image_prompt_and_vary_upscale": False, - "mixing_image_prompt_and_inpaint": False, - "debugging_cn_preprocessor": False, - "skipping_cn_preprocessor": False, - "controlnet_softness": 0.25, - "canny_low_threshold": 64, - "canny_high_threshold": 128, - "freeu_enabled": False, - "freeu_b1": 1.01, - "freeu_b2": 1.02, - "freeu_s1": 0.99, - "freeu_s2": 0.95, - "debugging_inpaint_preprocessor": False, - "inpaint_disable_initial_latent": False, - "inpaint_engine": inpaint_engine, - "inpaint_strength": 1, - "inpaint_respective_field": 1 - }, - "require_base64": False, - "async_process": False, "input_image": "", "input_mask": None, "inpaint_additional_prompt": None, - "outpaint_selections": [], - "outpaint_distance_left": 0, - "outpaint_distance_right": 0, - "outpaint_distance_top": 0, - "outpaint_distance_bottom": 0, } img_prompt_params = { - "prompt": "", - "negative_prompt": "", - "style_selections": [ - "Fooocus V2", - "Fooocus Enhance", - "Fooocus Sharp" - ], - "performance_selection": "Speed", - "aspect_ratios_selection": "1152*896", - "image_number": 1, - "image_seed": -1, - "sharpness": 2, - "guidance_scale": 4, - "base_model_name": "juggernautXL_version6Rundiffusion.safetensors", - "refiner_model_name": "None", - "refiner_switch": 0.5, - "loras": [ - { - "model_name": "sd_xl_offset_example-lora_1.0.safetensors", - "weight": 0.1 - } - ], - "advanced_params": { - "disable_preview": False, - "adm_scaler_positive": 1.5, - "adm_scaler_negative": 0.8, - "adm_scaler_end": 0.3, - "refiner_swap_method": "joint", - "adaptive_cfg": 7, - "sampler_name": "dpmpp_2m_sde_gpu", - "scheduler_name": "karras", - "overwrite_step": -1, - "overwrite_switch": -1, - "overwrite_width": -1, - "overwrite_height": -1, - "overwrite_vary_strength": -1, - "overwrite_upscale_strength": -1, - "mixing_image_prompt_and_vary_upscale": False, - "mixing_image_prompt_and_inpaint": False, - "debugging_cn_preprocessor": False, - "skipping_cn_preprocessor": False, - "controlnet_softness": 0.25, - "canny_low_threshold": 64, - "canny_high_threshold": 128, - "freeu_enabled": False, - "freeu_b1": 1.01, - "freeu_b2": 1.02, - "freeu_s1": 0.99, - "freeu_s2": 0.95, - "debugging_inpaint_preprocessor": False, - "inpaint_disable_initial_latent": False, - "inpaint_engine": inpaint_engine, - "inpaint_strength": 1, - "inpaint_respective_field": 1 - }, - "require_base64": False, - "async_process": False, "image_prompts": [] } @@ -291,6 +115,6 @@ def image_prompt(img_prompt: list, params: dict) -> dict: "cn_type": "ImagePrompt" } ] -print(upscale_vary(image=image_base64)) +# print(upscale_vary(image=image_base64)) # print(inpaint_outpaint(input_image=s_base64, input_mask=m_base64)) -# print(image_prompt(img_prompt=img_prompt, params=img_prompt_params)) +print(image_prompt(img_prompt=img_prompt, params=img_prompt_params)) diff --git a/examples/examples_v1.py b/examples/examples_v1.py index 5a4fa69..19c8140 100644 --- a/examples/examples_v1.py +++ b/examples/examples_v1.py @@ -2,6 +2,7 @@ import requests import os import base64 +from examples.models import ControlNetEnum, ImagePromptParams, ImagePromptParamsJson, ImgInpaintOrOutpaintParams, ImgInpaintOrOutpaintParamsJson, ImgUpscaleOrVaryParams, ImgUpscaleOrVaryParamsJson, Text2ImgParams, UpscaleOrVaryMethod from models import * @@ -24,9 +25,9 @@ def txt2img(params: Text2ImgParams) -> dict: """ text to image """ - date = json.dumps(params.model_dump()) + data = json.dumps(params.model_dump()) response = requests.post(url=f"{cfg.fooocus_host}{cfg.text2img}", - data=date, + data=data, timeout=30) return response.json() diff --git a/fooocusapi/api_utils.py b/fooocusapi/api_utils.py index f2acfd0..35f3095 100644 --- a/fooocusapi/api_utils.py +++ b/fooocusapi/api_utils.py @@ -82,9 +82,9 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams: for img_prompt in req.image_prompts: if img_prompt.cn_img is not None: cn_img = read_input_image(img_prompt.cn_img) - if img_prompt.cn_stop is None: + if img_prompt.cn_stop is None or img_prompt.cn_stop == 0: img_prompt.cn_stop = flags.default_parameters[img_prompt.cn_type.value][0] - if img_prompt.cn_weight is None: + if img_prompt.cn_weight is None or img_prompt.cn_weight == 0: img_prompt.cn_weight = flags.default_parameters[img_prompt.cn_type.value][1] image_prompts.append( (cn_img, img_prompt.cn_stop, img_prompt.cn_weight, img_prompt.cn_type.value)) diff --git a/fooocusapi/img_utils.py b/fooocusapi/img_utils.py index 3543b2d..abf2e9c 100644 --- a/fooocusapi/img_utils.py +++ b/fooocusapi/img_utils.py @@ -35,7 +35,9 @@ def read_input_image(input_image: UploadFile) -> np.ndarray: image = np.array(pil_image) return image -def base64_to_stream(image: str) -> UploadFile: +def base64_to_stream(image: str) -> UploadFile | None: + if image == '': + return None if image.startswith('data:image'): image = image.split(sep=',', maxsplit=1)[1] image_bytes = base64.b64decode(image) diff --git a/fooocusapi/models.py b/fooocusapi/models.py index 2033bd5..146ba65 100644 --- a/fooocusapi/models.py +++ b/fooocusapi/models.py @@ -4,7 +4,7 @@ from fastapi.params import File from fastapi.exceptions import RequestValidationError -from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError, parse_obj_as +from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError from pydantic_core import InitErrorDetails from typing import List diff --git a/fooocusapi/models_v2.py b/fooocusapi/models_v2.py index e3ec1e1..2bd475e 100644 --- a/fooocusapi/models_v2.py +++ b/fooocusapi/models_v2.py @@ -3,25 +3,25 @@ class ImgUpscaleOrVaryRequestJson(Text2ImgRequest): uov_method: UpscaleOrVaryMethod = "Upscale (2x)" - upscale_value: float | None = Field(None, ge=1.0, le=5.0, description="Upscale custom value, None for default value") + upscale_value: float | None = Field(1.0, ge=1.0, le=5.0, description="Upscale custom value, 1.0 for default value") input_image: str = Field(description="Init image for upsacale or outpaint as base64") class ImgInpaintOrOutpaintRequestJson(Text2ImgRequest): input_image: str = Field(description="Init image for inpaint or outpaint as base64") - input_mask: str | None = Field(None, description="Inpaint or outpaint mask as base64") - inpaint_additional_prompt: str | None = Field(None, description="Describe what you want to inpaint") + input_mask: str | None = Field('', description="Inpaint or outpaint mask as base64") + inpaint_additional_prompt: str | None = Field('', description="Describe what you want to inpaint") outpaint_selections: List[OutpaintExpansion] = [] - outpaint_distance_left: int = Field(default=0, description="Set outpaint left distance"), - outpaint_distance_right: int = Field(default=0, description="Set outpaint right distance"), - outpaint_distance_top: int = Field(default=0, description="Set outpaint top distance"), - outpaint_distance_bottom: int = Field(default=0, description="Set outpaint bottom distance"), + outpaint_distance_left: int | None = Field(-1, description="Set outpaint left distance") + outpaint_distance_right: int | None = Field(-1, description="Set outpaint right distance") + outpaint_distance_top: int | None = Field(-1, description="Set outpaint top distance") + outpaint_distance_bottom: int | None = Field(-1, description="Set outpaint bottom distance") class ImagePromptJson(BaseModel): cn_img: str | None = Field(None, description="Input image for image prompt as base64") - cn_stop: float | None = Field(None, ge=0, le=1, description="Stop at for image prompt, None for default value") - cn_weight: float | None = Field(None, ge=0, le=2, description="Weight for image prompt, None for default value") + cn_stop: float | None = Field(0, ge=0, le=1, description="Stop at for image prompt, 0 for default value") + cn_weight: float | None = Field(0, ge=0, le=2, description="Weight for image prompt, 0 for default value") cn_type: ControlNetType = Field(default=ControlNetType.cn_ip, description="ControlNet type for image prompt") diff --git a/fooocusapi/worker.py b/fooocusapi/worker.py index e561440..a2930f0 100644 --- a/fooocusapi/worker.py +++ b/fooocusapi/worker.py @@ -470,7 +470,7 @@ def yield_result(_, imgs, tasks): print(f'Image upscaled.') f = 1.0 - if upscale_value is not None: + if upscale_value is not None and upscale_value > 1.0: f = upscale_value else: pattern = r"([0-9]+(?:\.[0-9]+)?)x"