多标签图像分类

Multi-Label Image Classification

我自己试过,但无法到达终点,所以在这里发帖,请指导我。

I have attached an image file, that will give a clear picture of my problem.

因为如果我们遵循这些

  1. https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-to-classify-satellite-photos-of-the-amazon-rainforest/
  2. https://towardsdatascience.com/journey-to-the-center-of-multi-label-classification-384c40229bff
  3. https://www.analyticsvidhya.com/blog/2019/04/predicting-movie-genres-nlp-multi-label-classification/

等 他们对每个图像都有多个标签,但就我而言,我有多个标签加上它们的属性。

如果您的目标是预测“L”、“M”和“H[=35” =]',您使用的损失函数不正确。你应该使用 binary_crossentropy。在这种情况下,目标的形状将为 batch × 3。

  • categorical_crossentropy 假设输出是一个分类分布:一个总和为 1 的值向量。也就是说,你有多种可能,但只有其中一种可能是正确的。

  • binary_crossentropy 假设输出向量中的每个数字都是(条件)独立的二进制分布,因此每个数字都在 0 和 1 之间,但它们不一定总和为 1 ,因为很可能所有这些都是真的。

如果您的目标是预测每个标签 1、...、标签 6 的值,那么您应该为每个标签建模分类分布。您有六个标签,每个标签都有 3 个值,因此您需要 18 个数字(logits)。在这种情况下,目标的形状将为 batch × 6 × 3。

model.add(Dense(18, activation='none'))

因为您不希望单个分布超过 18 个值,而是超过 6 × 3 个值,所以您需要先对 logits 进行整形:

model.add(Reshape((6, 3))
model.add(Softmax())

基于以上讨论。这是上述问题的解决方案。 正如我提到的,我们总共有 5 个标签,每个标签还有另外三个标签,如 (L, M, H) 我们可以用这种方式进行编码

# create a one hot encoding for one list of tags
def custom_encode(tags, mapping):
    # create empty vector
    encoding=[]
    for tag in tags:
        if tag == 'L':
            encoding.append([1,0,0])
        elif tag == 'M':
            encoding.append([0,1,0])
        else:
            encoding.append([0,0,1])
    return encoding

所以编码后的 y 向量看起来像

**Labels     Tags             Encoded Tags** 
Label1 ----> [L,L,L,M,H] ---> [ [1,0,0], [1,0,0], [1,0,0], [0,1,0], [0,0,1] ]
Label2 ----> [L,H,L,M,H] ---> [ [1,0,0], [0,0,1], [1,0,0], [0,1,0], [0,0,1] ]
Label3 ----> [L,M,L,M,H] ---> [ [1,0,0], [0,1,0], [1,0,0], [0,1,0], [0,0,1] ]
Label4 ----> [M,M,L,M,H] ---> [ [0,1,0], [0,1,0], [1,0,0], [0,1,0], [0,0,1] ]
Label5 ----> [M,L,L,M,H] ---> [ [0,1,0], [1,0,0], [1,0,0], [0,1,0], [0,0,1] ]


最后一层会像

 model.add(Dense(15)) #because we have total 5 labels and each has 3 tags so 15 neurons will be on final layer
 model.add(Reshape((5,3))) # each 5 have further 3 tags we need to reshape it
 model.add(Activation('softmax'))