File size: 5,981 Bytes
4dd2c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa
import keras
from keras import Model, Sequential, initializers
from keras.layers import Layer, Conv2D, LeakyReLU, Dropout


class SPADE(Layer):
    def __init__(self, filters: int, epsilon=1e-5, **kwargs):
        super().__init__(**kwargs)
        self.epsilon = epsilon
        self.conv = Conv2D(128, 3, padding="same", activation="relu")
        self.conv_gamma = Conv2D(filters, 3, padding="same")
        self.conv_beta = Conv2D(filters, 3, padding="same")

    def build(self, input_shape):
        self.resize_shape = input_shape[1:3]

    def call(self, input_tensor, raw_mask):
        mask = tf.image.resize(raw_mask, self.resize_shape, method="nearest")
        x = self.conv(mask)
        gamma = self.conv_gamma(x)
        beta = self.conv_beta(x)
        mean, var = tf.nn.moments(input_tensor, axes=(0, 1, 2), keepdims=True)
        std = tf.sqrt(var + self.epsilon)
        normalized = (input_tensor - mean) / std
        output = gamma * normalized + beta
        return output

    def get_config(self):
        return {
            "epsilon": self.epsilon,
            "conv": self.conv,
            "conv_gamma": self.conv_gamma,
            "conv_beta": self.conv_beta
        }


class ResBlock(Layer):
    def __init__(self, filters: int, **kwargs):
        super().__init__(**kwargs)
        self.filters = filters

    def build(self, input_shape):
        input_filter = input_shape[-1]
        self.spade_1 = SPADE(input_filter)
        self.spade_2 = SPADE(self.filters)
        self.conv_1 = Conv2D(self.filters, 3, padding="same")
        self.conv_2 = Conv2D(self.filters, 3, padding="same")
        self.leaky_relu = LeakyReLU(0.2)
        self.learned_skip = False

        if self.filters != input_filter:
            self.learned_skip = True
            self.spade_3 = SPADE(input_filter)
            self.conv_3 = Conv2D(self.filters, 3, padding="same")

    def call(self, input_tensor, mask):
        x = self.spade_1(input_tensor, mask)
        x = self.conv_1(self.leaky_relu(x))
        x = self.spade_2(x, mask)
        x = self.conv_2(self.leaky_relu(x))
        skip = (
            self.conv_3(self.leaky_relu(self.spade_3(input_tensor, mask)))
            if self.learned_skip
            else input_tensor
        )
        output = skip + x
        return output

    def get_config(self):
        return {"filters": self.filters}
    
    
class Downsample(Layer):
    def __init__(self,
                 channels: int,
                 kernels: int,
                 strides: int = 2,
                 apply_norm=True,
                 apply_activation=True,
                 apply_dropout=False,
                 **kwargs
                 ):
        super().__init__(**kwargs)
        self.channels = channels
        self.kernels = kernels
        self.strides = strides
        self.apply_norm = apply_norm
        self.apply_activation = apply_activation
        self.apply_dropout = apply_dropout

    def build(self, input_shape):
        self.block = Sequential([
            Conv2D(
                self.channels,
                self.kernels,
                strides=self.strides,
                padding="same",
                use_bias=False,
                kernel_initializer=initializers.GlorotNormal(),
            )])
        if self.apply_norm:
            self.block.add(tfa.layers.InstanceNormalization())
        if self.apply_activation:
            self.block.add(LeakyReLU(0.2))
        if self.apply_dropout:
            self.block.add(Dropout(0.5))

    def call(self, inputs):
        return self.block(inputs)

    def get_config(self):
        return {
            "channels": self.channels,
            "kernels": self.kernels,
            "strides": self.strides,
            "apply_norm": self.apply_norm,
            "apply_activation": self.apply_activation,
            "apply_dropout": self.apply_dropout,
        }


class GaussianSampler(Layer):
    def __init__(self, latent_dim: int, **kwargs):
        super().__init__(**kwargs)
        self.latent_dim = latent_dim

    def call(self, inputs):
        means, variance = inputs
        epsilon = tf.random.normal(
            shape=(tf.shape(means)[0], self.latent_dim), mean=0.0, stddev=1.0
        )
        samples = means + tf.exp(0.5 * variance) * epsilon
        return samples

    def get_config(self):
        return {"latent_dim": self.latent_dim}


class GauganPredictor():
    
    CLASSES = (
        'unknown','wall', 'sky', 'tree', 'road', 'grass', 'earth',
        'mountain', 'plant', 'water', 'sea', 'field', 'fence', 'rock',
        'sand', 'path', 'river', 'flower', 'hill', 'palm', 'tower',
        'dirt', 'land', 'waterfall', 'lake'
    )
    
    def __init__(self, model_g_path: str, model_e_path: str = None) -> None:
        custom_objects = {
            'ResBlock': ResBlock,
            'Downsample': Downsample,
        }
        if model_e_path is not None:
            self.encoder: Model = keras.models.load_model(model_e_path, custom_objects=custom_objects)
            self.sampler = GaussianSampler(256)
        self.gen: Model = keras.models.load_model(
            model_g_path, custom_objects=custom_objects)

    def __call__(self, im: np.ndarray, z=None) -> np.ndarray:
        if len(im.shape) == 3:
            im = im[np.newaxis]
        if z is None:
            z = tf.random.normal((im.shape[0], 256))
        tmp = self.gen.predict_on_batch([z, im])
        x = np.array((tmp + 1) * 127.5, np.uint8)
        return x

    def predict_reference(self, im: np.ndarray, reference_im: np.ndarray) -> np.ndarray:
        if len(im.shape) == 3:
            im = im[np.newaxis]
            reference_im = reference_im[np.newaxis]
        mean, variance = self.encoder(reference_im)
        z = self.sampler([mean, variance])
        x = np.array((self.gen.predict_on_batch([z, im]) + 1) * 127.5, np.uint8)
        return x