Lambda 在此代码 (python keras) 中做了什么?

WHat does Lambda do in this code (python keras)?

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 只需要一个基于其声明的输入,但伽玛和贝塔如何也用作输入?请告诉我 Lambda 在这段代码中做了什么。

非常感谢。

Lambda keras 层用于调用模型内部的自定义函数。在 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)

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