如何在任何深度建模框架中实现均值和方差值作为输入的高斯渲染器(需要向后传播)

问题描述 投票:1回答:1

想象一个典型的自动编码器-解码器模型。但是,我需要使用结构化/自定义的解码器,而不是使用一般的解码器将去卷积和放大比例用于创建/合成类似于模型输入的张量。在这里,我需要解码器接受其输入,例如一个10 * 2张量,其中每一行代表x,y位置或坐标,并渲染固定的预定义尺寸图像,其中在输入指定的位置生成10个高斯分布。换句话说,我需要创建一个空的固定大小的张量,将10个坐标指定的位置填充为值1,然后在整个张量上扫描高斯核。例如,想象下面的一维场景。令整个模型的输入为大小为10的向量。如果解码器的输入为[3,7],这是两个x坐标(0索引)和我们想要的大小为3的高斯核use为[0.28,0.44,0.28],则解码器的输出应如下所示(应与模型的原始输入大小相同,为10):[0,0,0.28,0.44,0.28,0,0.28,0.44,0.28,0]与[0,0,0,1,0,0,0,1,0,0] * [0.28,0.44,0.28]相同,其中*表示卷积算符。请注意,在第一个向量中,考虑到0索引格式,其1或位于位置3和7处。

最后,将计算出典型的像素损失,例如MSE。重要的是,此渲染模块需要能够将错误从损失向后传播到其坐标输入。]

此模块本身没有任何可训练的参数。另外,我不想更改此渲染模块之前的图层,它们需要保持原样。在更高级的设置中,我还想提供4个协方差值作为输入,即,渲染器的输入将采用[num_points,5]的形式,其中每一行都是[x_coord,y_coord,cov(x, x),cov(x,y),cov(y,y)]。

我如何在任何可用的深度学习框架中实现这样的模块?暗示类似的东西也将非常有用。

tensorflow keras deep-learning pytorch autoencoder
1个回答
0
投票

根据我的经验,神经网络中的守时性将表现不佳,因为它会减少远距离像素的影响。

因此,最好将实际的高斯函数应用于所有像素,而不是使用高斯核。

因此,采用2D高斯分布函数:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS80N3hoci5wbmcifQ==” alt =“ 2d高斯” >>>

我们可以这样使用它:

“

这意味着自定义功能中的一些步骤:

import keras.backend as K

def coords_to_gaussian(x): #where x is shape (batch, 10, 2), and 2 = x, y

    #pixel coordinates - must match the values of x and y
    #here I suppose from 0 to image size, but you may want it normalized, maybe
    x_pixels = K.reshape(K.arange(image_size), (1,1,image_size,1))
    x_pixels = K.concatenate([x_pixels]*image_size, axis=-1) #shape(1,1,size,size)

    y_pixels = K.permute_dimensions(x_pixels, (0,1,3,2))

    pixels = K.stack([x_pixels, y_pixels], axis=-1) #shape(1,1,size,size,2)


    #adjusting the AE locations to a compatible shape:
    locations = K.reshape(x, (-1, 10, 1, 1, 2))


    #calculating the upper part of the equation
    result = K.square(pixels - locations) #shape (batch, 10, size, size, 2)
    result = - K.sum(result, axis=-1) / (2*square_sigma) #shape (batch, 10, size, size)

    #calculating the E:
    result = K.exp(result) / (2 * pi * square_sigma)

    #sum the 10 channels (principle of superposition)
    result = K.sum(result, axis=1) #shape (batch, size, size)

    #add a channel for future convolutions
    result = K.expand_dims(result, axis=-1) #shape (batch, size, size, 1)

    return result

Lambda层中使用:

from keras.layers import Lambda
Lambda(coords_to_gaussian)(coordinates_tensor_from_encoder)
© www.soinside.com 2019 - 2024. All rights reserved.