Views
No views yet
Prompt: A cat is sitting on a stone.
| Input (100px) | Output | Input (512px) | Output |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
Prompt: An anime girl under a cherry blossom tree, looking at the sky.
| Input (100px) | Output | Input (512px) | Output |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
Prompt: A hamburger with fries on a plate.
| Input (100px) | Output | Input (512px) | Output |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
git clone https://github.com/modelscope/DiffSynth-Studio.git
cd DiffSynth-Studio
pip install -e .1from diffsynth.diffusion.template import TemplatePipeline
2from diffsynth.pipelines.flux2_image import Flux2ImagePipeline, ModelConfig
3import torch
4from modelscope import dataset_snapshot_download
5from PIL import Image1pipe = Flux2ImagePipeline.from_pretrained(
2 torch_dtype=torch.bfloat16,
3 device="cuda",
4 model_configs=[
5 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-base-4B", origin_file_pattern="transformer/*.safetensors"),
6 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="text_encoder/*.safetensors"),
7 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
8 ],
9 tokenizer_config=ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="tokenizer/"),
10)
11template = TemplatePipeline.from_pretrained(
12 torch_dtype=torch.bfloat16,
13 device="cuda",
14 model_configs=[ModelConfig(model_id="DiffSynth-Studio/Template-KleinBase4B-Upscaler")],
15)
16dataset_snapshot_download(
17 "DiffSynth-Studio/examples_in_diffsynth",
18 allow_file_pattern=["templates/*"],
19 local_dir="data/examples",
20)
21image = template(
22 pipe,
23 prompt="A cat is sitting on a stone.",
24 seed=0, cfg_scale=4, num_inference_steps=50,
25 template_inputs=[{
26 "image": Image.open("data/examples/templates/image_lowres_512.jpg"),
27 "prompt": "A cat is sitting on a stone.",
28 }],
29 negative_template_inputs=[{
30 "image": Image.open("data/examples/templates/image_lowres_512.jpg"),
31 "prompt": "",
32 }],
33)
34image.save("image_Upscaler_1.png")
35image = template(
36 pipe,
37 prompt="A cat is sitting on a stone.",
38 seed=0, cfg_scale=4, num_inference_steps=50,
39 template_inputs=[{
40 "image": Image.open("data/examples/templates/image_lowres_100.jpg"),
41 "prompt": "A cat is sitting on a stone.",
42 }],
43 negative_template_inputs=[{
44 "image": Image.open("data/examples/templates/image_lowres_100.jpg"),
45 "prompt": "",
46 }],
47)
48image.save("image_Upscaler_2.png")1from diffsynth.diffusion.template import TemplatePipeline
2from diffsynth.pipelines.flux2_image import Flux2ImagePipeline, ModelConfig
3import torch
4from modelscope import dataset_snapshot_download
5from PIL import Image
6
7vram_config = {
8 "offload_dtype": "disk",
9 "offload_device": "disk",
10 "onload_dtype": torch.float8_e4m3fn,
11 "onload_device": "cpu",
12 "preparing_dtype": torch.float8_e4m3fn,
13 "preparing_device": "cuda",
14 "computation_dtype": torch.bfloat16,
15 "computation_device": "cuda",
16}
17pipe = Flux2ImagePipeline.from_pretrained(
18 torch_dtype=torch.bfloat16,
19 device="cuda",
20 model_configs=[
21 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-base-4B", origin_file_pattern="transformer/*.safetensors", **vram_config),
22 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="text_encoder/*.safetensors", **vram_config),
23 ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
24 ],
25 tokenizer_config=ModelConfig(model_id="black-forest-labs/FLUX.2-klein-4B", origin_file_pattern="tokenizer/"),
26 vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
27)
28template = TemplatePipeline.from_pretrained(
29 torch_dtype=torch.bfloat16,
30 device="cuda",
31 model_configs=[ModelConfig(model_id="DiffSynth-Studio/Template-KleinBase4B-Upscaler")],
32 lazy_loading=True,
33)
34dataset_snapshot_download(
35 "DiffSynth-Studio/examples_in_diffsynth",
36 allow_file_pattern=["templates/*"],
37 local_dir="data/examples",
38)
39image = template(
40 pipe,
41 prompt="A cat is sitting on a stone.",
42 seed=0, cfg_scale=4, num_inference_steps=50,
43 template_inputs = [{
44 "image": Image.open("data/examples/templates/image_lowres_512.jpg"),
45 "prompt": "A cat is sitting on a stone.",
46 }],
47 negative_template_inputs = [{
48 "image": Image.open("data/examples/templates/image_lowres_512.jpg"),
49 "prompt": "",
50 }],
51)
52image.save("image_Upscaler_1.png")
53image = template(
54 pipe,
55 prompt="A cat is sitting on a stone.",
56 seed=0, cfg_scale=4, num_inference_steps=50,
57 template_inputs = [{
58 "image": Image.open("data/examples/templates/image_lowres_100.jpg"),
59 "prompt": "A cat is sitting on a stone.",
60 }],
61 negative_template_inputs = [{
62 "image": Image.open("data/examples/templates/image_lowres_100.jpg"),
63 "prompt": "",
64 }],
65)
66image.save("image_Upscaler_2.png")
671modelscope download --dataset DiffSynth-Studio/diffsynth_example_dataset --include "flux2/Template-KleinBase4B-Upscaler/*" --local_dir ./data/diffsynth_example_dataset
2
3accelerate launch examples/flux2/model_training/train.py \
4 --dataset_base_path data/diffsynth_example_dataset/flux2/Template-KleinBase4B-Upscaler \
5 --dataset_metadata_path data/diffsynth_example_dataset/flux2/Template-KleinBase4B-Upscaler/metadata.jsonl \
6 --extra_inputs "template_inputs" \
7 --max_pixels 1048576 \
8 --dataset_repeat 50 \
9 --model_id_with_origin_paths "black-forest-labs/FLUX.2-klein-4B:text_encoder/*.safetensors,black-forest-labs/FLUX.2-klein-base-4B:transformer/*.safetensors,black-forest-labs/FLUX.2-klein-4B:vae/diffusion_pytorch_model.safetensors" \
10 --template_model_id_or_path "DiffSynth-Studio/Template-KleinBase4B-Upscaler:" \
11 --tokenizer_path "black-forest-labs/FLUX.2-klein-4B:tokenizer/" \
12 --learning_rate 1e-4 \
13 --num_epochs 2 \
14 --remove_prefix_in_ckpt "pipe.template_model." \
15 --output_path "./models/train/Template-KleinBase4B-Upscaler_full" \
16 --trainable_models "template_model" \
17 --use_gradient_checkpointing \
18 --find_unused_parameters