Skip to content

Commit

Permalink
2.1.826
Browse files Browse the repository at this point in the history
  • Loading branch information
lllyasviel committed Dec 12, 2023
1 parent 0d878b0 commit e8d88d3
Show file tree
Hide file tree
Showing 167 changed files with 135,010 additions and 265 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ __pycache__
*.backup
*.corrupted
sorted_styles.json
/input
/cache
/language/default.json
lena.png
lena_result.png
Expand All @@ -16,8 +18,6 @@ config.txt
config_modification_tutorial.txt
user_path_config.txt
user_path_config-deprecated.txt
build_chb.py
experiment.py
/modules/*.png
/repositories
/venv
Expand Down
30 changes: 14 additions & 16 deletions args_manager.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
from fcbh.options import enable_args_parsing
enable_args_parsing(False)
import fcbh.cli_args as fcbh_cli
import ldm_patched.modules.args_parser as args_parser


fcbh_cli.parser.add_argument("--share", action='store_true', help="Set whether to share on Gradio.")
fcbh_cli.parser.add_argument("--preset", type=str, default=None, help="Apply specified UI preset.")
args_parser.parser.add_argument("--share", action='store_true', help="Set whether to share on Gradio.")
args_parser.parser.add_argument("--preset", type=str, default=None, help="Apply specified UI preset.")

fcbh_cli.parser.add_argument("--language", type=str, default='default',
help="Translate UI using json files in [language] folder. "
args_parser.parser.add_argument("--language", type=str, default='default',
help="Translate UI using json files in [language] folder. "
"For example, [--language example] will use [language/example.json] for translation.")

# For example, https://github.com/lllyasviel/Fooocus/issues/849
fcbh_cli.parser.add_argument("--enable-smart-memory", action="store_true",
help="Force loading models to vram when the unload can be avoided. "
args_parser.parser.add_argument("--disable-offload-from-vram", action="store_true",
help="Force loading models to vram when the unload can be avoided. "
"Some Mac users may need this.")

fcbh_cli.parser.add_argument("--theme", type=str, help="launches the UI with light or dark theme", default=None)
fcbh_cli.parser.add_argument("--disable-image-log", action='store_true',
help="Prevent writing images and logs to hard drive.")
args_parser.parser.add_argument("--theme", type=str, help="launches the UI with light or dark theme", default=None)
args_parser.parser.add_argument("--disable-image-log", action='store_true',
help="Prevent writing images and logs to hard drive.")

fcbh_cli.parser.set_defaults(
args_parser.parser.set_defaults(
disable_cuda_malloc=True,
auto_launch=True,
port=None
)

fcbh_cli.args = fcbh_cli.parser.parse_args()
args_parser.args = args_parser.parser.parse_args()

# (Disable by default because of issues like https://github.com/lllyasviel/Fooocus/issues/724)
fcbh_cli.args.disable_smart_memory = not fcbh_cli.args.enable_smart_memory
args_parser.args.always_offload_from_vram = not args_parser.args.disable_offload_from_vram

args = fcbh_cli.args
args = args_parser.args
4 changes: 2 additions & 2 deletions fooocus_extras/face_crop.py → extras/face_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def crop_image(img_rgb):
global faceRestoreHelper

if faceRestoreHelper is None:
from fooocus_extras.facexlib.utils.face_restoration_helper import FaceRestoreHelper
from extras.facexlib.utils.face_restoration_helper import FaceRestoreHelper
faceRestoreHelper = FaceRestoreHelper(
upscale_factor=1,
model_rootpath=modules.config.path_controlnet,
device='cpu' # use cpu is safer since we are out of fcbh management
device='cpu' # use cpu is safer since we are out of memory management
)

faceRestoreHelper.clean_all()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import torch
from copy import deepcopy

from fooocus_extras.facexlib.utils import load_file_from_url
from extras.facexlib.utils import load_file_from_url
from .retinaface import RetinaFace


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from PIL import Image
from torchvision.models._utils import IntermediateLayerGetter as IntermediateLayerGetter

from fooocus_extras.facexlib.detection.align_trans import get_reference_facial_points, warp_and_crop_face
from fooocus_extras.facexlib.detection.retinaface_net import FPN, SSH, MobileNetV1, make_bbox_head, make_class_head, make_landmark_head
from fooocus_extras.facexlib.detection.retinaface_utils import (PriorBox, batched_decode, batched_decode_landm, decode, decode_landm,
from extras.facexlib.detection.align_trans import get_reference_facial_points, warp_and_crop_face
from extras.facexlib.detection.retinaface_net import FPN, SSH, MobileNetV1, make_bbox_head, make_class_head, make_landmark_head
from extras.facexlib.detection.retinaface_utils import (PriorBox, batched_decode, batched_decode_landm, decode, decode_landm,
py_cpu_nms)


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import torch

from fooocus_extras.facexlib.utils import load_file_from_url
from extras.facexlib.utils import load_file_from_url
from .bisenet import BiSeNet
from .parsenet import ParseNet

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import torch
from torchvision.transforms.functional import normalize

from fooocus_extras.facexlib.detection import init_detection_model
from fooocus_extras.facexlib.parsing import init_parsing_model
from fooocus_extras.facexlib.utils.misc import img2tensor, imwrite
from extras.facexlib.detection import init_detection_model
from extras.facexlib.parsing import init_parsing_model
from extras.facexlib.utils.misc import img2tensor, imwrite


def get_largest_face(det_faces, h, w):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ def paste_face_back(img, face, inverse_affine):
if __name__ == '__main__':
import os

from fooocus_extras.facexlib.detection import init_detection_model
from fooocus_extras.facexlib.utils.face_restoration_helper import get_largest_face
from fooocus_extras.facexlib.visualization import visualize_detection
from extras.facexlib.detection import init_detection_model
from extras.facexlib.utils.face_restoration_helper import get_largest_face
from extras.facexlib.visualization import visualize_detection

img_path = '/home/wxt/datasets/ffhq/ffhq_wild/00009.png'
img_name = os.splitext(os.path.basename(img_path))[0]
Expand Down
File renamed without changes.
31 changes: 13 additions & 18 deletions fooocus_extras/ip_adapter.py → extras/ip_adapter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import torch
import fcbh.clip_vision
import ldm_patched.modules.clip_vision
import safetensors.torch as sf
import fcbh.model_management as model_management
import ldm_patched.modules.model_management as model_management
import contextlib
import fcbh.ldm.modules.attention as attention
import ldm_patched.ldm.modules.attention as attention

from fooocus_extras.resampler import Resampler
from fcbh.model_patcher import ModelPatcher
from extras.resampler import Resampler
from ldm_patched.modules.model_patcher import ModelPatcher
from modules.core import numpy_to_pytorch


Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self, state_dict, plus, cross_attention_dim=768, clip_embeddings_di
self.ip_layers.load_state_dict_ordered(state_dict["ip_adapter"])


clip_vision: fcbh.clip_vision.ClipVisionModel = None
clip_vision: ldm_patched.modules.clip_vision.ClipVisionModel = None
ip_negative: torch.Tensor = None
ip_adapters: dict = {}

Expand All @@ -91,7 +91,7 @@ def load_ip_adapter(clip_vision_path, ip_negative_path, ip_adapter_path):
global clip_vision, ip_negative, ip_adapters

if clip_vision is None and isinstance(clip_vision_path, str):
clip_vision = fcbh.clip_vision.load(clip_vision_path)
clip_vision = ldm_patched.modules.clip_vision.load(clip_vision_path)

if ip_negative is None and isinstance(ip_negative_path, str):
ip_negative = sf.load_file(ip_negative_path)['data']
Expand Down Expand Up @@ -165,33 +165,28 @@ def preprocess(img, ip_adapter_path):
global ip_adapters
entry = ip_adapters[ip_adapter_path]

fcbh.model_management.load_model_gpu(clip_vision.patcher)
ldm_patched.modules.model_management.load_model_gpu(clip_vision.patcher)
pixel_values = clip_preprocess(numpy_to_pytorch(img).to(clip_vision.load_device))

if clip_vision.dtype != torch.float32:
precision_scope = torch.autocast
else:
precision_scope = lambda a, b: contextlib.nullcontext(a)

with precision_scope(fcbh.model_management.get_autocast_device(clip_vision.load_device), torch.float32):
outputs = clip_vision.model(pixel_values=pixel_values, output_hidden_states=True)
with precision_scope(ldm_patched.modules.model_management.get_autocast_device(clip_vision.load_device), torch.float32):
outputs = clip_vision.model(pixel_values=pixel_values, intermediate_output=-2)

ip_adapter = entry['ip_adapter']
ip_layers = entry['ip_layers']
image_proj_model = entry['image_proj_model']
ip_unconds = entry['ip_unconds']

if ip_adapter.plus:
cond = outputs.hidden_states[-2]
else:
cond = outputs.image_embeds

cond = cond.to(device=ip_adapter.load_device, dtype=ip_adapter.dtype)
cond = outputs[1].to(device=ip_adapter.load_device, dtype=ip_adapter.dtype)

fcbh.model_management.load_model_gpu(image_proj_model)
ldm_patched.modules.model_management.load_model_gpu(image_proj_model)
cond = image_proj_model.model(cond).to(device=ip_adapter.load_device, dtype=ip_adapter.dtype)

fcbh.model_management.load_model_gpu(ip_layers)
ldm_patched.modules.model_management.load_model_gpu(ip_layers)

if ip_unconds is None:
uncond = ip_negative.to(device=ip_adapter.load_device, dtype=ip_adapter.dtype)
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions fooocus_extras/vae_interpose.py → extras/vae_interpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import torch
import safetensors.torch as sf
import torch.nn as nn
import fcbh.model_management
import ldm_patched.modules.model_management

from fcbh.model_patcher import ModelPatcher
from ldm_patched.modules.model_patcher import ModelPatcher
from modules.config import path_vae_approx


Expand Down Expand Up @@ -76,17 +76,17 @@ def parse(x):
model.eval()
sd = sf.load_file(vae_approx_filename)
model.load_state_dict(sd)
fp16 = fcbh.model_management.should_use_fp16()
fp16 = ldm_patched.modules.model_management.should_use_fp16()
if fp16:
model = model.half()
vae_approx_model = ModelPatcher(
model=model,
load_device=fcbh.model_management.get_torch_device(),
load_device=ldm_patched.modules.model_management.get_torch_device(),
offload_device=torch.device('cpu')
)
vae_approx_model.dtype = torch.float16 if fp16 else torch.float32

fcbh.model_management.load_model_gpu(vae_approx_model)
ldm_patched.modules.model_management.load_model_gpu(vae_approx_model)

x = x_origin.to(device=vae_approx_model.load_device, dtype=vae_approx_model.dtype)
x = vae_approx_model.model(x).to(x_origin)
Expand Down
2 changes: 1 addition & 1 deletion face_experiments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cv2
import fooocus_extras.face_crop as cropper
import extras.face_crop as cropper


img = cv2.imread('lena.png')
Expand Down
2 changes: 1 addition & 1 deletion fooocus_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '2.1.825'
version = '2.1.826'
15 changes: 7 additions & 8 deletions launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
print('[System ARGV] ' + str(sys.argv))

root = os.path.dirname(os.path.abspath(__file__))
backend_path = os.path.join(root, 'backend', 'headless')
sys.path += [root, backend_path]

sys.path.append(root)
os.chdir(root)

os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"
os.environ["GRADIO_SERVER_PORT"] = "7865"
Expand Down Expand Up @@ -88,19 +87,19 @@ def download_models():
return


def ini_fcbh_args():
def ini_args():
from args_manager import args
return args


prepare_environment()
build_launcher()
args = ini_fcbh_args()
args = ini_args()


if args.cuda_device is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.cuda_device)
print("Set device to:", args.cuda_device)
if args.gpu_device_id is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_device_id)
print("Set device to:", args.gpu_device_id)


download_models()
Expand Down
Loading

0 comments on commit e8d88d3

Please sign in to comment.