Skip to content

Commit

Permalink
Optimize generation request default params
Browse files Browse the repository at this point in the history
  • Loading branch information
konieshadow committed Dec 28, 2023
1 parent e7aeade commit ea624b9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 194 deletions.
180 changes: 2 additions & 178 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}

Expand Down Expand Up @@ -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))
5 changes: 3 additions & 2 deletions examples/examples_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions fooocusapi/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion fooocusapi/img_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion fooocusapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions fooocusapi/models_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
2 changes: 1 addition & 1 deletion fooocusapi/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ea624b9

Please sign in to comment.