File size: 5,448 Bytes
7a07ff9
 
640a27b
 
843b14b
 
 
 
640a27b
 
 
 
 
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
import sys
sys.path.insert(0,'stable_diffusion')
import gradio as gr
from train_esd import train_esd
from convertModels import convert_ldm_unet_checkpoint, create_unet_diffusers_config
from omegaconf import OmegaConf
from StableDiffuser import StableDiffuser
from diffusers import UNet2DConditionModel

ckpt_path = "stable-diffusion/models/ldm/sd-v1-4-full-ema.ckpt"
config_path = "stable-diffusion/configs/stable-diffusion/v1-inference.yaml"
diffusers_config_path = "stable-diffusion/config.json"


class Demo:

    def __init__(self) -> None:
        demo = self.layout()
        demo.launch()


    def layout(self):

        with gr.Blocks() as demo:

            with gr.Row():
                with gr.Column() as training_column:
                    self.prompt_input = gr.Text(
                        placeholder="Enter prompt...",
                        label="Prompt",
                        info="Prompt corresponding to concept to erase"
                    )
                    self.train_method_input = gr.Dropdown(
                        choices=['noxattn', 'selfattn', 'xattn', 'full'],
                        value='xattn',
                        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=1000,
                        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.train_button = gr.Button(
                        value="Train",
                    )
                    self.train_button.click(self.train, inputs = [
                            self.prompt_input,
                            self.train_method_input, 
                            self.neg_guidance_input,
                            self.iterations_input,
                            self.lr_input
                        ]
                    )
                with gr.Column() as inference_column:

                    with gr.Row():

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

                    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",
                        )
                        self.infr_button.click(self.inference, inputs = [
                                self.prompt_input_infr,
                            ],
                            outputs=[
                                self.image_new,
                                self.image_orig
                            ]
                        )
        return demo


    def train(self, prompt, train_method, neg_guidance, iterations, lr):

        model_orig, model_edited = train_esd(prompt,
                train_method,
                3,
                neg_guidance,
                iterations,
                lr,
                config_path,
                ckpt_path, 
                diffusers_config_path,
                ['cuda', 'cuda'],
                gr.Progress()
                )
        
        original_config = OmegaConf.load(config_path)
        original_config["model"]["params"]["unet_config"]["params"]["in_channels"] = 4
        unet_config = create_unet_diffusers_config(original_config, image_size=512)
        model_edited_sd = convert_ldm_unet_checkpoint(model_edited.state_dict(), unet_config)
        model_orig_sd = convert_ldm_unet_checkpoint(model_orig.state_dict(), unet_config)

        self.init_inference(model_edited_sd, model_orig_sd, unet_config)

    def init_inference(self, model_edited_sd, model_orig_sd, unet_config):

        self.model_edited_sd = model_edited_sd
        self.model_orig_sd = model_orig_sd

        self.diffuser = StableDiffuser(42)

        self.diffuser.unet = UNet2DConditionModel(**unet_config)
        self.diffuser.to('cuda')


    def inference(self, prompt):

        self.diffuser.unet.load_state_dict(self.model_orig_sd)

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

        orig_image = images[0][0]

        self.diffuser.unet.load_state_dict(self.model_edited_sd)

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

        edited_image = images[0][0]

        return edited_image, orig_image