Keras multi-label image classification with F1-score

2024/5/19 23:36:33

I am working on a multi-label image classification problem with the evaluation being conducted in terms of F1-score between system predicted and ground truth labels.

Given that, should I use loss="binary_crossentropy" or loss=keras_metrics.f1_score() where keras_metrics.f1_score() is taken from here: https://pypi.org/project/keras-metrics/? I am a bit confused because all of the tutorials I have found on the Internet regarding multi-label classification are based on the binary_crossentropy loss function, but here I have to optimize against F1-score.

Furthermore, should I set metrics=["accuracy"] or maybe metrics=[keras_metrics.f1_score()] or I should left this completely empty?

Answer

Based on user706838 answer ...

use the f1_score in https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric

import tensorflow as tf
import keras.backend as Kdef f1_loss(y_true, y_pred):tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)p = tp / (tp + fp + K.epsilon())r = tp / (tp + fn + K.epsilon())f1 = 2*p*r / (p+r+K.epsilon())f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)return 1 - K.mean(f1)
https://en.xdnf.cn/q/73113.html

Related Q&A

Thread safe locale techniques

Were currently writing a web application based on a threaded python web server framework (cherrypy) and would like to simultaneously support users from multiple locales.The locale module doesnt appear …

How to draw image from raw bytes using ReportLab?

All the examples I encounter in the internet is loading the image from url (either locally or in the web). What I want is to draw the image directly to the pdf from raw bytes.UPDATE:@georgexsh Here is …

How to speed up numpy code

I have the following code. In principle it takes 2^6 * 1000 = 64000 iterations which is quite a small number. However it takes 9s on my computer and I would like to run it for n = 15 at least.from __f…

MyPy gives error Missing return statement even when all cases are tested

I am getting a MyPy error "Missing return statement", even when I check for all possible cases inside a function.For example, in the following code, MyPy is still giving me an error "9: …

Python Json with returns AttributeError: __enter__

Why does this return AttributeError: __enter__Sorting method is just a string created based on how the list is sorted, and current time uses stfttimecurrent_time = strftime("%Y-%m-%d %H-%M-%S"…

Workflow for adding new columns from Pandas to SQLite tables

SetupTwo tables: schools and students. The index (or keys) in SQLite will be id and time for the students table and school and time for the schools table. My dataset is about something different, but I…

What is the return type of the find_all method in Beautiful Soup?

from bs4 import BeautifulSoup, SoupStrainer from urllib.request import urlopen import pandas as pd import numpy as np import re import csv import ssl import json from googlesearch import search from…

All addresses to go to a single page (catch-all route to a single view) in Python Pyramid

I am trying to alter the Pyramid hello world example so that any request to the Pyramid server serves the same page. i.e. all routes point to the same view. This is what iv got so far: from wsgiref.sim…

Python singleton / object instantiation

Im learning Python and ive been trying to implement a Singleton-type class as a test. The code i have is as follows:_Singleton__instance = Noneclass Singleton:def __init__(self):global __instanceif __i…

Single-Byte XOR Cipher (python)

This is for a modern cryptography class that I am currently taking.The challenge is the cryptopals challenge 3: Single-Byte XOR Cipher, and I am trying to use python 3 to help complete this.I know that…