python - 移除 Keras CNN 中的垃圾箱
问题描述
我有以下问题,我想从我的 Keras 模型中的一个层的输出中删除一个“垃圾箱”。
没有删除垃圾箱的代码如下所示并且有效:
def create_detector_network():
input = Input(shape=(128, 128, 512))
x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(input)
x = BatchNormalization()(x)
x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
x = BatchNormalization()(x)
x = Activation('softmax')(x)
x = keras.layers.UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)
return Model(input, x)
但是,如果我将删除添加到网络:
def create_detector_network():
input = Input(shape=(128, 128, 512))
x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(input)
x = BatchNormalization()(x)
x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
x = BatchNormalization()(x)
x = Activation('softmax')(x)
x = Lambda(lambda x: x[:, :, :-1], output_shape= (128, 128, 64))(x) #x[:, :, :-1] <------
x = keras.layers.UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)
return Model(input, x)
我得到以下 model.summary() 输出,其中 lambda 层之后的维度再次增加到 65:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_38 (InputLayer) (None, 128, 128, 512) 0
_________________________________________________________________
detect_1 (Conv2D) (None, 128, 128, 128) 589952
_________________________________________________________________
batch_normalization_37 (Batc (None, 128, 128, 128) 512
_________________________________________________________________
detect_2 (Conv2D) (None, 128, 128, 65) 8385
_________________________________________________________________
batch_normalization_38 (Batc (None, 128, 128, 65) 260
_________________________________________________________________
activation_10 (Activation) (None, 128, 128, 65) 0
_________________________________________________________________
lambda_6 (Lambda) (None, 128, 128, 64) 0
_________________________________________________________________
up_sampling2d_18 (UpSampling (None, 1024, 1016, 65) 0
_________________________________________________________________
reduce_dim (Conv2D) (None, 1024, 1016, 1) 66
=================================================================
任何人都可以解释为什么会发生这种情况以及如何解决它?
解决方案
在我的机器上正常工作(TF 2.2)。我修改了 lambda 以处理批量维度
def create_detector_network():
inp = Input(shape=(128, 128, 512))
x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(inp)
x = BatchNormalization()(x)
x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
x = BatchNormalization()(x)
x = Activation('softmax')(x)
x = Lambda(lambda x: x[:,:,:,:-1])(x)
x = UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)
return Model(inp, x)
这是总结
_________________________________________________________________
Layer (type) Output Shape Param
=================================================================
input_33 (InputLayer) [(None, 128, 128, 512)] 0
_________________________________________________________________
detect_1 (Conv2D) (None, 128, 128, 128) 589952
_________________________________________________________________
batch_normalization_14 (Batc (None, 128, 128, 128) 512
_________________________________________________________________
detect_2 (Conv2D) (None, 128, 128, 65) 8385
_________________________________________________________________
batch_normalization_15 (Batc (None, 128, 128, 65) 260
_________________________________________________________________
activation_7 (Activation) (None, 128, 128, 65) 0
_________________________________________________________________
lambda_7 (Lambda) (None, 128, 128, 64) 0
_________________________________________________________________
up_sampling2d_7 (UpSampling2 (None, 1024, 1024, 64) 0
_________________________________________________________________
reduce_dim (Conv2D) (None, 1024, 1024, 1) 65
=================================================================
推荐阅读
- javascript - 路由到具有不同参数的同一页面时,NextJS 初始状态未更新
- c# - 如何获得异步调用的结果?
- reactjs - 使用 react-multi-carousel 但无法使用自定义点
- google-apps-script - Script.newTrigger() 返回错误“在对象上创建方法或属性时出现意外错误”
- python - 文件读/写脚本不会在其他 python 软件上运行
- r - 变量 X 和测试“fisher.test”的 add_p()' 中的错误,省略了 p 值
- scala - Scala - 迭代包含 List() 的 Any 变量
- r - 如何平滑ggplot中阴影区域的边缘?
- python - 使用另一个数据框在python中设置数据子集
- jenkins - Jenkins 的“管道:构建步骤”插件的奇怪行为