【发布时间】:2017-12-05 04:04:57
【问题描述】:
我正在 Keras 中构建一个卷积网络,将多个类分配给一个图像。鉴于图像具有9 兴趣点,可以按照我想添加27 输出神经元的三种方式之一进行分类,softmax 激活将计算每个连续三组神经元的概率。
有可能做到吗?我知道我可以简单地添加一个大的 softmax 层,但这会导致所有输出神经元的概率分布对于我的应用来说太宽泛了。
【问题讨论】:
我正在 Keras 中构建一个卷积网络,将多个类分配给一个图像。鉴于图像具有9 兴趣点,可以按照我想添加27 输出神经元的三种方式之一进行分类,softmax 激活将计算每个连续三组神经元的概率。
有可能做到吗?我知道我可以简单地添加一个大的 softmax 层,但这会导致所有输出神经元的概率分布对于我的应用来说太宽泛了。
【问题讨论】:
在最简单的实现中,您可以重塑您的数据,您将得到您所描述的内容:“每个连续三元组的概率”。
您将输出包含 27 个类,形状类似于 (batch_size,27) 并对其进行整形:
model.add(Reshape((9,3)))
model.add(Activation('softmax'))
也要注意重塑您的 y_true 数据。或者在模型中再添加一个 reshape 来恢复原来的形式:
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)
【讨论】: