Instructions to use paom/texture2albedo-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use paom/texture2albedo-v2 with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.2-klein-9B", dtype=torch.bfloat16, device_map="cuda") pipe.load_lora_weights("paom/texture2albedo-v2") prompt = "Unlit flat-shaded albedo map. Remove all shadows, reflections, highlights, and specularity. Maintain absolute pixel-per-pixel structural identity, shape, and spatial alignment with the original image, displaying only raw base color. " input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
Update README.md
Browse files
README.md
CHANGED
|
@@ -38,3 +38,147 @@ This will return pure albedo from your texture.
|
|
| 38 |
|
| 39 |
|
| 40 |
[Download](/paom/texture2albedo-v2/tree/main) them in the Files & versions tab.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
[Download](/paom/texture2albedo-v2/tree/main) them in the Files & versions tab.
|
| 41 |
+
|
| 42 |
+
'''
|
| 43 |
+
## Python script for inference in gradio (install gradio in python with 'pip install gradio')
|
| 44 |
+
```
|
| 45 |
+
import os
|
| 46 |
+
import torch
|
| 47 |
+
import gradio as gr
|
| 48 |
+
from PIL import Image
|
| 49 |
+
from diffusers import Flux2KleinPipeline
|
| 50 |
+
|
| 51 |
+
# --- Configuration & Initialization ---
|
| 52 |
+
|
| 53 |
+
BASE_MODEL_FILE = "black-forest-labs/FLUX.2-klein-9B"
|
| 54 |
+
LORA_REPO = "paom/texture2albedo-v2"
|
| 55 |
+
|
| 56 |
+
print("Initializing device and pipeline...")
|
| 57 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 58 |
+
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
print(f"Loading transformer component from single file: {BASE_MODEL_FILE}")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
pipe = Flux2KleinPipeline.from_pretrained(
|
| 65 |
+
BASE_MODEL_FILE,
|
| 66 |
+
torch_dtype=dtype
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
pipe.load_lora_weights(
|
| 71 |
+
LORA_REPO,
|
| 72 |
+
weight_name="pytorch_lora_weights.safetensors",
|
| 73 |
+
adapter_name="albedo"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if device == "cuda":
|
| 78 |
+
print("Enabling smart CPU offload...")
|
| 79 |
+
pipe.enable_model_cpu_offload()
|
| 80 |
+
else:
|
| 81 |
+
pipe.to(device)
|
| 82 |
+
|
| 83 |
+
print("Pipeline and LoRA weights loaded successfully.")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
import traceback
|
| 86 |
+
print("!!! DETAILED INITIALIZATION ERROR !!!")
|
| 87 |
+
traceback.print_exc()
|
| 88 |
+
pipe = None
|
| 89 |
+
|
| 90 |
+
# --- Prompt Presets ---
|
| 91 |
+
PROMPT_PRESETS = {
|
| 92 |
+
"Strict Unlit Flat (Default)": (
|
| 93 |
+
"Unlit flat-shaded albedo map. Remove all shadows, reflections, highlights, and specularity. "
|
| 94 |
+
"Maintain absolute pixel-per-pixel structural identity, shape, and spatial alignment with the "
|
| 95 |
+
"original image, displaying only raw base color."
|
| 96 |
+
)
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
# --- Core Inference Function ---
|
| 100 |
+
def generate_albedo(input_image, prompt_selection, custom_prompt, steps, guidance_scale, seed):
|
| 101 |
+
if pipe is None:
|
| 102 |
+
raise gr.Error("Model pipeline failed to initialize. Check your hardware compatibility.")
|
| 103 |
+
|
| 104 |
+
if input_image is None:
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
prompt = custom_prompt if custom_prompt.strip() else PROMPT_PRESETS[prompt_selection]
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
orig_width, orig_height = input_image.size
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
processed_input = input_image.resize((1024, 1024))
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
generator = torch.manual_seed(seed) if seed >= 0 else None
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
|
| 121 |
+
with torch.inference_mode():
|
| 122 |
+
output_image = pipe(
|
| 123 |
+
prompt=prompt,
|
| 124 |
+
image=processed_input,
|
| 125 |
+
guidance_scale=guidance_scale,
|
| 126 |
+
num_inference_steps=int(steps),
|
| 127 |
+
generator=generator
|
| 128 |
+
).images[0]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
albedo_map = output_image.resize((orig_width, orig_height))
|
| 132 |
+
|
| 133 |
+
return albedo_map
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
raise gr.Error(f"Inference error occurred: {str(e)}")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 140 |
+
gr.Markdown(
|
| 141 |
+
"""
|
| 142 |
+
# Texture-to-Albedo Studio (Flux.2 Klein)
|
| 143 |
+
Extract clean, flat, completely shadowless base color **Albedo maps** from textures and photos for your 3D/PBR pipelines.
|
| 144 |
+
"""
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
with gr.Row():
|
| 148 |
+
|
| 149 |
+
with gr.Column(scale=1):
|
| 150 |
+
input_img = gr.Image(label="Input Texture / Photo", type="pil")
|
| 151 |
+
|
| 152 |
+
prompt_dropdown = gr.Dropdown(
|
| 153 |
+
choices=list(PROMPT_PRESETS.keys()),
|
| 154 |
+
value="Strict Unlit Flat (Default)",
|
| 155 |
+
label="Prompt Style Preset"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
custom_prompt_box = gr.Textbox(
|
| 159 |
+
label="Custom Prompt Override",
|
| 160 |
+
placeholder="Leave blank to use chosen preset above...",
|
| 161 |
+
lines=2
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
with gr.Accordion("Advanced Parameters", open=False):
|
| 165 |
+
inference_steps = gr.Slider(minimum=1, maximum=12, value=4, step=1, label="Inference Steps")
|
| 166 |
+
guidance = gr.Slider(minimum=0.0, maximum=4.0, value=1.0, step=0.1, label="Guidance Scale")
|
| 167 |
+
seed_input = gr.Number(value=0, label="Seed (-1 for random)", precision=0)
|
| 168 |
+
|
| 169 |
+
submit_btn = gr.Button("Generate Albedo Map", variant="primary")
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
with gr.Column(scale=1):
|
| 173 |
+
albedo_out = gr.Image(label="Clean Albedo Texture Map", type="pil")
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
submit_btn.click(
|
| 177 |
+
fn=generate_albedo,
|
| 178 |
+
inputs=[input_img, prompt_dropdown, custom_prompt_box, inference_steps, guidance, seed_input],
|
| 179 |
+
outputs=[albedo_out]
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 184 |
+
```
|