多标签图像分类模型仅对包含超过 1 个标签的图像中的 1 个标签给出高概率预测

问题描述 投票:0回答:0
from efficientnet_v2 import EfficientNetV2B0
def build_model(num_classes=108):
    base_model = EfficientNetV2B0(
        input_shape=(224, 224, 3),
        include_top=False,
        pooling="avg",
        weights="imagenet-21k-ft1k"
    )

base_model.trainable=False

return tf.keras.Sequential([
    base_model,
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(num_classes, activation='sigmoid')
])

model = build_model()
model.summary()

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=        
['accuracy'])

model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), 
batch_size=32)

img = image.load_img('apple-banana.jpg',target_size=(224,224,3))
img = image.img_to_array(img)
img = img/255

classes = np.array(train.columns[1:])
proba = model.predict(img.reshape(1,224,224,3))
top_3 = np.argsort(proba[0])[:-6:-1]
for i in range(5):
    print("{}".format(classes[top_3[i]])+" ({:.5})".format(proba[0][top_3[I]]))
plt.imshow(img)

如何让我的模型分配一个平衡的概率预测? https://i.stack.imgur.com/0hWeK.png(苹果预测)

我期望发生的是对成分的概率预测是平衡的。从上图中它正确地预测了标签。而下图中有 2 种成分,但只有 1 种成分得到高概率预测。我认为问题与对象定位有关,它可能只识别香蕉而忽略苹果。

https://i.stack.imgur.com/22SNN.png(苹果香蕉预测)

我该如何解决这个问题? 有没有办法从 0-1 独立预测每个标签并应用 0.5 的阈值。例如:

香蕉 (0.9)

苹果 (0.8)

鳄梨 (0.1)

李子 (0.3)

所以不像我的预测输出,所有预测的总和不一定需要等于1.

python artificial-intelligence object-detection multilabel-classification
© www.soinside.com 2019 - 2024. All rights reserved.