-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlosses.py
54 lines (45 loc) · 1.54 KB
/
losses.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
import numpy as np
def Precision(outputs,targets):
num_sample = len(outputs)
precision=0
for i in range(num_sample):
output = outputs[i]
output = output.argmax(0)
#print output.shape
target = targets[i]
target = target[0,:,:,:]
#print target.shape
tp = sum(sum(sum(output*target)))/output.size
fp = sum(sum(sum(output*(1-target))))/output.size
prec = tp/(tp+fp)
precision+=prec
return precision/num_sample
def Recall(outputs,targets):
num_sample = len(outputs)
recall=0
for i in range(num_sample):
output = outputs[i]
output = output.argmax(0)
target = targets[i]
target = target[0,:,:,:]
tp = sum(sum(sum(output*target)))/output.size
fn = sum(sum(sum((1-output)*target)))/output.size
rec = tp/(tp+fn)
recall+=rec
return recall/num_sample
def F1_score(outputs,targets):
precision = Precision(outputs,targets)
recall = Recall(outputs,targets)
return 2*precision*recall/(precision+recall)
def Precision_img(output,target):
tp = sum(sum(sum(output*target)))/output.size
fp = sum(sum(sum(output*(1-target))))/output.size
return tp/(tp+fp)
def Recall_img(output,target):
tp = sum(sum(sum(output*target)))/output.size
fn = sum(sum(sum((1-output)*target)))/output.size
return tp/(tp+fn)
def F1_score_img(outputs,targets):
precision = Precision_img(outputs,targets)
recall = Recall_img(outputs,targets)
return 2*precision*recall/(precision+recall)