python - Keras 2.2.4 如何从 Keras 1.x.x 复制 merge()

标签 python tensorflow merge keras deprecated

我正在尝试使用 TensorFlow 后端将 Keras 1.x.x 代码转换为 2.2.x。

我在 Keras 1.x.x 中有以下内容,它接受以下输入:

  • org_image 3 个 RGB 颜色 channel 上的 256x256 图像 shape=(256,256,3)
  • mask 1 个黑白颜色 channel 上的 256x256 mask shape=(256,256,1)

我希望将图像与 mask 结合起来,以获得 mask 区域缺失的新裁剪图像。为此,我首先使用 1 - maskmask 取反,其中 1 是一个张量。然后我按元素乘以 org_image * (1 - mask) 以获得新裁剪的图像。 Keras 1.x.x 中的代码如下所示

from keras.layers import Input, merge

input_shape = (256,256,3)

org_img = Input(shape=input_shape)
mask = Input(shape=(input_shape[0], input_shape[1], 1))
input_img = merge([org_img, mask],
                   mode=lambda x: x[0] * (1 - x[1]),
                   output_shape=input_shape)

在 Keras 2.2.x 中,引入了一项重大更改,将 merge() 函数替换为 Add()Subtract()Multiply() ...等等。前面的 merge() 具有 mode=lambda x: x[0] * (1 - x[1]) 的便利性,它等于 mode= lambda [org_img, mask]: org_img * (1 - mask).

如何在 Keras 2.2.x 中复制 1 - mask?我需要在 tf.backend.ones 中导入吗?

或者我可能需要 tf.enable_eager_execution()

我对此很陌生,所以我知道很多事情都在我头上。如果有人能澄清我的误解在哪里,我将不胜感激,谢谢!

最佳答案

为自定义函数或 lambda 表达式使用 Lambda 层:

input_img = Lambda(lambda x: x[0] * (1 - x[1]), output_shape=input_shape)([org_img, mask])

如果您使用 tensorflow 作为后端,则 output_shape 是可选的。

其他有用的图层:

  • 连接(轴=...)(list_of_inputs)
  • Add()(list_of_inputs)
  • Multiply()(list_of_inputs)

关于python - Keras 2.2.4 如何从 Keras 1.x.x 复制 merge(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54624043/

相关文章:

python - Python 中两个线程之间交换数据

version-control - 使用 Mercurial 以三路 merge 工作保存什么文件?

python - Pandas:使用一个公共(public)列合并多个 DataFrame

python - 32 位 Windows 10 上的 Tensorflow : is not a supported wheel on this platform; package missing in current win-32 channels, 没有匹配的发行版

python-3.x - 为 tf.nn.embedding_lookup 预处理不同文本大小时 Pre-Padding 和 Post-Padding 文本的差异

git rebase 在之前的 git merge 之后

python - 使用 .save() 方法和 matplotlib.animation.ArtistAnimation() 保存 .gif IndexError

python - webapp2:路由中的正则表达式

python - 如何在文档中找到最大的空白(白色)正方形区域并返回其坐标和面积?

tensorflow - tf.losses.mean_squared_error 目标为负