在keras中实现跳过连接
Implementing skip connections in keras
我正在 keras 中实现 ApesNet。它有一个具有跳过连接的 ApesBlock。如何将其添加到 keras 中的顺序模型? ApesBlock 有两个平行层,最后通过逐元素相加合并。
简单的答案是不要为此使用顺序模型,而是使用函数 API,然后实现跳过连接(也称为剩余连接)非常容易,如本示例所示functional API guide:
from keras.layers import merge, Convolution2D, Input
# input tensor for a 3-channel 256x256 image
x = Input(shape=(3, 256, 256))
# 3x3 conv with 3 output channels (same as input channels)
y = Convolution2D(3, 3, 3, border_mode='same')(x)
# this returns x + y.
z = merge([x, y], mode='sum')
如果有人仍然遇到同样的问题并且 merge
图层不起作用。
我在史努比博士编写的 Keras 文档中找不到 merge
。我收到类型错误 'module' object is not callable
.
相反,我添加了一个 Add
层。
所以与史努比博士的回答相同的例子是:
from keras.layers import Add, Convolution2D, Input
# input tensor for a 3-channel 256x256 image
x = Input(shape=(3, 256, 256))
# 3x3 conv with 3 output channels (same as input channels)
y = Convolution2D(3, 3, 3, border_mode='same')(x)
# this returns x + y.
z = Add()([x, y])
有一种使用跳过连接的简单方法。这是我一直在做的事情的一个例子:
from keras.layers import Input, concatenate
from keras.models import Model
def define_skip_model():
input_net = Input((32,32,3))
## Encoder starts
conv1 = Conv2D(32, 3, strides=(2,2), activation = 'relu', padding = 'same')(input_net)
conv2 = Conv2D(64, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv1)
conv3 = Conv2D(128, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv2)
conv4 = Conv2D(128, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv3)
## And now the decoder
up1 = Conv2D(128, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv4))
merge1 = concatenate([conv3,up1], axis = 3)
up2 = Conv2D(64, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(merge1))
merge2 = concatenate([conv2,up2], axis = 3)
up3 = Conv2D(32, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(merge2))
merge3 = concatenate([conv1,up3], axis = 3)
up4 = Conv2D(32, 3, padding = 'same')(UpSampling2D(size = (2,2))(merge3))
output_net = Conv2D(3, 3, padding = 'same')(up4)
model = Model(inputs = input_net, outputs = output_net)
return model
我正在 keras 中实现 ApesNet。它有一个具有跳过连接的 ApesBlock。如何将其添加到 keras 中的顺序模型? ApesBlock 有两个平行层,最后通过逐元素相加合并。
简单的答案是不要为此使用顺序模型,而是使用函数 API,然后实现跳过连接(也称为剩余连接)非常容易,如本示例所示functional API guide:
from keras.layers import merge, Convolution2D, Input
# input tensor for a 3-channel 256x256 image
x = Input(shape=(3, 256, 256))
# 3x3 conv with 3 output channels (same as input channels)
y = Convolution2D(3, 3, 3, border_mode='same')(x)
# this returns x + y.
z = merge([x, y], mode='sum')
如果有人仍然遇到同样的问题并且 merge
图层不起作用。
我在史努比博士编写的 Keras 文档中找不到 merge
。我收到类型错误 'module' object is not callable
.
相反,我添加了一个 Add
层。
所以与史努比博士的回答相同的例子是:
from keras.layers import Add, Convolution2D, Input
# input tensor for a 3-channel 256x256 image
x = Input(shape=(3, 256, 256))
# 3x3 conv with 3 output channels (same as input channels)
y = Convolution2D(3, 3, 3, border_mode='same')(x)
# this returns x + y.
z = Add()([x, y])
有一种使用跳过连接的简单方法。这是我一直在做的事情的一个例子:
from keras.layers import Input, concatenate
from keras.models import Model
def define_skip_model():
input_net = Input((32,32,3))
## Encoder starts
conv1 = Conv2D(32, 3, strides=(2,2), activation = 'relu', padding = 'same')(input_net)
conv2 = Conv2D(64, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv1)
conv3 = Conv2D(128, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv2)
conv4 = Conv2D(128, 3, strides=(2,2), activation = 'relu', padding = 'same')(conv3)
## And now the decoder
up1 = Conv2D(128, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(conv4))
merge1 = concatenate([conv3,up1], axis = 3)
up2 = Conv2D(64, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(merge1))
merge2 = concatenate([conv2,up2], axis = 3)
up3 = Conv2D(32, 3, activation = 'relu', padding = 'same')(UpSampling2D(size = (2,2))(merge2))
merge3 = concatenate([conv1,up3], axis = 3)
up4 = Conv2D(32, 3, padding = 'same')(UpSampling2D(size = (2,2))(merge3))
output_net = Conv2D(3, 3, padding = 'same')(up4)
model = Model(inputs = input_net, outputs = output_net)
return model