Beispiel #1
0
def g_block(inp, style, noise, fil, u = True):
    
    b = Dense(fil)(style)
    b = Reshape([1, 1, fil])(b)
    g = Dense(fil)(style)
    g = Reshape([1, 1, fil])(g)

    n = Conv2D(filters = fil, kernel_size = 1, padding = 'same', kernel_initializer = 'he_normal')(noise)
    
    if u:
        out = UpSampling2D(interpolation = 'bilinear')(inp)
        out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(out)
    else:
        out = Activation('linear')(inp)
    
    out = AdaInstanceNormalization()([out, b, g])
    out = add([out, n])
    out = LeakyReLU(0.01)(out)
    
    b = Dense(fil)(style)                       
    b = Reshape([1, 1, fil])(b)
    g = Dense(fil)(style)
    g = Reshape([1, 1, fil])(g)

    n = Conv2D(filters = fil, kernel_size = 1, padding = 'same', kernel_initializer = 'he_normal')(noise)
    
    out = Conv2D(filters = fil, kernel_size = 3, padding = 'same', kernel_initializer = 'he_normal')(out)
    out = AdaInstanceNormalization()([out, b, g])
    out = add([out, n])
    out = LeakyReLU(0.01)(out)
    
    return out
def g_block(inp, style, noise, filter_size, upsample=True):
    # b and g = beta and gamma. these are learnt transformations for the
    # AdaIN operation. Both comprise 'A' in the paper diagram.
    b = Dense(filter_size)(style)
    b = Reshape([1, 1, filter_size])(b)
    g = Dense(filter_size)(style)
    g = Reshape([1, 1, filter_size])(g)
    # add noise to block ('B' in the paper diagram)
    n = Conv2D(filters=filter_size,
               kernel_size=1,
               padding='same',
               kernel_initializer='he_normal')(noise)

    # upsample if we need to.
    # this part takes our input and puts it through the conv layer.
    if upsample:
        out = UpSampling2D(interpolation='bilinear')(inp)
        out = Conv2D(filters=filter_size,
                     kernel_size=3,
                     padding='same',
                     kernel_initializer='he_normal')(out)
    else:
        out = inp
    # add our output with the noise
    out = add([out, n])
    # AdaIN gets previous output + b and g (beta and gamma for transformation)
    out = AdaInstanceNormalization()([out, b, g])
    out = LeakyReLU(0.01)(out)
    # the second half of the styleGAN generator block
    # beta and gamma for our second AdaIN operations
    b = Dense(filter_size)(style)
    b = Reshape([1, 1, filter_size])(b)
    g = Dense(filter_size)(style)
    g = Reshape([1, 1, filter_size])(g)
    # add noise again ('B' in the paper diagram)
    n = Conv2D(filters=filter_size,
               kernel_size=1,
               padding='same',
               kernel_initializer='he_normal')(noise)
    # pass output through conv
    out = Conv2D(filters=filter_size,
                 kernel_size=3,
                 padding='same',
                 kernel_initializer='he_normal')(out)
    # apply AdaIN to output and thats it!
    out = AdaInstanceNormalization()([out, b, g])
    # add output and noise
    out = add([out, n])
    out = LeakyReLU(0.01)(out)
    return out