File size: 8,505 Bytes
a24b16a
 
640a27b
f71eb42
a24b16a
 
 
640a27b
a24b16a
843b14b
 
 
 
88c9af4
 
a24b16a
f71eb42
a24b16a
 
 
88c9af4
f0cf9b0
 
a24b16a
f0cf9b0
 
 
d53dfab
843b14b
a24b16a
 
 
 
 
 
f0cf9b0
0166058
f0cf9b0
 
f9f0452
f0cf9b0
 
 
a24b16a
 
f0cf9b0
 
 
843b14b
f0cf9b0
 
 
 
 
 
 
a24b16a
f0cf9b0
 
 
 
843b14b
f0cf9b0
 
 
 
 
88c9af4
 
f0cf9b0
 
 
 
0166058
f0cf9b0
 
 
a24b16a
0166058
 
 
 
 
 
 
 
 
 
 
 
 
843b14b
f0cf9b0
843b14b
f0cf9b0
 
 
843b14b
f0cf9b0
 
 
843b14b
 
f0cf9b0
 
 
 
 
843b14b
f0cf9b0
 
0166058
f0cf9b0
 
 
 
843b14b
 
f0cf9b0
 
 
 
 
 
 
 
 
 
88c9af4
f0cf9b0
843b14b
f0cf9b0
843b14b
88c9af4
 
 
 
 
a24b16a
f71eb42
 
843b14b
a24b16a
843b14b
a24b16a
f0cf9b0
a24b16a
843b14b
a24b16a
f71eb42
a24b16a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f71eb42
a24b16a
843b14b
a24b16a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
843b14b
88c9af4
a24b16a
 
 
843b14b
0166058
843b14b
88c9af4
 
 
 
 
0166058
 
843b14b
 
 
 
 
 
 
 
f71eb42
 
a24b16a
843b14b
a24b16a
 
 
 
 
843b14b
 
 
88c9af4
 
f71eb42
 
843b14b
 
 
f0cf9b0
843b14b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from pathlib import Path

import gradio as gr
import torch
from finetuning import FineTunedModel
from StableDiffuser import StableDiffuser
from tqdm import tqdm

gr.
class Demo:

    def __init__(self) -> None:

        self.training = False
        self.generating = False
        self.nsteps = 50

        self.diffuser = StableDiffuser(scheduler='DDIM', seed=42).to('cuda')
        self.finetuner = None
        

        with gr.Blocks() as demo:
            self.layout()
            demo.queue(concurrency_count=2).launch()

    def disable(self):
        return [gr.update(interactive=False), gr.update(interactive=False)]
  
    def layout(self):

        with gr.Row():

            self.explain = gr.HTML(interactive=False, 
                                      value="<p>This page demonstrates Erasing Concepts in Stable Diffusion (Ganikota, Materzynska, Fiotto-Kaufman and Bau; paper and code linked from https://erasing.baulab.info/). <br> Use it in two steps <br> 1. First, on the left fine-tune your own custom model by naming the concept that you want to erase.  For example,  you can try erasing all cars from a model by entering the prompt corresponding to the concept to erase as 'car'.  This can take awhile.  For example, with the default settings, this can take about an hour. <br> 2. Second, on the right once you have your model fine-tuned, you can try running it in inference. <br>If you want to run it yourself, then you can create your own instance.  Configuration, code, and details are at https://github.com/xxxx/xxxx/xxx</p>")

        with gr.Row():
            with gr.Column(scale=1) as training_column:
                self.prompt_input = gr.Text(
                    placeholder="Enter prompt...",
                    label="Prompt to Erase",
                    info="Prompt corresponding to concept to erase"
                )
                self.train_method_input = gr.Dropdown(
                    choices=['ESD-x', 'ESD-self'],
                    value='ESD-x',
                    label='Train Method',
                    info='Method of training'
                )

                self.neg_guidance_input = gr.Number(
                    value=1,
                    label="Negative Guidance",
                    info='Guidance of negative training used to train'
                )

                self.iterations_input = gr.Number(
                    value=150,
                    precision=0,
                    label="Iterations",
                    info='iterations used to train'
                )

                self.lr_input = gr.Number(
                    value=1e-5,
                    label="Learning Rate",
                    info='Learning rate used to train'
                )
                self.progress_bar = gr.Text(interactive=False, label="Training Progress")

                self.train_button = gr.Button(
                    value="Train",
                )
                
            with gr.Column(scale=2) as inference_column:

                with gr.Row():

                    with gr.Column(scale=5):

                        self.prompt_input_infr = gr.Text(
                            placeholder="Enter prompt...",
                            label="Prompt",
                            info="Prompt to generate"
                        )

                    with gr.Column(scale=1):

                        self.seed_infr = gr.Number(
                            label="Seed",
                            value=42
                        )

                with gr.Row():

                    self.image_new = gr.Image(
                        label="New Image",
                        interactive=False
                    )
                    self.image_orig = gr.Image(
                        label="Orig Image",
                        interactive=False
                    )

                with gr.Row():

                    self.infr_button = gr.Button(
                        value="Generate",
                        interactive=False
                    )
                    self.infr_button.click(self.inference, inputs = [
                            self.prompt_input_infr,
                            self.seed_infr
                        ],
                        outputs=[
                            self.image_new,
                            self.image_orig
                        ]
                    )
                    self.train_button.click(self.disable, 
                        outputs=[self.train_button, self.infr_button]
                    )
                    self.train_button.click(self.train, inputs = [
                        self.prompt_input,
                        self.train_method_input, 
                        self.neg_guidance_input,
                        self.iterations_input,
                        self.lr_input
                    ],
                    outputs=[self.train_button, self.infr_button, self.progress_bar]
                )

    def train(self, prompt, train_method, neg_guidance, iterations, lr, pbar = gr.Progress(track_tqdm=True)):

        if self.training:
            return [None, None, None]
        else:
            self.training = True

        del self.finetuner

        torch.cuda.empty_cache()

        self.diffuser = self.diffuser.train().float()

        if train_method == 'ESD-x':

            modules = ".*attn2$"

        elif train_method == 'ESD-self':

            modules = ".*attn1$"

        finetuner = FineTunedModel(self.diffuser, modules)

        optimizer = torch.optim.Adam(finetuner.parameters(), lr=lr)
        criteria = torch.nn.MSELoss()

        pbar = tqdm(range(iterations))

        with torch.no_grad():

            neutral_text_embeddings = self.diffuser.get_text_embeddings([''],n_imgs=1)
            positive_text_embeddings = self.diffuser.get_text_embeddings([prompt],n_imgs=1)

        for i in pbar:
            
            with torch.no_grad():

                self.diffuser.set_scheduler_timesteps(self.nsteps)

                optimizer.zero_grad()

                iteration = torch.randint(1, self.nsteps - 1, (1,)).item()

                latents = self.diffuser.get_initial_latents(1, 512, 1)

                with finetuner:

                    latents_steps, _ = self.diffuser.diffusion(
                        latents,
                        positive_text_embeddings,
                        start_iteration=0,
                        end_iteration=iteration,
                        guidance_scale=3, 
                        show_progress=False
                    )

                self.diffuser.set_scheduler_timesteps(1000)

                iteration = int(iteration / self.nsteps * 1000)
                
                positive_latents = self.diffuser.predict_noise(iteration, latents_steps[0], positive_text_embeddings, guidance_scale=3)
                neutral_latents = self.diffuser.predict_noise(iteration, latents_steps[0], neutral_text_embeddings, guidance_scale=3)

            with finetuner:
                negative_latents = self.diffuser.predict_noise(iteration, latents_steps[0], positive_text_embeddings, guidance_scale=3)

            positive_latents.requires_grad = False
            neutral_latents.requires_grad = False

            loss = criteria(negative_latents, neutral_latents - (neg_guidance*(positive_latents - neutral_latents))) #loss = criteria(e_n, e_0) works the best try 5000 epochs
            loss.backward()
            optimizer.step()

        self.finetuner = finetuner.eval().half()

        self.diffuser = self.diffuser.eval().half()

        torch.cuda.empty_cache()

        self.training = False

        return [gr.update(interactive=True), gr.update(interactive=True), None]


    def inference(self, prompt, seed, pbar = gr.Progress(track_tqdm=True)):

        if self.generating:
            return [None, None]
        else:
            self.generating = True

        self.diffuser._seed = seed

        images = self.diffuser(
            prompt,
            n_steps=50,
            reseed=True
        )

        orig_image = images[0][0]

        torch.cuda.empty_cache()

        with self.finetuner:

            images = self.diffuser(
                prompt,
                n_steps=50,
                reseed=True
            )

        edited_image = images[0][0]

        self.generating = False

        torch.cuda.empty_cache()

        return edited_image, orig_image


demo = Demo()