我在Keras中构建一个卷积网络,它为图像分配多个类。考虑到图像具有9兴趣点,可以通过以下三种方式之一来分类,我想添加27输出神经元,使其具有softmax激活,从而计算每一个连续的三重神经元的概率。
有可能这样做吗?我知道我可以简单地添加一个大的softmax层,但是这会导致在所有输出神经元上的概率分布,这对我的应用来说太宽了。
发布于 2017-12-05 11:42:34
在最简单的实现中,您可以重塑数据,并得到您所描述的“每个连续三胞胎的概率”。
您可以使用27个类(形状类似于(batch_size,27) )来获取输出,并对其进行整形:
model.add(Reshape((9,3)))
model.add(Activation('softmax'))还要注意重塑您的y_true数据。或者在模型中添加另一个重塑以恢复原始表单:
model.add(Reshape((27,))在更详细的解决方案中,您可能会根据9个实例点的位置(如果它们具有大致静态的位置)并创建并行路径。例如,假设您的9个位置是间隔均匀的矩形,并且您希望对这些段使用相同的网络和类:
inputImage = Input((height,width,channels))
#supposing the width and height are multiples of 3, for easiness in this example
recHeight = height//3
recWidth = width//3
#create layers here without calling them
someConv1 = Conv2D(...)
someConv2 = Conv2D(...)
flatten = Flatten()
classificator = Dense(..., activation='softmax')
outputs = []
for i in range(3):
for j in range(3):
fromH = i*recHeight
toH = fromH + recHeight
fromW = j*recWidth
toW = fromW + recWidth
imagePart = Lambda(
lambda x: x[:,fromH:toH, fromW:toW,:],
output_shape=(recHeight,recWidth,channels)
)(inputImage)
#using the same net and classes for all segments
#if this is not true, create new layers here instead of using the same
output = someConv1(imagePart)
output = someConv2(output)
output = flatten(output)
output = classificator(output)
outputs.append(output)
outputs = Concatenate()(outputs)
model = Model(inputImage,outputs)https://stackoverflow.com/questions/47645910
复制相似问题