Team Ai
Modelpublic

DiffSynth-Studio/LoRA-Encoder-FLUX.1-Dev

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
README_from_modelscope.md119 linesDownload Raw Back to root
1---2frameworks:3- Pytorch4license: Apache License 2.05tasks:6- text-to-image-synthesis7 8#model-type:9##如 gpt、phi、llama、chatglm、baichuan 等10#- gpt11 12#domain:13##如 nlp、cv、audio、multi-modal14#- nlp15 16#language:17##语言代码列表 https://help.aliyun.com/document_detail/215387.html?spm=a2c4g.11186623.0.0.9f8d7467kni6Aa18#- cn 19 20#metrics:21##如 CIDEr、Blue、ROUGE 等22#- CIDEr23 24#tags:25##各种自定义,包括 pretrained、fine-tuned、instruction-tuned、RL-tuned 等训练方法和其他26#- pretrained27 28#tools:29##如 vllm、fastchat、llamacpp、AdaSeq 等30#- vllm31---32# LoRA 编码器(FLUX.1-Dev)33 34本模型可以将 FLUX 模型的 LoRA 模型编码为 Embedding 向量,激发出 LoRA 模型的能力。35 36以 LoRA 模型 [VoidOc/F.1_动物森友会LoRA](https://www.modelscope.cn/models/VoidOc/flux_animal_forest1) 为例,LoRA 编码器有以下几种使用方法。37 38## 使用方法1:LoRA 用途推断39 40给定一个 LoRA 模型,在没有任何额外信息的条件下,使用空提示词可以直接激发 LoRA 模型的能力,进而推断出 LoRA 的用途。41 42提示词:`""`43 44|不使用 LoRA 编码器|使用 LoRA 编码器|45|-|-|46|![](./assets/image_1_origin.jpg)|![](./assets/image_1.jpg)|47 48## 使用方法2:免触发词激发 LoRA 能力49 50无需填写触发词,即可自动激发 LoRA 的能力。51 52提示词:`"a car"`53 54|不使用 LoRA 编码器|使用 LoRA 编码器|55|-|-|56|![](./assets/image_2_origin.jpg)|![](./assets/image_2.jpg)|57 58## 使用方法3:LoRA 强度控制59 60我们预留了一个额外的参数 `scale`,控制 LoRA 对模型生成图像的影响大小。61 62在下面的例子中,提示词为“a cat”,当 `scale=1` 时,LoRA 强度为最大,画面中生成了动物森友会中的角色和一只猫;当 `scale=0.5` 时,LoRA 强度被减弱,画面中生成了动物森友会中的猫猫角色。`scale` 的最优数值与 LoRA 模型本身有关,我们建议在角色 LoRA 上使用较大的数值,在风格 LoRA 上使用较小的数值。63 64提示词:`"a cat"`65 66|`scale=1`|`scale=0.5`|67|-|-|68|![](./assets/image_3.jpg)|![](./assets/image_3_scale.jpg)|69 70## 推理代码71 72```73git clone https://github.com/modelscope/DiffSynth-Studio.git  74cd DiffSynth-Studio75pip install -e .76```77 78```python79import torch80from diffsynth.pipelines.flux_image_new import FluxImagePipeline, ModelConfig81 82 83pipe = FluxImagePipeline.from_pretrained(84    torch_dtype=torch.bfloat16,85    device="cuda",86    model_configs=[87        ModelConfig(model_id="black-forest-labs/FLUX.1-dev", origin_file_pattern="flux1-dev.safetensors"),88        ModelConfig(model_id="black-forest-labs/FLUX.1-dev", origin_file_pattern="text_encoder/model.safetensors"),89        ModelConfig(model_id="black-forest-labs/FLUX.1-dev", origin_file_pattern="text_encoder_2/"),90        ModelConfig(model_id="black-forest-labs/FLUX.1-dev", origin_file_pattern="ae.safetensors"),91        ModelConfig(model_id="DiffSynth-Studio/LoRA-Encoder-FLUX.1-Dev", origin_file_pattern="model.safetensors"),92    ],93)94pipe.enable_lora_magic()95 96lora = ModelConfig(model_id="VoidOc/flux_animal_forest1", origin_file_pattern="20.safetensors")97pipe.load_lora(pipe.dit, lora, hotload=True) # Use `pipe.clear_lora()` to drop the loaded LoRA.98 99# Empty prompt can automatically activate LoRA capabilities.100image = pipe(prompt="", seed=0, lora_encoder_inputs=lora)101image.save("image_1.jpg")102 103image = pipe(prompt="", seed=0)104image.save("image_1_origin.jpg")105 106# Prompt without trigger words can also activate LoRA capabilities.107image = pipe(prompt="a car", seed=0, lora_encoder_inputs=lora)108image.save("image_2.jpg")109 110image = pipe(prompt="a car", seed=0,)111image.save("image_2_origin.jpg")112 113# Adjust the activation intensity through the scale parameter.114image = pipe(prompt="a cat", seed=0, lora_encoder_inputs=lora, lora_encoder_scale=1.0)115image.save("image_3.jpg")116 117image = pipe(prompt="a cat", seed=0, lora_encoder_inputs=lora, lora_encoder_scale=0.5)118image.save("image_3_scale.jpg")119```