ayushtues commited on
Commit
1a70fad
1 Parent(s): b077a99

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +167 -0
README.md ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ library_name: diffusers
6
+ ---
7
+ # BLIP-Diffusion: Pre-trained Subject Representation for Controllable Text-to-Image Generation and Editing
8
+
9
+
10
+ <!-- Provide a quick summary of what the model is/does. -->
11
+
12
+ Model card for BLIP-Diffusion, a text to image Diffusion model which enables zero-shot subject-driven generation and control-guided zero-shot generation.
13
+
14
+ The abstract from the paper is:
15
+
16
+ *Subject-driven text-to-image generation models create novel renditions of an input subject based on text prompts. Existing models suffer from lengthy fine-tuning and difficulties preserving the subject fidelity. To overcome these limitations, we introduce BLIP-Diffusion, a new subject-driven image generation model that supports multimodal control which consumes inputs of subject images and text prompts. Unlike other subject-driven generation models, BLIP-Diffusion introduces a new multimodal encoder which is pre-trained to provide subject representation. We first pre-train the multimodal encoder following BLIP-2 to produce visual representation aligned with the text. Then we design a subject representation learning task which enables a diffusion model to leverage such visual representation and generates new subject renditions. Compared with previous methods such as DreamBooth, our model enables zero-shot subject-driven generation, and efficient fine-tuning for customized subject with up to 20x speedup. We also demonstrate that BLIP-Diffusion can be flexibly combined with existing techniques such as ControlNet and prompt-to-prompt to enable novel subject-driven generation and editing applications.*
17
+
18
+ The model is created by Dongxu Li, Junnan Li, Steven C.H. Hoi.
19
+
20
+ ### Model Sources
21
+
22
+ <!-- Provide the basic links for the model. -->
23
+
24
+ - **Original Repository:** https://github.com/salesforce/LAVIS/tree/main
25
+ - **Project Page:** https://dxli94.github.io/BLIP-Diffusion-website/
26
+
27
+ ## Uses
28
+
29
+
30
+ ### Zero-Shot Subject Driven Generation
31
+ ```python
32
+ from diffusers.pipelines import BlipDiffusionPipeline
33
+ from diffusers.utils import load_image
34
+ blip_diffusion_pipe= BlipDiffusionPipeline.from_pretrained('ayushtues/blipdiffusion')
35
+ blip_diffusion_pipe.to('cuda')
36
+ cond_subject = "dog"
37
+ tgt_subject = "dog"
38
+ text_prompt_input = "swimming underwater"
39
+ cond_image = load_image("https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/dog.jpg")
40
+ guidance_scale = 7.5
41
+ num_inference_steps = 50
42
+ negative_prompt = "over-exposure, under-exposure, saturated, duplicate, out of frame, lowres, cropped, worst quality, low quality, jpeg artifacts, morbid, mutilated, out of frame, ugly, bad anatomy, bad proportions, deformed, blurry, duplicate"
43
+ output = blip_diffusion_pipe(
44
+ text_prompt_input,
45
+ cond_image,
46
+ cond_subject,
47
+ tgt_subject,
48
+ guidance_scale=guidance_scale,
49
+ num_inference_steps=num_inference_steps,
50
+ neg_prompt=negative_prompt,
51
+ height=512,
52
+ width=512,
53
+ )
54
+ output[0][0].save("image.png")
55
+ ```
56
+ Input Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/dog.jpg" style="width:500px;"/>
57
+
58
+ Generatred Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/dog_underwater.png" style="width:500px;"/>
59
+
60
+ ### Controlled subject-driven generation
61
+
62
+ ```python
63
+ from diffusers.pipelines import BlipDiffusionControlNetPipeline
64
+ from diffusers.utils import load_image
65
+ from controlnet_aux import CannyDetector
66
+ blip_diffusion_pipe= BlipDiffusionControlNetPipeline.from_pretrained("ayushtues/blipdiffusion-controlnet")
67
+ blip_diffusion_pipe.to('cuda')
68
+ style_subject = "flower" # subject that defines the style
69
+ tgt_subject = "teapot" # subject to generate.
70
+ text_prompt = "on a marble table"
71
+ cldm_cond_image = load_image("https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/kettle.jpg").resize((512, 512))
72
+ canny = CannyDetector()
73
+ cldm_cond_image = canny(cldm_cond_image, 30, 70, output_type='pil')
74
+ style_image = load_image("https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/flower.jpg")
75
+ guidance_scale = 7.5
76
+ num_inference_steps = 50
77
+ negative_prompt = "over-exposure, under-exposure, saturated, duplicate, out of frame, lowres, cropped, worst quality, low quality, jpeg artifacts, morbid, mutilated, out of frame, ugly, bad anatomy, bad proportions, deformed, blurry, duplicate"
78
+ output = blip_diffusion_pipe(
79
+ text_prompt,
80
+ style_image,
81
+ cldm_cond_image,
82
+ style_subject,
83
+ tgt_subject,
84
+ guidance_scale=guidance_scale,
85
+ num_inference_steps=num_inference_steps,
86
+ neg_prompt=negative_prompt,
87
+ height=512,
88
+ width=512,
89
+ )
90
+ output[0][0].save("image.png")
91
+ ```
92
+
93
+ Input Style Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/flower.jpg" style="width:500px;"/>
94
+ Canny Edge Input : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/kettle.jpg" style="width:500px;"/>
95
+ Generated Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/canny_generated.png" style="width:500px;"/>
96
+
97
+ ### Controlled subject-driven generation Scribble
98
+ ```python
99
+ from diffusers.pipelines import BlipDiffusionControlNetPipeline
100
+ from diffusers.utils import load_image
101
+ from controlnet_aux import HEDdetector
102
+ blip_diffusion_pipe= BlipDiffusionControlNetPipeline.from_pretrained("ayushtues/blipdiffusion-controlnet")
103
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-scribble")
104
+ blip_diffusion_pipe.controlnet = controlnet
105
+ blip_diffusion_pipe.to('cuda')
106
+ style_subject = "flower" # subject that defines the style
107
+ tgt_subject = "bag" # subject to generate.
108
+ text_prompt = "on a table"
109
+ cldm_cond_image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-scribble/resolve/main/images/bag.png" ).resize((512, 512))
110
+ hed = HEDdetector.from_pretrained("lllyasviel/Annotators")
111
+ cldm_cond_image = hed(cldm_cond_image)
112
+ style_image = load_image("https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/flower.jpg")
113
+ guidance_scale = 7.5
114
+ num_inference_steps = 50
115
+ negative_prompt = "over-exposure, under-exposure, saturated, duplicate, out of frame, lowres, cropped, worst quality, low quality, jpeg artifacts, morbid, mutilated, out of frame, ugly, bad anatomy, bad proportions, deformed, blurry, duplicate"
116
+ output = blip_diffusion_pipe(
117
+ text_prompt,
118
+ style_image,
119
+ cldm_cond_image,
120
+ style_subject,
121
+ tgt_subject,
122
+ guidance_scale=guidance_scale,
123
+ num_inference_steps=num_inference_steps,
124
+ neg_prompt=negative_prompt,
125
+ height=512,
126
+ width=512,
127
+ )
128
+ output[0][0].save("image.png"')
129
+ ```
130
+
131
+ Input Style Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/flower.jpg" style="width:500px;"/>
132
+ Scribble Input : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/scribble.png" style="width:500px;"/>
133
+ Generated Image : <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/scribble_output.png" style="width:500px;"/>
134
+
135
+ ## Model Architecture
136
+
137
+ Blip-Diffusion learns a **pre-trained subject representation**. uch representation aligns with text embeddings and in the meantime also encodes the subject appearance. This allows efficient fine-tuning of the model for high-fidelity subject-driven applications, such as text-to-image generation, editing and style transfer.
138
+
139
+ To this end, they design a two-stage pre-training strategy to learn generic subject representation. In the first pre-training stage, they perform multimodal representation learning, which enforces BLIP-2 to produce text-aligned visual features based on the input image. In the second pre-training stage, they design a subject representation learning task, called prompted context generation, where the diffusion model learns to generate novel subject renditions based on the input visual features.
140
+
141
+ To achieve this, they curate pairs of input-target images with the same subject appearing in different contexts. Specifically, they synthesize input images by composing the subject with a random background. During pre-training, they feed the synthetic input image and the subject class label through BLIP-2 to obtain the multimodal embeddings as subject representation. The subject representation is then combined with a text prompt to guide the generation of the target image.
142
+
143
+ ![img](https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/arch.jpg)
144
+
145
+ The architecture is also compatible to integrate with established techniques built on top of the diffusion model, such as ControlNet.
146
+
147
+ They attach the U-Net of the pre-trained ControlNet to that of BLIP-Diffusion via residuals. In this way, the model takes into account the input structure condition, such as edge maps and depth maps, in addition to the subject cues. Since the model inherits the architecture of the original latent diffusion model, they observe satisfying generations using off-the-shelf integration with pre-trained ControlNet without further training.
148
+
149
+ <img src="https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/arch_controlnet.png" style="width:50%;"/>
150
+
151
+ ## Citation
152
+
153
+
154
+ **BibTeX:**
155
+
156
+ If you find this repository useful in your research, please cite:
157
+
158
+ ```
159
+ @misc{li2023blipdiffusion,
160
+ title={BLIP-Diffusion: Pre-trained Subject Representation for Controllable Text-to-Image Generation and Editing},
161
+ author={Dongxu Li and Junnan Li and Steven C. H. Hoi},
162
+ year={2023},
163
+ eprint={2305.14720},
164
+ archivePrefix={arXiv},
165
+ primaryClass={cs.CV}
166
+ }
167
+ ```