惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Security Blog
Microsoft Security Blog
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
博客园 - 聂微东
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
S
Secure Thoughts
C
Check Point Blog
WordPress大学
WordPress大学
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy
IT之家
IT之家
美团技术团队
罗磊的独立博客
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
博客园 - 司徒正美
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
TaoSecurity Blog
TaoSecurity Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
Vercel News
Vercel News
有赞技术团队
有赞技术团队
J
Java Code Geeks
博客园 - 【当耐特】
Project Zero
Project Zero
NISL@THU
NISL@THU
P
Privacy & Cybersecurity Law Blog
The Last Watchdog
The Last Watchdog
aimingoo的专栏
aimingoo的专栏
S
Securelist
The Cloudflare Blog

博客园 - 鹅要长大

【linux基础知识】理解nohup 【linux基础】如何理解python train_dtld.py 2>&1 | tee my_error_log.txt 【工具使用】docker的使用 【基础算法】银行家舍入法 【ubuntu工具】ubuntu好用工具总结 【ubuntu基础工具使用】ubuntu中screen的安装与使用 【linux操作基础】多目录文件复制且同名不覆盖 【linux基础操作】windows系统生成的txt文件在ubuntu打开乱码 【opencv编程基础】fillpoly和polylines函数的理解 【c++编程基础】std::unique的理解 【编程基础】计算三个顶点之间的夹角 【计算几何算法】道格拉斯普克(Douglas-Peuker)算法 [c++]c++ 工程代码中的debug时条件编译隐去的代码会影响程序运行的性能和耗时吗 【opencv基础】resize使用的问题 git stash的用法 gflags的使用 opencv 判断某个坐标点是否在多边形内cv::pointPolygonTest 判断多边形的顶点内外角点以及对approxPolyDP函数的理解 用Python从零实现贝叶斯分类器 【CV数据集】智慧城市之CCPD车牌数据集
【CV基础】语义分割任务计算类别权重
鹅要长大 · 2024-12-09 · via 博客园 - 鹅要长大

前言

 语义分割任务一般都存在样本类别不平衡的问题,采用类别权重来解决这个问题,本文记录类别权重的计算过程。

类别权重计算的基本思路

code

# 20240620: calculate class weights with semantic segmentation gt images.
import os
import numpy as np
import cv2 as cv

void_classes = [2, 4, 10, 12, 16, 17, 19, 21, 25, 30, 32, 33, 35]
valid_classes = [0, 1, 3, 5, 6, 7, 8, 9, 11, 13, 14, 15, 18, 20, 22, 23, 24, 26, 27, 28, 29, 31, 34]

# edgeai-torchvision
def calc_median_frequency(classes, present_num):
    """
    Class balancing by median frequency balancing method.
    Reference: https://arxiv.org/pdf/1411.4734.pdf
       'a = median_freq / freq(c) where freq(c) is the number of pixels
        of class c divided by the total number of pixels in images where
        c is present, and median_freq is the median of these frequencies.'
    """
    class_freq = classes / present_num
    median_freq = np.median(class_freq)
    return median_freq / class_freq

# edgeai-torchvision
def calc_log_frequency(classes, value=1.02):
    """Class balancing by ERFNet method.
       prob = each_sum_pixel / each_sum_pixel.max()
       a = 1 / (log(1.02 + prob)).
    """
    class_freq = classes / classes.sum()  # ERFNet is max, but ERFNet is sum
    # print(class_freq)
    # print(np.log(value + class_freq))
    return 1 / np.log(value + class_freq)

def calculate_class_weight_present(path):
    # edgeai-torchvision
    gtpath = os.path.join(path, 'gt')
    class_counts = np.zeros(len(valid_classes), dtype="f")
    class_freq = np.zeros(len(valid_classes), dtype="f")
    class_weights = np.zeros(len(valid_classes))
    present_num = np.zeros(len(valid_classes), dtype="f")
    for filename in os.listdir(gtpath):  
        # print('filename: ', filename)
        if filename.endswith('.png') or filename.endswith('.jpg'): 
            filepath = os.path.join(gtpath, filename)  
            gtimg = cv.imread(filepath, cv.IMREAD_GRAYSCALE)  
            if gtimg is not None:
                # for i in range(len(valid_classes)):
                #    class_counts[i] += np.sum(gtimg == valid_classes[i])
                for i, classid in enumerate(valid_classes): 
                    num_pixel = np.sum(gtimg == classid)
                    if num_pixel:
                        class_counts[i] += np.sum(gtimg == classid)
                        present_num[i] += 1

    for i, count in enumerate(class_counts):  
        class_freq[i] = count / present_num[i] if present_num[i] > 0 else 0
    # print('class_freq: ', class_freq)
    medval = np.median(class_freq)
    # print('medval: ', medval)
    for i, freq in enumerate(class_freq):  
        # class_weights[i] = medval / freq
        class_weights[i] = medval / freq if freq > 0 else 0
    print(class_weights)
    # for i, weight in enumerate(class_weights):  
    #     print(f"类别 {valid_classes[i]}: 权重 = {weight}")  

    # Normalization
    # # 对权重进行归一化,使它们的和为1(可选步骤,取决于你的应用)
    # class_weights = class_weights / class_weights.sum()
    # for i, weight in enumerate(class_weights):  
    #     print(f"类别 {valid_classes[i]}: 权重 = {weight}")  



def calculate_class_weight_all(path):
    gtpath = os.path.join(path, 'gt')
    class_counts = np.zeros(len(valid_classes), dtype=np.int64)
    class_weights = np.zeros(len(valid_classes))
    for filename in os.listdir(gtpath):  
        # print('filename: ', filename)
        if filename.endswith('.png') or filename.endswith('.jpg'): 
            filepath = os.path.join(gtpath, filename)  
            gtimg = cv.imread(filepath, cv.IMREAD_GRAYSCALE)  
            if gtimg is not None:
                # for i in range(len(valid_classes)):
                #    class_counts[i] += np.sum(gtimg == valid_classes[i])
                for i, classid in enumerate(valid_classes): 
                    class_counts[i] += np.sum(gtimg == classid)

    total_pixels = class_counts.sum()
    # print('class_counts: \n', class_counts)
    # print('totalpixel: ', total_pixels)
    medval = np.median(class_counts)
    # print('medval: ', medval)
    for i, count in enumerate(class_counts):  
        class_weights[i] = medval / count if count > 0 else 0
    print(class_weights)
    # for i, weight in enumerate(class_weights):  
    #     print(f"类别 {valid_classes[i]}: 权重 = {weight}")  
    # Normalization

    # # 对权重进行归一化,使它们的和为1(可选步骤,取决于你的应用)
    # class_weights = class_weights / class_weights.sum()
    # for i, weight in enumerate(class_weights):  
    #     print(f"类别 {valid_classes[i]}: 权重 = {weight}")  


if __name__ == "__main__":
    path = os.path.dirname(os.path.realpath(__file__))
    # calculate_class_weight_all(path)
    # print("\n\n\n  start present \n\n\n")
    calculate_class_weight_present(path)

View Code

参考

各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。

Simple is clean. Complex is clever.
Logic & Aesthetics
Seeking Ground Truth within the pixels.

版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/