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
|
4 |
+
license: other
|
5 |
+
license_link: https://bria.ai/bria-huggingface-model-license-agreement/
|
6 |
+
library_name: diffusers
|
7 |
+
inference: false
|
8 |
+
tags:
|
9 |
+
- text-to-image
|
10 |
+
- legal liability
|
11 |
+
- commercial use
|
12 |
+
- ID preservation Adapter
|
13 |
+
extra_gated_description: Model weights from BRIA AI can be obtained with the purchase of a commercial license. Fill in the form below and we reach out to you.
|
14 |
+
extra_gated_heading: "Fill in this form to request a commercial license for the model"
|
15 |
+
extra_gated_fields:
|
16 |
+
Name: text
|
17 |
+
Company/Org name: text
|
18 |
+
Org Type (Early/Growth Startup, Enterprise, Academy): text
|
19 |
+
Role: text
|
20 |
+
Country: text
|
21 |
+
Email: text
|
22 |
+
By submitting this form, I agree to BRIA’s Privacy policy and Terms & conditions, see links below: checkbox
|
23 |
+
---
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
# BRIA 2.3 ID preservation Adapter
|
28 |
+
|
29 |
+
|
30 |
+
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.
|
31 |
+
|
32 |
+
|
33 |
+
BRIA 2.3 ID preservation Adapter is a model designed to allows various style transfer operations or tweaks on your facial image using textual prompts. The model is fully compatible with auxiliary models like ControlNets and LoRAs, enabling seamless integration into existing workflows.
|
34 |
+
|
35 |
+
This model is optimized to work seamlessly high resolution upper body part facial imags.
|
36 |
+
|
37 |
+
|
38 |
+
Join our [Discord community](https://discord.gg/Nxe9YW9zHS) for more information, tutorials, tools, and to connect with other users!
|
39 |
+
|
40 |
+
|
41 |
+
# What's New
|
42 |
+
|
43 |
+
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.
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
### Model Description
|
50 |
+
|
51 |
+
- **Developed by:** BRIA AI
|
52 |
+
- **Model type:** Latent diffusion image-to-image model
|
53 |
+
- **License:** [bria-2.3 inpainting Licensing terms & conditions](https://bria.ai/bria-huggingface-model-license-agreement/).
|
54 |
+
- Purchase is required to license and access the model.
|
55 |
+
- **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.
|
56 |
+
- **Resources for more information:** [BRIA AI](https://bria.ai/)
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
### Get Access to the source code and pre-trained model
|
62 |
+
Interested in BRIA 2.3 ID preservation Adapter? Our Model is available for purchase.
|
63 |
+
|
64 |
+
**Purchasing access to BRIA 2.3 ID preservation Adapter ensures royalty management and full liability for commercial use.**
|
65 |
+
|
66 |
+
|
67 |
+
*Are you a startup or a student?* We encourage you to apply for our specialized Academia and [Startup Programs](https://pages.bria.ai/the-visual-generative-ai-platform-for-builders-startups-plan?_gl=1*cqrl81*_ga*MTIxMDI2NzI5OC4xNjk5NTQ3MDAz*_ga_WRN60H46X4*MTcwOTM5OTMzNC4yNzguMC4xNzA5Mzk5MzM0LjYwLjAuMA..) to gain access. These programs are designed to support emerging businesses and academic pursuits with our cutting-edge technology.
|
68 |
+
|
69 |
+
|
70 |
+
**Contact us today to unlock the potential of BRIA 2.3 ID preservation Adapter!**
|
71 |
+
|
72 |
+
By submitting the form above, you agree to BRIA’s [Privacy policy](https://bria.ai/privacy-policy/) and [Terms & conditions](https://bria.ai/terms-and-conditions/).
|
73 |
+
|
74 |
+
|
75 |
+
### How To Use
|
76 |
+
```python
|
77 |
+
opencv-python==4.10.0.84
|
78 |
+
torch==2.4.0
|
79 |
+
torchvision==0.19.0
|
80 |
+
pillow==10.4.0
|
81 |
+
transformers==4.43.4
|
82 |
+
diffusers==0.29.2
|
83 |
+
```
|
84 |
+
|
85 |
+
|
86 |
+
```python
|
87 |
+
import gc
|
88 |
+
import os
|
89 |
+
import random
|
90 |
+
import gradio as gr
|
91 |
+
|
92 |
+
|
93 |
+
import cv2
|
94 |
+
import torch
|
95 |
+
import numpy as np
|
96 |
+
from PIL import Image
|
97 |
+
|
98 |
+
from transformers import CLIPVisionModelWithProjection
|
99 |
+
from diffusers.utils import load_image
|
100 |
+
from diffusers.models import ControlNetModel
|
101 |
+
from insightface.app import FaceAnalysis
|
102 |
+
|
103 |
+
import io
|
104 |
+
import spaces
|
105 |
+
|
106 |
+
from pipeline_stable_diffusion_xl_instantid import StableDiffusionXLInstantIDPipeline, draw_kps
|
107 |
+
|
108 |
+
import pandas as pd
|
109 |
+
import json
|
110 |
+
import requests
|
111 |
+
from PIL import Image
|
112 |
+
from io import BytesIO
|
113 |
+
|
114 |
+
|
115 |
+
def resize_img(input_image, max_side=1280, min_side=1024, size=None,
|
116 |
+
pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
|
117 |
+
|
118 |
+
w, h = input_image.size
|
119 |
+
if size is not None:
|
120 |
+
w_resize_new, h_resize_new = size
|
121 |
+
else:
|
122 |
+
ratio = min_side / min(h, w)
|
123 |
+
w, h = round(ratio*w), round(ratio*h)
|
124 |
+
ratio = max_side / max(h, w)
|
125 |
+
input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
|
126 |
+
w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
|
127 |
+
h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
|
128 |
+
input_image = input_image.resize([w_resize_new, h_resize_new], mode)
|
129 |
+
|
130 |
+
if pad_to_max_side:
|
131 |
+
res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
|
132 |
+
offset_x = (max_side - w_resize_new) // 2
|
133 |
+
offset_y = (max_side - h_resize_new) // 2
|
134 |
+
res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
|
135 |
+
input_image = Image.fromarray(res)
|
136 |
+
return input_image
|
137 |
+
|
138 |
+
|
139 |
+
def make_canny_condition(image, min_val=100, max_val=200, w_bilateral=True):
|
140 |
+
if w_bilateral:
|
141 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
142 |
+
bilateral_filtered_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)
|
143 |
+
image = cv2.Canny(bilateral_filtered_image, min_val, max_val)
|
144 |
+
else:
|
145 |
+
image = np.array(image)
|
146 |
+
image = cv2.Canny(image, min_val, max_val)
|
147 |
+
image = image[:, :, None]
|
148 |
+
image = np.concatenate([image, image, image], axis=2)
|
149 |
+
image = Image.fromarray(image)
|
150 |
+
return image
|
151 |
+
|
152 |
+
|
153 |
+
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"
|
154 |
+
|
155 |
+
# Load face detection and recognition package
|
156 |
+
app = FaceAnalysis(name='antelopev2', root='./', providers=['CPUExecutionProvider'])
|
157 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
face_adapter = f"./ip-adapter.bin"
|
162 |
+
controlnet_path = f"./controlnet"
|
163 |
+
|
164 |
+
|
165 |
+
base_model_path = f'briaai/BRIA-2.3'
|
166 |
+
resolution = 1024
|
167 |
+
|
168 |
+
controlnet_lnmks = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
|
169 |
+
|
170 |
+
controlnet_canny = ControlNetModel.from_pretrained("briaai/BRIA-2.3-ControlNet-Canny",
|
171 |
+
torch_dtype=torch.float16)
|
172 |
+
|
173 |
+
controlnet = [controlnet_lnmks, controlnet_canny]
|
174 |
+
|
175 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
176 |
+
|
177 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
178 |
+
f"./checkpoints/image_encoder",
|
179 |
+
torch_dtype=torch.float16,
|
180 |
+
)
|
181 |
+
|
182 |
+
pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
|
183 |
+
base_model_path,
|
184 |
+
controlnet=controlnet,
|
185 |
+
torch_dtype=torch.float16,
|
186 |
+
image_encoder=image_encoder # For compatibility issues - needs to be there
|
187 |
+
)
|
188 |
+
|
189 |
+
pipe = pipe.to(device)
|
190 |
+
|
191 |
+
pipe.use_native_ip_adapter=True
|
192 |
+
|
193 |
+
pipe.load_ip_adapter_instantid(face_adapter)
|
194 |
+
|
195 |
+
clip_embeds=None
|
196 |
+
|
197 |
+
|
198 |
+
image_path = "<define a path to image>"
|
199 |
+
img = Image.open(image_path)
|
200 |
+
|
201 |
+
prompt = "A male with brown eyes, gray hair, short hair, and wearing sunglasses."
|
202 |
+
face_image = resize_img(face_image_orig, max_side=resolution, min_side=resolution)
|
203 |
+
face_image_padded = resize_img(face_image_orig, max_side=resolution, min_side=resolution, pad_to_max_side=True)
|
204 |
+
face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
|
205 |
+
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
|
206 |
+
face_emb = face_info['embedding']
|
207 |
+
face_kps = draw_kps(face_image, face_info['kps'])
|
208 |
+
|
209 |
+
|
210 |
+
# ================= Parameters =================
|
211 |
+
kps_scale = 0.6
|
212 |
+
canny_scale = 0.4
|
213 |
+
ip_adapter_scale = 0.8
|
214 |
+
|
215 |
+
if canny_scale>0.0:
|
216 |
+
canny_img = make_canny_condition(face_image, min_val=20, max_val=40, w_bilateral=True)
|
217 |
+
|
218 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
219 |
+
|
220 |
+
|
221 |
+
images = pipe(
|
222 |
+
prompt = full_prompt,
|
223 |
+
negative_prompt = default_negative_prompt,
|
224 |
+
image_embeds = face_emb,
|
225 |
+
image = [face_kps, canny_img] if canny_scale > 0.0 else face_kps,
|
226 |
+
controlnet_conditioning_scale = [kps_scale, canny_scale] if canny_scale>0.0 else kps_scale,
|
227 |
+
ip_adapter_scale = ip_adapter_scale,
|
228 |
+
num_inference_steps = num_steps,
|
229 |
+
guidance_scale = guidance_scale,
|
230 |
+
generator = generator,
|
231 |
+
visual_prompt_embds = clip_embeds,
|
232 |
+
cross_attention_kwargs = None,
|
233 |
+
num_images_per_prompt=num_images,
|
234 |
+
).images[0]
|
235 |
+
|
236 |
+
|
237 |
+
```
|