dvir-bria commited on
Commit
75a2ed4
·
verified ·
1 Parent(s): 24de7f0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +202 -5
README.md CHANGED
@@ -1,5 +1,202 @@
1
- ---
2
- license: other
3
- license_name: bria-t2i
4
- license_link: https://bria.ai/customer-general-terms-and-conditions#
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ license_name: bria-2.3
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
+ 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.
13
+ extra_gated_heading: "Fill in this form to request a commercial license for the model"
14
+ extra_gated_fields:
15
+ Name: text
16
+ Company/Org name: text
17
+ Org Type (Early/Growth Startup, Enterprise, Academy): text
18
+ Role: text
19
+ Country: text
20
+ Email: text
21
+ By submitting this form, I agree to BRIA’s Privacy policy and Terms & conditions, see links below: checkbox
22
+ ---
23
+
24
+
25
+
26
+ # BRIA 2.3 ControlNet Generative Fill Fast
27
+
28
+
29
+ Trained exclusively on the largest multi-source commercial-grade licensed dataset, BRIA 2.3 Generative Fill 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.
30
+
31
+
32
+ BRIA 2.3 is an Generative Fill model designed to fill masked regions in images based on user-provided textual prompts. The model can be applied in different scenarios, including object removal, replacement, addition, and modification within an image, while also possessing the capability to expand the image.
33
+
34
+
35
+ Join our [Discord community](https://discord.gg/Nxe9YW9zHS) for more information, tutorials, tools, and to connect with other users!
36
+
37
+
38
+ # What's New
39
+
40
+ BRIA 2.3 ControlNet Generative Fill can be applied on top of BRIA 2.3 Text-to-Image and therefore enable to use [Fast-LORA](https://huggingface.co/briaai/BRIA-2.3-FAST-LORA). This results in extremely fast inpainting model, requires only 1.6s using A10 GPU.
41
+
42
+
43
+
44
+
45
+
46
+ ### Model Description
47
+
48
+ - **Developed by:** BRIA AI
49
+ - **Model type:** Latent diffusion image-to-image model
50
+ - **License:** [bria-2.3 inpainting Licensing terms & conditions](https://bria.ai/bria-huggingface-model-license-agreement/).
51
+ - Purchase is required to license and access the model.
52
+ - **Model Description:** BRIA 2.3 Generative Fill was trained exclusively on a professional-grade, licensed dataset. It is designed for commercial use and includes full legal liability coverage.
53
+ - **Resources for more information:** [BRIA AI](https://bria.ai/)
54
+
55
+
56
+
57
+
58
+ ### Get Access to the source code and pre-trained model
59
+ Interested in BRIA 2.3 Generative Fill? Our Model is available for purchase.
60
+
61
+ **Purchasing access to BRIA 2.3 Generative Fill ensures royalty management and full liability for commercial use.**
62
+
63
+
64
+ *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.
65
+
66
+
67
+ **Contact us today to unlock the potential of BRIA 2.3 Generative Fill!**
68
+
69
+ 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/).
70
+
71
+
72
+ ### How To Use
73
+
74
+ ```python
75
+ from diffusers import (
76
+ AutoencoderKL,
77
+ LCMScheduler,
78
+ )
79
+ from pipeline_controlnet_sd_xl import StableDiffusionXLControlNetPipeline
80
+ from controlnet import ControlNetModel, ControlNetConditioningEmbedding
81
+ import torch
82
+ import numpy as np
83
+ from PIL import Image
84
+ import requests
85
+ import PIL
86
+ from io import BytesIO
87
+ from torchvision import transforms
88
+ import pandas as pd
89
+ import os
90
+
91
+
92
+ def resize_image_to_retain_ratio(image):
93
+ pixel_number = 1024*1024
94
+ granularity_val = 8
95
+ ratio = image.size[0] / image.size[1]
96
+ width = int((pixel_number * ratio) ** 0.5)
97
+ width = width - (width % granularity_val)
98
+ height = int(pixel_number / width)
99
+ height = height - (height % granularity_val)
100
+
101
+ image = image.resize((width, height))
102
+ return image
103
+
104
+
105
+ def download_image(url):
106
+ response = requests.get(url)
107
+ return PIL.Image.open(BytesIO(response.content)).convert("RGB")
108
+
109
+
110
+ def get_masked_image(image, image_mask, width, height):
111
+ image_mask = image_mask # inpaint area is white
112
+ image_mask = image_mask.resize((width, height)) # object to remove is white (1)
113
+ image_mask_pil = image_mask
114
+ image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
115
+ image_mask = np.array(image_mask_pil.convert("L")).astype(np.float32) / 255.0
116
+ assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
117
+ masked_image_to_present = image.copy()
118
+ masked_image_to_present[image_mask > 0.5] = (0.5,0.5,0.5) # set as masked pixel
119
+ image[image_mask > 0.5] = 0.5 # set as masked pixel - s.t. will be grey
120
+ image = Image.fromarray((image * 255.0).astype(np.uint8))
121
+ masked_image_to_present = Image.fromarray((masked_image_to_present * 255.0).astype(np.uint8))
122
+ return image, image_mask_pil, masked_image_to_present
123
+
124
+
125
+ image_transforms = transforms.Compose(
126
+ [
127
+ transforms.ToTensor(),
128
+ ]
129
+ )
130
+
131
+ default_negative_prompt = "blurry"
132
+
133
+ img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
134
+ mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
135
+
136
+ init_image = download_image(img_url).resize((1024, 1024))
137
+ mask_image = download_image(mask_url).resize((1024, 1024))
138
+
139
+
140
+ init_image = resize_image_to_retain_ratio(init_image)
141
+ width, height = init_image.size
142
+
143
+ mask_image = mask_image.convert("L").resize(init_image.size)
144
+
145
+ width, height = init_image.size
146
+
147
+ # Load, init model
148
+ controlnet = ControlNetModel().from_pretrained("briaai/BRIA-2.3-ControlNet-Generative-Fill", torch_dtype=torch.float16)
149
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
150
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained("briaai/BRIA-2.3", controlnet=controlnet.to(dtype=torch.float16), torch_dtype=torch.float16, vae=vae) #force_zeros_for_empty_prompt=False, # vae=vae)
151
+
152
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
153
+ pipe.load_lora_weights("briaai/BRIA-2.3-FAST-LORA")
154
+ pipe.fuse_lora()
155
+ pipe = pipe.to(device="cuda")
156
+
157
+ # pipe.enable_xformers_memory_efficient_attention()
158
+
159
+ generator = torch.Generator(device="cuda").manual_seed(123456)
160
+
161
+ vae = pipe.vae
162
+
163
+
164
+ masked_image, image_mask, masked_image_to_present = get_masked_image(init_image, mask_image, width, height)
165
+
166
+ masked_image_tensor = image_transforms(masked_image)
167
+ masked_image_tensor = (masked_image_tensor - 0.5) / 0.5
168
+
169
+
170
+ masked_image_tensor = masked_image_tensor.unsqueeze(0).to(device="cuda")
171
+ control_latents = vae.encode(
172
+ masked_image_tensor[:, :3, :, :].to(vae.dtype)
173
+ ).latent_dist.sample()
174
+ control_latents = control_latents * vae.config.scaling_factor
175
+
176
+
177
+ image_mask = np.array(image_mask)[:,:]
178
+ mask_tensor = torch.tensor(image_mask, dtype=torch.float32)[None, ...]
179
+ # binarize the mask
180
+ mask_tensor = torch.where(mask_tensor > 128.0, 255.0, 0)
181
+
182
+ mask_tensor = mask_tensor / 255.0
183
+
184
+ mask_tensor = mask_tensor.to(device="cuda")
185
+ mask_resized = torch.nn.functional.interpolate(mask_tensor[None, ...], size=(control_latents.shape[2], control_latents.shape[3]), mode='nearest')
186
+
187
+ masked_image = torch.cat([control_latents, mask_resized], dim=1)
188
+
189
+ prompt = ""
190
+
191
+ gen_img = pipe(negative_prompt=default_negative_prompt, prompt=prompt,
192
+ controlnet_conditioning_scale=1.0,
193
+ num_inference_steps=12,
194
+ height=height, width=width,
195
+ image = masked_image, # control image
196
+ init_image = init_image,
197
+ mask_image = mask_tensor,
198
+ guidance_scale = 1.2,
199
+ generator=generator).images[0]
200
+
201
+ ```
202
+