File size: 11,934 Bytes
95b1715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372a1fe
 
 
 
 
 
 
 
 
87d55be
95b1715
 
 
 
 
 
 
 
 
 
 
 
 
 
51cbd8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95b1715
 
 
 
 
 
 
 
 
 
 
bfac43d
 
 
 
 
 
 
95b1715
 
bfac43d
 
 
 
 
 
95b1715
 
 
 
 
bfac43d
 
 
 
 
 
 
 
95b1715
 
 
 
bfac43d
 
 
 
 
 
95b1715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372a1fe
 
 
 
 
 
 
 
 
87d55be
95b1715
 
 
 
 
 
 
 
 
 
 
 
 
51cbd8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95b1715
 
 
 
 
 
 
 
 
 
 
bfac43d
 
 
 
 
 
95b1715
 
bfac43d
 
 
 
 
 
 
95b1715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import math
import sys
import pickle
import torch
import argparse
import numpy as np
import torch.nn.functional as F

from torch import nn
from models.psp.encoders import psp_encoders
from models.psp.stylegan2.model import Generator
from models.hyperinverter.stylegan2_ada import Discriminator 
from utils.class_registry import ClassRegistry
from utils.common_utils import get_keys
from utils.model_utils import toogle_grad
from configs.paths import DefaultPaths
from argparse import Namespace
from training.loggers import BaseTimer


sys.path.append("./utils")
methods_registry = ClassRegistry()


@methods_registry.add_to_registry("fse_full", stop_args=("self", "checkpoint_path"))
class FSEFull(nn.Module):
    def __init__(self,
                 device="cuda:0",
                 paths=DefaultPaths,
                 checkpoint_path=None,
                 inverter_pth=None):
        super(FSEFull, self).__init__()
        self.opts = {
            "device": device,
            "checkpoint_path": checkpoint_path,
            "stylegan_size": 1024
        }
        self.opts.update(paths)
        self.opts = Namespace(**self.opts)

        # Handle device detection and fallback to CPU if CUDA is not available
        try:
            torch.randn(1).to(device)
            print("Device: {}".format(device))
        except Exception as e:
            print("Could not use device {}, {}".format(device, e))
            print("Set device to CPU")
            device = "cpu"
        
        self.device = torch.device(device)
        self.inverter_pth = inverter_pth

        self.encoder = self.set_encoder()
        self.decoder = Generator(self.opts.stylegan_size, 512, 8)
        self.latent_avg = None
        self.load_disc()

        self.pool = torch.nn.AdaptiveAvgPool2d((256, 256))
        self.load_weights()


    def load_disc(self):
        # We used the hyperinverter discriminator since it has a cars checkpoint
        print("Loading default Discriminator from ", self.opts.stylegan_weights_pkl)
        try:
            with open(self.opts.stylegan_weights_pkl, "rb") as f:
                ckpt = pickle.load(f)

            D_original = ckpt["D"]
            D_original = D_original.float()

            self.discriminator = Discriminator(**D_original.init_kwargs)
            self.discriminator.load_state_dict(D_original.state_dict())
            self.discriminator.to(self.device)
        except FileNotFoundError:
            print(f"Warning: {self.opts.stylegan_weights_pkl} not found, using uninitialized discriminator")
            # Create a dummy discriminator
            self.discriminator = Discriminator(c_dim=0, img_resolution=1024, img_channels=3)
            self.discriminator.to(self.device)

    def load_disc_from_ckpt(self, ckpt):
        unique_keys = set(key.split(".")[0] for key in ckpt["state_dict"].keys())
        if "discriminator" in unique_keys:
            self.discriminator.load_state_dict(get_keys(ckpt, "discriminator"), strict=True)
        else:
            print("Can not find Discriminator weights in checkpoint, leave default weights.")

    def load_weights(self):
        if self.opts.checkpoint_path != "":
            print(f"Loading from checkpoint: {self.opts.checkpoint_path}")
            try:
                ckpt = torch.load(self.opts.checkpoint_path, map_location="cpu")
                self.load_disc_from_ckpt(ckpt)
                self.encoder.load_state_dict(get_keys(ckpt, "encoder"), strict=True)
                self.inverter.load_state_dict(get_keys(ckpt, "inverter"), strict=True)
            except FileNotFoundError:
                print(f"Warning: {self.opts.checkpoint_path} not found, using uninitialized weights")
        else:
            print(f"Loading Discriminator and Inverter from Inverter checkpoint: {self.inverter_pth}")
            try:
                ckpt = torch.load(self.inverter_pth, map_location="cpu")
                self.load_disc_from_ckpt(ckpt)
                self.inverter.load_state_dict(get_keys(ckpt, "encoder"), strict=True)
            except FileNotFoundError:
                print(f"Warning: {self.inverter_pth} not found, using uninitialized weights")

        self.inverter = self.inverter.eval().to(self.device)
        toogle_grad(self.inverter, False)

        print("Loading Decoder from", self.opts.stylegan_weights)
        try:
            ckpt = torch.load(self.opts.stylegan_weights)
            self.decoder.load_state_dict(ckpt["g_ema"], strict=False)
            self.latent_avg = ckpt['latent_avg'].to(self.device)
        except FileNotFoundError:
            print(f"Warning: {self.opts.stylegan_weights} not found, using uninitialized decoder")
            self.latent_avg = torch.zeros(18, 512).to(self.device)
        
        self.decoder = self.decoder.eval().to(self.device)
        toogle_grad(self.decoder, False)

        print("Loading E4E from", self.opts.e4e_path)
        try:
            ckpt = torch.load(self.opts.e4e_path, map_location="cpu")
            self.e4e_encoder.load_state_dict(get_keys(ckpt, "encoder"), strict=True)
        except FileNotFoundError:
            print(f"Warning: {self.opts.e4e_path} not found, using uninitialized E4E encoder")
        
        self.e4e_encoder = self.e4e_encoder.eval().to(self.device)
        toogle_grad(self.e4e_encoder, False)


    def set_encoder(self):
        self.inverter = psp_encoders.Inverter(opts=self.opts, n_styles=18) 
        self.e4e_encoder = psp_encoders.Encoder4Editing(50, "ir_se", self.opts)
        feat_editor = psp_encoders.ContentLayerDeepFast(6, 1024, 512)
        return feat_editor  # trainable part
    
    def forward(self, x, return_latents=False, n_iter=1e5):
        x = F.interpolate(x, size=(256, 256), mode="bilinear", align_corners=False)

        with torch.no_grad():
            w_recon, predicted_feat = self.inverter.fs_backbone(x)
            w_recon = w_recon + self.latent_avg
                    
            _, w_feats = self.decoder(
                [w_recon],
                input_is_latent=True,
                return_features=True,
                is_stylespace=False,
                randomize_noise=False,
                early_stop=64
            )

            w_feat = w_feats[9]  # bs x 512 x 64 x 64 
            
            fused_feat = self.inverter.fuser(torch.cat([predicted_feat, w_feat], dim=1))
            delta = torch.zeros_like(fused_feat)  # inversion case

        edited_feat = self.encoder(torch.cat([fused_feat, delta], dim=1))
        feats = [None] * 9 + [edited_feat] + [None] * (17 - 9)

        images, _ = self.decoder(
            [w_recon],
            input_is_latent=True,
            return_features=True,
            new_features=feats,
            feature_scale=min(1.0, 0.0001 * n_iter),
            is_stylespace=False,
            randomize_noise=False
        )

        if return_latents:
            if not self.encoder.training:
                fused_feat = fused_feat.cpu()
                predicted_feat = predicted_feat.cpu()
            return images, w_recon, fused_feat, predicted_feat
        return images


@methods_registry.add_to_registry("fse_inverter", stop_args=("self", "checkpoint_path"))
class FSEInverter(nn.Module):
    def __init__(self,
                 device="cuda:0",
                 paths=DefaultPaths,
                 checkpoint_path=None):
        super(FSEInverter, self).__init__()
        self.opts = {
            "device": device,
            "checkpoint_path": checkpoint_path,
            "stylegan_size": 1024
        }
        self.opts.update(paths)
        self.opts = Namespace(**self.opts)

        # Handle device detection and fallback to CPU if CUDA is not available
        try:
            torch.randn(1).to(device)
            print("Device: {}".format(device))
        except Exception as e:
            print("Could not use device {}, {}".format(device, e))
            print("Set device to CPU")
            device = "cpu"
        
        self.device = torch.device(device)
        self.encoder = self.set_encoder()

        self.decoder = Generator(self.opts.stylegan_size, 512, 8)
        self.latent_avg = None
        self.load_disc()

        self.pool = torch.nn.AdaptiveAvgPool2d((256, 256))
        self.load_weights()


    def load_disc(self):
        print("Loading default Discriminator from ", self.opts.stylegan_weights_pkl)
        # We used the hyperinverter discriminator since it has a cars checkpoint
        try:
            with open(self.opts.stylegan_weights_pkl, "rb") as f:
                ckpt = pickle.load(f)

            D_original = ckpt["D"]
            D_original = D_original.float()

            self.discriminator = Discriminator(**D_original.init_kwargs)
            self.discriminator.load_state_dict(D_original.state_dict())
            self.discriminator.to(self.device)
        except FileNotFoundError:
            print(f"Warning: {self.opts.stylegan_weights_pkl} not found, using uninitialized discriminator")
            # Create a dummy discriminator
            self.discriminator = Discriminator(c_dim=0, img_resolution=1024, img_channels=3)
            self.discriminator.to(self.device)

    def load_disc_from_ckpt(self, ckpt):
        unique_keys = set(key.split(".")[0] for key in ckpt["state_dict"].keys())
        if "discriminator" in unique_keys:
            self.discriminator.load_state_dict(get_keys(ckpt, "discriminator"), strict=True)
        else:
            print("Can not find Discriminator weights in checkpoint, leave default weights.")

    def load_weights(self):
        if self.opts.checkpoint_path != "":
            print("Loading  from checkpoint: {}".format(self.opts.checkpoint_path))
            try:
                ckpt = torch.load(self.opts.checkpoint_path, map_location="cpu")
                self.load_disc_from_ckpt(ckpt)
                self.encoder.load_state_dict(get_keys(ckpt, "encoder"), strict=True)
            except FileNotFoundError:
                print(f"Warning: {self.opts.checkpoint_path} not found, using uninitialized weights")

        print("Loading decoder from", self.opts.stylegan_weights)
        try:
            ckpt = torch.load(self.opts.stylegan_weights)
            self.decoder.load_state_dict(ckpt["g_ema"], strict=False)
            self.latent_avg = ckpt['latent_avg'].to(self.device)
        except FileNotFoundError:
            print(f"Warning: {self.opts.stylegan_weights} not found, using uninitialized decoder")
            self.latent_avg = torch.zeros(18, 512).to(self.device)

    def set_encoder(self):
        inverter = psp_encoders.Inverter(opts=self.opts, n_styles=18)
        return inverter  # trainable part
    
    def forward(self, x, return_latents=False, n_iter=1e5):
        x = F.interpolate(x, size=(256, 256), mode="bilinear", align_corners=False)

        w_recon, predicted_feat = self.encoder.fs_backbone(x)
        w_recon = w_recon + self.latent_avg
                
        _, w_feats = self.decoder(
            [w_recon],
            input_is_latent=True,
            return_features=True,
            is_stylespace=False,
            randomize_noise=False,
            early_stop=64
        )

        w_feat = w_feats[9]  # bs x 512 x 64 x 64 
        fused_feat = self.encoder.fuser(torch.cat([predicted_feat, w_feat], dim=1))
        feats = [None] * 9 + [fused_feat] + [None] * (17 - 9)

        images, _ = self.decoder(
            [w_recon],
            input_is_latent=True,
            return_features=True,
            new_features=feats,
            feature_scale=min(1.0, 0.0001 * n_iter),
            is_stylespace=False,
            randomize_noise=False
        )
        
        if return_latents:
            if not self.encoder.training:
                fused_feat = fused_feat.cpu()
                w_feat = w_feat.cpu()
            return images, w_recon, fused_feat, w_feat
        return images