1
def AdaIN(x):
    #Normalize x[0] (image representation)
    mean = K.mean(x[0], axis = [1, 2], keepdims = True)
    std = K.std(x[0], axis = [1, 2], keepdims = True) + 1e-7
    y = (x[0] - mean) / std
    
    #Reshape scale and bias parameters
    pool_shape = [-1, 1, 1, y.shape[-1]]
    scale = K.reshape(x[1], pool_shape)
    bias = K.reshape(x[2], pool_shape)#Multiply by x[1] (GAMMA) and add x[2] (BETA)
    return y * scale + bias

    

def g_block(input_tensor, latent_vector, filters):
    gamma = Dense(filters, bias_initializer = 'ones')(latent_vector)
    beta = Dense(filters)(latent_vector)
    
    out = UpSampling2D()(input_tensor)
    out = Conv2D(filters, 3, padding = 'same')(out)
    out = Lambda(AdaIN)([out, gamma, beta])
    out = Activation('relu')(out)
    
    return out

请看上面的代码。我目前正在学习 styleGAN。我正在尝试将此代码转换为 pytorch,但我似乎无法理解 Lambda 在 g_block 中的作用。AdaIN 只需要一个基于其声明的输入,但有些如何 gamma 和 beta 也用作输入?请告诉我 Lambda 在这段代码中做了什么。

非常感谢。

4

1 回答 1

0

Lambdakeras用于调用模型内部的自定义函数。在g_block Lambda调用函数并作为参数AdaIN传递到列表中。out, gamma, beta并且AdaIN函数接收这 3 个封装在单个列表中的张量作为x. 并且这些张量也可以AdaIN通过索引列表x(x[0], x[1], x[2]) 在函数内部访问。

这是pytorch等效的:

import torch
import torch.nn as nn
import torch.nn.functional as F

class AdaIN(nn.Module):
    def forward(self, out, gamma, beta):
        bs, ch = out.size()[:2]
        mean   = out.reshape(bs, ch, -1).mean(dim=2).reshape(bs, ch, 1, 1)
        std    = out.reshape(bs, ch, -1).std(dim=2).reshape(bs, ch, 1, 1) + 1e-7
        y      = (out - mean) / std
        bias   = beta.unsqueeze(-1).unsqueeze(-1).expand_as(out)
        scale  = gamma.unsqueeze(-1).unsqueeze(-1).expand_as(out)
        return y * scale + bias

           

class g_block(nn.Module):
    def __init__(self, filters, latent_vector_shape, input_tensor_channels):
        super().__init__()
        self.gamma = nn.Linear(in_features = latent_vector_shape, out_features = filters)
        # Initializes all bias to 1
        self.gamma.bias.data = torch.ones(filters)
        self.beta  = nn.Linear(in_features = latent_vector_shape, out_features = filters)
        # calculate appropriate padding 
        self.conv  = nn.Conv2d(input_tensor_channels, filters, 3, 1, padding=1)# calc padding
        self.adain = AdaIN()

    def forward(self, input_tensor, latent_vector):
        gamma = self.gamma(latent_vector)
        beta  = self.beta(latent_vector)
        # check default interpolation mode in keras and replace mode below if different
        out   = F.interpolate(input_tensor, scale_factor=2, mode='nearest') 
        out   = self.conv(out)
        out   = self.adain(out, gamma, beta)
        out   = torch.relu(out)        
        return out

# Sample:
input_tensor  = torch.randn((1, 3, 10, 10))
latent_vector = torch.randn((1, 5))
g   = g_block(3, latent_vector.shape[1], input_tensor.shape[1])
out = g(input_tensor, latent_vector)
print(out)

注意:您需要在创建时传递latent_vectorinput_tensor形状g_block

于 2020-08-18T14:34:06.973 回答