Views
No views yet
pip install -U diffusers transformers torch sentencepiece peft controlnet-aux moviepy protobuf1import torch
2from diffusers import FluxPipeline
3
4pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
5pipe.load_lora_weights("svjack/FLUX_Shenhe_Lora")
6pipe.enable_sequential_cpu_offload()
7
8prompt = "tj_sthenhe, hair ornament,sliver hair,long hair,braid,"
9
10image = pipe(prompt,
11 num_inference_steps=24,
12 guidance_scale=3.5,
13 ).images[0]
14image.save("shenhe.png")
15
16from IPython import display
17display.Image("shenhe.png", width=512, height=512)
conda create --name py310 python=3.10 && conda activate py310 && pip install ipykernel && python -m ipykernel install --user --name py310 --display-name "py310"diffusers repository to ensure reproducibility, as newer versions may produce different results.sudo apt-get update && sudo apt-get install git-lfs ffmpeg cbm1# Install diffusers locally
2git clone https://github.com/huggingface/diffusers.git
3cd diffusers
4
5# Reset diffusers version to 0.31.dev
6git reset --hard d13b0d63c0208f2c4c078c4261caf8bf587beb3b
7pip install -e ".[torch]"
8cd ..
9
10# Install other dependencies
11pip install -U transformers sentencepiece protobuf PEFT
12
13# Clone this repo
14git clone https://github.com/svjack/Regional-Prompting-FLUX
15
16# Replace file in diffusers
17cd Regional-Prompting-FLUX
18cp transformer_flux.py ../diffusers/src/diffusers/models/transformers/transformer_flux.py
19huggingface-cli login1import torch
2from pipeline_flux_regional import RegionalFluxPipeline, RegionalFluxAttnProcessor2_0
3
4pipeline = RegionalFluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
5pipeline.load_lora_weights("svjack/FLUX_Shenhe_Lora")
6pipeline.to("cuda")RegionalFluxAttnProcessor2_0 for specific attention layers:1attn_procs = {}
2for name in pipeline.transformer.attn_processors.keys():
3 if 'transformer_blocks' in name and name.endswith("attn.processor"):
4 attn_procs[name] = RegionalFluxAttnProcessor2_0()
5 else:
6 attn_procs[name] = pipeline.transformer.attn_processors[name]
7pipeline.transformer.set_attn_processor(attn_procs)1image_width = 1280
2image_height = 768
3num_inference_steps = 24
4seed = 124
5
6base_prompt = "A snowy chinese hill in the background, A big sun rises."
7background_prompt = "a photo of a snowy chinese hill"1regional_prompt_mask_pairs = {
2 "0": {
3 "description": "A dignified woman stands in the foreground, her sliver hair and long braid adorned with a hair ornament, her face illuminated by the cold light of the snow. Her expression is one of determination and sorrow, her clothing and appearance reflecting the historical period. The snow casts a serene yet dramatic light across her features, its cold embrace enveloping her in a world of ice and frost. tj_sthenhe, hair ornament, sliver hair, long hair, braid.",
4 "mask": [128, 128, 640, 768]
5 }
6}1mask_inject_steps = 10
2double_inject_blocks_interval = 1
3single_inject_blocks_interval = 1
4base_ratio = 0.21regional_prompts = []
2regional_masks = []
3background_mask = torch.ones((image_height, image_width))
4
5for region_idx, region in regional_prompt_mask_pairs.items():
6 description = region['description']
7 mask = region['mask']
8 x1, y1, x2, y2 = mask
9 mask = torch.zeros((image_height, image_width))
10 mask[y1:y2, x1:x2] = 1.0
11 background_mask -= mask
12 regional_prompts.append(description)
13 regional_masks.append(mask)
14
15if background_mask.sum() > 0:
16 regional_prompts.append(background_prompt)
17 regional_masks.append(background_mask)
18
19image = pipeline(
20 prompt=base_prompt,
21 width=image_width, height=image_height,
22 mask_inject_steps=mask_inject_steps,
23 num_inference_steps=num_inference_steps,
24 generator=torch.Generator("cuda").manual_seed(seed),
25 joint_attention_kwargs={
26 "regional_prompts": regional_prompts,
27 "regional_masks": regional_masks,
28 "double_inject_blocks_interval": double_inject_blocks_interval,
29 "single_inject_blocks_interval": single_inject_blocks_interval,
30 "base_ratio": base_ratio
31 },
32).images[0]
33
34image.save(f"shenhe_in_snow_hill.jpg")1from IPython import display
2display.Image("shenhe_in_snow_hill.jpg", width=512, height=512)1from PIL import Image, ImageDraw
2
3def draw_transparent_rectangle(image_path, bbox, color, alpha=128, output_path=None):
4 """
5 在指定区域绘制一个半透明的矩形,并将修改后的图片保存到本地新路径。
6
7 :param image_path: 图片路径
8 :param bbox: 长度为4的列表,表示矩形的边界框 [x1, y1, x2, y2]
9 :param color: 颜色,格式为 (R, G, B)
10 :param alpha: 透明度,范围为 0(完全透明)到 255(完全不透明),默认值为 128
11 :param output_path: 保存修改后图片的路径,如果为 None,则覆盖原图
12 :return: 修改后的图片对象
13 """
14 image = Image.open(image_path).convert("RGBA")
15 overlay = Image.new('RGBA', image.size, (0, 0, 0, 0))
16 draw = ImageDraw.Draw(overlay)
17
18 x1, y1, x2, y2 = bbox
19 draw.rectangle([x1, y1, x2, y2], fill=(*color, alpha))
20
21 image = Image.alpha_composite(image, overlay)
22
23 if output_path is None:
24 output_path = image_path
25
26 image.save(output_path)
27 return image
28
29draw_transparent_rectangle("shenhe_in_snow_hill.jpg", [128, 128, 640, 768], (255, 0, 0), alpha=128, output_path="shenhe_in_snow_hill_rec.png")
30display.Image("shenhe_in_snow_hill_rec.png", width=512, height=512)base_prompt: "背景是雪中的中国山丘,一轮大太阳正在升起。"background_prompt: "一张雪中的中国山丘的照片"regional_prompt_mask_pairs 中的内容翻译如下:1{
2 "0": {
3 "description": "一位端庄的女子站在前景中,她的银发和长辫子上装饰着发饰,她的脸被雪的冷光照亮。她的表情既坚定又悲伤,她的服装和外貌反映了历史时期。雪花在她脸上投下宁静而戏剧性的光线,它的寒冷拥抱将她包裹在冰雪世界中。tj_sthenhe,发饰,银发,长发,辫子。",
4 "mask": [128, 128, 640, 768]
5 }
6}