当前位置:网站首页>paddlepaddle implements CS_CE Loss and incorporates PaddleClas
paddlepaddle implements CS_CE Loss and incorporates PaddleClas
2022-08-11 04:52:00 【shier_smile】
文章目录
1、 环境
1.1 paddlepaddle版本
paddlepaddle-gpu 2.2.2
1.2 PaddleClas版本
* release/2.4
2、 CS_CE Loss
CS_CE Loss在CElossAdd the number to the category of weight coefficient,Increase model for less sample size categories of regression ability
计算方式:
L o s s ( z , c ) = − ( N m i n N c ) γ ∗ C r o s s E n t r o p y ( z , c ) Loss(z, c) = - (\frac{N_{min}}{N_c})^\gamma * CrossEntropy(z, c) Loss(z,c)=−(NcNmin)γ∗CrossEntropy(z,c)
其中:
- γ \gamma γSuper parameter to control the weight
- N m i n N_{min} NminFor samples for a minimum of the category of the sample amount
- N c N_c Nc为cType sample quantity
3、paddle代码
import paddle.nn as nn
import paddle
import numpy as np
import paddle.nn.functional as F
class CostSensitiveCE(nn.Layer):
r""" Equation: Loss(z, c) = - (\frac{N_min}{N_c})^\gamma * CrossEntropy(z, c), where gamma is a hyper-parameter to control the weights, N_min is the number of images in the smallest class, and N_c is the number of images in the class c. The representative re-weighting methods, which assigns class-dependent weights to the loss function Args: gamma (float or double): to control the loss weights: (N_min/N_i)^gamma """
def __init__(self, num_class_list, gamma):
super(CostSensitiveCE, self).__init__()
self.num_class_list = num_class_list
self.csce_weight = paddle.to_tensor(np.array([(min(self.num_class_list) / N)**gamma for N in self.num_class_list], dtype=np.float32))
def forward(self, x, label):
if isinstance(x, dict):
x = x["logits"]
if label.shape[-1] == x.shape[-1]:
label = F.softmax(label, axis=-1)
soft_label = True
else:
soft_label = False
cs_ce_loss = F.cross_entropy(x, label=label, soft_label=soft_label, weight=self.csce_weight)
cs_ce_loss = cs_ce_loss.mean()
return {
"CS_CELoss":cs_ce_loss}
4、并入PaddleClas
4.1 loss代码
在PaddeClas/ppcls/loss目录下添加文件cs_celoss.py文件,并在PaddeClas/ppcls/loss/__init__.py中添加
from .cs_celoss import CostSensitiveCE
4.2 config 文件
Loss:
Train:
- CostSensitiveCE:
gamma: 1
num_class_list: [4400, 1520, 560, 1680] # Each category sample size
weight: 1 # 一个Classfier默认为1
Eval:
- CostSensitiveCE:
gamma: 1
num_class_list: [4400, 1520, 560, 1680] # Each category sample size
weight: 1
5、参考文献
- 论文:Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
- 官方代码:https://github.com/zhangyongshun/BagofTricks-LT
This post was written:2022年8月6号, 未经本人允许,禁止转载.
边栏推荐
- 关于pom.xml文件
- 交换机和路由器技术-21-RIP路由协议
- 论文笔记:Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks
- ERROR: Could not install packages due to an OSError: [Errno 2] 没有那个文件或目录: ‘/data/xxxx
- ALSA音频架构 -- snd_pcm_open函数分析
- The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
- 如何阅读论文
- findViewById返回null的问题
- Australia cyberspace security system construction
- 洛谷P2580 于是他错误的点名开始了
猜你喜欢
随机推荐
利用Navicat Premium导出数据库表结构信息至Excel
绿盾加密如何顺利切换成IP-Guard加密
洛谷P4560 Wall 砖墙
剑指offer_抽象建模能力
源代码加密技术浅析
Redis: Solve the problem of modifying the same key with distributed high concurrency
async(异步)和await的使用
2022新员工公司级安全教育基础培训(118页)
svg-icon的使用方法(svg-sprite-loader插件)
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
Research on a Consensus Mechanism-Based Anti-Runaway Scheme for Digital Trunking Terminals
快速使用UE4制作”大场景游戏“
Do you understand how the Selenium automated testing framework works?
send_sig: kernel execution flow
[QNX Hypervisor 2.2用户手册]10.15 vdev timer8254
Application of Identification Cryptography in IMS Network
Selenium自动化测试框架工作原理你明白了吗?
map and set - natural search and lookup semantics
How to add icons to web pages?
直播平台开发,Flutter,Drawer侧滑









