Skip to content

FLUX.1: Black Forest Labs'ın Yeni Text-to-Image AI Modeli

Published: at 05:18 PMSuggest an edit

Merhaba! Bugün AI dünyasında büyük ses getiren yeni bir text-to-image model’den bahsedeceğiz: FLUX.1. Stable Diffusion’ın yaratıcılarının kurduğu Black Forest Labs’ın bu yeni modeli, image generation alanında yeni standartlar belirlemeye aday.

Black Forest Labs ve FLUX.1’in Doğuşu

2024 Ağustos’unda Black Forest Labs, Stable Diffusion teknolojisini geliştiren ve Latent Diffusion tekniğini icat eden araştırmacılar tarafından kuruldu. Şirketin kurucu ekibinde:

Bu ekip, Stability AI’dan ayrıldıktan sonra Almanya merkezli bu yeni şirketi kurdu.

Neden FLUX.1 Önemli?

Stable Diffusion 3’ün Problemleri

Stable Diffusion 3 Medium’un Haziran 2024’teki problematik release’i industry’de büyük hayal kırıklığı yarattı:

# SD3'ün tipik problemleri
problems_sd3 = {
    "human_anatomy": "distorted limbs and bodies",
    "text_rendering": "poor text generation",
    "prompt_adherence": "low fidelity to prompts",
    "training_data": "controversial dataset choices"
}

# Community reaction
community_feedback = [
    "Worse than SD 1.5",
    "Anatomy completely broken",
    "Not worth the wait",
    "Kalitede geri adım"
]

FLUX.1 bu problemleri çözmek için ground-up yeniden tasarlandı.

FLUX.1 Model Variants

Black Forest Labs üç farklı variant sunuyor:

1. FLUX.1 [pro]

2. FLUX.1 [dev]

3. FLUX.1 [schnell]

# Model comparison
flux_variants = {
    "pro": {
        "kalite": "en yüksek",
        "speed": "moderate",
        "license": "commercial",
        "access": "api_only",
        "parameters": "12B estimated"
    },
    "dev": {
        "kalite": "yüksek",
        "speed": "moderate",
        "license": "non_commercial",
        "access": "open_weights",
        "parameters": "12B"
    },
    "schnell": {
        "kalite": "iyi",
        "speed": "fastest",
        "license": "open_source",
        "access": "full_access",
        "parameters": "12B"
    }
}

Teknik Architecture

Flow Matching ile Diffusion

FLUX.1, traditional DDPM diffusion yerine Flow Matching kullanıyor:

import torch
import torch.nn as nn

class FlowMatching(nn.Module):
    def __init__(self, model, sigma_min=0.002):
        super().__init__()
        self.model = model
        self.sigma_min = sigma_min

    def forward(self, x, t, conditioning):
        # Flow matching training objective
        # More stable than DDPM noise prediction

        # Linear interpolation path
        alpha_t = self.get_alpha(t)

        # Flow field prediction instead of noise
        flow_field = self.model(x, t, conditioning)

        return flow_field

    def get_alpha(self, t):
        # Linear schedule for flow matching
        return t

    def sample(self, noise, conditioning, num_steps=50):
        # Euler method for flow ODE
        x = noise
        dt = 1.0 / num_steps

        for i in range(num_steps):
            t = torch.ones(x.shape[0]) * (i / num_steps)
            flow = self.model(x, t, conditioning)
            x = x + flow * dt

        return x

Multimodal DiT Architecture

FLUX.1, Diffusion Transformer (DiT) architecture kullanıyor:

class FluxTransformerBlock(nn.Module):
    def __init__(self, dim, num_heads, mlp_ratio=4.0):
        super().__init__()
        self.norm1 = nn.LayerNorm(dim)
        self.norm2 = nn.LayerNorm(dim)

        # Multimodal attention
        self.cross_attn = nn.MultiheadAttention(dim, num_heads)
        self.self_attn = nn.MultiheadAttention(dim, num_heads)

        # MLP
        mlp_dim = int(dim * mlp_ratio)
        self.mlp = nn.Sequential(
            nn.Linear(dim, mlp_dim),
            nn.GELU(),
            nn.Linear(mlp_dim, dim)
        )

    def forward(self, x, text_embeds, timestep):
        # Self attention on image patches
        x_norm = self.norm1(x)
        x = x + self.self_attn(x_norm, x_norm, x_norm)[0]

        # Cross attention with text
        x_norm = self.norm1(x)
        x = x + self.cross_attn(x_norm, text_embeds, text_embeds)[0]

        # MLP
        x = x + self.mlp(self.norm2(x))

        return x

class FluxModel(nn.Module):
    def __init__(self,
                 img_size=1024,
                 patch_size=16,
                 dim=3072,
                 depth=24,
                 num_heads=24):
        super().__init__()

        self.patch_embed = nn.Conv2d(4, dim, patch_size, patch_size)  # VAE latents
        self.pos_embed = nn.Parameter(torch.randn(1, (img_size//patch_size)**2, dim))

        # Text encoder (T5-XXL)
        self.text_encoder = T5EncoderModel.from_pretrained("google/t5-v1_1-xxl")

        # Transformer blocks
        self.blocks = nn.ModuleList([
            FluxTransformerBlock(dim, num_heads) for _ in range(depth)
        ])

        # Output projection
        self.norm_out = nn.LayerNorm(dim)
        self.proj_out = nn.Linear(dim, patch_size * patch_size * 4)

    def forward(self, x, text, timestep):
        # Encode text
        text_embeds = self.text_encoder(text).last_hidden_state

        # Patch embedding
        x = self.patch_embed(x).flatten(2).transpose(1, 2)
        x = x + self.pos_embed

        # Add timestep embedding
        timestep_embed = self.get_timestep_embedding(timestep)
        x = x + timestep_embed.unsqueeze(1)

        # Transformer blocks
        for block in self.blocks:
            x = block(x, text_embeds, timestep)

        # Output
        x = self.norm_out(x)
        x = self.proj_out(x)

        return x

Training Improvements

1. Better Text Encoder

# FLUX.1 text encoding pipeline
def encode_text_flux(prompt):
    # Dual text encoder approach
    clip_embeds = clip_encoder(prompt)  # CLIP-ViT-L/14
    t5_embeds = t5_encoder(prompt)      # T5-XXL

    # Concatenate embeddings
    combined_embeds = torch.cat([clip_embeds, t5_embeds], dim=-1)

    return combined_embeds

# Better prompt understanding
complex_prompt = """
A photorealistic portrait of a young woman with curly red hair,
wearing a vintage leather jacket, standing in a neon-lit alley
at night, with rain reflecting the colorful lights on the pavement
"""

# FLUX.1 handles complex prompts much better than SD3

2. Improved VAE

class FluxVAE(nn.Module):
    def __init__(self):
        super().__init__()
        # Higher resolution latent space
        self.latent_channels = 4
        self.downsample_factor = 8  # vs 8 in SD

        # Daha iyi reconstruction kalitesi
        self.encoder = FluxEncoder()
        self.decoder = FluxDecoder()

    def encode(self, x):
        # Daha iyi algısal kalite
        latent = self.encoder(x)
        return latent

    def decode(self, latent):
        # Less artifacts in reconstruction
        reconstructed = self.decoder(latent)
        return reconstructed

3. Gelişmiş Sampling

def flux_sampling_pipeline(prompt, model, steps=50):
    # Daha iyi kalite için geliştirilmiş sampling

    # Text encoding
    text_embeds = encode_text_flux(prompt)

    # Initial noise
    noise = torch.randn(1, 4, 128, 128)  # 1024x1024 output

    # Flow matching sampling
    x = noise
    dt = 1.0 / steps

    for i in range(steps):
        t = torch.tensor([i / steps])

        # Classifier-free guidance
        uncond_flow = model(x, empty_text_embeds, t)
        cond_flow = model(x, text_embeds, t)

        # Apply guidance
        guidance_scale = 7.5
        flow = uncond_flow + guidance_scale * (cond_flow - uncond_flow)

        # Euler step
        x = x + flow * dt

    # VAE decode
    image = vae.decode(x)

    return image

Performans Karşılaştırması

Kalite Metrikleri

# Benchmark results (community reported)
benchmark_results = {
    "model": ["FLUX.1 [pro]", "FLUX.1 [dev]", "SD3 Medium", "SDXL", "Midjourney v6"],
    "prompt_adherence": [9.2, 8.8, 6.5, 7.2, 8.5],
    "human_anatomy": [9.5, 9.1, 5.2, 7.8, 9.0],
    "text_rendering": [8.8, 8.5, 4.1, 5.5, 7.2],
    "genel_kalite": [9.3, 8.9, 6.8, 7.5, 8.7]
}

# FLUX.1 consistently outperforms other open models

Speed Comparison

# Generation speeds (approximate)
generation_times = {
    "FLUX.1 [schnell]": "2-4 steps, ~1-2 seconds",
    "FLUX.1 [dev]": "20-50 steps, ~10-30 seconds",
    "FLUX.1 [pro]": "20-50 steps, ~15-45 seconds",
    "SD3 Medium": "28 steps, ~20-40 seconds",
    "SDXL": "25-50 steps, ~15-35 seconds"
}

# Schnell variant is significantly faster

Code Examples

Using FLUX.1 with Diffusers

from diffusers import FluxPipeline
import torch

# Load FLUX.1 dev model
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16
)
pipe.to("cuda")

# Generate image
prompt = "A majestic lion standing on a cliff overlooking a vast savanna at sunset"

image = pipe(
    prompt=prompt,
    num_inference_steps=50,
    guidance_scale=7.5,
    height=1024,
    width=1024,
    generator=torch.Generator().manual_seed(42)
).images[0]

image.save("flux_generated_lion.png")

Using FLUX.1 Schnell for Fast Generation

from diffusers import FluxPipeline

# Load schnell variant for speed
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-schnell",
    torch_dtype=torch.bfloat16
)
pipe.to("cuda")

# Fast generation (2-4 steps)
prompt = "A cute robot dog playing in a park, digital art style"

image = pipe(
    prompt=prompt,
    num_inference_steps=4,  # Very fast!
    guidance_scale=1.0,     # Schnell doesn't need high guidance
    height=1024,
    width=1024
).images[0]

image.save("flux_schnell_robot_dog.png")

Fine-tuning FLUX.1

from diffusers import FluxPipeline
from diffusers.training_utils import EMAModel

def fine_tune_flux(dataset, base_model="black-forest-labs/FLUX.1-dev"):
    # Load base model
    pipe = FluxPipeline.from_pretrained(base_model)
    model = pipe.transformer

    # Setup training
    optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
    ema_model = EMAModel(model.parameters())

    for epoch in range(num_epochs):
        for batch in dataset:
            images, captions = batch

            # Encode images to latents
            latents = pipe.vae.encode(images).latent_dist.sample()
            latents = latents * pipe.vae.config.scaling_factor

            # Add noise
            noise = torch.randn_like(latents)
            timesteps = torch.randint(0, 1000, (len(latents),))
            noisy_latents = pipe.scheduler.add_noise(latents, noise, timesteps)

            # Encode text
            text_embeds = pipe.text_encoder(captions)

            # Predict flow
            flow_pred = model(noisy_latents, timesteps, text_embeds)

            # Loss calculation (flow matching objective)
            target_flow = latents - noise  # Simplified
            loss = F.mse_loss(flow_pred, target_flow)

            # Backprop
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            ema_model.step(model.parameters())

    return model

Real-World Applications

1. E-commerce Product Visualization

def generate_product_images(product_description, style="photorealistic"):
    prompts = [
        f"{product_description}, {style}, studio lighting, white background",
        f"{product_description}, {style}, lifestyle setting, natural lighting",
        f"{product_description}, {style}, macro photography, detailed textures"
    ]

    images = []
    for prompt in prompts:
        image = pipe(prompt, num_inference_steps=50).images[0]
        images.append(image)

    return images

# Example usage
product_desc = "Premium leather handbag with gold hardware"
product_images = generate_product_images(product_desc)

2. Architectural Visualization

def generate_architectural_renders(building_description):
    prompt = f"""
    {building_description}, architectural photography,
    golden hour lighting, professional camera,
    detailed textures, realistic materials,
    8k resolution, award winning architecture
    """

    image = pipe(
        prompt=prompt,
        num_inference_steps=50,
        guidance_scale=8.0,
        height=1024,
        width=1536  # Wide aspect ratio for architecture
    ).images[0]

    return image

# Günümüz ev tasarımı oluştur
house_render = generate_architectural_renders(
    "Minimalist günümüz evi, büyük cam pencereler ve beton cephe"
)

Karşılaştırma: FLUX.1 ve Rakipleri

ÖzellikFLUX.1 [pro]FLUX.1 [dev]SD3 MediumSDXLMidjourney v6
Prompt Adherence⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Human Anatomy⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Text Rendering⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Speed⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Open Source
Commercial Use

Son Sözler

FLUX.1, text-to-image generation alanında gerçekten önemli bir breakthrough. Özellikle:

Black Forest Labs’ın Stable Diffusion background’u sayesinde community’nin ihtiyaçlarını iyi anlayarak geliştirdikleri bu model, AI art creation’da yeni bir standard belirlemiş durumda.

FLUX.1 [schnell] özellikle local development için harika - 4 step’te makul kalite alabiliyorsunuz. Dev variant ise research için mükemmel, pro ise commercial applications’a odaklı.

Gelecekte video generation capabilities de ekleneceği söyleniyor. Bu alanda da breakthrough’lar bekleyebiliriz!

Keep generating!



Previous Post
UFW (Uncomplicated Firewall): Linux Firewall Yönetimi Rehberi
Next Post
PixelCNN: Pixel Pixel Görüntü Üretme Sanatı