-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstage4_train.py
More file actions
153 lines (131 loc) · 5.15 KB
/
stage4_train.py
File metadata and controls
153 lines (131 loc) · 5.15 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import argparse
import os
import sys
import numpy as np
import torch
import torch.nn as nn
import torchvision
from PIL import Image
from torch.utils.data import DataLoader, Dataset
from utils.vis_utils import vis_trun
parser = argparse.ArgumentParser(description='Stage IV')
parser.add_argument('--unet-checkpoint', type=str)
parser.add_argument('--stage3-output', type=str)
parser.add_argument('--stage4-output', type=str)
args = parser.parse_args()
class TestDiceLoss(nn.Module):
def __init__(self):
super(TestDiceLoss, self).__init__()
def forward(self, y_true, y_pred, **kwargs):
"""
compute mean dice for binary segmentation map via numpy
"""
intersection = torch.sum(torch.abs(y_pred * y_true), [1, 2, 3])
mask_sum = torch.sum(torch.abs(y_true), [1, 2, 3]) + torch.sum(torch.abs(y_pred), [1, 2, 3])
smooth = .000001
dice = 1 - 2 * (intersection + smooth) / (mask_sum + smooth)
return dice
class PairDatset(Dataset):
def __init__(self, data_path):
self.data_path = data_path
self.images = []
self.masks = []
self.turn = torchvision.transforms.ToTensor()
for root, dirs, files in os.walk(data_path):
for file in files:
path = str(os.path.join(self.data_path, file))
if file.startswith("image_"):
self.images.append(path)
elif file.startswith("mask_"):
self.masks.append(path)
else:
continue
import re
self.indexs = [re.findall(r"\d+", str(self.images[i]))[-1] for i in range(len(self.images))]
# print(self.indexs)
# exit(-1)
def __len__(self):
return len(self.indexs)
def __getitem__(self, item):
image_path = os.path.join(self.data_path, "image_" + str(self.indexs[item]) + ".png")
mask_path = os.path.join(self.data_path, "mask_" + str(self.indexs[item]) + ".png")
image, mask = Image.open(image_path).convert("L"), Image.open(mask_path).convert("L")
image, mask = self.turn(image), self.turn(mask)
mask = (mask > 0.5).float()
return image, mask #, self.indexs[item]
def classifier(model_path):
from backbone import UNet
classifier_fn = UNet(n_classes=1, n_channels=1)
classifier_fn.load_state_dict(torch.load(model_path, map_location="cpu"))
classifier_fn = classifier_fn.cuda()
return classifier_fn
class DiceLoss(nn.Module):
"""Dice loss, need one hot encode input
Args:
weight: An array of shape [num_classes,]
ignore_index: class index to ignore
predict: A tensor of shape [N, C, *]
target: A tensor of same shape with predict
other args pass to BinaryDiceLoss
Return:
same as BinaryDiceLoss
"""
def __init__(self, weight=None, ignore_index=None, **kwargs):
super(DiceLoss, self).__init__()
self.kwargs = kwargs
self.weight = weight
self.ignore_index = ignore_index
self.epsilon = 1e-5
def forward(self, predict, target):
assert predict.size() == target.size(), "the size of predict and target must be equal."
num = predict.size(0)
pre = predict.view(num, -1)
tar = target.view(num, -1)
intersection = (pre * tar).sum(-1) # 利用预测值与标签相乘当作交集
union = (pre + tar).sum(-1)
score = 1 - 2 * (intersection + self.epsilon) / (union + self.epsilon)
return score
def choose(model_path, data_path, save_path, tau=0.2):
classifier_fn = classifier(model_path)
dataset = PairDatset(data_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
dataloader = DataLoader(dataset, num_workers=4, shuffle=False, batch_size=1)
pass_list = []
no_pass_list = []
dice_loss = TestDiceLoss()
with torch.no_grad():
classifier_fn.eval()
for i, (image, label, indexs) in enumerate(dataloader):
image, label = image.cuda(), label.cuda()
pred = (classifier_fn(image).sigmoid() > 0.5).float()
label = label.float()
dices = dice_loss(pred, label).tolist()
print(dices)
j = 0
for (dice, index) in zip(dices, indexs):
if_good = dice < tau
if if_good:
pass_list.append([image[j], label[j]])
elif dice < tau * 2:
pass_list.append([image[j], pred[j]])
else:
no_pass_list.append([image[j], label[j]])
j += 1
turn = torchvision.transforms.ToPILImage()
print(f"{len(pass_list) / (len(pass_list) + len(no_pass_list))}")
# tau = 1/1 0.7% 4.234%
# tau = 1/2 0.7% 4.567%
# tau = 1/3 -- 4.725%
for i in range(len(pass_list)):
image = pass_list[i][0].cpu()
mask = pass_list[i][1].cpu()
image = turn(image)
mask = turn(mask)
image.save(f"{save_path}/image_{i}.png")
mask.save(f"{save_path}/mask_{i}.png")
if __name__ == "__main__":
choose(args.unet_checkpoint,
args.stage3_output,
args.stage4_output,
0.065)