Comments (7)
Can you try to run python focalloss_test.py
?
from focal_loss_pytorch.
But when i replace CrossEntropyLoss with FocalLoss to train my network, the corresponding trainning loss is lower.
from focal_loss_pytorch.
I use the same value of alpha of FocalLoss and weight of CrossEntropyLoss ([0.366, 1]), the max_error is 0.12252533435821533.
from focal_loss_pytorch.
i rewrite the FocalLoss code according to my experiments and i meet the same problem as you.
class BCFocalLoss(nn.Module):
def __init__(self, gamma=0, alpha=None, size_average=True):
super(BCFocalLoss, self).__init__()
self.gamma = gamma
self.alpha = alpha
if isinstance(alpha,(float,int)): self.alpha = torch.Tensor([alpha,1-alpha])
if isinstance(alpha,list): self.alpha = torch.Tensor(alpha)
self.size_average = size_average
def forward(self, input, target):
if input.dim()>2:
input = input.view(input.size(0),input.size(1),-1) # N,C,H,W => N,C,H*W
#input = input.transpose(1,2) # N,C,H*W => N,H*W,C
#input = input.contiguous().view(-1,input.size(2)) # N,H*W,C => N*H*W,C
input = input.contiguous().view(-1)
#target = target.view(-1,1)
target = target.view(-1)
#logpt = F.log_softmax(input)
#logpt = F.sigmoid(input)
logpt = F.logsigmoid(input)
#logpt = logpt.gather(1,target)
#logpt = logpt.view(-1)
pt = Variable(logpt.data.exp())
if self.alpha is not None:
if self.alpha.type()!=input.data.type():
self.alpha = self.alpha.type_as(input.data)
at = self.alpha.gather(0,target.data.view(-1))
logpt = logpt * Variable(at)
loss = -1 * (1-pt)**self.gamma * logpt
if self.size_average: return loss.mean()
else: return loss.sum()
when gamma=0 it works as BCEWithLogitsLoss in my test code.But when i replace BCEWithLogitsLoss with BCFocalLoss to train my network, the corresponding trainning loss is much lower.Here is the test code:
torch.random.manual_seed(32)
p=torch.randn(1,1,56,56,requires_grad=True).type(torch.float32)
t=torch.ones(1,1,56,56).type(torch.float32)
#c=torch.sigmoid(p)
#result=F.binary_cross_entropy(c,t)
criterion=BCFocalLoss()
#result=criterion(p,t)
result=criterion(p,t)
result2=nn.BCEWithLogitsLoss()(p,t)
result.backward()
#result2.backward()
print(p.grad.sum())
from focal_loss_pytorch.
same problem have you solved that @nationalflag @clcarwin @huaifeng1993
from focal_loss_pytorch.
same problem
from focal_loss_pytorch.
If you guys find that the loss using CE-loss is much lower than that using Focal-loss, you can try like below:
logpt=-F.cross_entropy(input,target.view(-1),reduction="none")
the item " reduction="none" " is important.
from focal_loss_pytorch.
Related Issues (14)
- NameError: name 'long' is not defined HOT 3
- alpha value is not consistent with paper?
- Data-Sample vs Focal-Loss, which is the better method for data-imbalance task? HOT 2
- Multi-class focal loss HOT 1
- Loss occurs nan!
- Negative examples
- Why softmax instead of sigmoid is used?
- What are the best parameters? HOT 2
- if I set alpha the program crash HOT 2
- shall the pt bp? HOT 1
- the problem of reshape
- Shouldn't it be log_sigmoid instead of softmax ? HOT 1
- Normalisation & mean over classes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from focal_loss_pytorch.