Commit ·
2f3af37
1
Parent(s): 6c4ecfe
1.0.19 (#33)
Browse filesUnlock to allow changing model.
Files changed (7)
- fooocus_version.py+1-2
- modules/core.py+13-3
- modules/default_pipeline.py+128-28
- modules/path.py+32
- modules/sdxl_styles.py+2-2
- update_log.md+4
- webui.py+38-7
12version = '1.0.17'1version = '1.0.19'
28 unmodified lines2930313233347 unmodified lines424344454647484942 unmodified lines9293949596979819 unmodified lines11811912012112212328 unmodified linesself.clip = clipself.clip_vision = clip_vision@torch.no_grad()def load_model(ckpt_filename):7 unmodified linesreturn modellora = comfy.utils.load_torch_file(lora_filename, safe_load=True)model.unet, model.clip = comfy.sd.load_lora_for_models(model.unet, model.clip, lora, strength_model, strength_clip)return model@torch.no_grad()42 unmodified lines@torch.no_grad()def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=7.0, sampler_name='dpmpp_2m_sde_gpu',scheduler='karras', denoise=1.0, disable_noise=False, start_step=None, last_step=None,force_full_denoise=False):# SCHEDULERS = ["normal", "karras", "exponential", "simple", "ddim_uniform"]# SAMPLERS = ["euler", "euler_ancestral", "heun", "dpm_2", "dpm_2_ancestral",# "lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu",19 unmodified linespbar = comfy.utils.ProgressBar(steps)def callback(step, x0, x, total_steps):if previewer and step % 3 == 0:previewer.preview(x0, step, total_steps)pbar.update_absolute(step + 1, total_steps, None)28 unmodified lines29303132333435363738394041427 unmodified lines505152535455565742 unmodified lines10010110210310410510619 unmodified lines12612712812913013113213328 unmodified linesself.clip = clipself.clip_vision = clip_visiondef to_meta(self):if self.unet is not None:self.unet.model.to('meta')if self.clip is not None:self.clip.cond_stage_model.to('meta')if self.vae is not None:self.vae.first_stage_model.to('meta')@torch.no_grad()def load_model(ckpt_filename):7 unmodified linesreturn modellora = comfy.utils.load_torch_file(lora_filename, safe_load=True)unet, clip = comfy.sd.load_lora_for_models(model.unet, model.clip, lora, strength_model, strength_clip)return StableDiffusionModel(unet=unet, clip=clip, vae=model.vae, clip_vision=model.clip_vision)@torch.no_grad()42 unmodified lines@torch.no_grad()def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=7.0, sampler_name='dpmpp_2m_sde_gpu',scheduler='karras', denoise=1.0, disable_noise=False, start_step=None, last_step=None,force_full_denoise=False, callback_function=None):# SCHEDULERS = ["normal", "karras", "exponential", "simple", "ddim_uniform"]# SAMPLERS = ["euler", "euler_ancestral", "heun", "dpm_2", "dpm_2_ancestral",# "lms", "dpm_fast", "dpm_adaptive", "dpmpp_2s_ancestral", "dpmpp_sde", "dpmpp_sde_gpu",19 unmodified linespbar = comfy.utils.ProgressBar(steps)def callback(step, x0, x, total_steps):if callback_function is not None:callback_function(step, x0, x, total_steps)if previewer and step % 3 == 0:previewer.preview(x0, step, total_steps)pbar.update_absolute(step + 1, total_steps, None)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import modules.core as coreimport osimport torchfrom modules.path import modelfile_path, lorafile_pathxl_base_filename = os.path.join(modelfile_path, 'sd_xl_base_1.0_0.9vae.safetensors')xl_refiner_filename = os.path.join(modelfile_path, 'sd_xl_refiner_1.0_0.9vae.safetensors')xl_base_offset_lora_filename = os.path.join(lorafile_path, 'sd_xl_offset_example-lora_1.0.safetensors')xl_base = core.load_model(xl_base_filename)xl_base = core.load_lora(xl_base, xl_base_offset_lora_filename, strength_model=0.5, strength_clip=0.0)del xl_base.vaexl_refiner = core.load_model(xl_refiner_filename)@torch.no_grad()def process(positive_prompt, negative_prompt, steps, switch, width, height, image_seed, callback):positive_conditions = core.encode_prompt_condition(clip=xl_base.clip, prompt=positive_prompt)negative_conditions = core.encode_prompt_condition(clip=xl_base.clip, prompt=negative_prompt)positive_conditions_refiner = core.encode_prompt_condition(clip=xl_refiner.clip, prompt=positive_prompt)negative_conditions_refiner = core.encode_prompt_condition(clip=xl_refiner.clip, prompt=negative_prompt)empty_latent = core.generate_empty_latent(width=width, height=height, batch_size=1)sampled_latent = core.ksampler_with_refiner(model=xl_base.unet,positive=positive_conditions,negative=negative_conditions,refiner=xl_refiner.unet,refiner_positive=positive_conditions_refiner,refiner_negative=negative_conditions_refiner,refiner_switch_step=switch,latent=empty_latent,steps=steps, start_step=0, last_step=steps, disable_noise=False, force_full_denoise=True,seed=image_seed,callback_function=callback)decoded_latent = core.decode_vae(vae=xl_refiner.vae, latent_image=sampled_latent)images = core.image_to_numpy(decoded_latent)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146import modules.core as coreimport osimport torchimport modules.pathfrom comfy.model_base import SDXL, SDXLRefinerxl_base: core.StableDiffusionModel = Nonexl_base_hash = ''xl_refiner: core.StableDiffusionModel = Nonexl_refiner_hash = ''xl_base_patched: core.StableDiffusionModel = Nonexl_base_patched_hash = ''def refresh_base_model(name):global xl_base, xl_base_hash, xl_base_patched, xl_base_patched_hashif xl_base_hash == str(name):returnfilename = os.path.join(modules.path.modelfile_path, name)if xl_base is not None:xl_base.to_meta()xl_base = Nonexl_base = core.load_model(filename)if not isinstance(xl_base.unet.model, SDXL):print('Model not supported. Fooocus only support SDXL model as the base model.')xl_base = Nonexl_base_hash = ''refresh_base_model(modules.path.default_base_model_name)xl_base_hash = namexl_base_patched = xl_basexl_base_patched_hash = ''returnxl_base_hash = namexl_base_patched = xl_basexl_base_patched_hash = ''print(f'Base model loaded: {xl_base_hash}')returndef refresh_refiner_model(name):global xl_refiner, xl_refiner_hashif xl_refiner_hash == str(name):returnif name == 'None':xl_refiner = Nonexl_refiner_hash = ''print(f'Refiner unloaded.')returnfilename = os.path.join(modules.path.modelfile_path, name)if xl_refiner is not None:xl_refiner.to_meta()xl_refiner = Nonexl_refiner = core.load_model(filename)if not isinstance(xl_refiner.unet.model, SDXLRefiner):print('Model not supported. Fooocus only support SDXL refiner as the refiner.')xl_refiner = Nonexl_refiner_hash = ''print(f'Refiner unloaded.')returnxl_refiner_hash = nameprint(f'Refiner model loaded: {xl_refiner_hash}')xl_refiner.vae.first_stage_model.to('meta')xl_refiner.vae = Nonereturndef refresh_loras(loras):global xl_base, xl_base_patched, xl_base_patched_hashif xl_base_patched_hash == str(loras):returnmodel = xl_basefor name, weight in loras:if name == 'None':continuefilename = os.path.join(modules.path.lorafile_path, name)model = core.load_lora(model, filename, strength_model=weight, strength_clip=weight)xl_base_patched = modelxl_base_patched_hash = str(loras)print(f'LoRAs loaded: {xl_base_patched_hash}')returnrefresh_base_model(modules.path.default_base_model_name)refresh_refiner_model(modules.path.default_refiner_model_name)refresh_loras([(modules.path.default_lora_name, 0.5), ('None', 0.5), ('None', 0.5), ('None', 0.5), ('None', 0.5)])@torch.no_grad()def process(positive_prompt, negative_prompt, steps, switch, width, height, image_seed, callback):positive_conditions = core.encode_prompt_condition(clip=xl_base_patched.clip, prompt=positive_prompt)negative_conditions = core.encode_prompt_condition(clip=xl_base_patched.clip, prompt=negative_prompt)empty_latent = core.generate_empty_latent(width=width, height=height, batch_size=1)if xl_refiner is not None:positive_conditions_refiner = core.encode_prompt_condition(clip=xl_refiner.clip, prompt=positive_prompt)negative_conditions_refiner = core.encode_prompt_condition(clip=xl_refiner.clip, prompt=negative_prompt)sampled_latent = core.ksampler_with_refiner(model=xl_base_patched.unet,positive=positive_conditions,negative=negative_conditions,refiner=xl_refiner.unet,refiner_positive=positive_conditions_refiner,refiner_negative=negative_conditions_refiner,refiner_switch_step=switch,latent=empty_latent,steps=steps, start_step=0, last_step=steps, disable_noise=False, force_full_denoise=True,seed=image_seed,callback_function=callback)else:sampled_latent = core.ksampler(model=xl_base_patched.unet,positive=positive_conditions,negative=negative_conditions,latent=empty_latent,steps=steps, start_step=0, last_step=steps, disable_noise=False, force_full_denoise=True,seed=image_seed,callback_function=callback)decoded_latent = core.decode_vae(vae=xl_base_patched.vae, latent_image=sampled_latent)images = core.image_to_numpy(decoded_latent)
4 unmodified lines5674 unmodified linestemp_outputs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../outputs/'))os.makedirs(temp_outputs_path, exist_ok=True)4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394 unmodified linestemp_outputs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../outputs/'))os.makedirs(temp_outputs_path, exist_ok=True)default_base_model_name = 'sd_xl_base_1.0_0.9vae.safetensors'default_refiner_model_name = 'sd_xl_refiner_1.0_0.9vae.safetensors'default_lora_name = 'sd_xl_offset_example-lora_1.0.safetensors'default_lora_weight = 0.5model_filenames = []lora_filenames = []def get_model_filenames(folder_path):if not os.path.isdir(folder_path):raise ValueError("Folder path is not a valid directory.")filenames = []for filename in os.listdir(folder_path):if os.path.isfile(os.path.join(folder_path, filename)):_, file_extension = os.path.splitext(filename)if file_extension.lower() in ['.pth', '.ckpt', '.bin', '.safetensors']:filenames.append(filename)return filenamesdef update_all_model_names():global model_filenames, lora_filenamesmodel_filenames = get_model_filenames(modelfile_path)lora_filenames = get_model_filenames(lorafile_path)returnupdate_all_model_names()
1 unmodified line2345678520 unmodified lines5295305315325335345351 unmodified linestyles = [{"name": "sai-base","prompt": "{prompt}","negative_prompt": ""},520 unmodified lines]styles = {k['name']: (k['prompt'], k['negative_prompt']) for k in styles}default_style = styles['sai-base']style_keys = list(styles.keys())1 unmodified line2345678520 unmodified lines5295305315325335345351 unmodified linestyles = [{"name": "None","prompt": "{prompt}","negative_prompt": ""},520 unmodified lines]styles = {k['name']: (k['prompt'], k['negative_prompt']) for k in styles}default_style = styles['None']style_keys = list(styles.keys())
123### 1.0.17* Change default model to SDXL-1.0-vae-0.9. (This means the models will be downloaded again, but we should do it as early as possible so that all new users only need to download once. Really sorry for day-0 users. But frankly this is not too late considering that the project is just publicly available in less than 24 hours - if it has been a week then we will prefer more lightweight tricks to update.)1234567### 1.0.19* Unlock to allow changing model.### 1.0.17* Change default model to SDXL-1.0-vae-0.9. (This means the models will be downloaded again, but we should do it as early as possible so that all new users only need to download once. Really sorry for day-0 users. But frankly this is not too late considering that the project is just publicly available in less than 24 hours - if it has been a week then we will prefer more lightweight tricks to update.)
1234567891011121314151618 unmodified lines3536373839404142434416 unmodified lines616263646566676869707172737475767778798081import gradio as grimport randomimport fooocus_versionfrom modules.sdxl_styles import apply_style, style_keys, aspect_ratiosfrom modules.default_pipeline import processfrom modules.cv2win32 import close_all_preview, save_imagefrom modules.util import generate_temp_filenamefrom modules.path import temp_outputs_pathdef generate_clicked(prompt, negative_prompt, style_selction, performance_selction,aspect_ratios_selction, image_number, image_seed, progress=gr.Progress()):p_txt, n_txt = apply_style(style_selction, prompt, negative_prompt)18 unmodified linesprogress(float(done_steps) / float(all_steps), f'Step {step}/{total_steps} in the {i}-th Sampling')for i in range(image_number):imgs = process(p_txt, n_txt, steps, switch, width, height, seed, callback=callback)for x in imgs:local_temp_filename = generate_temp_filename(folder=temp_outputs_path, extension='png')save_image(local_temp_filename, x)seed += 116 unmodified lineswith gr.Row():advanced_checkbox = gr.Checkbox(label='Advanced', value=False, container=False)with gr.Column(scale=0.5, visible=False) as right_col:with gr.Tab(label='Generator Setting'):performance_selction = gr.Radio(label='Performance', choices=['Speed', 'Quality'], value='Speed')aspect_ratios_selction = gr.Radio(label='Aspect Ratios (width × height)', choices=list(aspect_ratios.keys()),value='1152×896')image_number = gr.Slider(label='Image Number', minimum=1, maximum=32, step=1, value=2)image_seed = gr.Number(label='Random Seed', value=-1, precision=0)negative_prompt = gr.Textbox(label='Negative Prompt', show_label=True, placeholder="Type prompt here.")with gr.Tab(label='Image Style'):style_selction = gr.Radio(show_label=False, container=True,choices=style_keys, value='cinematic-default')advanced_checkbox.change(lambda x: gr.update(visible=x), advanced_checkbox, right_col)ctrls = [prompt, negative_prompt, style_selction,performance_selction, aspect_ratios_selction, image_number, image_seed]run_button.click(fn=generate_clicked, inputs=ctrls, outputs=[gallery])block.launch(inbrowser=True)123456789101112131415161718192021222318 unmodified lines4243444546474849505116 unmodified lines6869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import gradio as grimport modules.pathimport randomimport fooocus_versionimport modules.default_pipeline as pipelinefrom modules.sdxl_styles import apply_style, style_keys, aspect_ratiosfrom modules.cv2win32 import close_all_preview, save_imagefrom modules.util import generate_temp_filenamedef generate_clicked(prompt, negative_prompt, style_selction, performance_selction,aspect_ratios_selction, image_number, image_seed, base_model_name, refiner_model_name,l1, w1, l2, w2, l3, w3, l4, w4, l5, w5, progress=gr.Progress()):loras = [(l1, w1), (l2, w2), (l3, w3), (l4, w4), (l5, w5)]pipeline.refresh_base_model(base_model_name)pipeline.refresh_refiner_model(refiner_model_name)pipeline.refresh_loras(loras)p_txt, n_txt = apply_style(style_selction, prompt, negative_prompt)18 unmodified linesprogress(float(done_steps) / float(all_steps), f'Step {step}/{total_steps} in the {i}-th Sampling')for i in range(image_number):imgs = pipeline.process(p_txt, n_txt, steps, switch, width, height, seed, callback=callback)for x in imgs:local_temp_filename = generate_temp_filename(folder=modules.path.temp_outputs_path, extension='png')save_image(local_temp_filename, x)seed += 116 unmodified lineswith gr.Row():advanced_checkbox = gr.Checkbox(label='Advanced', value=False, container=False)with gr.Column(scale=0.5, visible=False) as right_col:with gr.Tab(label='Setting'):performance_selction = gr.Radio(label='Performance', choices=['Speed', 'Quality'], value='Speed')aspect_ratios_selction = gr.Radio(label='Aspect Ratios (width × height)', choices=list(aspect_ratios.keys()),value='1152×896')image_number = gr.Slider(label='Image Number', minimum=1, maximum=32, step=1, value=2)image_seed = gr.Number(label='Random Seed', value=-1, precision=0)negative_prompt = gr.Textbox(label='Negative Prompt', show_label=True, placeholder="Type prompt here.")with gr.Tab(label='Style'):style_selction = gr.Radio(show_label=False, container=True,choices=style_keys, value='cinematic-default')with gr.Tab(label='Advanced'):with gr.Row():base_model = gr.Dropdown(label='SDXL Base Model', choices=modules.path.model_filenames, value=modules.path.default_base_model_name, show_label=True)refiner_model = gr.Dropdown(label='SDXL Refiner', choices=['None'] + modules.path.model_filenames, value=modules.path.default_refiner_model_name, show_label=True)with gr.Accordion(label='LoRAs', open=True):lora_ctrls = []for i in range(5):with gr.Row():lora_model = gr.Dropdown(label=f'SDXL LoRA {i+1}', choices=['None'] + modules.path.lora_filenames, value=modules.path.default_lora_name if i == 0 else 'None')lora_weight = gr.Slider(label='Weight', minimum=-2, maximum=2, step=0.01, value=modules.path.default_lora_weight)lora_ctrls += [lora_model, lora_weight]model_refresh = gr.Button(label='Refresh', value='Refresh All Files', variant='secondary')def model_refresh_clicked():modules.path.update_all_model_names()results = []results += [gr.update(choices=modules.path.model_filenames), gr.update(choices=['None'] + modules.path.model_filenames)]for i in range(5):results += [gr.update(choices=['None'] + modules.path.lora_filenames), gr.update()]return resultsmodel_refresh.click(model_refresh_clicked, [], [base_model, refiner_model] + lora_ctrls)advanced_checkbox.change(lambda x: gr.update(visible=x), advanced_checkbox, right_col)ctrls = [prompt, negative_prompt, style_selction,performance_selction, aspect_ratios_selction, image_number, image_seed]ctrls += [base_model, refiner_model] + lora_ctrlsrun_button.click(fn=generate_clicked, inputs=ctrls, outputs=[gallery])block.launch(inbrowser=True)
fooocus_version.py+1-2
modules/core.py+13-3
modules/default_pipeline.py+128-28
modules/path.py+32
modules/sdxl_styles.py+2-2
update_log.md+4
webui.py+38-7