Spaces:
Build error
Build error
Upload 69 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +65 -35
- Dockerfile +21 -0
- README.md +13 -10
- app.py +35 -0
- check_packages.py +4 -0
- log.py +78 -0
- requirements.txt +10 -0
- wfControl/LICENSE +201 -0
- wfControl/README.md +170 -0
- wfControl/assets/book.jpg +0 -0
- wfControl/assets/cartoon_boy.png +3 -0
- wfControl/assets/clock.jpg +3 -0
- wfControl/assets/coffee.png +0 -0
- wfControl/assets/demo/book_omini.jpg +0 -0
- wfControl/assets/demo/clock_omini.jpg +0 -0
- wfControl/assets/demo/demo_this_is_omini_control.jpg +3 -0
- wfControl/assets/demo/dreambooth_res.jpg +3 -0
- wfControl/assets/demo/man_omini.jpg +0 -0
- wfControl/assets/demo/monalisa_omini.jpg +3 -0
- wfControl/assets/demo/oranges_omini.jpg +0 -0
- wfControl/assets/demo/panda_omini.jpg +0 -0
- wfControl/assets/demo/penguin_omini.jpg +0 -0
- wfControl/assets/demo/rc_car_omini.jpg +0 -0
- wfControl/assets/demo/room_corner_canny.jpg +0 -0
- wfControl/assets/demo/room_corner_coloring.jpg +0 -0
- wfControl/assets/demo/room_corner_deblurring.jpg +0 -0
- wfControl/assets/demo/room_corner_depth.jpg +0 -0
- wfControl/assets/demo/scene_variation.jpg +3 -0
- wfControl/assets/demo/shirt_omini.jpg +0 -0
- wfControl/assets/demo/try_on.jpg +3 -0
- wfControl/assets/monalisa.jpg +3 -0
- wfControl/assets/oranges.jpg +0 -0
- wfControl/assets/penguin.jpg +0 -0
- wfControl/assets/rc_car.jpg +3 -0
- wfControl/assets/room_corner.jpg +3 -0
- wfControl/assets/test_in.jpg +0 -0
- wfControl/assets/test_out.jpg +0 -0
- wfControl/assets/tshirt.jpg +3 -0
- wfControl/assets/vase.jpg +0 -0
- wfControl/assets/vase_hq.jpg +3 -0
- wfControl/examples/inpainting.ipynb +143 -0
- wfControl/examples/spatial.ipynb +184 -0
- wfControl/examples/subject.ipynb +214 -0
- wfControl/examples/subject_1024.ipynb +221 -0
- wfControl/requirements.txt +7 -0
- wfControl/src/flux/block.py +339 -0
- wfControl/src/flux/condition.py +138 -0
- wfControl/src/flux/generate.py +321 -0
- wfControl/src/flux/lora_controller.py +75 -0
- wfControl/src/flux/pipeline_tools.py +52 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,65 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
OminiControl/assets/cartoon_boy.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
OminiControl/assets/clock.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
OminiControl/assets/demo/demo_this_is_omini_control.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
OminiControl/assets/demo/dreambooth_res.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
OminiControl/assets/demo/monalisa_omini.jpg filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
OminiControl/assets/demo/scene_variation.jpg filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
OminiControl/assets/demo/try_on.jpg filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
OminiControl/assets/monalisa.jpg filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
OminiControl/assets/rc_car.jpg filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
OminiControl/assets/room_corner.jpg filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
OminiControl/assets/tshirt.jpg filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
OminiControl/assets/vase_hq.jpg filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
examples/breakingbad.jpg filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
examples/DistractedBoyfriend.webp filter=lfs diff=lfs merge=lfs -text
|
| 50 |
+
examples/doge.jpg filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
examples/oiiai.png filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
examples/PulpFiction.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
examples/steve.webp filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
wfControl/assets/cartoon_boy.png filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
wfControl/assets/clock.jpg filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
wfControl/assets/demo/demo_this_is_omini_control.jpg filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
wfControl/assets/demo/dreambooth_res.jpg filter=lfs diff=lfs merge=lfs -text
|
| 58 |
+
wfControl/assets/demo/monalisa_omini.jpg filter=lfs diff=lfs merge=lfs -text
|
| 59 |
+
wfControl/assets/demo/scene_variation.jpg filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
wfControl/assets/demo/try_on.jpg filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
wfControl/assets/monalisa.jpg filter=lfs diff=lfs merge=lfs -text
|
| 62 |
+
wfControl/assets/rc_car.jpg filter=lfs diff=lfs merge=lfs -text
|
| 63 |
+
wfControl/assets/room_corner.jpg filter=lfs diff=lfs merge=lfs -text
|
| 64 |
+
wfControl/assets/tshirt.jpg filter=lfs diff=lfs merge=lfs -text
|
| 65 |
+
wfControl/assets/vase_hq.jpg filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY . .
|
| 5 |
+
|
| 6 |
+
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 git
|
| 7 |
+
|
| 8 |
+
# Gỡ bản cũ (nếu bị inject sẵn)
|
| 9 |
+
RUN pip uninstall -y diffusers huggingface_hub || true
|
| 10 |
+
|
| 11 |
+
# Clone và cài diffusers v0.17.1 từ GitHub
|
| 12 |
+
RUN git clone --branch v0.17.1 https://github.com/huggingface/diffusers.git /tmp/diffusers && \
|
| 13 |
+
pip install /tmp/diffusers && \
|
| 14 |
+
rm -rf /tmp/diffusers
|
| 15 |
+
|
| 16 |
+
RUN pip install huggingface_hub==0.17.3
|
| 17 |
+
|
| 18 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# 🔍 CMD để test xem diffusers đã thực sự được cài chưa
|
| 21 |
+
CMD ["python", "-c", "import diffusers; print('✅ Diffusers version:', diffusers.__version__)"]
|
README.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Wayfu
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: docker
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Wayfu Art V2
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: docker
|
| 7 |
+
sdk_version: 5.23.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: unknown
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io, base64, traceback
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from PIL import Image
|
| 5 |
+
try:
|
| 6 |
+
from wfcontrol import generate_image
|
| 7 |
+
except Exception as e:
|
| 8 |
+
import traceback
|
| 9 |
+
print("🔥 Import failed:")
|
| 10 |
+
print(traceback.format_exc())
|
| 11 |
+
raise e
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
@app.post("/transform")
|
| 16 |
+
async def transform_image_api(request: Request):
|
| 17 |
+
try:
|
| 18 |
+
data = await request.json()
|
| 19 |
+
img_data = data["image"].split(",")[1]
|
| 20 |
+
prompt = data.get("prompt", "Studio Ghibli")
|
| 21 |
+
style = data.get("style", "ghibli")
|
| 22 |
+
|
| 23 |
+
img = Image.open(io.BytesIO(base64.b64decode(img_data))).convert("RGB")
|
| 24 |
+
|
| 25 |
+
result = generate_image(input_image=img, prompt=prompt, style=style)
|
| 26 |
+
|
| 27 |
+
buffer = io.BytesIO()
|
| 28 |
+
result.save(buffer, format="JPEG")
|
| 29 |
+
result_b64 = base64.b64encode(buffer.getvalue()).decode()
|
| 30 |
+
|
| 31 |
+
return JSONResponse(content={"image": "data:image/jpeg;base64," + result_b64})
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print("🔥 ERROR:", traceback.format_exc())
|
| 35 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
check_packages.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
output = subprocess.getoutput("pip list")
|
| 3 |
+
print("📦 Installed packages:")
|
| 4 |
+
print(output)
|
log.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import boto3
|
| 2 |
+
import uuid
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MAX_PIXELS = 2048
|
| 11 |
+
|
| 12 |
+
AWS_BUCKET_NAME = os.environ.get("AWS_BUCKET_NAME", "")
|
| 13 |
+
AWS_INFERENCE_LOG_TABLE = os.environ.get("AWS_INFERENCE_LOG_TABLE", "")
|
| 14 |
+
AWS_FEEDBACK_LOG_TABLE = os.environ.get("AWS_FEEDBACK_LOG_TABLE", "")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
AWS_REGION = os.environ.get("AWS_REGION", "")
|
| 18 |
+
AWS_ACCESS_ID = os.environ.get("AWS_ACCESS_ID", "")
|
| 19 |
+
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY", "")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
aws_cfg = {
|
| 23 |
+
"aws_access_key_id": AWS_ACCESS_ID,
|
| 24 |
+
"aws_secret_access_key": AWS_ACCESS_KEY,
|
| 25 |
+
"region_name": AWS_REGION,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
s3_client = boto3.client("s3", **aws_cfg)
|
| 29 |
+
dynamodb = boto3.resource("dynamodb", **aws_cfg)
|
| 30 |
+
|
| 31 |
+
inference_log = dynamodb.Table(AWS_INFERENCE_LOG_TABLE)
|
| 32 |
+
feedback_log = dynamodb.Table(AWS_FEEDBACK_LOG_TABLE)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def get_metadata():
|
| 36 |
+
return {
|
| 37 |
+
"_id": uuid.uuid4().hex,
|
| 38 |
+
"created_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def insert_log(table_type: str, data: dict):
|
| 43 |
+
assert table_type in ["inference", "feedback"], "Invalid table type"
|
| 44 |
+
table = inference_log if table_type == "inference" else feedback_log
|
| 45 |
+
metadata = get_metadata()
|
| 46 |
+
response = table.put_item(
|
| 47 |
+
Item={
|
| 48 |
+
**data,
|
| 49 |
+
**metadata,
|
| 50 |
+
}
|
| 51 |
+
)
|
| 52 |
+
return response, metadata["_id"]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Example usage:
|
| 56 |
+
# insert_log("inference", {"data": "test"})
|
| 57 |
+
# insert_log("feedback", {"data": "test"})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_image_obj(image: Image) -> BytesIO:
|
| 61 |
+
image.thumbnail((MAX_PIXELS, MAX_PIXELS))
|
| 62 |
+
image_obj = BytesIO()
|
| 63 |
+
image.save(image_obj, format="WEBP")
|
| 64 |
+
image_obj.seek(0)
|
| 65 |
+
return image_obj
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def log_image(image: Image) -> str:
|
| 69 |
+
metadata = get_metadata()
|
| 70 |
+
image_obj = get_image_obj(image)
|
| 71 |
+
s3_key = f"images/{metadata['_id']}.webp"
|
| 72 |
+
s3_client.upload_fileobj(image_obj, AWS_BUCKET_NAME, s3_key)
|
| 73 |
+
return metadata["_id"]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Example usage:
|
| 77 |
+
# image = Image.open("examples/doge.jpg")
|
| 78 |
+
# log_image(image)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
Pillow
|
| 4 |
+
numpy
|
| 5 |
+
torch==2.0.1
|
| 6 |
+
transformers==4.30.2
|
| 7 |
+
peft
|
| 8 |
+
opencv-python-headless
|
| 9 |
+
scipy
|
| 10 |
+
safetensors
|
wfControl/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [2024] [Zhenxiong Tan]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
wfControl/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OminiControl
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
<img src='./assets/demo/demo_this_is_omini_control.jpg' width='100%' />
|
| 5 |
+
<br>
|
| 6 |
+
|
| 7 |
+
<a href="https://arxiv.org/abs/2411.15098"><img src="https://img.shields.io/badge/ariXv-2411.15098-A42C25.svg" alt="arXiv"></a>
|
| 8 |
+
<a href="https://huggingface.co/Yuanshi/OminiControl"><img src="https://img.shields.io/badge/🤗_HuggingFace-Model-ffbd45.svg" alt="HuggingFace"></a>
|
| 9 |
+
<a href="https://huggingface.co/spaces/Yuanshi/OminiControl"><img src="https://img.shields.io/badge/🤗_HuggingFace-Space-ffbd45.svg" alt="HuggingFace"></a>
|
| 10 |
+
<a href="https://github.com/Yuanshi9815/Subjects200K"><img src="https://img.shields.io/badge/GitHub-Dataset-blue.svg?logo=github&" alt="GitHub"></a>
|
| 11 |
+
<a href="https://huggingface.co/datasets/Yuanshi/Subjects200K"><img src="https://img.shields.io/badge/🤗_HuggingFace-Dataset-ffbd45.svg" alt="HuggingFace"></a>
|
| 12 |
+
|
| 13 |
+
> **OminiControl: Minimal and Universal Control for Diffusion Transformer**
|
| 14 |
+
> <br>
|
| 15 |
+
> Zhenxiong Tan,
|
| 16 |
+
> [Songhua Liu](http://121.37.94.87/),
|
| 17 |
+
> [Xingyi Yang](https://adamdad.github.io/),
|
| 18 |
+
> Qiaochu Xue,
|
| 19 |
+
> and
|
| 20 |
+
> [Xinchao Wang](https://sites.google.com/site/sitexinchaowang/)
|
| 21 |
+
> <br>
|
| 22 |
+
> [Learning and Vision Lab](http://lv-nus.org/), National University of Singapore
|
| 23 |
+
> <br>
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
## Features
|
| 27 |
+
|
| 28 |
+
OminiControl is a minimal yet powerful universal control framework for Diffusion Transformer models like [FLUX](https://github.com/black-forest-labs/flux).
|
| 29 |
+
|
| 30 |
+
* **Universal Control 🌐**: A unified control framework that supports both subject-driven control and spatial control (such as edge-guided and in-painting generation).
|
| 31 |
+
|
| 32 |
+
* **Minimal Design 🚀**: Injects control signals while preserving original model structure. Only introduces 0.1% additional parameters to the base model.
|
| 33 |
+
|
| 34 |
+
## News
|
| 35 |
+
- **2024-12-26**: ⭐️ Training code are released. Now you can create your own OminiControl model by customizing any control tasks (3D, multi-view, pose-guided, try-on, etc.) with the FLUX model. Check the [training folder](./train) for more details.
|
| 36 |
+
|
| 37 |
+
## Quick Start
|
| 38 |
+
### Setup (Optional)
|
| 39 |
+
1. **Environment setup**
|
| 40 |
+
```bash
|
| 41 |
+
conda create -n omini python=3.10
|
| 42 |
+
conda activate omini
|
| 43 |
+
```
|
| 44 |
+
2. **Requirements installation**
|
| 45 |
+
```bash
|
| 46 |
+
pip install -r requirements.txt
|
| 47 |
+
```
|
| 48 |
+
### Usage example
|
| 49 |
+
1. Subject-driven generation: `examples/subject.ipynb`
|
| 50 |
+
2. In-painting: `examples/inpainting.ipynb`
|
| 51 |
+
3. Canny edge to image, depth to image, colorization, deblurring: `examples/spatial.ipynb`
|
| 52 |
+
|
| 53 |
+
### Gradio app
|
| 54 |
+
To run the Gradio app for subject-driven generation:
|
| 55 |
+
```bash
|
| 56 |
+
python -m src.gradio.gradio_app
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### Guidelines for subject-driven generation
|
| 60 |
+
1. Input images are automatically center-cropped and resized to 512x512 resolution.
|
| 61 |
+
2. When writing prompts, refer to the subject using phrases like `this item`, `the object`, or `it`. e.g.
|
| 62 |
+
1. *A close up view of this item. It is placed on a wooden table.*
|
| 63 |
+
2. *A young lady is wearing this shirt.*
|
| 64 |
+
3. The model primarily works with objects rather than human subjects currently, due to the absence of human data in training.
|
| 65 |
+
|
| 66 |
+
## Generated samples
|
| 67 |
+
### Subject-driven generation
|
| 68 |
+
<a href="https://huggingface.co/spaces/Yuanshi/OminiControl"><img src="https://img.shields.io/badge/🤗_HuggingFace-Space-ffbd45.svg" alt="HuggingFace"></a>
|
| 69 |
+
|
| 70 |
+
**Demos** (Left: condition image; Right: generated image)
|
| 71 |
+
|
| 72 |
+
<div float="left">
|
| 73 |
+
<img src='./assets/demo/oranges_omini.jpg' width='48%'/>
|
| 74 |
+
<img src='./assets/demo/rc_car_omini.jpg' width='48%' />
|
| 75 |
+
<img src='./assets/demo/clock_omini.jpg' width='48%' />
|
| 76 |
+
<img src='./assets/demo/shirt_omini.jpg' width='48%' />
|
| 77 |
+
</div>
|
| 78 |
+
|
| 79 |
+
<details>
|
| 80 |
+
<summary>Text Prompts</summary>
|
| 81 |
+
|
| 82 |
+
- Prompt1: *A close up view of this item. It is placed on a wooden table. The background is a dark room, the TV is on, and the screen is showing a cooking show. With text on the screen that reads 'Omini Control!.'*
|
| 83 |
+
- Prompt2: *A film style shot. On the moon, this item drives across the moon surface. A flag on it reads 'Omini'. The background is that Earth looms large in the foreground.*
|
| 84 |
+
- Prompt3: *In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.*
|
| 85 |
+
- Prompt4: *"On the beach, a lady sits under a beach umbrella with 'Omini' written on it. She's wearing this shirt and has a big smile on her face, with her surfboard hehind her. The sun is setting in the background. The sky is a beautiful shade of orange and purple."*
|
| 86 |
+
</details>
|
| 87 |
+
<details>
|
| 88 |
+
<summary>More results</summary>
|
| 89 |
+
|
| 90 |
+
* Try on:
|
| 91 |
+
<img src='./assets/demo/try_on.jpg'/>
|
| 92 |
+
* Scene variations:
|
| 93 |
+
<img src='./assets/demo/scene_variation.jpg'/>
|
| 94 |
+
* Dreambooth dataset:
|
| 95 |
+
<img src='./assets/demo/dreambooth_res.jpg'/>
|
| 96 |
+
* Oye-cartoon finetune:
|
| 97 |
+
<div float="left">
|
| 98 |
+
<img src='./assets/demo/man_omini.jpg' width='48%' />
|
| 99 |
+
<img src='./assets/demo/panda_omini.jpg' width='48%' />
|
| 100 |
+
</div>
|
| 101 |
+
</details>
|
| 102 |
+
|
| 103 |
+
### Spatially aligned control
|
| 104 |
+
1. **Image Inpainting** (Left: original image; Center: masked image; Right: filled image)
|
| 105 |
+
- Prompt: *The Mona Lisa is wearing a white VR headset with 'Omini' written on it.*
|
| 106 |
+
</br>
|
| 107 |
+
<img src='./assets/demo/monalisa_omini.jpg' width='700px' />
|
| 108 |
+
- Prompt: *A yellow book with the word 'OMINI' in large font on the cover. The text 'for FLUX' appears at the bottom.*
|
| 109 |
+
</br>
|
| 110 |
+
<img src='./assets/demo/book_omini.jpg' width='700px' />
|
| 111 |
+
2. **Other spatially aligned tasks** (Canny edge to image, depth to image, colorization, deblurring)
|
| 112 |
+
</br>
|
| 113 |
+
<details>
|
| 114 |
+
<summary>Click to show</summary>
|
| 115 |
+
<div float="left">
|
| 116 |
+
<img src='./assets/demo/room_corner_canny.jpg' width='48%'/>
|
| 117 |
+
<img src='./assets/demo/room_corner_depth.jpg' width='48%' />
|
| 118 |
+
<img src='./assets/demo/room_corner_coloring.jpg' width='48%' />
|
| 119 |
+
<img src='./assets/demo/room_corner_deblurring.jpg' width='48%' />
|
| 120 |
+
</div>
|
| 121 |
+
|
| 122 |
+
Prompt: *A light gray sofa stands against a white wall, featuring a black and white geometric patterned pillow. A white side table sits next to the sofa, topped with a white adjustable desk lamp and some books. Dark hardwood flooring contrasts with the pale walls and furniture.*
|
| 123 |
+
</details>
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
## Models
|
| 129 |
+
|
| 130 |
+
**Subject-driven control:**
|
| 131 |
+
| Model | Base model | Description | Resolution |
|
| 132 |
+
| ------------------------------------------------------------------------------------------------ | -------------- | -------------------------------------------------------------------------------------------------------- | ------------ |
|
| 133 |
+
| [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `subject` | FLUX.1-schnell | The model used in the paper. | (512, 512) |
|
| 134 |
+
| [`omini`](https://huggingface.co/Yuanshi/OminiControl/tree/main/omini) / `subject_512` | FLUX.1-schnell | The model has been fine-tuned on a larger dataset. | (512, 512) |
|
| 135 |
+
| [`omini`](https://huggingface.co/Yuanshi/OminiControl/tree/main/omini) / `subject_1024` | FLUX.1-schnell | The model has been fine-tuned on a larger dataset and accommodates higher resolution. (To be released) | (1024, 1024) |
|
| 136 |
+
| [`oye-cartoon`](https://huggingface.co/saquiboye/oye-cartoon) | FLUX.1-dev | The model has been fine-tuned on [oye-cartoon](https://huggingface.co/datasets/saquiboye/oye-cartoon) dataset by [@saquib764](https://github.com/Saquib764) | (512, 512) |
|
| 137 |
+
|
| 138 |
+
**Spatial aligned control:**
|
| 139 |
+
| Model | Base model | Description | Resolution |
|
| 140 |
+
| --------------------------------------------------------------------------------------------------------- | ---------- | -------------------------------------------------------------------------- | ------------ |
|
| 141 |
+
| [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `<task_name>` | FLUX.1 | Canny edge to image, depth to image, colorization, deblurring, in-painting | (512, 512) |
|
| 142 |
+
| [`experimental`](https://huggingface.co/Yuanshi/OminiControl/tree/main/experimental) / `<task_name>_1024` | FLUX.1 | Supports higher resolution.(To be released) | (1024, 1024) |
|
| 143 |
+
|
| 144 |
+
## Community Extensions
|
| 145 |
+
- [ComfyUI-Diffusers-OminiControl](https://github.com/Macoron/ComfyUI-Diffusers-OminiControl) - ComfyUI integration by [@Macoron](https://github.com/Macoron)
|
| 146 |
+
- [ComfyUI_RH_OminiControl](https://github.com/HM-RunningHub/ComfyUI_RH_OminiControl) - ComfyUI integration by [@HM-RunningHub](https://github.com/HM-RunningHub)
|
| 147 |
+
|
| 148 |
+
## Limitations
|
| 149 |
+
1. The model's subject-driven generation primarily works with objects rather than human subjects due to the absence of human data in training.
|
| 150 |
+
2. The subject-driven generation model may not work well with `FLUX.1-dev`.
|
| 151 |
+
3. The released model currently only supports the resolution of 512x512.
|
| 152 |
+
|
| 153 |
+
## Training
|
| 154 |
+
Training instructions can be found in this [folder](./train).
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
## To-do
|
| 158 |
+
- [x] Release the training code.
|
| 159 |
+
- [ ] Release the model for higher resolution (1024x1024).
|
| 160 |
+
|
| 161 |
+
## Citation
|
| 162 |
+
```
|
| 163 |
+
@article{tan2024ominicontrol,
|
| 164 |
+
title={Ominicontrol: Minimal and universal control for diffusion transformer},
|
| 165 |
+
author={Tan, Zhenxiong and Liu, Songhua and Yang, Xingyi and Xue, Qiaochu and Wang, Xinchao},
|
| 166 |
+
journal={arXiv preprint arXiv:2411.15098},
|
| 167 |
+
volume={3},
|
| 168 |
+
year={2024}
|
| 169 |
+
}
|
| 170 |
+
```
|
wfControl/assets/book.jpg
ADDED
|
wfControl/assets/cartoon_boy.png
ADDED
|
Git LFS Details
|
wfControl/assets/clock.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/coffee.png
ADDED
|
wfControl/assets/demo/book_omini.jpg
ADDED
|
wfControl/assets/demo/clock_omini.jpg
ADDED
|
wfControl/assets/demo/demo_this_is_omini_control.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/demo/dreambooth_res.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/demo/man_omini.jpg
ADDED
|
wfControl/assets/demo/monalisa_omini.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/demo/oranges_omini.jpg
ADDED
|
wfControl/assets/demo/panda_omini.jpg
ADDED
|
wfControl/assets/demo/penguin_omini.jpg
ADDED
|
wfControl/assets/demo/rc_car_omini.jpg
ADDED
|
wfControl/assets/demo/room_corner_canny.jpg
ADDED
|
wfControl/assets/demo/room_corner_coloring.jpg
ADDED
|
wfControl/assets/demo/room_corner_deblurring.jpg
ADDED
|
wfControl/assets/demo/room_corner_depth.jpg
ADDED
|
wfControl/assets/demo/scene_variation.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/demo/shirt_omini.jpg
ADDED
|
wfControl/assets/demo/try_on.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/monalisa.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/oranges.jpg
ADDED
|
wfControl/assets/penguin.jpg
ADDED
|
wfControl/assets/rc_car.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/room_corner.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/test_in.jpg
ADDED
|
wfControl/assets/test_out.jpg
ADDED
|
wfControl/assets/tshirt.jpg
ADDED
|
Git LFS Details
|
wfControl/assets/vase.jpg
ADDED
|
wfControl/assets/vase_hq.jpg
ADDED
|
Git LFS Details
|
wfControl/examples/inpainting.ipynb
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"os.chdir(\"..\")"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"import torch\n",
|
| 21 |
+
"from diffusers.pipelines import FluxPipeline\n",
|
| 22 |
+
"from src.flux.condition import Condition\n",
|
| 23 |
+
"from PIL import Image\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"from src.flux.generate import generate, seed_everything"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"pipe = FluxPipeline.from_pretrained(\n",
|
| 35 |
+
" \"black-forest-labs/FLUX.1-dev\", torch_dtype=torch.bfloat16\n",
|
| 36 |
+
")\n",
|
| 37 |
+
"pipe = pipe.to(\"cuda\")"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": [
|
| 46 |
+
"pipe.load_lora_weights(\n",
|
| 47 |
+
" \"Yuanshi/OminiControl\",\n",
|
| 48 |
+
" weight_name=f\"experimental/fill.safetensors\",\n",
|
| 49 |
+
" adapter_name=\"fill\",\n",
|
| 50 |
+
")"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": null,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"image = Image.open(\"assets/monalisa.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"masked_image = image.copy()\n",
|
| 62 |
+
"masked_image.paste((0, 0, 0), (128, 100, 384, 220))\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"condition = Condition(\"fill\", masked_image)\n",
|
| 65 |
+
"\n",
|
| 66 |
+
"seed_everything()\n",
|
| 67 |
+
"result_img = generate(\n",
|
| 68 |
+
" pipe,\n",
|
| 69 |
+
" prompt=\"The Mona Lisa is wearing a white VR headset with 'Omini' written on it.\",\n",
|
| 70 |
+
" conditions=[condition],\n",
|
| 71 |
+
").images[0]\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 74 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 75 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 76 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 77 |
+
"concat_image"
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"cell_type": "code",
|
| 82 |
+
"execution_count": null,
|
| 83 |
+
"metadata": {},
|
| 84 |
+
"outputs": [],
|
| 85 |
+
"source": [
|
| 86 |
+
"image = Image.open(\"assets/book.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"w, h, min_dim = image.size + (min(image.size),)\n",
|
| 89 |
+
"image = image.crop(\n",
|
| 90 |
+
" ((w - min_dim) // 2, (h - min_dim) // 2, (w + min_dim) // 2, (h + min_dim) // 2)\n",
|
| 91 |
+
").resize((512, 512))\n",
|
| 92 |
+
"\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"masked_image = image.copy()\n",
|
| 95 |
+
"masked_image.paste((0, 0, 0), (150, 150, 350, 250))\n",
|
| 96 |
+
"masked_image.paste((0, 0, 0), (200, 380, 320, 420))\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"condition = Condition(\"fill\", masked_image)\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"seed_everything()\n",
|
| 101 |
+
"result_img = generate(\n",
|
| 102 |
+
" pipe,\n",
|
| 103 |
+
" prompt=\"A yellow book with the word 'OMINI' in large font on the cover. The text 'for FLUX' appears at the bottom.\",\n",
|
| 104 |
+
" conditions=[condition],\n",
|
| 105 |
+
").images[0]\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 108 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 109 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 110 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 111 |
+
"concat_image"
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "code",
|
| 116 |
+
"execution_count": null,
|
| 117 |
+
"metadata": {},
|
| 118 |
+
"outputs": [],
|
| 119 |
+
"source": []
|
| 120 |
+
}
|
| 121 |
+
],
|
| 122 |
+
"metadata": {
|
| 123 |
+
"kernelspec": {
|
| 124 |
+
"display_name": "base",
|
| 125 |
+
"language": "python",
|
| 126 |
+
"name": "python3"
|
| 127 |
+
},
|
| 128 |
+
"language_info": {
|
| 129 |
+
"codemirror_mode": {
|
| 130 |
+
"name": "ipython",
|
| 131 |
+
"version": 3
|
| 132 |
+
},
|
| 133 |
+
"file_extension": ".py",
|
| 134 |
+
"mimetype": "text/x-python",
|
| 135 |
+
"name": "python",
|
| 136 |
+
"nbconvert_exporter": "python",
|
| 137 |
+
"pygments_lexer": "ipython3",
|
| 138 |
+
"version": "3.12.7"
|
| 139 |
+
}
|
| 140 |
+
},
|
| 141 |
+
"nbformat": 4,
|
| 142 |
+
"nbformat_minor": 2
|
| 143 |
+
}
|
wfControl/examples/spatial.ipynb
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"os.chdir(\"..\")"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"import torch\n",
|
| 21 |
+
"from diffusers.pipelines import FluxPipeline\n",
|
| 22 |
+
"from src.flux.condition import Condition\n",
|
| 23 |
+
"from PIL import Image\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"from src.flux.generate import generate, seed_everything"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"pipe = FluxPipeline.from_pretrained(\n",
|
| 35 |
+
" \"black-forest-labs/FLUX.1-dev\", torch_dtype=torch.bfloat16\n",
|
| 36 |
+
")\n",
|
| 37 |
+
"pipe = pipe.to(\"cuda\")"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": [
|
| 46 |
+
"for condition_type in [\"canny\", \"depth\", \"coloring\", \"deblurring\"]:\n",
|
| 47 |
+
" pipe.load_lora_weights(\n",
|
| 48 |
+
" \"Yuanshi/OminiControl\",\n",
|
| 49 |
+
" weight_name=f\"experimental/{condition_type}.safetensors\",\n",
|
| 50 |
+
" adapter_name=condition_type,\n",
|
| 51 |
+
" )"
|
| 52 |
+
]
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"cell_type": "code",
|
| 56 |
+
"execution_count": null,
|
| 57 |
+
"metadata": {},
|
| 58 |
+
"outputs": [],
|
| 59 |
+
"source": [
|
| 60 |
+
"image = Image.open(\"assets/coffee.png\").convert(\"RGB\")\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"w, h, min_dim = image.size + (min(image.size),)\n",
|
| 63 |
+
"image = image.crop(\n",
|
| 64 |
+
" ((w - min_dim) // 2, (h - min_dim) // 2, (w + min_dim) // 2, (h + min_dim) // 2)\n",
|
| 65 |
+
").resize((512, 512))\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"prompt = \"In a bright room. A cup of a coffee with some beans on the side. They are placed on a dark wooden table.\""
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "code",
|
| 72 |
+
"execution_count": null,
|
| 73 |
+
"metadata": {},
|
| 74 |
+
"outputs": [],
|
| 75 |
+
"source": [
|
| 76 |
+
"condition = Condition(\"canny\", image)\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"seed_everything()\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"result_img = generate(\n",
|
| 81 |
+
" pipe,\n",
|
| 82 |
+
" prompt=prompt,\n",
|
| 83 |
+
" conditions=[condition],\n",
|
| 84 |
+
").images[0]\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 87 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 88 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 89 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 90 |
+
"concat_image"
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"cell_type": "code",
|
| 95 |
+
"execution_count": null,
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"outputs": [],
|
| 98 |
+
"source": [
|
| 99 |
+
"condition = Condition(\"depth\", image)\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"seed_everything()\n",
|
| 102 |
+
"\n",
|
| 103 |
+
"result_img = generate(\n",
|
| 104 |
+
" pipe,\n",
|
| 105 |
+
" prompt=prompt,\n",
|
| 106 |
+
" conditions=[condition],\n",
|
| 107 |
+
").images[0]\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 110 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 111 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 112 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 113 |
+
"concat_image"
|
| 114 |
+
]
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"cell_type": "code",
|
| 118 |
+
"execution_count": null,
|
| 119 |
+
"metadata": {},
|
| 120 |
+
"outputs": [],
|
| 121 |
+
"source": [
|
| 122 |
+
"condition = Condition(\"deblurring\", image)\n",
|
| 123 |
+
"\n",
|
| 124 |
+
"seed_everything()\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"result_img = generate(\n",
|
| 127 |
+
" pipe,\n",
|
| 128 |
+
" prompt=prompt,\n",
|
| 129 |
+
" conditions=[condition],\n",
|
| 130 |
+
").images[0]\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 133 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 134 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 135 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 136 |
+
"concat_image"
|
| 137 |
+
]
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"cell_type": "code",
|
| 141 |
+
"execution_count": null,
|
| 142 |
+
"metadata": {},
|
| 143 |
+
"outputs": [],
|
| 144 |
+
"source": [
|
| 145 |
+
"condition = Condition(\"coloring\", image)\n",
|
| 146 |
+
"\n",
|
| 147 |
+
"seed_everything()\n",
|
| 148 |
+
"\n",
|
| 149 |
+
"result_img = generate(\n",
|
| 150 |
+
" pipe,\n",
|
| 151 |
+
" prompt=prompt,\n",
|
| 152 |
+
" conditions=[condition],\n",
|
| 153 |
+
").images[0]\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"concat_image = Image.new(\"RGB\", (1536, 512))\n",
|
| 156 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 157 |
+
"concat_image.paste(condition.condition, (512, 0))\n",
|
| 158 |
+
"concat_image.paste(result_img, (1024, 0))\n",
|
| 159 |
+
"concat_image"
|
| 160 |
+
]
|
| 161 |
+
}
|
| 162 |
+
],
|
| 163 |
+
"metadata": {
|
| 164 |
+
"kernelspec": {
|
| 165 |
+
"display_name": "base",
|
| 166 |
+
"language": "python",
|
| 167 |
+
"name": "python3"
|
| 168 |
+
},
|
| 169 |
+
"language_info": {
|
| 170 |
+
"codemirror_mode": {
|
| 171 |
+
"name": "ipython",
|
| 172 |
+
"version": 3
|
| 173 |
+
},
|
| 174 |
+
"file_extension": ".py",
|
| 175 |
+
"mimetype": "text/x-python",
|
| 176 |
+
"name": "python",
|
| 177 |
+
"nbconvert_exporter": "python",
|
| 178 |
+
"pygments_lexer": "ipython3",
|
| 179 |
+
"version": "3.12.7"
|
| 180 |
+
}
|
| 181 |
+
},
|
| 182 |
+
"nbformat": 4,
|
| 183 |
+
"nbformat_minor": 2
|
| 184 |
+
}
|
wfControl/examples/subject.ipynb
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"os.chdir(\"..\")"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"import torch\n",
|
| 21 |
+
"from diffusers.pipelines import FluxPipeline\n",
|
| 22 |
+
"from src.flux.condition import Condition\n",
|
| 23 |
+
"from PIL import Image\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"from src.flux.generate import generate, seed_everything"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"pipe = FluxPipeline.from_pretrained(\n",
|
| 35 |
+
" \"black-forest-labs/FLUX.1-schnell\", torch_dtype=torch.bfloat16\n",
|
| 36 |
+
")\n",
|
| 37 |
+
"pipe = pipe.to(\"cuda\")\n",
|
| 38 |
+
"pipe.load_lora_weights(\n",
|
| 39 |
+
" \"Yuanshi/OminiControl\",\n",
|
| 40 |
+
" weight_name=f\"omini/subject_512.safetensors\",\n",
|
| 41 |
+
" adapter_name=\"subject\",\n",
|
| 42 |
+
")"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": null,
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"outputs": [],
|
| 50 |
+
"source": [
|
| 51 |
+
"image = Image.open(\"assets/penguin.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"condition = Condition(\"subject\", image, position_delta=(0, 32))\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"prompt = \"On Christmas evening, on a crowded sidewalk, this item sits on the road, covered in snow and wearing a Christmas hat.\"\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"seed_everything(0)\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"result_img = generate(\n",
|
| 61 |
+
" pipe,\n",
|
| 62 |
+
" prompt=prompt,\n",
|
| 63 |
+
" conditions=[condition],\n",
|
| 64 |
+
" num_inference_steps=8,\n",
|
| 65 |
+
" height=512,\n",
|
| 66 |
+
" width=512,\n",
|
| 67 |
+
").images[0]\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"concat_image = Image.new(\"RGB\", (1024, 512))\n",
|
| 70 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 71 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 72 |
+
"concat_image"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": null,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"image = Image.open(\"assets/tshirt.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"condition = Condition(\"subject\", image, position_delta=(0, 32))\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"prompt = \"On the beach, a lady sits under a beach umbrella. She's wearing this shirt and has a big smile on her face, with her surfboard hehind her. The sun is setting in the background. The sky is a beautiful shade of orange and purple.\"\n",
|
| 86 |
+
"\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"seed_everything()\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"result_img = generate(\n",
|
| 91 |
+
" pipe,\n",
|
| 92 |
+
" prompt=prompt,\n",
|
| 93 |
+
" conditions=[condition],\n",
|
| 94 |
+
" num_inference_steps=8,\n",
|
| 95 |
+
" height=512,\n",
|
| 96 |
+
" width=512,\n",
|
| 97 |
+
").images[0]\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"concat_image = Image.new(\"RGB\", (1024, 512))\n",
|
| 100 |
+
"concat_image.paste(condition.condition, (0, 0))\n",
|
| 101 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 102 |
+
"concat_image"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": null,
|
| 108 |
+
"metadata": {},
|
| 109 |
+
"outputs": [],
|
| 110 |
+
"source": [
|
| 111 |
+
"image = Image.open(\"assets/rc_car.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"condition = Condition(\"subject\", image, position_delta=(0, 32))\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"prompt = \"A film style shot. On the moon, this item drives across the moon surface. The background is that Earth looms large in the foreground.\"\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"seed_everything()\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"result_img = generate(\n",
|
| 120 |
+
" pipe,\n",
|
| 121 |
+
" prompt=prompt,\n",
|
| 122 |
+
" conditions=[condition],\n",
|
| 123 |
+
" num_inference_steps=8,\n",
|
| 124 |
+
" height=512,\n",
|
| 125 |
+
" width=512,\n",
|
| 126 |
+
").images[0]\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"concat_image = Image.new(\"RGB\", (1024, 512))\n",
|
| 129 |
+
"concat_image.paste(condition.condition, (0, 0))\n",
|
| 130 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 131 |
+
"concat_image"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"cell_type": "code",
|
| 136 |
+
"execution_count": null,
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"outputs": [],
|
| 139 |
+
"source": [
|
| 140 |
+
"image = Image.open(\"assets/clock.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"condition = Condition(\"subject\", image, position_delta=(0, 32))\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"prompt = \"In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.\"\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"seed_everything()\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"result_img = generate(\n",
|
| 149 |
+
" pipe,\n",
|
| 150 |
+
" prompt=prompt,\n",
|
| 151 |
+
" conditions=[condition],\n",
|
| 152 |
+
" num_inference_steps=8,\n",
|
| 153 |
+
" height=512,\n",
|
| 154 |
+
" width=512,\n",
|
| 155 |
+
").images[0]\n",
|
| 156 |
+
"\n",
|
| 157 |
+
"concat_image = Image.new(\"RGB\", (1024, 512))\n",
|
| 158 |
+
"concat_image.paste(condition.condition, (0, 0))\n",
|
| 159 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 160 |
+
"concat_image"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "code",
|
| 165 |
+
"execution_count": null,
|
| 166 |
+
"metadata": {},
|
| 167 |
+
"outputs": [],
|
| 168 |
+
"source": [
|
| 169 |
+
"image = Image.open(\"assets/oranges.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 170 |
+
"\n",
|
| 171 |
+
"condition = Condition(\"subject\", image, position_delta=(0, 32))\n",
|
| 172 |
+
"\n",
|
| 173 |
+
"prompt = \"A very close up view of this item. It is placed on a wooden table. The background is a dark room, the TV is on, and the screen is showing a cooking show.\"\n",
|
| 174 |
+
"\n",
|
| 175 |
+
"seed_everything()\n",
|
| 176 |
+
"\n",
|
| 177 |
+
"result_img = generate(\n",
|
| 178 |
+
" pipe,\n",
|
| 179 |
+
" prompt=prompt,\n",
|
| 180 |
+
" conditions=[condition],\n",
|
| 181 |
+
" num_inference_steps=8,\n",
|
| 182 |
+
" height=512,\n",
|
| 183 |
+
" width=512,\n",
|
| 184 |
+
").images[0]\n",
|
| 185 |
+
"\n",
|
| 186 |
+
"concat_image = Image.new(\"RGB\", (1024, 512))\n",
|
| 187 |
+
"concat_image.paste(condition.condition, (0, 0))\n",
|
| 188 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 189 |
+
"concat_image"
|
| 190 |
+
]
|
| 191 |
+
}
|
| 192 |
+
],
|
| 193 |
+
"metadata": {
|
| 194 |
+
"kernelspec": {
|
| 195 |
+
"display_name": "base",
|
| 196 |
+
"language": "python",
|
| 197 |
+
"name": "python3"
|
| 198 |
+
},
|
| 199 |
+
"language_info": {
|
| 200 |
+
"codemirror_mode": {
|
| 201 |
+
"name": "ipython",
|
| 202 |
+
"version": 3
|
| 203 |
+
},
|
| 204 |
+
"file_extension": ".py",
|
| 205 |
+
"mimetype": "text/x-python",
|
| 206 |
+
"name": "python",
|
| 207 |
+
"nbconvert_exporter": "python",
|
| 208 |
+
"pygments_lexer": "ipython3",
|
| 209 |
+
"version": "3.12.7"
|
| 210 |
+
}
|
| 211 |
+
},
|
| 212 |
+
"nbformat": 4,
|
| 213 |
+
"nbformat_minor": 2
|
| 214 |
+
}
|
wfControl/examples/subject_1024.ipynb
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 4,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"os.chdir(\"..\")"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"import torch\n",
|
| 21 |
+
"from diffusers.pipelines import FluxPipeline\n",
|
| 22 |
+
"from src.flux.condition import Condition\n",
|
| 23 |
+
"from PIL import Image\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"from src.flux.generate import generate, seed_everything"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"pipe = FluxPipeline.from_pretrained(\n",
|
| 35 |
+
" \"black-forest-labs/FLUX.1-schnell\", torch_dtype=torch.bfloat16\n",
|
| 36 |
+
")\n",
|
| 37 |
+
"pipe = pipe.to(\"cuda\")\n",
|
| 38 |
+
"pipe.load_lora_weights(\n",
|
| 39 |
+
" \"Yuanshi/OminiControl\",\n",
|
| 40 |
+
" weight_name=f\"omini/subject_1024_beta.safetensors\",\n",
|
| 41 |
+
" adapter_name=\"subject\",\n",
|
| 42 |
+
")"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": null,
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"outputs": [],
|
| 50 |
+
"source": [
|
| 51 |
+
"image = Image.open(\"assets/penguin.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"condition = Condition(\"subject\", image)\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"prompt = \"On Christmas evening, on a crowded sidewalk, this item sits on the road, covered in snow and wearing a Christmas hat.\"\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"seed_everything(0)\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"result_img = generate(\n",
|
| 61 |
+
" pipe,\n",
|
| 62 |
+
" prompt=prompt,\n",
|
| 63 |
+
" conditions=[condition],\n",
|
| 64 |
+
" num_inference_steps=8,\n",
|
| 65 |
+
" height=1024,\n",
|
| 66 |
+
" width=1024,\n",
|
| 67 |
+
").images[0]\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"concat_image = Image.new(\"RGB\", (1024+512, 1024))\n",
|
| 70 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 71 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 72 |
+
"concat_image"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": null,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"image = Image.open(\"assets/tshirt.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"condition = Condition(\"subject\", image)\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"prompt = \"On the beach, a lady sits under a beach umbrella. She's wearing this shirt and has a big smile on her face, with her surfboard hehind her. The sun is setting in the background. The sky is a beautiful shade of orange and purple.\"\n",
|
| 86 |
+
"\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"seed_everything(0)\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"result_img = generate(\n",
|
| 91 |
+
" pipe,\n",
|
| 92 |
+
" prompt=prompt,\n",
|
| 93 |
+
" conditions=[condition],\n",
|
| 94 |
+
" num_inference_steps=8,\n",
|
| 95 |
+
" height=1024,\n",
|
| 96 |
+
" width=1024,\n",
|
| 97 |
+
").images[0]\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"concat_image = Image.new(\"RGB\", (1024+512, 1024))\n",
|
| 100 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 101 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 102 |
+
"concat_image"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": null,
|
| 108 |
+
"metadata": {},
|
| 109 |
+
"outputs": [],
|
| 110 |
+
"source": [
|
| 111 |
+
"image = Image.open(\"assets/rc_car.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"condition = Condition(\"subject\", image)\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"prompt = \"A film style shot. On the moon, this item drives across the moon surface. The background is that Earth looms large in the foreground.\"\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"seed_everything()\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"result_img = generate(\n",
|
| 120 |
+
" pipe,\n",
|
| 121 |
+
" prompt=prompt,\n",
|
| 122 |
+
" conditions=[condition],\n",
|
| 123 |
+
" num_inference_steps=8,\n",
|
| 124 |
+
" height=1024,\n",
|
| 125 |
+
" width=1024,\n",
|
| 126 |
+
").images[0]\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"concat_image = Image.new(\"RGB\", (1024+512, 1024))\n",
|
| 129 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 130 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 131 |
+
"concat_image"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"cell_type": "code",
|
| 136 |
+
"execution_count": null,
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"outputs": [],
|
| 139 |
+
"source": [
|
| 140 |
+
"image = Image.open(\"assets/clock.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"condition = Condition(\"subject\", image)\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"prompt = \"In a Bauhaus style room, this item is placed on a shiny glass table, with a vase of flowers next to it. In the afternoon sun, the shadows of the blinds are cast on the wall.\"\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"seed_everything(0)\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"result_img = generate(\n",
|
| 149 |
+
" pipe,\n",
|
| 150 |
+
" prompt=prompt,\n",
|
| 151 |
+
" conditions=[condition],\n",
|
| 152 |
+
" num_inference_steps=8,\n",
|
| 153 |
+
" height=1024,\n",
|
| 154 |
+
" width=1024,\n",
|
| 155 |
+
").images[0]\n",
|
| 156 |
+
"\n",
|
| 157 |
+
"concat_image = Image.new(\"RGB\", (1024+512, 1024))\n",
|
| 158 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 159 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 160 |
+
"concat_image"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "code",
|
| 165 |
+
"execution_count": null,
|
| 166 |
+
"metadata": {},
|
| 167 |
+
"outputs": [],
|
| 168 |
+
"source": [
|
| 169 |
+
"image = Image.open(\"assets/oranges.jpg\").convert(\"RGB\").resize((512, 512))\n",
|
| 170 |
+
"\n",
|
| 171 |
+
"condition = Condition(\"subject\", image)\n",
|
| 172 |
+
"\n",
|
| 173 |
+
"prompt = \"A very close up view of this item. It is placed on a wooden table. The background is a dark room, the TV is on, and the screen is showing a cooking show.\"\n",
|
| 174 |
+
"\n",
|
| 175 |
+
"seed_everything()\n",
|
| 176 |
+
"\n",
|
| 177 |
+
"result_img = generate(\n",
|
| 178 |
+
" pipe,\n",
|
| 179 |
+
" prompt=prompt,\n",
|
| 180 |
+
" conditions=[condition],\n",
|
| 181 |
+
" num_inference_steps=8,\n",
|
| 182 |
+
" height=1024,\n",
|
| 183 |
+
" width=1024,\n",
|
| 184 |
+
").images[0]\n",
|
| 185 |
+
"\n",
|
| 186 |
+
"concat_image = Image.new(\"RGB\", (1024+512, 1024))\n",
|
| 187 |
+
"concat_image.paste(image, (0, 0))\n",
|
| 188 |
+
"concat_image.paste(result_img, (512, 0))\n",
|
| 189 |
+
"concat_image"
|
| 190 |
+
]
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
"cell_type": "code",
|
| 194 |
+
"execution_count": null,
|
| 195 |
+
"metadata": {},
|
| 196 |
+
"outputs": [],
|
| 197 |
+
"source": []
|
| 198 |
+
}
|
| 199 |
+
],
|
| 200 |
+
"metadata": {
|
| 201 |
+
"kernelspec": {
|
| 202 |
+
"display_name": "Python 3 (ipykernel)",
|
| 203 |
+
"language": "python",
|
| 204 |
+
"name": "python3"
|
| 205 |
+
},
|
| 206 |
+
"language_info": {
|
| 207 |
+
"codemirror_mode": {
|
| 208 |
+
"name": "ipython",
|
| 209 |
+
"version": 3
|
| 210 |
+
},
|
| 211 |
+
"file_extension": ".py",
|
| 212 |
+
"mimetype": "text/x-python",
|
| 213 |
+
"name": "python",
|
| 214 |
+
"nbconvert_exporter": "python",
|
| 215 |
+
"pygments_lexer": "ipython3",
|
| 216 |
+
"version": "3.9.21"
|
| 217 |
+
}
|
| 218 |
+
},
|
| 219 |
+
"nbformat": 4,
|
| 220 |
+
"nbformat_minor": 2
|
| 221 |
+
}
|
wfControl/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.30.2
|
| 2 |
+
peft
|
| 3 |
+
opencv-python-headless # ✅ thay cho opencv-python
|
| 4 |
+
protobuf
|
| 5 |
+
sentencepiece
|
| 6 |
+
gradio # (chỉ cần nếu bạn test local với UI)
|
| 7 |
+
|
wfControl/src/flux/block.py
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import List, Union, Optional, Dict, Any, Callable
|
| 3 |
+
from diffusers.models.attention_processor import Attention, F
|
| 4 |
+
from .lora_controller import enable_lora
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def attn_forward(
|
| 8 |
+
attn: Attention,
|
| 9 |
+
hidden_states: torch.FloatTensor,
|
| 10 |
+
encoder_hidden_states: torch.FloatTensor = None,
|
| 11 |
+
condition_latents: torch.FloatTensor = None,
|
| 12 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 13 |
+
image_rotary_emb: Optional[torch.Tensor] = None,
|
| 14 |
+
cond_rotary_emb: Optional[torch.Tensor] = None,
|
| 15 |
+
model_config: Optional[Dict[str, Any]] = {},
|
| 16 |
+
) -> torch.FloatTensor:
|
| 17 |
+
batch_size, _, _ = (
|
| 18 |
+
hidden_states.shape
|
| 19 |
+
if encoder_hidden_states is None
|
| 20 |
+
else encoder_hidden_states.shape
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
with enable_lora(
|
| 24 |
+
(attn.to_q, attn.to_k, attn.to_v), model_config.get("latent_lora", False)
|
| 25 |
+
):
|
| 26 |
+
# `sample` projections.
|
| 27 |
+
query = attn.to_q(hidden_states)
|
| 28 |
+
key = attn.to_k(hidden_states)
|
| 29 |
+
value = attn.to_v(hidden_states)
|
| 30 |
+
|
| 31 |
+
inner_dim = key.shape[-1]
|
| 32 |
+
head_dim = inner_dim // attn.heads
|
| 33 |
+
|
| 34 |
+
query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 35 |
+
key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 36 |
+
value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 37 |
+
|
| 38 |
+
if attn.norm_q is not None:
|
| 39 |
+
query = attn.norm_q(query)
|
| 40 |
+
if attn.norm_k is not None:
|
| 41 |
+
key = attn.norm_k(key)
|
| 42 |
+
|
| 43 |
+
# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states`
|
| 44 |
+
if encoder_hidden_states is not None:
|
| 45 |
+
# `context` projections.
|
| 46 |
+
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
|
| 47 |
+
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
|
| 48 |
+
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
|
| 49 |
+
|
| 50 |
+
encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
|
| 51 |
+
batch_size, -1, attn.heads, head_dim
|
| 52 |
+
).transpose(1, 2)
|
| 53 |
+
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
|
| 54 |
+
batch_size, -1, attn.heads, head_dim
|
| 55 |
+
).transpose(1, 2)
|
| 56 |
+
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view(
|
| 57 |
+
batch_size, -1, attn.heads, head_dim
|
| 58 |
+
).transpose(1, 2)
|
| 59 |
+
|
| 60 |
+
if attn.norm_added_q is not None:
|
| 61 |
+
encoder_hidden_states_query_proj = attn.norm_added_q(
|
| 62 |
+
encoder_hidden_states_query_proj
|
| 63 |
+
)
|
| 64 |
+
if attn.norm_added_k is not None:
|
| 65 |
+
encoder_hidden_states_key_proj = attn.norm_added_k(
|
| 66 |
+
encoder_hidden_states_key_proj
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# attention
|
| 70 |
+
query = torch.cat([encoder_hidden_states_query_proj, query], dim=2)
|
| 71 |
+
key = torch.cat([encoder_hidden_states_key_proj, key], dim=2)
|
| 72 |
+
value = torch.cat([encoder_hidden_states_value_proj, value], dim=2)
|
| 73 |
+
|
| 74 |
+
if image_rotary_emb is not None:
|
| 75 |
+
from diffusers.models.embeddings import apply_rotary_emb
|
| 76 |
+
|
| 77 |
+
query = apply_rotary_emb(query, image_rotary_emb)
|
| 78 |
+
key = apply_rotary_emb(key, image_rotary_emb)
|
| 79 |
+
|
| 80 |
+
if condition_latents is not None:
|
| 81 |
+
cond_query = attn.to_q(condition_latents)
|
| 82 |
+
cond_key = attn.to_k(condition_latents)
|
| 83 |
+
cond_value = attn.to_v(condition_latents)
|
| 84 |
+
|
| 85 |
+
cond_query = cond_query.view(batch_size, -1, attn.heads, head_dim).transpose(
|
| 86 |
+
1, 2
|
| 87 |
+
)
|
| 88 |
+
cond_key = cond_key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
|
| 89 |
+
cond_value = cond_value.view(batch_size, -1, attn.heads, head_dim).transpose(
|
| 90 |
+
1, 2
|
| 91 |
+
)
|
| 92 |
+
if attn.norm_q is not None:
|
| 93 |
+
cond_query = attn.norm_q(cond_query)
|
| 94 |
+
if attn.norm_k is not None:
|
| 95 |
+
cond_key = attn.norm_k(cond_key)
|
| 96 |
+
|
| 97 |
+
if cond_rotary_emb is not None:
|
| 98 |
+
cond_query = apply_rotary_emb(cond_query, cond_rotary_emb)
|
| 99 |
+
cond_key = apply_rotary_emb(cond_key, cond_rotary_emb)
|
| 100 |
+
|
| 101 |
+
if condition_latents is not None:
|
| 102 |
+
query = torch.cat([query, cond_query], dim=2)
|
| 103 |
+
key = torch.cat([key, cond_key], dim=2)
|
| 104 |
+
value = torch.cat([value, cond_value], dim=2)
|
| 105 |
+
|
| 106 |
+
if not model_config.get("union_cond_attn", True):
|
| 107 |
+
# If we don't want to use the union condition attention, we need to mask the attention
|
| 108 |
+
# between the hidden states and the condition latents
|
| 109 |
+
attention_mask = torch.ones(
|
| 110 |
+
query.shape[2], key.shape[2], device=query.device, dtype=torch.bool
|
| 111 |
+
)
|
| 112 |
+
condition_n = cond_query.shape[2]
|
| 113 |
+
attention_mask[-condition_n:, :-condition_n] = False
|
| 114 |
+
attention_mask[:-condition_n, -condition_n:] = False
|
| 115 |
+
elif model_config.get("independent_condition", False):
|
| 116 |
+
attention_mask = torch.ones(
|
| 117 |
+
query.shape[2], key.shape[2], device=query.device, dtype=torch.bool
|
| 118 |
+
)
|
| 119 |
+
condition_n = cond_query.shape[2]
|
| 120 |
+
attention_mask[-condition_n:, :-condition_n] = False
|
| 121 |
+
if hasattr(attn, "c_factor"):
|
| 122 |
+
attention_mask = torch.zeros(
|
| 123 |
+
query.shape[2], key.shape[2], device=query.device, dtype=query.dtype
|
| 124 |
+
)
|
| 125 |
+
condition_n = cond_query.shape[2]
|
| 126 |
+
bias = torch.log(attn.c_factor[0])
|
| 127 |
+
attention_mask[-condition_n:, :-condition_n] = bias
|
| 128 |
+
attention_mask[:-condition_n, -condition_n:] = bias
|
| 129 |
+
hidden_states = F.scaled_dot_product_attention(
|
| 130 |
+
query, key, value, dropout_p=0.0, is_causal=False, attn_mask=attention_mask
|
| 131 |
+
)
|
| 132 |
+
hidden_states = hidden_states.transpose(1, 2).reshape(
|
| 133 |
+
batch_size, -1, attn.heads * head_dim
|
| 134 |
+
)
|
| 135 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 136 |
+
|
| 137 |
+
if encoder_hidden_states is not None:
|
| 138 |
+
if condition_latents is not None:
|
| 139 |
+
encoder_hidden_states, hidden_states, condition_latents = (
|
| 140 |
+
hidden_states[:, : encoder_hidden_states.shape[1]],
|
| 141 |
+
hidden_states[
|
| 142 |
+
:, encoder_hidden_states.shape[1] : -condition_latents.shape[1]
|
| 143 |
+
],
|
| 144 |
+
hidden_states[:, -condition_latents.shape[1] :],
|
| 145 |
+
)
|
| 146 |
+
else:
|
| 147 |
+
encoder_hidden_states, hidden_states = (
|
| 148 |
+
hidden_states[:, : encoder_hidden_states.shape[1]],
|
| 149 |
+
hidden_states[:, encoder_hidden_states.shape[1] :],
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with enable_lora((attn.to_out[0],), model_config.get("latent_lora", False)):
|
| 153 |
+
# linear proj
|
| 154 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 155 |
+
# dropout
|
| 156 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 157 |
+
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
|
| 158 |
+
|
| 159 |
+
if condition_latents is not None:
|
| 160 |
+
condition_latents = attn.to_out[0](condition_latents)
|
| 161 |
+
condition_latents = attn.to_out[1](condition_latents)
|
| 162 |
+
|
| 163 |
+
return (
|
| 164 |
+
(hidden_states, encoder_hidden_states, condition_latents)
|
| 165 |
+
if condition_latents is not None
|
| 166 |
+
else (hidden_states, encoder_hidden_states)
|
| 167 |
+
)
|
| 168 |
+
elif condition_latents is not None:
|
| 169 |
+
# if there are condition_latents, we need to separate the hidden_states and the condition_latents
|
| 170 |
+
hidden_states, condition_latents = (
|
| 171 |
+
hidden_states[:, : -condition_latents.shape[1]],
|
| 172 |
+
hidden_states[:, -condition_latents.shape[1] :],
|
| 173 |
+
)
|
| 174 |
+
return hidden_states, condition_latents
|
| 175 |
+
else:
|
| 176 |
+
return hidden_states
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def block_forward(
|
| 180 |
+
self,
|
| 181 |
+
hidden_states: torch.FloatTensor,
|
| 182 |
+
encoder_hidden_states: torch.FloatTensor,
|
| 183 |
+
condition_latents: torch.FloatTensor,
|
| 184 |
+
temb: torch.FloatTensor,
|
| 185 |
+
cond_temb: torch.FloatTensor,
|
| 186 |
+
cond_rotary_emb=None,
|
| 187 |
+
image_rotary_emb=None,
|
| 188 |
+
model_config: Optional[Dict[str, Any]] = {},
|
| 189 |
+
):
|
| 190 |
+
use_cond = condition_latents is not None
|
| 191 |
+
with enable_lora((self.norm1.linear,), model_config.get("latent_lora", False)):
|
| 192 |
+
norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(
|
| 193 |
+
hidden_states, emb=temb
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = (
|
| 197 |
+
self.norm1_context(encoder_hidden_states, emb=temb)
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
if use_cond:
|
| 201 |
+
(
|
| 202 |
+
norm_condition_latents,
|
| 203 |
+
cond_gate_msa,
|
| 204 |
+
cond_shift_mlp,
|
| 205 |
+
cond_scale_mlp,
|
| 206 |
+
cond_gate_mlp,
|
| 207 |
+
) = self.norm1(condition_latents, emb=cond_temb)
|
| 208 |
+
|
| 209 |
+
# Attention.
|
| 210 |
+
result = attn_forward(
|
| 211 |
+
self.attn,
|
| 212 |
+
model_config=model_config,
|
| 213 |
+
hidden_states=norm_hidden_states,
|
| 214 |
+
encoder_hidden_states=norm_encoder_hidden_states,
|
| 215 |
+
condition_latents=norm_condition_latents if use_cond else None,
|
| 216 |
+
image_rotary_emb=image_rotary_emb,
|
| 217 |
+
cond_rotary_emb=cond_rotary_emb if use_cond else None,
|
| 218 |
+
)
|
| 219 |
+
attn_output, context_attn_output = result[:2]
|
| 220 |
+
cond_attn_output = result[2] if use_cond else None
|
| 221 |
+
|
| 222 |
+
# Process attention outputs for the `hidden_states`.
|
| 223 |
+
# 1. hidden_states
|
| 224 |
+
attn_output = gate_msa.unsqueeze(1) * attn_output
|
| 225 |
+
hidden_states = hidden_states + attn_output
|
| 226 |
+
# 2. encoder_hidden_states
|
| 227 |
+
context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output
|
| 228 |
+
encoder_hidden_states = encoder_hidden_states + context_attn_output
|
| 229 |
+
# 3. condition_latents
|
| 230 |
+
if use_cond:
|
| 231 |
+
cond_attn_output = cond_gate_msa.unsqueeze(1) * cond_attn_output
|
| 232 |
+
condition_latents = condition_latents + cond_attn_output
|
| 233 |
+
if model_config.get("add_cond_attn", False):
|
| 234 |
+
hidden_states += cond_attn_output
|
| 235 |
+
|
| 236 |
+
# LayerNorm + MLP.
|
| 237 |
+
# 1. hidden_states
|
| 238 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 239 |
+
norm_hidden_states = (
|
| 240 |
+
norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
|
| 241 |
+
)
|
| 242 |
+
# 2. encoder_hidden_states
|
| 243 |
+
norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states)
|
| 244 |
+
norm_encoder_hidden_states = (
|
| 245 |
+
norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None]
|
| 246 |
+
)
|
| 247 |
+
# 3. condition_latents
|
| 248 |
+
if use_cond:
|
| 249 |
+
norm_condition_latents = self.norm2(condition_latents)
|
| 250 |
+
norm_condition_latents = (
|
| 251 |
+
norm_condition_latents * (1 + cond_scale_mlp[:, None])
|
| 252 |
+
+ cond_shift_mlp[:, None]
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
# Feed-forward.
|
| 256 |
+
with enable_lora((self.ff.net[2],), model_config.get("latent_lora", False)):
|
| 257 |
+
# 1. hidden_states
|
| 258 |
+
ff_output = self.ff(norm_hidden_states)
|
| 259 |
+
ff_output = gate_mlp.unsqueeze(1) * ff_output
|
| 260 |
+
# 2. encoder_hidden_states
|
| 261 |
+
context_ff_output = self.ff_context(norm_encoder_hidden_states)
|
| 262 |
+
context_ff_output = c_gate_mlp.unsqueeze(1) * context_ff_output
|
| 263 |
+
# 3. condition_latents
|
| 264 |
+
if use_cond:
|
| 265 |
+
cond_ff_output = self.ff(norm_condition_latents)
|
| 266 |
+
cond_ff_output = cond_gate_mlp.unsqueeze(1) * cond_ff_output
|
| 267 |
+
|
| 268 |
+
# Process feed-forward outputs.
|
| 269 |
+
hidden_states = hidden_states + ff_output
|
| 270 |
+
encoder_hidden_states = encoder_hidden_states + context_ff_output
|
| 271 |
+
if use_cond:
|
| 272 |
+
condition_latents = condition_latents + cond_ff_output
|
| 273 |
+
|
| 274 |
+
# Clip to avoid overflow.
|
| 275 |
+
if encoder_hidden_states.dtype == torch.float16:
|
| 276 |
+
encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
|
| 277 |
+
|
| 278 |
+
return encoder_hidden_states, hidden_states, condition_latents if use_cond else None
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def single_block_forward(
|
| 282 |
+
self,
|
| 283 |
+
hidden_states: torch.FloatTensor,
|
| 284 |
+
temb: torch.FloatTensor,
|
| 285 |
+
image_rotary_emb=None,
|
| 286 |
+
condition_latents: torch.FloatTensor = None,
|
| 287 |
+
cond_temb: torch.FloatTensor = None,
|
| 288 |
+
cond_rotary_emb=None,
|
| 289 |
+
model_config: Optional[Dict[str, Any]] = {},
|
| 290 |
+
):
|
| 291 |
+
|
| 292 |
+
using_cond = condition_latents is not None
|
| 293 |
+
residual = hidden_states
|
| 294 |
+
with enable_lora(
|
| 295 |
+
(
|
| 296 |
+
self.norm.linear,
|
| 297 |
+
self.proj_mlp,
|
| 298 |
+
),
|
| 299 |
+
model_config.get("latent_lora", False),
|
| 300 |
+
):
|
| 301 |
+
norm_hidden_states, gate = self.norm(hidden_states, emb=temb)
|
| 302 |
+
mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states))
|
| 303 |
+
if using_cond:
|
| 304 |
+
residual_cond = condition_latents
|
| 305 |
+
norm_condition_latents, cond_gate = self.norm(condition_latents, emb=cond_temb)
|
| 306 |
+
mlp_cond_hidden_states = self.act_mlp(self.proj_mlp(norm_condition_latents))
|
| 307 |
+
|
| 308 |
+
attn_output = attn_forward(
|
| 309 |
+
self.attn,
|
| 310 |
+
model_config=model_config,
|
| 311 |
+
hidden_states=norm_hidden_states,
|
| 312 |
+
image_rotary_emb=image_rotary_emb,
|
| 313 |
+
**(
|
| 314 |
+
{
|
| 315 |
+
"condition_latents": norm_condition_latents,
|
| 316 |
+
"cond_rotary_emb": cond_rotary_emb if using_cond else None,
|
| 317 |
+
}
|
| 318 |
+
if using_cond
|
| 319 |
+
else {}
|
| 320 |
+
),
|
| 321 |
+
)
|
| 322 |
+
if using_cond:
|
| 323 |
+
attn_output, cond_attn_output = attn_output
|
| 324 |
+
|
| 325 |
+
with enable_lora((self.proj_out,), model_config.get("latent_lora", False)):
|
| 326 |
+
hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2)
|
| 327 |
+
gate = gate.unsqueeze(1)
|
| 328 |
+
hidden_states = gate * self.proj_out(hidden_states)
|
| 329 |
+
hidden_states = residual + hidden_states
|
| 330 |
+
if using_cond:
|
| 331 |
+
condition_latents = torch.cat([cond_attn_output, mlp_cond_hidden_states], dim=2)
|
| 332 |
+
cond_gate = cond_gate.unsqueeze(1)
|
| 333 |
+
condition_latents = cond_gate * self.proj_out(condition_latents)
|
| 334 |
+
condition_latents = residual_cond + condition_latents
|
| 335 |
+
|
| 336 |
+
if hidden_states.dtype == torch.float16:
|
| 337 |
+
hidden_states = hidden_states.clip(-65504, 65504)
|
| 338 |
+
|
| 339 |
+
return hidden_states if not using_cond else (hidden_states, condition_latents)
|
wfControl/src/flux/condition.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import Optional, Union, List, Tuple
|
| 3 |
+
from diffusers.pipelines import FluxPipeline
|
| 4 |
+
from PIL import Image, ImageFilter
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
from .pipeline_tools import encode_images
|
| 9 |
+
|
| 10 |
+
condition_dict = {
|
| 11 |
+
"depth": 0,
|
| 12 |
+
"canny": 1,
|
| 13 |
+
"subject": 4,
|
| 14 |
+
"coloring": 6,
|
| 15 |
+
"deblurring": 7,
|
| 16 |
+
"depth_pred": 8,
|
| 17 |
+
"fill": 9,
|
| 18 |
+
"sr": 10,
|
| 19 |
+
"cartoon": 11,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Condition(object):
|
| 24 |
+
def __init__(
|
| 25 |
+
self,
|
| 26 |
+
condition_type: str,
|
| 27 |
+
raw_img: Union[Image.Image, torch.Tensor] = None,
|
| 28 |
+
condition: Union[Image.Image, torch.Tensor] = None,
|
| 29 |
+
mask=None,
|
| 30 |
+
position_delta=None,
|
| 31 |
+
position_scale=1.0,
|
| 32 |
+
) -> None:
|
| 33 |
+
self.condition_type = condition_type
|
| 34 |
+
assert raw_img is not None or condition is not None
|
| 35 |
+
if raw_img is not None:
|
| 36 |
+
self.condition = self.get_condition(condition_type, raw_img)
|
| 37 |
+
else:
|
| 38 |
+
self.condition = condition
|
| 39 |
+
self.position_delta = position_delta
|
| 40 |
+
self.position_scale = position_scale
|
| 41 |
+
# TODO: Add mask support
|
| 42 |
+
assert mask is None, "Mask not supported yet"
|
| 43 |
+
|
| 44 |
+
def get_condition(
|
| 45 |
+
self, condition_type: str, raw_img: Union[Image.Image, torch.Tensor]
|
| 46 |
+
) -> Union[Image.Image, torch.Tensor]:
|
| 47 |
+
"""
|
| 48 |
+
Returns the condition image.
|
| 49 |
+
"""
|
| 50 |
+
if condition_type == "depth":
|
| 51 |
+
from transformers import pipeline
|
| 52 |
+
|
| 53 |
+
depth_pipe = pipeline(
|
| 54 |
+
task="depth-estimation",
|
| 55 |
+
model="LiheYoung/depth-anything-small-hf",
|
| 56 |
+
device="cuda",
|
| 57 |
+
)
|
| 58 |
+
source_image = raw_img.convert("RGB")
|
| 59 |
+
condition_img = depth_pipe(source_image)["depth"].convert("RGB")
|
| 60 |
+
return condition_img
|
| 61 |
+
elif condition_type == "canny":
|
| 62 |
+
img = np.array(raw_img)
|
| 63 |
+
edges = cv2.Canny(img, 100, 200)
|
| 64 |
+
edges = Image.fromarray(edges).convert("RGB")
|
| 65 |
+
return edges
|
| 66 |
+
elif condition_type == "subject":
|
| 67 |
+
return raw_img
|
| 68 |
+
elif condition_type == "coloring":
|
| 69 |
+
return raw_img.convert("L").convert("RGB")
|
| 70 |
+
elif condition_type == "deblurring":
|
| 71 |
+
condition_image = (
|
| 72 |
+
raw_img.convert("RGB")
|
| 73 |
+
.filter(ImageFilter.GaussianBlur(10))
|
| 74 |
+
.convert("RGB")
|
| 75 |
+
)
|
| 76 |
+
return condition_image
|
| 77 |
+
elif condition_type == "fill":
|
| 78 |
+
return raw_img.convert("RGB")
|
| 79 |
+
elif condition_type == "cartoon":
|
| 80 |
+
return raw_img.convert("RGB")
|
| 81 |
+
return self.condition
|
| 82 |
+
|
| 83 |
+
@property
|
| 84 |
+
def type_id(self) -> int:
|
| 85 |
+
"""
|
| 86 |
+
Returns the type id of the condition.
|
| 87 |
+
"""
|
| 88 |
+
return condition_dict[self.condition_type]
|
| 89 |
+
|
| 90 |
+
@classmethod
|
| 91 |
+
def get_type_id(cls, condition_type: str) -> int:
|
| 92 |
+
"""
|
| 93 |
+
Returns the type id of the condition.
|
| 94 |
+
"""
|
| 95 |
+
return condition_dict[condition_type]
|
| 96 |
+
|
| 97 |
+
def encode(
|
| 98 |
+
self, pipe: FluxPipeline, empty: bool = False
|
| 99 |
+
) -> Tuple[torch.Tensor, torch.Tensor, int]:
|
| 100 |
+
"""
|
| 101 |
+
Encodes the condition into tokens, ids and type_id.
|
| 102 |
+
"""
|
| 103 |
+
if self.condition_type in [
|
| 104 |
+
"depth",
|
| 105 |
+
"canny",
|
| 106 |
+
"subject",
|
| 107 |
+
"coloring",
|
| 108 |
+
"deblurring",
|
| 109 |
+
"depth_pred",
|
| 110 |
+
"fill",
|
| 111 |
+
"sr",
|
| 112 |
+
"cartoon",
|
| 113 |
+
]:
|
| 114 |
+
if empty:
|
| 115 |
+
# make the condition black
|
| 116 |
+
e_condition = Image.new("RGB", self.condition.size, (0, 0, 0))
|
| 117 |
+
e_condition = e_condition.convert("RGB")
|
| 118 |
+
tokens, ids = encode_images(pipe, e_condition)
|
| 119 |
+
else:
|
| 120 |
+
tokens, ids = encode_images(pipe, self.condition)
|
| 121 |
+
tokens, ids = encode_images(pipe, self.condition)
|
| 122 |
+
else:
|
| 123 |
+
raise NotImplementedError(
|
| 124 |
+
f"Condition type {self.condition_type} not implemented"
|
| 125 |
+
)
|
| 126 |
+
if self.position_delta is None and self.condition_type == "subject":
|
| 127 |
+
self.position_delta = [0, -self.condition.size[0] // 16]
|
| 128 |
+
if self.position_delta is not None:
|
| 129 |
+
ids[:, 1] += self.position_delta[0]
|
| 130 |
+
ids[:, 2] += self.position_delta[1]
|
| 131 |
+
if self.position_scale != 1.0:
|
| 132 |
+
scale_bias = (self.position_scale - 1.0) / 2
|
| 133 |
+
ids[:, 1] *= self.position_scale
|
| 134 |
+
ids[:, 2] *= self.position_scale
|
| 135 |
+
ids[:, 1] += scale_bias
|
| 136 |
+
ids[:, 2] += scale_bias
|
| 137 |
+
type_id = torch.ones_like(ids[:, :1]) * self.type_id
|
| 138 |
+
return tokens, ids, type_id
|
wfControl/src/flux/generate.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import yaml, os
|
| 3 |
+
from diffusers.pipelines import FluxPipeline
|
| 4 |
+
from typing import List, Union, Optional, Dict, Any, Callable
|
| 5 |
+
from .transformer import tranformer_forward
|
| 6 |
+
from .condition import Condition
|
| 7 |
+
|
| 8 |
+
from diffusers.pipelines.flux.pipeline_flux import (
|
| 9 |
+
FluxPipelineOutput,
|
| 10 |
+
calculate_shift,
|
| 11 |
+
retrieve_timesteps,
|
| 12 |
+
np,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_config(config_path: str = None):
|
| 17 |
+
config_path = config_path or os.environ.get("XFL_CONFIG")
|
| 18 |
+
if not config_path:
|
| 19 |
+
return {}
|
| 20 |
+
with open(config_path, "r") as f:
|
| 21 |
+
config = yaml.safe_load(f)
|
| 22 |
+
return config
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def prepare_params(
|
| 26 |
+
prompt: Union[str, List[str]] = None,
|
| 27 |
+
prompt_2: Optional[Union[str, List[str]]] = None,
|
| 28 |
+
height: Optional[int] = 512,
|
| 29 |
+
width: Optional[int] = 512,
|
| 30 |
+
num_inference_steps: int = 28,
|
| 31 |
+
timesteps: List[int] = None,
|
| 32 |
+
guidance_scale: float = 3.5,
|
| 33 |
+
num_images_per_prompt: Optional[int] = 1,
|
| 34 |
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
| 35 |
+
latents: Optional[torch.FloatTensor] = None,
|
| 36 |
+
prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 37 |
+
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
| 38 |
+
output_type: Optional[str] = "pil",
|
| 39 |
+
return_dict: bool = True,
|
| 40 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 41 |
+
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
| 42 |
+
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
| 43 |
+
max_sequence_length: int = 512,
|
| 44 |
+
**kwargs: dict,
|
| 45 |
+
):
|
| 46 |
+
return (
|
| 47 |
+
prompt,
|
| 48 |
+
prompt_2,
|
| 49 |
+
height,
|
| 50 |
+
width,
|
| 51 |
+
num_inference_steps,
|
| 52 |
+
timesteps,
|
| 53 |
+
guidance_scale,
|
| 54 |
+
num_images_per_prompt,
|
| 55 |
+
generator,
|
| 56 |
+
latents,
|
| 57 |
+
prompt_embeds,
|
| 58 |
+
pooled_prompt_embeds,
|
| 59 |
+
output_type,
|
| 60 |
+
return_dict,
|
| 61 |
+
joint_attention_kwargs,
|
| 62 |
+
callback_on_step_end,
|
| 63 |
+
callback_on_step_end_tensor_inputs,
|
| 64 |
+
max_sequence_length,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def seed_everything(seed: int = 42):
|
| 69 |
+
torch.backends.cudnn.deterministic = True
|
| 70 |
+
torch.manual_seed(seed)
|
| 71 |
+
np.random.seed(seed)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@torch.no_grad()
|
| 75 |
+
def generate(
|
| 76 |
+
pipeline: FluxPipeline,
|
| 77 |
+
conditions: List[Condition] = None,
|
| 78 |
+
config_path: str = None,
|
| 79 |
+
model_config: Optional[Dict[str, Any]] = {},
|
| 80 |
+
condition_scale: float = 1.0,
|
| 81 |
+
default_lora: bool = False,
|
| 82 |
+
image_guidance_scale: float = 1.0,
|
| 83 |
+
**params: dict,
|
| 84 |
+
):
|
| 85 |
+
model_config = model_config or get_config(config_path).get("model", {})
|
| 86 |
+
if condition_scale != 1:
|
| 87 |
+
for name, module in pipeline.transformer.named_modules():
|
| 88 |
+
if not name.endswith(".attn"):
|
| 89 |
+
continue
|
| 90 |
+
module.c_factor = torch.ones(1, 1) * condition_scale
|
| 91 |
+
|
| 92 |
+
self = pipeline
|
| 93 |
+
(
|
| 94 |
+
prompt,
|
| 95 |
+
prompt_2,
|
| 96 |
+
height,
|
| 97 |
+
width,
|
| 98 |
+
num_inference_steps,
|
| 99 |
+
timesteps,
|
| 100 |
+
guidance_scale,
|
| 101 |
+
num_images_per_prompt,
|
| 102 |
+
generator,
|
| 103 |
+
latents,
|
| 104 |
+
prompt_embeds,
|
| 105 |
+
pooled_prompt_embeds,
|
| 106 |
+
output_type,
|
| 107 |
+
return_dict,
|
| 108 |
+
joint_attention_kwargs,
|
| 109 |
+
callback_on_step_end,
|
| 110 |
+
callback_on_step_end_tensor_inputs,
|
| 111 |
+
max_sequence_length,
|
| 112 |
+
) = prepare_params(**params)
|
| 113 |
+
|
| 114 |
+
height = height or self.default_sample_size * self.vae_scale_factor
|
| 115 |
+
width = width or self.default_sample_size * self.vae_scale_factor
|
| 116 |
+
|
| 117 |
+
# 1. Check inputs. Raise error if not correct
|
| 118 |
+
self.check_inputs(
|
| 119 |
+
prompt,
|
| 120 |
+
prompt_2,
|
| 121 |
+
height,
|
| 122 |
+
width,
|
| 123 |
+
prompt_embeds=prompt_embeds,
|
| 124 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 125 |
+
callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
|
| 126 |
+
max_sequence_length=max_sequence_length,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
self._guidance_scale = guidance_scale
|
| 130 |
+
self._joint_attention_kwargs = joint_attention_kwargs
|
| 131 |
+
self._interrupt = False
|
| 132 |
+
|
| 133 |
+
# 2. Define call parameters
|
| 134 |
+
if prompt is not None and isinstance(prompt, str):
|
| 135 |
+
batch_size = 1
|
| 136 |
+
elif prompt is not None and isinstance(prompt, list):
|
| 137 |
+
batch_size = len(prompt)
|
| 138 |
+
else:
|
| 139 |
+
batch_size = prompt_embeds.shape[0]
|
| 140 |
+
|
| 141 |
+
device = self._execution_device
|
| 142 |
+
|
| 143 |
+
lora_scale = (
|
| 144 |
+
self.joint_attention_kwargs.get("scale", None)
|
| 145 |
+
if self.joint_attention_kwargs is not None
|
| 146 |
+
else None
|
| 147 |
+
)
|
| 148 |
+
(
|
| 149 |
+
prompt_embeds,
|
| 150 |
+
pooled_prompt_embeds,
|
| 151 |
+
text_ids,
|
| 152 |
+
) = self.encode_prompt(
|
| 153 |
+
prompt=prompt,
|
| 154 |
+
prompt_2=prompt_2,
|
| 155 |
+
prompt_embeds=prompt_embeds,
|
| 156 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 157 |
+
device=device,
|
| 158 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 159 |
+
max_sequence_length=max_sequence_length,
|
| 160 |
+
lora_scale=lora_scale,
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# 4. Prepare latent variables
|
| 164 |
+
num_channels_latents = self.transformer.config.in_channels // 4
|
| 165 |
+
latents, latent_image_ids = self.prepare_latents(
|
| 166 |
+
batch_size * num_images_per_prompt,
|
| 167 |
+
num_channels_latents,
|
| 168 |
+
height,
|
| 169 |
+
width,
|
| 170 |
+
prompt_embeds.dtype,
|
| 171 |
+
device,
|
| 172 |
+
generator,
|
| 173 |
+
latents,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# 4.1. Prepare conditions
|
| 177 |
+
condition_latents, condition_ids, condition_type_ids = ([] for _ in range(3))
|
| 178 |
+
use_condition = conditions is not None or []
|
| 179 |
+
if use_condition:
|
| 180 |
+
assert len(conditions) <= 1, "Only one condition is supported for now."
|
| 181 |
+
if not default_lora:
|
| 182 |
+
pipeline.set_adapters(conditions[0].condition_type)
|
| 183 |
+
for condition in conditions:
|
| 184 |
+
tokens, ids, type_id = condition.encode(self)
|
| 185 |
+
condition_latents.append(tokens) # [batch_size, token_n, token_dim]
|
| 186 |
+
condition_ids.append(ids) # [token_n, id_dim(3)]
|
| 187 |
+
condition_type_ids.append(type_id) # [token_n, 1]
|
| 188 |
+
condition_latents = torch.cat(condition_latents, dim=1)
|
| 189 |
+
condition_ids = torch.cat(condition_ids, dim=0)
|
| 190 |
+
condition_type_ids = torch.cat(condition_type_ids, dim=0)
|
| 191 |
+
|
| 192 |
+
# 5. Prepare timesteps
|
| 193 |
+
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
|
| 194 |
+
image_seq_len = latents.shape[1]
|
| 195 |
+
mu = calculate_shift(
|
| 196 |
+
image_seq_len,
|
| 197 |
+
self.scheduler.config.base_image_seq_len,
|
| 198 |
+
self.scheduler.config.max_image_seq_len,
|
| 199 |
+
self.scheduler.config.base_shift,
|
| 200 |
+
self.scheduler.config.max_shift,
|
| 201 |
+
)
|
| 202 |
+
timesteps, num_inference_steps = retrieve_timesteps(
|
| 203 |
+
self.scheduler,
|
| 204 |
+
num_inference_steps,
|
| 205 |
+
device,
|
| 206 |
+
timesteps,
|
| 207 |
+
sigmas,
|
| 208 |
+
mu=mu,
|
| 209 |
+
)
|
| 210 |
+
num_warmup_steps = max(
|
| 211 |
+
len(timesteps) - num_inference_steps * self.scheduler.order, 0
|
| 212 |
+
)
|
| 213 |
+
self._num_timesteps = len(timesteps)
|
| 214 |
+
|
| 215 |
+
# 6. Denoising loop
|
| 216 |
+
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
| 217 |
+
for i, t in enumerate(timesteps):
|
| 218 |
+
if self.interrupt:
|
| 219 |
+
continue
|
| 220 |
+
|
| 221 |
+
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
| 222 |
+
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
| 223 |
+
|
| 224 |
+
# handle guidance
|
| 225 |
+
if self.transformer.config.guidance_embeds:
|
| 226 |
+
guidance = torch.tensor([guidance_scale], device=device)
|
| 227 |
+
guidance = guidance.expand(latents.shape[0])
|
| 228 |
+
else:
|
| 229 |
+
guidance = None
|
| 230 |
+
noise_pred = tranformer_forward(
|
| 231 |
+
self.transformer,
|
| 232 |
+
model_config=model_config,
|
| 233 |
+
# Inputs of the condition (new feature)
|
| 234 |
+
condition_latents=condition_latents if use_condition else None,
|
| 235 |
+
condition_ids=condition_ids if use_condition else None,
|
| 236 |
+
condition_type_ids=condition_type_ids if use_condition else None,
|
| 237 |
+
# Inputs to the original transformer
|
| 238 |
+
hidden_states=latents,
|
| 239 |
+
# YiYi notes: divide it by 1000 for now because we scale it by 1000 in the transforme rmodel (we should not keep it but I want to keep the inputs same for the model for testing)
|
| 240 |
+
timestep=timestep / 1000,
|
| 241 |
+
guidance=guidance,
|
| 242 |
+
pooled_projections=pooled_prompt_embeds,
|
| 243 |
+
encoder_hidden_states=prompt_embeds,
|
| 244 |
+
txt_ids=text_ids,
|
| 245 |
+
img_ids=latent_image_ids,
|
| 246 |
+
joint_attention_kwargs=self.joint_attention_kwargs,
|
| 247 |
+
return_dict=False,
|
| 248 |
+
)[0]
|
| 249 |
+
|
| 250 |
+
if image_guidance_scale != 1.0:
|
| 251 |
+
uncondition_latents = condition.encode(self, empty=True)[0]
|
| 252 |
+
unc_pred = tranformer_forward(
|
| 253 |
+
self.transformer,
|
| 254 |
+
model_config=model_config,
|
| 255 |
+
# Inputs of the condition (new feature)
|
| 256 |
+
condition_latents=uncondition_latents if use_condition else None,
|
| 257 |
+
condition_ids=condition_ids if use_condition else None,
|
| 258 |
+
condition_type_ids=condition_type_ids if use_condition else None,
|
| 259 |
+
# Inputs to the original transformer
|
| 260 |
+
hidden_states=latents,
|
| 261 |
+
# YiYi notes: divide it by 1000 for now because we scale it by 1000 in the transforme rmodel (we should not keep it but I want to keep the inputs same for the model for testing)
|
| 262 |
+
timestep=timestep / 1000,
|
| 263 |
+
guidance=torch.ones_like(guidance),
|
| 264 |
+
pooled_projections=pooled_prompt_embeds,
|
| 265 |
+
encoder_hidden_states=prompt_embeds,
|
| 266 |
+
txt_ids=text_ids,
|
| 267 |
+
img_ids=latent_image_ids,
|
| 268 |
+
joint_attention_kwargs=self.joint_attention_kwargs,
|
| 269 |
+
return_dict=False,
|
| 270 |
+
)[0]
|
| 271 |
+
|
| 272 |
+
noise_pred = unc_pred + image_guidance_scale * (noise_pred - unc_pred)
|
| 273 |
+
|
| 274 |
+
# compute the previous noisy sample x_t -> x_t-1
|
| 275 |
+
latents_dtype = latents.dtype
|
| 276 |
+
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
| 277 |
+
|
| 278 |
+
if latents.dtype != latents_dtype:
|
| 279 |
+
if torch.backends.mps.is_available():
|
| 280 |
+
# some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
|
| 281 |
+
latents = latents.to(latents_dtype)
|
| 282 |
+
|
| 283 |
+
if callback_on_step_end is not None:
|
| 284 |
+
callback_kwargs = {}
|
| 285 |
+
for k in callback_on_step_end_tensor_inputs:
|
| 286 |
+
callback_kwargs[k] = locals()[k]
|
| 287 |
+
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
| 288 |
+
|
| 289 |
+
latents = callback_outputs.pop("latents", latents)
|
| 290 |
+
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
| 291 |
+
|
| 292 |
+
# call the callback, if provided
|
| 293 |
+
if i == len(timesteps) - 1 or (
|
| 294 |
+
(i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0
|
| 295 |
+
):
|
| 296 |
+
progress_bar.update()
|
| 297 |
+
|
| 298 |
+
if output_type == "latent":
|
| 299 |
+
image = latents
|
| 300 |
+
|
| 301 |
+
else:
|
| 302 |
+
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
| 303 |
+
latents = (
|
| 304 |
+
latents / self.vae.config.scaling_factor
|
| 305 |
+
) + self.vae.config.shift_factor
|
| 306 |
+
image = self.vae.decode(latents, return_dict=False)[0]
|
| 307 |
+
image = self.image_processor.postprocess(image, output_type=output_type)
|
| 308 |
+
|
| 309 |
+
# Offload all models
|
| 310 |
+
self.maybe_free_model_hooks()
|
| 311 |
+
|
| 312 |
+
if condition_scale != 1:
|
| 313 |
+
for name, module in pipeline.transformer.named_modules():
|
| 314 |
+
if not name.endswith(".attn"):
|
| 315 |
+
continue
|
| 316 |
+
del module.c_factor
|
| 317 |
+
|
| 318 |
+
if not return_dict:
|
| 319 |
+
return (image,)
|
| 320 |
+
|
| 321 |
+
return FluxPipelineOutput(images=image)
|
wfControl/src/flux/lora_controller.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from peft.tuners.tuners_utils import BaseTunerLayer
|
| 2 |
+
from typing import List, Any, Optional, Type
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class enable_lora:
|
| 6 |
+
def __init__(self, lora_modules: List[BaseTunerLayer], activated: bool) -> None:
|
| 7 |
+
self.activated: bool = activated
|
| 8 |
+
if activated:
|
| 9 |
+
return
|
| 10 |
+
self.lora_modules: List[BaseTunerLayer] = [
|
| 11 |
+
each for each in lora_modules if isinstance(each, BaseTunerLayer)
|
| 12 |
+
]
|
| 13 |
+
self.scales = [
|
| 14 |
+
{
|
| 15 |
+
active_adapter: lora_module.scaling[active_adapter]
|
| 16 |
+
for active_adapter in lora_module.active_adapters
|
| 17 |
+
}
|
| 18 |
+
for lora_module in self.lora_modules
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
def __enter__(self) -> None:
|
| 22 |
+
if self.activated:
|
| 23 |
+
return
|
| 24 |
+
|
| 25 |
+
for lora_module in self.lora_modules:
|
| 26 |
+
if not isinstance(lora_module, BaseTunerLayer):
|
| 27 |
+
continue
|
| 28 |
+
lora_module.scale_layer(0)
|
| 29 |
+
|
| 30 |
+
def __exit__(
|
| 31 |
+
self,
|
| 32 |
+
exc_type: Optional[Type[BaseException]],
|
| 33 |
+
exc_val: Optional[BaseException],
|
| 34 |
+
exc_tb: Optional[Any],
|
| 35 |
+
) -> None:
|
| 36 |
+
if self.activated:
|
| 37 |
+
return
|
| 38 |
+
for i, lora_module in enumerate(self.lora_modules):
|
| 39 |
+
if not isinstance(lora_module, BaseTunerLayer):
|
| 40 |
+
continue
|
| 41 |
+
for active_adapter in lora_module.active_adapters:
|
| 42 |
+
lora_module.scaling[active_adapter] = self.scales[i][active_adapter]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class set_lora_scale:
|
| 46 |
+
def __init__(self, lora_modules: List[BaseTunerLayer], scale: float) -> None:
|
| 47 |
+
self.lora_modules: List[BaseTunerLayer] = [
|
| 48 |
+
each for each in lora_modules if isinstance(each, BaseTunerLayer)
|
| 49 |
+
]
|
| 50 |
+
self.scales = [
|
| 51 |
+
{
|
| 52 |
+
active_adapter: lora_module.scaling[active_adapter]
|
| 53 |
+
for active_adapter in lora_module.active_adapters
|
| 54 |
+
}
|
| 55 |
+
for lora_module in self.lora_modules
|
| 56 |
+
]
|
| 57 |
+
self.scale = scale
|
| 58 |
+
|
| 59 |
+
def __enter__(self) -> None:
|
| 60 |
+
for lora_module in self.lora_modules:
|
| 61 |
+
if not isinstance(lora_module, BaseTunerLayer):
|
| 62 |
+
continue
|
| 63 |
+
lora_module.scale_layer(self.scale)
|
| 64 |
+
|
| 65 |
+
def __exit__(
|
| 66 |
+
self,
|
| 67 |
+
exc_type: Optional[Type[BaseException]],
|
| 68 |
+
exc_val: Optional[BaseException],
|
| 69 |
+
exc_tb: Optional[Any],
|
| 70 |
+
) -> None:
|
| 71 |
+
for i, lora_module in enumerate(self.lora_modules):
|
| 72 |
+
if not isinstance(lora_module, BaseTunerLayer):
|
| 73 |
+
continue
|
| 74 |
+
for active_adapter in lora_module.active_adapters:
|
| 75 |
+
lora_module.scaling[active_adapter] = self.scales[i][active_adapter]
|
wfControl/src/flux/pipeline_tools.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers.pipelines import FluxPipeline
|
| 2 |
+
from diffusers.utils import logging
|
| 3 |
+
from diffusers.pipelines.flux.pipeline_flux import logger
|
| 4 |
+
from torch import Tensor
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def encode_images(pipeline: FluxPipeline, images: Tensor):
|
| 8 |
+
images = pipeline.image_processor.preprocess(images)
|
| 9 |
+
images = images.to(pipeline.device).to(pipeline.dtype)
|
| 10 |
+
images = pipeline.vae.encode(images).latent_dist.sample()
|
| 11 |
+
images = (
|
| 12 |
+
images - pipeline.vae.config.shift_factor
|
| 13 |
+
) * pipeline.vae.config.scaling_factor
|
| 14 |
+
images_tokens = pipeline._pack_latents(images, *images.shape)
|
| 15 |
+
images_ids = pipeline._prepare_latent_image_ids(
|
| 16 |
+
images.shape[0],
|
| 17 |
+
images.shape[2],
|
| 18 |
+
images.shape[3],
|
| 19 |
+
pipeline.device,
|
| 20 |
+
pipeline.dtype,
|
| 21 |
+
)
|
| 22 |
+
if images_tokens.shape[1] != images_ids.shape[0]:
|
| 23 |
+
images_ids = pipeline._prepare_latent_image_ids(
|
| 24 |
+
images.shape[0],
|
| 25 |
+
images.shape[2] // 2,
|
| 26 |
+
images.shape[3] // 2,
|
| 27 |
+
pipeline.device,
|
| 28 |
+
pipeline.dtype,
|
| 29 |
+
)
|
| 30 |
+
return images_tokens, images_ids
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def prepare_text_input(pipeline: FluxPipeline, prompts, max_sequence_length=512):
|
| 34 |
+
# Turn off warnings (CLIP overflow)
|
| 35 |
+
logger.setLevel(logging.ERROR)
|
| 36 |
+
(
|
| 37 |
+
prompt_embeds,
|
| 38 |
+
pooled_prompt_embeds,
|
| 39 |
+
text_ids,
|
| 40 |
+
) = pipeline.encode_prompt(
|
| 41 |
+
prompt=prompts,
|
| 42 |
+
prompt_2=None,
|
| 43 |
+
prompt_embeds=None,
|
| 44 |
+
pooled_prompt_embeds=None,
|
| 45 |
+
device=pipeline.device,
|
| 46 |
+
num_images_per_prompt=1,
|
| 47 |
+
max_sequence_length=max_sequence_length,
|
| 48 |
+
lora_scale=None,
|
| 49 |
+
)
|
| 50 |
+
# Turn on warnings
|
| 51 |
+
logger.setLevel(logging.WARNING)
|
| 52 |
+
return prompt_embeds, pooled_prompt_embeds, text_ids
|