-
Notifications
You must be signed in to change notification settings - Fork 0
/
camvid_utils.py
executable file
·166 lines (140 loc) · 5.28 KB
/
camvid_utils.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 19 19:31:17 2022
@author: deniz
"""
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import scipy.misc
import random
import os
import imageio
#############################
# global variables #
#############################
root_dir = "CamVid/"
data_dir = os.path.join(root_dir, "701_StillsRaw_full") # train data
label_dir = os.path.join(root_dir, "LabeledApproved_full") # train label
label_colors_file = os.path.join(root_dir, "label_colors.txt") # color to label
val_label_file = os.path.join(root_dir, "val.txt") # validation file
train_label_file = os.path.join(root_dir, "train.txt") # train file
# create dir for label index
label_idx_dir = os.path.join(root_dir, "Labeled_idx")
if not os.path.exists(label_idx_dir):
os.makedirs(label_idx_dir)
label2color = {}
color2label = {}
label2index = {}
index2label = {}
def divide_train_val(val_rate=0.1, shuffle=True, random_seed=None):
data_list = os.listdir(data_dir)
data_len = len(data_list)
val_len = int(data_len * val_rate)
if random_seed:
random.seed(random_seed)
if shuffle:
data_idx = random.sample(range(data_len), data_len)
else:
data_idx = list(range(data_len))
val_idx = [data_list[i] for i in data_idx[:val_len]]
train_idx = [data_list[i] for i in data_idx[val_len:]]
# create val.csv
v = open(val_label_file, "w")
v.write("img,label\n")
for idx, name in enumerate(val_idx):
if 'png' not in name:
continue
img_name = os.path.join(data_dir, name)
lab_name = os.path.join(label_idx_dir, name)
lab_name = lab_name.split(".")[0] + "_L.png.npy"
v.write("{},{}\n".format(img_name, lab_name))
# create train.csv
t = open(train_label_file, "w")
t.write("img,label\n")
for idx, name in enumerate(train_idx):
if 'png' not in name:
continue
img_name = os.path.join(data_dir, name)
lab_name = os.path.join(label_idx_dir, name)
lab_name = lab_name.split(".")[0] + "_L.png.npy"
t.write("{},{}\n".format(img_name, lab_name))
def parse_label():
# change label to class index
f = open(label_colors_file, "r").read().split("\n")[:-1] # ignore the last empty line
for idx, line in enumerate(f):
label = line.split()[-1]
color = tuple([int(x) for x in line.split()[:-1]])
print(label, color)
label2color[label] = color
color2label[color] = label
label2index[label] = idx
index2label[idx] = label
# rgb = np.zeros((255, 255, 3), dtype=np.uint8)
# rgb[..., 0] = color[0]
# rgb[..., 1] = color[1]
# rgb[..., 2] = color[2]
# imshow(rgb, title=label)
for idx, name in enumerate(os.listdir(label_dir)):
filename = os.path.join(label_idx_dir, name)
if os.path.exists(filename + '.npy'):
print("Skip %s" % (name))
continue
print("Parse %s" % (name))
img = os.path.join(label_dir, name)
#img = scipy.misc.imread(img, mode='RGB')
img = imageio.imread(img, pilmode="RGB")
height, weight, _ = img.shape
idx_mat = np.zeros((height, weight))
for h in range(height):
for w in range(weight):
color = tuple(img[h, w])
try:
label = color2label[color]
index = label2index[label]
idx_mat[h, w] = index
except:
print("error: img:%s, h:%d, w:%d" % (name, h, w))
idx_mat = idx_mat.astype(np.uint8)
np.save(filename, idx_mat)
print("Finish %s" % (name))
# # test some pixels' label
# img = os.path.join(label_dir, os.listdir(label_dir)[0])
# #img = scipy.misc.imread(img, mode='RGB')
# img = imageio.imread(img, pilmode="RGB")
# test_cases = [(555, 405), (0, 0), (380, 645), (577, 943)]
# test_ans = ['Car', 'Building', 'Truck_Bus', 'Car']
# for idx, t in enumerate(test_cases):
# color = img[t]
# assert color2label[tuple(color)] == test_ans[idx]
'''debug function'''
def imshow(img, title=None):
try:
img = mpimg.imread(img)
imgplot = plt.imshow(img)
except:
plt.imshow(img, interpolation='nearest')
if title is not None:
plt.title(title)
plt.show()
def iou(pred, target, nclasses=32):
ious = []
for cls in range(nclasses):
pred_inds = pred == cls
target_inds = target == cls
intersection = (pred_inds[target_inds].sum()) + 1e-6
union = (pred_inds.sum() + target_inds.sum() - intersection) +1e-6
if union == 0:
ious.append(float('nan')) # if there is no ground truth, do not include in evaluation
else:
ious.append(float(intersection) / max(union, 1))
# print("cls", cls, pred_inds.sum(), target_inds.sum(), intersection, float(intersection) / max(union, 1))
return ious
def pixel_acc(pred, target):
correct = (pred == target).sum()
total = (target == target).sum()
return (correct / total) + 1e-6
if __name__ == '__main__':
divide_train_val(random_seed=1)
parse_label()