欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch實現(xiàn)focal loss的兩種方式小結

 更新時間:2020年01月02日 09:52:18   作者:WYXHAHAHA123  
今天小編就為大家分享一篇pytorch實現(xiàn)focal loss的兩種方式小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,直接上代碼吧!

import torch
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
'''
pytorch實現(xiàn)focal loss的兩種方式(現(xiàn)在討論的是基于分割任務)
在計算損失函數(shù)的過程中考慮到類別不平衡的問題,假設加上背景類別共有6個類別
'''
def compute_class_weights(histogram):
  classWeights = np.ones(6, dtype=np.float32)
  normHist = histogram / np.sum(histogram)
  for i in range(6):
    classWeights[i] = 1 / (np.log(1.10 + normHist[i]))
  return classWeights
def focal_loss_my(input,target):
  '''
  :param input: shape [batch_size,num_classes,H,W] 僅僅經(jīng)過卷積操作后的輸出,并沒有經(jīng)過任何激活函數(shù)的作用
  :param target: shape [batch_size,H,W]
  :return:
  '''
  n, c, h, w = input.size()

  target = target.long()
  input = input.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
  target = target.contiguous().view(-1)

  number_0 = torch.sum(target == 0).item()
  number_1 = torch.sum(target == 1).item()
  number_2 = torch.sum(target == 2).item()
  number_3 = torch.sum(target == 3).item()
  number_4 = torch.sum(target == 4).item()
  number_5 = torch.sum(target == 5).item()

  frequency = torch.tensor((number_0, number_1, number_2, number_3, number_4, number_5), dtype=torch.float32)
  frequency = frequency.numpy()
  classWeights = compute_class_weights(frequency)
  '''
  根據(jù)當前給出的ground truth label計算出每個類別所占據(jù)的權重
  '''

  # weights=torch.from_numpy(classWeights).float().cuda()
  weights = torch.from_numpy(classWeights).float()
  focal_frequency = F.nll_loss(F.softmax(input, dim=1), target, reduction='none')
  '''
  上面一篇博文講過
  F.nll_loss(torch.log(F.softmax(inputs, dim=1),target)的函數(shù)功能與F.cross_entropy相同
  可見F.nll_loss中實現(xiàn)了對于target的one-hot encoding編碼功能,將其編碼成與input shape相同的tensor
  然后與前面那一項(即F.nll_loss輸入的第一項)進行 element-wise production
  相當于取出了 log(p_gt)即當前樣本點被分類為正確類別的概率
  現(xiàn)在去掉取log的操作,相當于 focal_frequency shape [num_samples]
  即取出ground truth類別的概率數(shù)值,并取了負號
  '''

  focal_frequency += 1.0#shape [num_samples] 1-P(gt_classes)

  focal_frequency = torch.pow(focal_frequency, 2) # torch.Size([75])
  focal_frequency = focal_frequency.repeat(c, 1)
  '''
  進行repeat操作后,focal_frequency shape [num_classes,num_samples]
  '''
  focal_frequency = focal_frequency.transpose(1, 0)
  loss = F.nll_loss(focal_frequency * (torch.log(F.softmax(input, dim=1))), target, weight=None,
           reduction='elementwise_mean')
  return loss


def focal_loss_zhihu(input, target):
  '''
  :param input: 使用知乎上面大神給出的方案 https://zhuanlan.zhihu.com/p/28527749
  :param target:
  :return:
  '''
  n, c, h, w = input.size()

  target = target.long()
  inputs = input.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
  target = target.contiguous().view(-1)

  N = inputs.size(0)
  C = inputs.size(1)

  number_0 = torch.sum(target == 0).item()
  number_1 = torch.sum(target == 1).item()
  number_2 = torch.sum(target == 2).item()
  number_3 = torch.sum(target == 3).item()
  number_4 = torch.sum(target == 4).item()
  number_5 = torch.sum(target == 5).item()

  frequency = torch.tensor((number_0, number_1, number_2, number_3, number_4, number_5), dtype=torch.float32)
  frequency = frequency.numpy()
  classWeights = compute_class_weights(frequency)

  weights = torch.from_numpy(classWeights).float()
  weights=weights[target.view(-1)]#這行代碼非常重要

  gamma = 2

  P = F.softmax(inputs, dim=1)#shape [num_samples,num_classes]

  class_mask = inputs.data.new(N, C).fill_(0)
  class_mask = Variable(class_mask)
  ids = target.view(-1, 1)
  class_mask.scatter_(1, ids.data, 1.)#shape [num_samples,num_classes] one-hot encoding

  probs = (P * class_mask).sum(1).view(-1, 1)#shape [num_samples,]
  log_p = probs.log()

  print('in calculating batch_loss',weights.shape,probs.shape,log_p.shape)

  # batch_loss = -weights * (torch.pow((1 - probs), gamma)) * log_p
  batch_loss = -(torch.pow((1 - probs), gamma)) * log_p

  print(batch_loss.shape)

  loss = batch_loss.mean()
  return loss

if __name__=='__main__':
  pred=torch.rand((2,6,5,5))
  y=torch.from_numpy(np.random.randint(0,6,(2,5,5)))
  loss1=focal_loss_my(pred,y)
  loss2=focal_loss_zhihu(pred,y)

  print('loss1',loss1)
  print('loss2', loss2)
'''
in calculating batch_loss torch.Size([50]) torch.Size([50, 1]) torch.Size([50, 1])
torch.Size([50, 1])
loss1 tensor(1.3166)
loss2 tensor(1.3166)
'''

以上這篇pytorch實現(xiàn)focal loss的兩種方式小結就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python中設置超時跳過,超時退出的方式

    python中設置超時跳過,超時退出的方式

    今天小編就為大家分享一篇python中設置超時跳過,超時退出的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python Django切換MySQL數(shù)據(jù)庫實例詳解

    Python Django切換MySQL數(shù)據(jù)庫實例詳解

    這篇文章主要介紹了Python Django切換MySQL數(shù)據(jù)庫實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Python for循環(huán)中的陷阱詳解

    Python for循環(huán)中的陷阱詳解

    這篇文章主要給大家介紹了關于Python for循環(huán)中陷阱的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • python_tkinter彈出對話框創(chuàng)建2

    python_tkinter彈出對話框創(chuàng)建2

    這篇文章主要介紹了python_tkinter彈出對話框創(chuàng)建,上以篇文章我們簡單的對對話框創(chuàng)建做了簡單介紹,本文將繼續(xù)更多相關內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • jupyter notebook的安裝與使用詳解

    jupyter notebook的安裝與使用詳解

    這篇文章主要介紹了jupyter notebook的安裝與使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • python 實現(xiàn)對文件夾內(nèi)的文件排序編號

    python 實現(xiàn)對文件夾內(nèi)的文件排序編號

    下面小編就為大家分享一篇python 實現(xiàn)對文件夾內(nèi)的文件排序編號,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python中用memcached來減少數(shù)據(jù)庫查詢次數(shù)的教程

    Python中用memcached來減少數(shù)據(jù)庫查詢次數(shù)的教程

    這篇文章主要介紹了Python中用memcached來減少數(shù)據(jù)庫查詢次數(shù)的教程,memcached是一種分布式的內(nèi)存緩存工具,使用后可以減少對硬盤的I/O次數(shù),需要的朋友可以參考下
    2015-04-04
  • Numpy中轉(zhuǎn)置transpose、T和swapaxes的實例講解

    Numpy中轉(zhuǎn)置transpose、T和swapaxes的實例講解

    下面小編就為大家分享一篇Numpy中轉(zhuǎn)置transpose、T和swapaxes的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python django集成cas驗證系統(tǒng)

    python django集成cas驗證系統(tǒng)

    cas是什么東西就不多說了,簡而言之就是單點登陸系統(tǒng),一處登陸,全網(wǎng)有權限的系統(tǒng)均可以訪問
    2014-07-07
  • python一繪制元二次方程曲線的實例分析

    python一繪制元二次方程曲線的實例分析

    在本篇文章里小編給大家整理的是一篇關于python一繪制元二次方程曲線的實例分析內(nèi)容,有興趣的朋友們可以跟著學習參考下。
    2021-07-07

最新評論