BRIA 2.3 ID preservation Adapter

BRIA 2.3 ID preservation Adapter is a model designed to allows various style transfer operations or tweaks on facial image using textual prompts. The model is fully compatible with auxiliary models like ControlNets and LoRAs, enabling seamless integration into existing workflows.

Trained exclusively on the largest multi-source commercial-grade licensed dataset, BRIA 2.3 ID preservation Adapter guarantees best quality while safe for commercial use. The model provides full legal liability coverage for copyright and privacy infrigement and harmful content mitigation, as our dataset does not represent copyrighted materials, such as fictional characters, logos or trademarks, public figures, harmful content or privacy infringing content.

This model is optimized to work sseamlessly in high resolution, upper body part facial images.

Join our Discord community for more information, tutorials, tools, and to connect with other users!

What's New

BRIA 2.3 ID preservation Adapter can be applied on top of BRIA 2.3 Text-to-Image and therefore enable to use BRIA auxiliary models.

Model Description

  • Developed by: BRIA AI
  • Model type: Latent diffusion image-to-image model
  • License: bria-2.3 inpainting Licensing terms & conditions.
  • Purchase is required to license and access the model.
  • Model Description: BRIA 2.3 ID preservation Adapter was trained exclusively on a professional-grade, licensed dataset. It is designed for commercial use and includes full legal liability coverage.
  • Resources for more information: BRIA AI

Get Access to the source code and pre-trained model

Interested in BRIA 2.3 ID preservation Adapter? Our Model is available for purchase.

Purchasing access to BRIA 2.3 ID preservation Adapter ensures royalty management and full liability for commercial use.

Are you a startup or a student? We encourage you to apply for our specialized Academia and Startup Programs to gain access. These programs are designed to support emerging businesses and academic pursuits with our cutting-edge technology.

Contact us today to unlock the potential of BRIA 2.3 ID preservation Adapter!

By submitting the form above, you agree to BRIA’s Privacy policy and Terms & conditions.

Best practices:

  1. In your text prompt, start with a short description of the person in the image e.g., A Caucasian female with brown eyes and gray long hair.
  2. Use a protrait image with large face (~20-80% of the image).
  3. Use clean background (or use Bria's RMBG) - this will help you with the canny condition.
  4. You can add style images, and apply using Bria's IP adapter
  5. You can train your own LoRA on your desired style and use within this model.

How To Use

opencv-python==4.10.0.84
torch==2.4.0
torchvision==0.19.0
pillow==10.4.0
transformers==4.43.4
diffusers==0.29.2
insightface==0.7.3
onnx==1.16.2
onnxruntime==1.18.1
import gc
import os
import random
import gradio as gr

import cv2
import torch
import numpy as np
from PIL import Image

from transformers import CLIPVisionModelWithProjection
from diffusers.models import ControlNetModel

from huggingface_hub import snapshot_download

from insightface.app import FaceAnalysis

import io
import spaces

from pipeline_stable_diffusion_xl_instantid import StableDiffusionXLInstantIDPipeline, draw_kps

import pandas as pd
import json
import requests
from io import BytesIO
from huggingface_hub import hf_hub_download, HfApi


def resize_img(input_image, max_side=1280, min_side=1024, size=None, 
               pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
    
    w, h = input_image.size
    if size is not None:
        w_resize_new, h_resize_new = size
    else:
        ratio = min_side / min(h, w)
        w, h = round(ratio*w), round(ratio*h)
        ratio = max_side / max(h, w)
        input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
        w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
        h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
    input_image = input_image.resize([w_resize_new, h_resize_new], mode)

    if pad_to_max_side:
        res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
        offset_x = (max_side - w_resize_new) // 2
        offset_y = (max_side - h_resize_new) // 2
        res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
        input_image = Image.fromarray(res)
    return input_image


def make_canny_condition(image, min_val=100, max_val=200, w_bilateral=True):
    if w_bilateral:
        image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
        bilateral_filtered_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)
        image = cv2.Canny(bilateral_filtered_image, min_val, max_val)
    else:
        image = np.array(image)
        image = cv2.Canny(image, min_val, max_val)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    image = Image.fromarray(image)
    return image


default_negative_prompt = "Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"

# Download face encoder
snapshot_download(
    "fal/AuraFace-v1",
    local_dir="models/auraface",
)

app = FaceAnalysis(
    name="auraface",
    providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
    root=".",
)

app.prepare(ctx_id=0, det_size=(640, 640))


# download checkpoints
print("Downloading checkpoints")
hf_hub_download(repo_id="briaai/ID_preservation_2.3_auraFaceEnc", filename="checkpoint_105000/controlnet/config.json", local_dir="./checkpoints")
hf_hub_download(repo_id="briaai/ID_preservation_2.3_auraFaceEnc", filename="checkpoint_105000/controlnet/diffusion_pytorch_model.safetensors", local_dir="./checkpoints")
hf_hub_download(repo_id="briaai/ID_preservation_2.3_auraFaceEnc", filename="checkpoint_105000/ip-adapter.bin", local_dir="./checkpoints")
hf_hub_download(repo_id="briaai/ID_preservation_2.3_auraFaceEnc", filename="image_encoder/pytorch_model.bin", local_dir="./checkpoints")
hf_hub_download(repo_id="briaai/ID_preservation_2.3_auraFaceEnc", filename="image_encoder/config.json", local_dir="./checkpoints")


device = "cuda" if torch.cuda.is_available() else "cpu"

# ckpts paths
face_adapter = f"./checkpoints/checkpoint_105000/ip-adapter.bin"
controlnet_path = f"./checkpoints/checkpoint_105000/controlnet"
base_model_path = f'briaai/BRIA-2.3'
resolution = 1024

# Load ControlNet models
controlnet_lnmks = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
controlnet_canny = ControlNetModel.from_pretrained("briaai/BRIA-2.3-ControlNet-Canny",
                                                torch_dtype=torch.float16)
    
controlnet = [controlnet_lnmks, controlnet_canny]


image_encoder = CLIPVisionModelWithProjection.from_pretrained(
        f"./checkpoints/image_encoder",
        torch_dtype=torch.float16,
    )
pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
        base_model_path,
        controlnet=controlnet,
        torch_dtype=torch.float16,
        image_encoder=image_encoder # For compatibility issues - needs to be there
    )

pipe = pipe.to(device)

pipe.use_native_ip_adapter=True

pipe.load_ip_adapter_instantid(face_adapter)

clip_embeds=None


image_path = "<define a path to image>"
img = Image.open(image_path)

prompt = "A male with brown eyes, gray hair, short hair, and wearing sunglasses."
face_image = resize_img(face_image_orig, max_side=resolution, min_side=resolution)
face_image_padded = resize_img(face_image_orig, max_side=resolution, min_side=resolution, pad_to_max_side=True)
face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x['bbox'][1]))[-1] # only use the maximum face
face_emb = face_info['embedding']
face_kps = draw_kps(face_image, face_info['kps'])


# ================= Parameters =================
kps_scale = 0.6
canny_scale = 0.4
ip_adapter_scale = 0.8

if canny_scale>0.0:
  canny_img = make_canny_condition(face_image, min_val=20, max_val=40, w_bilateral=True)
                
generator = torch.Generator(device=device).manual_seed(seed)


images = pipe(
    prompt = full_prompt,
    negative_prompt = default_negative_prompt,
    image_embeds = face_emb,
    image = [face_kps, canny_img] if canny_scale > 0.0 else face_kps,
    controlnet_conditioning_scale = [kps_scale, canny_scale] if canny_scale>0.0 else kps_scale,
    ip_adapter_scale = ip_adapter_scale,
    num_inference_steps = num_steps,
    guidance_scale = guidance_scale,
    generator = generator,
    visual_prompt_embds = clip_embeds,
    cross_attention_kwargs = None,
    num_images_per_prompt=num_images,
).images[0]

Downloads last month
90
Inference Examples
Inference API (serverless) has been turned off for this model.