File size: 10,753 Bytes
8c212a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import numpy as np
import os.path as osp
import torch
from lib import GENFORCE_MODELS
from models.load_generator import load_generator
from sklearn import linear_model
from collections import defaultdict
from tqdm import tqdm
import json


def make_dict():
    return defaultdict(make_dict)


def main():
    """A script for calculating the radii of minimal enclosing balls for the latent space of a (i.e., in Z/W/W+ space),
    given a truncation parameter. When applicable, a linear model is trained in order to predict the radii of the latent
    codes, given a truncation parameter.

    The parameters of the linear model (i.e., the weight w and the bias b) are stored for each GAN type and each latent
    space in a json file (i.e., models/jung_radii.json) as a dictionary with the following format:
        {
            ...
            <gan>:
                {
                    'Z': (<w>, <b>),
                    'W':
                        {
                            ...
                            <stylegan-layer>: (<w>, <b>),
                            ...
                        },
                },
            ...
        }
    so as, given a truncation parameter t, the radius is given as `w * t + b`.

    Options:
        -v, --verbose    : set verbose mode on
        --num-samples    : set the number of latent codes to sample for generating images
        --cuda           : use CUDA (default)
        --no-cuda        : do not use CUDA
    """
    parser = argparse.ArgumentParser(description="Fit a linear model for the jung radius of GAN's latent code given "
                                                 "a truncation parameter")
    parser.add_argument('-v', '--verbose', action='store_true', help="verbose mode on")
    parser.add_argument('--num-samples', type=int, default=1000, help="set number of latent codes to sample")
    parser.add_argument('--cuda', dest='cuda', action='store_true', help="use CUDA during training")
    parser.add_argument('--no-cuda', dest='cuda', action='store_false', help="do NOT use CUDA during training")
    parser.set_defaults(cuda=True)
    # ================================================================================================================ #

    # Parse given arguments
    args = parser.parse_args()

    # CUDA
    use_cuda = False
    if torch.cuda.is_available():
        if args.cuda:
            use_cuda = True
            torch.set_default_tensor_type('torch.cuda.FloatTensor')
        else:
            print("*** WARNING ***: It looks like you have a CUDA device, but aren't using CUDA.\n"
                  "                 Run with --cuda for optimal training speed.")
            torch.set_default_tensor_type('torch.FloatTensor')
    else:
        torch.set_default_tensor_type('torch.FloatTensor')

    # Build jung radii dictionary and populate it
    nested_dict = lambda: defaultdict(nested_dict)
    jung_radii_dict = nested_dict()
    for gan in GENFORCE_MODELS.keys():
        ################################################################################################################
        ##                                                                                                            ##
        ##                                               [ StyleGANs ]                                                ##
        ##                                                                                                            ##
        ################################################################################################################
        if 'stylegan' in gan:
            ############################################################################################################
            ##                                                                                                        ##
            ##                                         [ StyleGAN / Z-space ]                                         ##
            ##                                                                                                        ##
            ############################################################################################################
            # Build GAN generator model and load with pre-trained weights
            if args.verbose:
                print("  \\__Build GAN generator model G and load with pre-trained weights...")
                print("      \\__GAN generator : {} (res: {})".format(gan, GENFORCE_MODELS[gan][1]))
                print("      \\__Pre-trained weights: {}".format(GENFORCE_MODELS[gan][0]))

            G = load_generator(model_name=gan, latent_is_w=False, verbose=args.verbose).eval()

            # Upload GAN generator model to GPU
            if use_cuda:
                G = G.cuda()

            # Latent codes sampling
            if args.verbose:
                print("  \\__Sample {} {}-dimensional latent codes...".format(args.num_samples, G.dim_z))
            zs = torch.randn(args.num_samples, G.dim_z)

            if use_cuda:
                zs = zs.cuda()

            # Calculate expected latent norm
            if args.verbose:
                print("  \\__Calculate Jung radius...")
            jung_radius = torch.cdist(zs, zs).max() * np.sqrt(G.dim_z / (2 * (G.dim_z + 1)))
            jung_radii_dict[gan]['Z'] = (0.0, jung_radius.cpu().detach().item())

            ############################################################################################################
            ##                                                                                                        ##
            ##                                       [ StyleGAN / W/W+-space ]                                        ##
            ##                                                                                                        ##
            ############################################################################################################
            # Build GAN generator model and load with pre-trained weights
            if args.verbose:
                print("  \\__Build GAN generator model G and load with pre-trained weights...")
                print("      \\__GAN generator : {} (res: {})".format(gan, GENFORCE_MODELS[gan][1]))
                print("      \\__Pre-trained weights: {}".format(GENFORCE_MODELS[gan][0]))

            G = load_generator(model_name=gan, latent_is_w=True, verbose=args.verbose).eval()

            # Upload GAN generator model to GPU
            if use_cuda:
                G = G.cuda()

            # Latent codes sampling
            if args.verbose:
                print("  \\__Sample {} {}-dimensional latent codes...".format(args.num_samples, G.dim_z))
            zs = torch.randn(args.num_samples, G.dim_z)

            if use_cuda:
                zs = zs.cuda()

            # Get number of W layers for the given StyleGAN
            stylegan_num_layers = G.get_w(zs, truncation=1.0).shape[1]

            # Calculate expected latent norm and fit a linear model for each version of the W+ space
            if args.verbose:
                print("  \\__Calculate Jung radii and fit linear models...")
            data_per_layer = dict()
            tmp = []
            for truncation in tqdm(np.linspace(0.1, 1.0, 100), desc="  \\__Calculate radii (W space): "):
                ws = G.get_w(zs, truncation=truncation)[:, 0, :]
                jung_radius = torch.cdist(ws, ws).max() * np.sqrt(ws.shape[1] / (2 * (ws.shape[1] + 1)))
                tmp.append([truncation, jung_radius.cpu().detach().item()])
            data_per_layer.update({0: tmp})

            for ll in tqdm(range(1, stylegan_num_layers), desc="  \\__Calculate radii (W+ space): "):
                tmp = []
                for truncation in np.linspace(0.1, 1.0, 100):
                    ws_plus = G.get_w(zs, truncation=truncation)[:, :ll + 1, :]
                    ws_plus = ws_plus.reshape(ws_plus.shape[0], -1)
                    jung_radius = torch.cdist(ws_plus, ws_plus).max() * \
                        np.sqrt(ws_plus.shape[1] / (2 * (ws_plus.shape[1] + 1)))
                    tmp.append([truncation, jung_radius.cpu().detach().item()])
                data_per_layer.update({ll: tmp})

            for ll, v in tqdm(data_per_layer.items(), desc="  \\__Fit linear models"):
                v = np.array(v)
                lm = linear_model.LinearRegression()
                lm.fit(v[:, 0].reshape(-1, 1), v[:, 1].reshape(-1, 1))
                jung_radii_dict[gan]['W'][ll] = (float(lm.coef_[0, 0]), float(lm.intercept_[0]))

        ################################################################################################################
        ##                                                                                                            ##
        ##                                                [ ProgGAN ]                                                 ##
        ##                                                                                                            ##
        ################################################################################################################
        else:
            # Build GAN generator model and load with pre-trained weights
            if args.verbose:
                print("  \\__Build GAN generator model G and load with pre-trained weights...")
                print("      \\__GAN generator : {} (res: {})".format(gan, GENFORCE_MODELS[gan][1]))
                print("      \\__Pre-trained weights: {}".format(GENFORCE_MODELS[gan][0]))

            G = load_generator(model_name=gan, latent_is_w=False, verbose=args.verbose).eval()

            # Upload GAN generator model to GPU
            if use_cuda:
                G = G.cuda()

            # Latent codes sampling
            if args.verbose:
                print("  \\__Sample {} {}-dimensional latent codes...".format(args.num_samples, G.dim_z))
            zs = torch.randn(args.num_samples, G.dim_z)

            if use_cuda:
                zs = zs.cuda()

            # Calculate expected latent norm
            if args.verbose:
                print("  \\__Calculate Jung radius...")
            jung_radius = torch.cdist(zs, zs).max() * np.sqrt(G.dim_z / (2 * (G.dim_z + 1)))

            print("jung_radius")
            print(jung_radius)
            print(type(jung_radius))

            jung_radii_dict[gan]['Z'] = (0.0, jung_radius.cpu().detach().item())

    # Save expected latent norms dictionary
    with open(osp.join('models', 'jung_radii.json'), 'w') as fp:
        json.dump(jung_radii_dict, fp)


if __name__ == '__main__':
    main()