-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
175 lines (148 loc) · 4.88 KB
/
Copy pathutils.py
File metadata and controls
175 lines (148 loc) · 4.88 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import os
import torch
import torchvision
from torch.utils.data import DataLoader
from config import CHECKPOINT_PATH, SAVE_PREDS_IMG_DIR, THRESHOLD
from dataload import CrackSegmentationDataset
def save_checkpoint(state: dict, filename: str = CHECKPOINT_PATH):
os.makedirs(os.path.dirname(filename), exist_ok=True)
torch.save(state, filename)
print(f"=> Saved checkpoint: {filename}")
def load_checkpoint(checkpoint_path: str, model: torch.nn.Module, device: str = "cuda"):
print(f"=> Loading checkpoint: {checkpoint_path}")
checkpoint = torch.load(checkpoint_path, map_location=device)
state_dict = checkpoint.get("state_dict", checkpoint)
model.load_state_dict(state_dict)
return checkpoint
def get_loaders(
train_dir,
train_maskdir,
val_dir,
val_maskdir,
batch_size,
train_transform,
val_transform,
num_workers=4,
pin_memory=True,
):
train_ds = CrackSegmentationDataset(
image_dir=train_dir,
mask_dir=train_maskdir,
transform=train_transform,
)
val_ds = CrackSegmentationDataset(
image_dir=val_dir,
mask_dir=val_maskdir,
transform=val_transform,
)
train_loader = DataLoader(
train_ds,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=True,
)
val_loader = DataLoader(
val_ds,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=pin_memory,
shuffle=False,
)
return train_loader, val_loader
def _ensure_bchw(mask: torch.Tensor) -> torch.Tensor:
if mask.dim() == 3:
mask = mask.unsqueeze(1)
return mask.float()
def update_confusion_counts(
logits: torch.Tensor,
targets: torch.Tensor,
threshold: float = THRESHOLD,
) -> dict[str, torch.Tensor]:
probs = torch.sigmoid(logits)
preds = (probs > threshold).float()
targets = _ensure_bchw(targets).float()
tp = (preds * targets).sum()
fp = (preds * (1.0 - targets)).sum()
fn = ((1.0 - preds) * targets).sum()
tn = ((1.0 - preds) * (1.0 - targets)).sum()
return {"tp": tp, "fp": fp, "fn": fn, "tn": tn}
def metrics_from_counts(counts: dict[str, torch.Tensor], eps: float = 1e-7) -> dict:
tp = counts["tp"].double()
fp = counts["fp"].double()
fn = counts["fn"].double()
tn = counts["tn"].double()
precision = tp / (tp + fp + eps)
recall = tp / (tp + fn + eps)
f1 = 2.0 * precision * recall / (precision + recall + eps)
foreground_iou = tp / (tp + fp + fn + eps)
background_iou = tn / (tn + fp + fn + eps)
miou = 0.5 * (foreground_iou + background_iou)
pixel_acc = (tp + tn) / (tp + fp + fn + tn + eps)
return {
"precision": float(precision.cpu()),
"recall": float(recall.cpu()),
"f1": float(f1.cpu()),
"foreground_iou": float(foreground_iou.cpu()),
"background_iou": float(background_iou.cpu()),
"miou": float(miou.cpu()),
"pixel_acc": float(pixel_acc.cpu()),
}
def check_accuracy(
loader,
model,
device="cuda",
threshold: float = THRESHOLD,
print_metrics: bool = True,
) -> dict:
counts = {
"tp": torch.tensor(0.0, device=device),
"fp": torch.tensor(0.0, device=device),
"fn": torch.tensor(0.0, device=device),
"tn": torch.tensor(0.0, device=device),
}
model.eval()
with torch.no_grad():
for images, masks in loader:
images = images.to(device)
masks = _ensure_bchw(masks).to(device)
outputs = model(images)
if isinstance(outputs, dict):
outputs = outputs["out"]
batch_counts = update_confusion_counts(outputs, masks, threshold)
for key in counts:
counts[key] += batch_counts[key]
metrics = metrics_from_counts(counts)
if print_metrics:
print(
"Validation metrics | "
f"F1: {metrics['f1']:.4f} | "
f"mIoU: {metrics['miou']:.4f} | "
f"Precision: {metrics['precision']:.4f} | "
f"Recall: {metrics['recall']:.4f}"
)
model.train()
return metrics
def save_predictions_as_imgs(
loader,
model,
folder: str = SAVE_PREDS_IMG_DIR,
device="cuda",
threshold: float = THRESHOLD,
):
os.makedirs(folder, exist_ok=True)
model.eval()
for idx, (images, masks) in enumerate(loader):
images = images.to(device=device)
with torch.no_grad():
logits = model(images)
if isinstance(logits, dict):
logits = logits["out"]
preds = (torch.sigmoid(logits) > threshold).float()
torchvision.utils.save_image(preds, os.path.join(folder, f"pred_{idx}.png"))
torchvision.utils.save_image(
_ensure_bchw(masks),
os.path.join(folder, f"gt_{idx}.png"),
)
model.train()