keras 层中缺少特定选项 class

Specific options missing in keras layer class

我想在计算机视觉任务的深度学习架构中对两个 keras conv2d 层 (Ix,Iy) 的结果执行操作。操作如下所示:

G = np.hypot(Ix, Iy)
G = G / G.max() * 255
theta = np.arctan2(Iy, Ix)

我花了一些时间寻找 keras 提供的操作,但到目前为止没有成功。在其他一些功能中,有一个“添加”功能,允许用户添加两个 conv2d 层的结果 (tf.keras.layers.Add(Ix,Iy))。但是,我想要一个毕达哥拉斯加法(第一行),然后是一个 arctan2 操作(第三行)。

理想情况下,如果已经由 keras 实现,它将如下所示:

tf.keras.layers.Hypot(Ix,Iy)   
tf.keras.layers.Arctan2(Ix,Iy)

有谁知道是否可以在我的深度学习架构中实现这些功能?是否可以编写满足我需要的自定义图层?

您可以为您的用例使用简单的 Lambda 层,尽管它们不是绝对必要的:

import tensorflow as tf

inputs = tf.keras.layers.Input((16, 16, 1))
x = tf.keras.layers.Conv2D(32, (3, 3), padding='same')(inputs)
y = tf.keras.layers.Conv2D(32, (2, 2), padding='same')(inputs)
hypot = tf.keras.layers.Lambda(lambda z: tf.math.sqrt(tf.math.square(z[0]) + tf.math.square(z[1])))([x, y])
hypot = tf.keras.layers.Lambda(lambda z: z / tf.reduce_max(z) * 255)(hypot)
atan2 = tf.keras.layers.Lambda(lambda z: tf.math.atan2(z[0], z[1]))([x, y])

model = tf.keras.Model(inputs, [hypot, atan2])
print(model.summary())

model.compile(optimizer='adam', loss='mse')

model.fit(tf.random.normal((64, 16, 16, 1)), [tf.random.normal((64, 16, 16, 32)), tf.random.normal((64, 16, 16, 32))])
Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_3 (InputLayer)           [(None, 16, 16, 1)]  0           []                               
                                                                                                  
 conv2d_2 (Conv2D)              (None, 16, 16, 32)   320         ['input_3[0][0]']                
                                                                                                  
 conv2d_3 (Conv2D)              (None, 16, 16, 32)   160         ['input_3[0][0]']                
                                                                                                  
 lambda_2 (Lambda)              (None, 16, 16, 32)   0           ['conv2d_2[0][0]',               
                                                                  'conv2d_3[0][0]']               
                                                                                                  
 lambda_3 (Lambda)              (None, 16, 16, 32)   0           ['lambda_2[0][0]']               
                                                                                                  
 lambda_4 (Lambda)              (None, 16, 16, 32)   0           ['conv2d_2[0][0]',               
                                                                  'conv2d_3[0][0]']               
                                                                                                  
==================================================================================================
Total params: 480
Trainable params: 480
Non-trainable params: 0
__________________________________________________________________________________________________
None
2/2 [==============================] - 1s 71ms/step - loss: 3006.0469 - lambda_3_loss: 3001.7981 - lambda_4_loss: 4.2489
<keras.callbacks.History at 0x7ffa93dc2890>