forked from kirilcvetkov92/Semantic-Segmentation-BiSeNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
102 lines (79 loc) · 2.91 KB
/
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
import csv
import os
import numpy as np
def get_label_info(csv_path):
"""
Retrieve the class names and label values for the selected dataset.
Must be in CSV format!
# Arguments
csv_path: The file path of the class dictionairy
# Returns
Two lists: one for the class names and the other for the label values
"""
filename, file_extension = os.path.splitext(csv_path)
if not file_extension == ".csv":
return ValueError("File is not a CSV!")
class_names = []
label_values = []
with open(csv_path, 'r') as csvfile:
file_reader = csv.reader(csvfile, delimiter=',')
header = next(file_reader)
for row in file_reader:
class_names.append(row[0])
label_values.append([int(row[1]), int(row[2]), int(row[3])])
# print(class_dict)
return class_names, label_values
def one_hot_it(label, label_values):
"""
Convert a segmentation image label array to one-hot format
by replacing each pixel value with a vector of length num_classes
# Arguments
label: The 2D array segmentation image label
label_values
# Returns
A 2D array with the same width and hieght as the input, but
with a depth size of num_classes
"""
semantic_map = []
for colour in label_values:
equality = np.equal(label, colour)
class_map = np.all(equality, axis = -1)
semantic_map.append(class_map)
semantic_map = np.stack(semantic_map, axis=-1)
return semantic_map
def reverse_one_hot(image):
"""
Transform a 2D array in one-hot format (depth is num_classes),
to a 2D array with only 1 channel, where each pixel value is
the classified class key.
# Arguments
image: The one-hot format image
# Returns
A 2D array with the same width and hieght as the input, but
with a depth size of 1, where each pixel value is the classified
class key.
"""
x = np.argmax(image, axis = -1)
return x
def colour_code_segmentation(image, label_values):
"""
Given a 1-channel array of class keys, colour code the segmentation results.
# Arguments
image: single channel array where each value represents the class key.
label_values
# Returns
Colour coded image for segmentation visualization
"""
colour_codes = np.array(label_values)
x = colour_codes[image.astype(int)]
return x
def get_label_values():
class_names_list, label_values = get_label_info("data/class_dict.csv")
class_names_string = ""
for class_name in class_names_list:
if not class_name == class_names_list[-1]:
class_names_string = class_names_string + class_name + ", "
else:
class_names_string = class_names_string + class_name
num_classes = len(label_values)
return label_values, class_names_list, num_classes