Keras 对神经元子集应用softmax

Keras 对神经元子集应用softmax,keras,softmax,Keras,Softmax,我正在Keras中构建一个卷积网络,它为一个图像分配多个类。考虑到图像中有9个兴趣点,这些兴趣点可以通过我想要添加27的三种方式之一进行分类,并通过softmax激活输出神经元,从而计算出每三个连续神经元的概率 有可能吗?我知道我可以简单地添加一个大的softmax层,但这将导致所有输出神经元的概率分布,这对于我的应用来说太宽了 在最简单的实现中,您可以重塑数据,您将得到您所描述的:“每个连续三元组的概率” 您使用27个类(形状类似于(batch\u size,27))获取输出并对其进行重塑:

我正在Keras中构建一个卷积网络,它为一个图像分配多个类。考虑到图像中有
9
个兴趣点,这些兴趣点可以通过我想要添加
27
的三种方式之一进行分类,并通过softmax激活输出神经元,从而计算出每三个连续神经元的概率


有可能吗?我知道我可以简单地添加一个大的softmax层,但这将导致所有输出神经元的概率分布,这对于我的应用来说太宽了

在最简单的实现中,您可以重塑数据,您将得到您所描述的:“每个连续三元组的概率”

您使用27个类(形状类似于
(batch\u size,27)
)获取输出并对其进行重塑:

model.add(Reshape((9,3)))
model.add(Activation('softmax'))
还要注意重塑您的
y_true
数据。或者在模型中添加另一个重塑以恢复原始形状:

model.add(Reshape((27,))

在更复杂的解决方案中,您可能会根据其位置(如果它们具有大致的静态位置)分离insterest的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)

在最简单的实现中,您可以重塑数据,您将得到您所描述的:“每个连续三元组的概率”

您使用27个类(形状类似于
(batch\u size,27)
)获取输出并对其进行重塑:

model.add(Reshape((9,3)))
model.add(Activation('softmax'))
还要注意重塑您的
y_true
数据。或者在模型中添加另一个重塑以恢复原始形状:

model.add(Reshape((27,))

在更复杂的解决方案中,您可能会根据其位置(如果它们具有大致的静态位置)分离insterest的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)