-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize_night.py
167 lines (129 loc) · 6.6 KB
/
visualize_night.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
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
import cv2
import os
import argparse
import torch
import numpy as np
from torch.utils.data import random_split, DataLoader
from dataload.dataset_video import LaneDatasetVid
from model.model import STM
def load_model(model, optimizer, load_path):
checkpoint = torch.load(load_path)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
return model, optimizer, epoch, loss
def process_images(model, device, frames, masks):
frames = frames.to(device)
masks = masks.to(device)
original_frames = []
estimated_masks = []
ground_truth = []
Estimates = torch.zeros_like(masks)
Estimates[:, 0, ...] = masks[:, 0, ...]
n0_key, n0_val = model("memorize", frames[:, 0, ...], Estimates[:, 0, ...])
n1_logit = model("segment", frames[:, 1, ...], n0_key, n0_val)
n1_label = masks[:, 1, ...]
original_frames.append(frames[:, 1, ...].cpu().numpy()[0])
estimated_masks.append(torch.sigmoid(n1_logit).detach().cpu().numpy()[0, 0, ...])
ground_truth.append(masks[:, 1, ...].cpu().numpy()[0, 0, ...])
Estimates[:, 1, ...] = torch.sigmoid(n1_logit).detach()
n1_key, n1_val = model("memorize", frames[:, 1, ...], Estimates[:, 1, ...])
n2_logit = model("segment", frames[:, 2, ...], n1_key, n1_val)
n2_label = masks[:, 2, ...]
original_frames.append(frames[:, 2, ...].cpu().numpy()[0])
estimated_masks.append(torch.sigmoid(n2_logit).detach().cpu().numpy()[0, 0, ...])
ground_truth.append(masks[:, 2, ...].cpu().numpy()[0, 0,...])
Estimates[:, 2, ...] = torch.sigmoid(n2_logit).detach()
n2_key, n2_val = model("memorize", frames[:, 2, ...], Estimates[:, 2, ...])
n3_logit = model("segment", frames[:, 3, ...], n2_key, n2_val)
n3_label = masks[:, 3, ...]
original_frames.append(frames[:, 3, ...].cpu().numpy()[0])
estimated_masks.append(torch.sigmoid(n3_logit).detach().cpu().numpy()[0, 0, ...])
ground_truth.append(masks[:, 3, ...].cpu().numpy() [0, 0, ...])
Estimates[:, 3, ...] = torch.sigmoid(n3_logit).detach()
n3_key, n3_val = model("memorize", frames[:, 3, ...], Estimates[:, 3, ...])
n4_logit = model("segment", frames[:, 4, ...], n3_key, n3_val)
n4_label = masks[:, 4, ...]
original_frames.append(frames[:, 4, ...].cpu().numpy()[0])
estimated_masks.append(torch.sigmoid(n4_logit).detach().cpu().numpy()[0, 0, ...])
ground_truth.append(masks[:, 4, ...].cpu().numpy()[0, 0, ...])
Estimates[:, 4, ...] = torch.sigmoid(n4_logit).detach()
return original_frames, estimated_masks, ground_truth
def reconstruct_image(patches, canvas, img_type="original"):
# type in ["original", "estimation", "ground_truth"]
if img_type == "original":
for (i, j), patch in patches.items():
for k in range(4):
canvas[k][:, i*224:(i+1)*224, j*224:(j+1)*224] = patch[k] # (3, 224, 224)
elif img_type == "estimation":
for (i, j), patch in patches.items():
for k in range(4):
patch[k] = ((patch[k] > 0.35) * 255).astype(np.uint8)
canvas[k][i*224:(i+1)*224, j*224:(j+1)*224] = patch[k]
else : # image type == ground_truth
for (i, j), patch in patches.items():
for k in range(4):
patch[k] = (patch[k]* 255).astype(np.uint8)
canvas[k][i*224:(i+1)*224, j*224:(j+1)*224] = patch[k]
return canvas
def get_arguments():
parser = argparse.ArgumentParser(description="LIN")
parser.add_argument("--model_dir", type=str, help="path to pth", default='/home/mindong/lane-in-night/result/exp_1/006_0.140.pth')
return parser.parse_args()
def visualize(args):
IMSET = 'image_paths_2.csv'
DATA_ROOT = '/home/mindong/lane-in-night/data'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dataset = LaneDatasetVid(DATA_ROOT, IMSET, to_crop=False, test=True)
train_size = int(0.9 * len(dataset)) # 80% for training
val_size = len(dataset) - train_size # Remaining 20% for validation
# Split the dataset into training and validation subsets
_, test_dataset = random_split(dataset, [train_size, val_size])
test_loader = DataLoader(test_dataset,
batch_size=1,
num_workers=4,
shuffle = True,
pin_memory=True)
model = STM()
model.to(device=device)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
model, optimizer, epoch, loss = load_model(model, optimizer, args.model_dir)
model.eval()
for iter, data in enumerate(test_loader):
if iter == 5:
break
original_frames, estimated_masks, ground_truth = {}, {}, {}
img_height, img_width = data['img']
for key, patch in data.items():
if key == 'img':
continue
patch_frames, patch_estimated_masks, patch_ground_truth = process_images(model, device, patch[0], patch[1])
original_frames[key] = patch_frames
estimated_masks[key] = patch_estimated_masks
ground_truth[key] = patch_ground_truth
rows, cols = img_height // 224, img_width // 224
originals = [np.zeros((3, rows * 224, cols * 224), dtype=np.float32) for _ in range(4)]
estimates = [np.zeros((rows * 224, cols * 224), dtype=np.uint8) for _ in range(4)]
ground_truths = [np.zeros((rows * 224, cols * 224), dtype=np.uint8) for _ in range(4)]
original_frames = reconstruct_image(original_frames, originals, img_type="original")
estimated_masks = reconstruct_image(estimated_masks, estimates, img_type="estimation")
ground_truth = reconstruct_image(ground_truth, ground_truths, img_type="ground_truth")
if not os.path.exists("report/results_final"):
os.makedirs("report/results_final")
save_dir = f"report/results_final/res_{iter}"
os.makedirs(save_dir, exist_ok=True)
for i in range(4):
try:
org_frame = original_frames[i].transpose(1, 2, 0)
org_frame *= 255
org_frame = org_frame.astype(np.uint8)
org_frame = cv2.cvtColor(org_frame, cv2.COLOR_RGB2BGR)
cv2.imwrite(f"{save_dir}/org_{i}.png", org_frame)
cv2.imwrite(f"{save_dir}/est_{i}.png", estimated_masks[i])
cv2.imwrite(f"{save_dir}/gt_{i}.png", ground_truth[i])
except Exception as e:
print(f"Error saving image: {e}")
if __name__ == "__main__":
args = get_arguments()
visualize(args)