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

罗德(Rhodel Quizon)
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在此代码中的作用。

非常感谢你。

吉里斯·达塔特拉·黑德

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

lambda在此python代码中做什么?

来自分类Dev

连接层在 Keras 多任务中做什么?

来自分类Dev

Keras的CategoricalCrossEntropy到底在做什么?

来自分类Dev

在此SendInput代码段中我在做什么错?

来自分类Dev

运算符“:\”在此scala代码中做什么?

来自分类Dev

self [identifier] = some_value在此代码中做什么?

来自分类Dev

编译器在此汇编代码中做什么?

来自分类Dev

Data.Semigroup((<>))在此Haskell排序代码中做什么?

来自分类Dev

在此C ++代码中我在做什么错?

来自分类Dev

this.props.txt在此代码段中做什么?

来自分类Dev

完整的 Keras Lambda 混淆

来自分类Dev

为什么在此处(在Keras中)出现重复的节点名称?

来自分类Dev

array [:]在此回溯算法或Python中到底能做什么?

来自分类Dev

tf.keras.layers.Dense到底做什么?

来自分类Dev

keras ConvLSTM2D层做什么?

来自分类Dev

在Keras中绘制模型

来自分类Dev

keras中的增量学习

来自分类Dev

Keras中的串联层

来自分类Dev

keras中的断言错误

来自分类Dev

在 keras 中输入 LSTM

来自分类Dev

Keras 中的图卷积

来自分类Dev

keras 中的训练问题

来自分类Dev

Python 中的 Keras:LSTM 维度

来自分类Dev

在 keras 中运行分类代码时出错

来自分类Dev

在此lambda代码中,[value](x)有什么作用?

来自分类Dev

为什么 lambda 在此代码片段中更好?

来自分类Dev

Fedora在此TCPdump中做什么?

来自分类Dev

Fedora在此TCPdump中做什么?

来自分类Dev

Keras 与 TensorFlow - Keras 有什么实际好处吗?