keras 图层类中缺少特定选项

发布于 2025-01-14 05:24:48 字数 496 浏览 4 评论 0原文

我想在计算机视觉任务的深度学习架构中对两个 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)

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

I would like to implement operations on the results of two keras conv2d layers (Ix,Iy) in a deep learning architecture for a computer vision task. The operation looks as follows:

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

I've spent some time looking for operations provided by keras but did not have success so far. Among a few others, there's a "add" functionality that allows the user to add the results of two conv2d layers (tf.keras.layers.Add(Ix,Iy)). However, I would like to have a Pythagorean addition (first line) followed by a arctan2 operation (third line).

So ideally, if already implemented by keras it would look as follows:

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

Does anyone know if it is possible to implement those functionalities within my deep learning architecture? Is it possible to write custom layers that meet my needs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2025-01-21 05:24:48

您可以为您的用例使用简单的 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>

You could probably use simple Lambda layers for your use case, although they are not absolutely necessary:

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文