質問編集履歴
3
ファイル変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
github上で公開されているPytorch-UNetでサンプルデータをそのまま出力しました。
|
2
2
|
|
3
|
-
https://github.com/
|
3
|
+
https://github.com/Paul0629/unet-pytorch
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -8,13 +8,13 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
同じフォルダにeval
|
11
|
+
同じフォルダにeval.pyとdice_loss.pyあったので、評価用のプログラムだと思ったのですが、実行すると、何も保存されず、何も出力されませんでした。
|
12
12
|
|
13
13
|
このプログラムは何を実行するプログラムでしょうか?
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
1,eval
|
17
|
+
1,eval.py
|
18
18
|
|
19
19
|
```python
|
20
20
|
|
@@ -26,73 +26,153 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
-
from
|
29
|
+
from dice_loss import dice_coeff
|
30
30
|
|
31
31
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
def eval
|
35
|
+
def eval_net(net, loader, device):
|
36
|
+
|
37
|
+
"""Evaluation without the densecrf with the dice coefficient"""
|
36
38
|
|
37
39
|
net.eval()
|
38
40
|
|
39
|
-
|
41
|
+
mask_type = torch.float32 if net.n_classes == 1 else torch.long
|
40
42
|
|
43
|
+
n_val = len(loader) # the number of batch
|
44
|
+
|
41
|
-
|
45
|
+
tot = 0
|
42
46
|
|
43
47
|
|
44
48
|
|
45
|
-
|
49
|
+
with tqdm(total=n_val, desc='Validation round', unit='batch', leave=False) as pbar:
|
46
50
|
|
47
|
-
for batch in
|
51
|
+
for batch in loader:
|
48
52
|
|
49
|
-
im
|
53
|
+
imgs, true_masks = batch['image'], batch['mask']
|
50
54
|
|
51
|
-
|
55
|
+
imgs = imgs.to(device=device, dtype=torch.float32)
|
52
56
|
|
53
|
-
image = image.to(device=device, dtype=torch.float32)
|
54
|
-
|
55
|
-
mask
|
57
|
+
true_masks = true_masks.to(device=device, dtype=mask_type)
|
56
|
-
|
57
|
-
mask_true = F.one_hot(mask_true, net.n_classes).permute(0, 3, 1, 2).float()
|
58
58
|
|
59
59
|
|
60
60
|
|
61
|
-
with torch.no_grad():
|
61
|
+
with torch.no_grad():
|
62
62
|
|
63
|
-
# predict the mask
|
64
|
-
|
65
|
-
mask_pred = net(im
|
63
|
+
mask_pred = net(imgs)
|
66
64
|
|
67
65
|
|
68
66
|
|
69
|
-
|
67
|
+
if net.n_classes > 1:
|
70
68
|
|
71
|
-
if net.n_classes == 1:
|
72
|
-
|
73
|
-
|
69
|
+
tot += F.cross_entropy(mask_pred, true_masks).item()
|
74
|
-
|
75
|
-
# compute the Dice score
|
76
|
-
|
77
|
-
dice_score += dice_coeff(mask_pred, mask_true, reduce_batch_first=False)
|
78
70
|
|
79
71
|
else:
|
80
72
|
|
81
|
-
|
73
|
+
pred = torch.sigmoid(mask_pred)
|
82
74
|
|
83
|
-
|
75
|
+
pred = (pred > 0.5).float()
|
84
76
|
|
85
|
-
|
77
|
+
tot += dice_coeff(pred, true_masks).item()
|
86
78
|
|
87
|
-
|
79
|
+
pbar.update()
|
88
|
-
|
89
|
-
|
90
80
|
|
91
81
|
|
92
82
|
|
93
83
|
net.train()
|
94
84
|
|
95
|
-
return
|
85
|
+
return tot / n_val
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
2,dice_loss.py
|
90
|
+
|
91
|
+
```python
|
92
|
+
|
93
|
+
import torch
|
94
|
+
|
95
|
+
from torch.autograd import Function
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
class DiceCoeff(Function):
|
102
|
+
|
103
|
+
"""Dice coeff for individual examples"""
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def forward(self, input, target):
|
108
|
+
|
109
|
+
self.save_for_backward(input, target)
|
110
|
+
|
111
|
+
eps = 0.0001
|
112
|
+
|
113
|
+
self.inter = torch.dot(input.view(-1), target.view(-1))
|
114
|
+
|
115
|
+
self.union = torch.sum(input) + torch.sum(target) + eps
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
t = (2 * self.inter.float() + eps) / self.union.float()
|
120
|
+
|
121
|
+
return t
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# This function has only a single output, so it gets only one gradient
|
126
|
+
|
127
|
+
def backward(self, grad_output):
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
input, target = self.saved_variables
|
132
|
+
|
133
|
+
grad_input = grad_target = None
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
if self.needs_input_grad[0]:
|
138
|
+
|
139
|
+
grad_input = grad_output * 2 * (target * self.union - self.inter) \
|
140
|
+
|
141
|
+
/ (self.union * self.union)
|
142
|
+
|
143
|
+
if self.needs_input_grad[1]:
|
144
|
+
|
145
|
+
grad_target = None
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
return grad_input, grad_target
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def dice_coeff(input, target):
|
156
|
+
|
157
|
+
"""Dice coeff for batches"""
|
158
|
+
|
159
|
+
if input.is_cuda:
|
160
|
+
|
161
|
+
s = torch.FloatTensor(1).cuda().zero_()
|
162
|
+
|
163
|
+
else:
|
164
|
+
|
165
|
+
s = torch.FloatTensor(1).zero_()
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for i, c in enumerate(zip(input, target)):
|
170
|
+
|
171
|
+
s = s + DiceCoeff().forward(c[0], c[1])
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
return s / (i + 1)
|
96
176
|
|
97
177
|
|
98
178
|
|
2
ファイル変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
github上で公開されているPytorch-UNetでサンプルデータをそのまま出力しました。
|
2
2
|
|
3
|
-
https://github.com/
|
3
|
+
https://github.com/milesial/Pytorch-UNet
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -8,13 +8,15 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
同じフォルダにeval
|
11
|
+
同じフォルダにevaluate.pyあったので、評価用のプログラムだと思ったのですが、実行すると、何も保存されず、何も出力されませんでした。
|
12
12
|
|
13
|
-
この
|
13
|
+
このプログラムは何を実行するプログラムでしょうか?
|
14
14
|
|
15
|
-
1,eval.py
|
16
15
|
|
16
|
+
|
17
|
+
1,evaluate.py
|
18
|
+
|
17
|
-
```python
|
19
|
+
```python
|
18
20
|
|
19
21
|
import torch
|
20
22
|
|
@@ -24,156 +26,74 @@
|
|
24
26
|
|
25
27
|
|
26
28
|
|
27
|
-
from dice_
|
29
|
+
from utils.dice_score import multiclass_dice_coeff, dice_coeff
|
28
30
|
|
29
31
|
|
30
32
|
|
31
33
|
|
32
34
|
|
33
|
-
def eval
|
35
|
+
def evaluate(net, dataloader, device):
|
34
|
-
|
35
|
-
"""Evaluation without the densecrf with the dice coefficient"""
|
36
36
|
|
37
37
|
net.eval()
|
38
38
|
|
39
|
-
ma
|
39
|
+
num_val_batches = len(dataloader)
|
40
40
|
|
41
|
-
n_val = len(loader) # the number of batch
|
42
|
-
|
43
|
-
|
41
|
+
dice_score = 0
|
44
42
|
|
45
43
|
|
46
44
|
|
47
|
-
|
45
|
+
# iterate over the validation set
|
48
46
|
|
49
|
-
|
47
|
+
for batch in tqdm(dataloader, total=num_val_batches, desc='Validation round', unit='batch', leave=False):
|
50
48
|
|
51
|
-
|
49
|
+
image, mask_true = batch['image'], batch['mask']
|
52
50
|
|
53
|
-
|
51
|
+
# move images and labels to correct device and type
|
54
52
|
|
53
|
+
image = image.to(device=device, dtype=torch.float32)
|
54
|
+
|
55
|
-
|
55
|
+
mask_true = mask_true.to(device=device, dtype=torch.long)
|
56
|
+
|
57
|
+
mask_true = F.one_hot(mask_true, net.n_classes).permute(0, 3, 1, 2).float()
|
56
58
|
|
57
59
|
|
58
60
|
|
59
|
-
|
61
|
+
with torch.no_grad():
|
60
62
|
|
63
|
+
# predict the mask
|
64
|
+
|
61
|
-
|
65
|
+
mask_pred = net(image)
|
62
66
|
|
63
67
|
|
64
68
|
|
65
|
-
|
69
|
+
# convert to one-hot format
|
66
70
|
|
71
|
+
if net.n_classes == 1:
|
72
|
+
|
67
|
-
|
73
|
+
mask_pred = (F.sigmoid(mask_pred) > 0.5).float()
|
74
|
+
|
75
|
+
# compute the Dice score
|
76
|
+
|
77
|
+
dice_score += dice_coeff(mask_pred, mask_true, reduce_batch_first=False)
|
68
78
|
|
69
79
|
else:
|
70
80
|
|
71
|
-
pred =
|
81
|
+
mask_pred = F.one_hot(mask_pred.argmax(dim=1), net.n_classes).permute(0, 3, 1, 2).float()
|
72
82
|
|
73
|
-
p
|
83
|
+
# compute the Dice score, ignoring background
|
74
84
|
|
75
|
-
|
85
|
+
dice_score += multiclass_dice_coeff(mask_pred[:, 1:, ...], mask_true[:, 1:, ...], reduce_batch_first=False)
|
76
86
|
|
77
|
-
|
87
|
+
|
88
|
+
|
89
|
+
|
78
90
|
|
79
91
|
|
80
92
|
|
81
93
|
net.train()
|
82
94
|
|
83
|
-
return
|
95
|
+
return dice_score / num_val_batches
|
84
96
|
|
85
97
|
|
86
98
|
|
87
99
|
```
|
88
|
-
|
89
|
-
2,dice_loss.py
|
90
|
-
|
91
|
-
```python
|
92
|
-
|
93
|
-
import torch
|
94
|
-
|
95
|
-
from torch.autograd import Function
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
class DiceCoeff(Function):
|
102
|
-
|
103
|
-
"""Dice coeff for individual examples"""
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
def forward(self, input, target):
|
108
|
-
|
109
|
-
self.save_for_backward(input, target)
|
110
|
-
|
111
|
-
eps = 0.0001
|
112
|
-
|
113
|
-
self.inter = torch.dot(input.view(-1), target.view(-1))
|
114
|
-
|
115
|
-
self.union = torch.sum(input) + torch.sum(target) + eps
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
t = (2 * self.inter.float() + eps) / self.union.float()
|
120
|
-
|
121
|
-
return t
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
# This function has only a single output, so it gets only one gradient
|
126
|
-
|
127
|
-
def backward(self, grad_output):
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
input, target = self.saved_variables
|
132
|
-
|
133
|
-
grad_input = grad_target = None
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
if self.needs_input_grad[0]:
|
138
|
-
|
139
|
-
grad_input = grad_output * 2 * (target * self.union - self.inter) \
|
140
|
-
|
141
|
-
/ (self.union * self.union)
|
142
|
-
|
143
|
-
if self.needs_input_grad[1]:
|
144
|
-
|
145
|
-
grad_target = None
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
return grad_input, grad_target
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
def dice_coeff(input, target):
|
156
|
-
|
157
|
-
"""Dice coeff for batches"""
|
158
|
-
|
159
|
-
if input.is_cuda:
|
160
|
-
|
161
|
-
s = torch.FloatTensor(1).cuda().zero_()
|
162
|
-
|
163
|
-
else:
|
164
|
-
|
165
|
-
s = torch.FloatTensor(1).zero_()
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
for i, c in enumerate(zip(input, target)):
|
170
|
-
|
171
|
-
s = s + DiceCoeff().forward(c[0], c[1])
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
return s / (i + 1)
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
```
|
1
URLが間違っていた
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
github上で公開されているPytorch-UNetでサンプルデータをそのまま出力しました。
|
2
2
|
|
3
|
-
https://github.com/
|
3
|
+
https://github.com/Paul0629/unet-pytorch
|
4
4
|
|
5
5
|
|
6
6
|
|