-
Notifications
You must be signed in to change notification settings - Fork 1
/
criterion.py
43 lines (37 loc) · 1.25 KB
/
criterion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch
import pandas as pd
import numpy as np
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import os
from glob import glob
import nibabel as nib
import pickle
from tqdm import tqdm
import random
from torchvision.transforms import transforms
from scipy import ndimage
import matplotlib.pyplot as plt
def Dice(output, target, eps=1e-5):
target = target.float()
num = 2 * (output * target).sum()
den = output.sum() + target.sum() + eps
return 1.0 - num/den
class softmax_dice(nn.Module):
'''
The dice loss for using softmax activation function
:param output: (b, num_class, d, h, w)
:param target: (b, d, h, w)
:return: softmax dice loss
'''
def __init__(self):
super(softmax_dice, self).__init__()
def forward(self, output, target):
target[target == 4] = 3
output = output.cuda()
target = target.cuda()
loss0 = Dice(output[:, 0, ...], (target == 0).float())
loss1 = Dice(output[:, 1, ...], (target == 1).float())
loss2 = Dice(output[:, 2, ...], (target == 2).float())
loss3 = Dice(output[:, 3, ...], (target == 3).float())
return loss1 + loss2 + loss3 + loss0, 1-loss1.data, 1-loss2.data, 1-loss3.data