如果您的模型中有带有 shape 的图像(batch, width, height, channels)
,您可以重塑数据以隐藏其中一个空间维度并使用 1D 池化:
对于宽度:
model.add(Reshape((width, height*channels)))
model.add(MaxPooling1D())
model.add(Reshape((width/2, height, channels))) #if you had an odd number, add +1 or -1 (one of them will work)
对于高度:
#Here, the time distributed will consider that "width" is an extra time dimension,
#and will simply think of it as an extra "batch" dimension
model.add(TimeDistributed(MaxPooling1D()))
工作示例,具有两个分支的功能性 API 模型,每个分支一个用于池化:
import numpy as np
from keras.layers import *
from keras.models import *
inp = Input((30,50,4))
out1 = Reshape((30,200))(inp)
out1 = MaxPooling1D()(out1)
out1 = Reshape((15,50,4))(out1)
out2 = TimeDistributed(MaxPooling1D())(inp)
model = Model(inp,[out1,out2])
model.summary()
或者Reshape
,如果您不想打扰数字:
#swap height and width
model.add(Permute((2,1,3)))
#apply the pooling to width
model.add(TimeDistributed(MaxPooling1D()))
#bring height and width to the correct order
model.add(Permute((2,1,3)))