-
Notifications
You must be signed in to change notification settings - Fork 5
/
updater.py
54 lines (41 loc) · 1.44 KB
/
updater.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
44
45
46
47
48
49
50
51
52
53
54
#coding:utf-8
"""
@auther tzw
@date 2018-6-15
"""
import os, sys, time
import numpy as np
import chainer
import chainer.links as L
import chainer.functions as F
class Unet3DUpdater(chainer.training.StandardUpdater):
def __init__(self, *args, **kwargs):
self.unet = kwargs.pop("models")
super(Unet3DUpdater, self).__init__(*args, **kwargs)
def loss_softmax_cross_entropy(self, unet, predict, ground_truth):
"""
* @param unet Unet
* @param predict Output of unet
* @param ground_truth Ground truth label
"""
loss = -F.mean(F.log(predict+1e-16) * ground_truth)
chainer.report({"loss":loss}, unet)#mistery
return loss
def jaccard_index(predict, ground_truth):
JI_numerator=0.0
JI_denominator=0.0
predict = predict.ravel()
ground_truth = ground_truth.ravel()
seg = (predict > 0.5)
JI_numerator = (seg * ground_truth).sum()
JI_denominator =(seg + ground_truth> 0).sum()
return JI_numerator/JI_denominator
def update_core(self):
#load optimizer called "unet"
unet_optimizer = self.get_optimizer("unet")
batch = self.get_iterator("main").next()#iterator
# iterator
label, data = self.converter(batch, self.device)
unet = self.unet
predict = unet(data)
unet_optimizer.update(self.loss_softmax_cross_entropy, unet, predict, label)