-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.py
executable file
·135 lines (117 loc) · 3.77 KB
/
content.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
#!/usr/bin/env python3
# This file is covered by the LICENSE file in the root of this project.
import argparse
import os
import yaml
import numpy as np
import collections
from auxiliary.laserscan import SemLaserScan
if __name__ == '__main__':
parser = argparse.ArgumentParser("./content.py")
parser.add_argument(
'--dataset', '-d',
type=str,
required=True,
help='Dataset to calculate content. No Default',
)
parser.add_argument(
'--config', '-c',
type=str,
required=False,
default="config/semantic-kitti.yaml",
help='Dataset config file. Defaults to %(default)s',
)
FLAGS, unparsed = parser.parse_known_args()
# print summary of what we will do
print("*" * 80)
print("INTERFACE:")
print("Dataset", FLAGS.dataset)
print("Config", FLAGS.config)
print("*" * 80)
# open config file
try:
print("Opening config file %s" % FLAGS.config)
CFG = yaml.safe_load(open(FLAGS.config, 'r'))
except Exception as e:
print(e)
print("Error opening yaml file.")
quit()
# get training sequences to calculate statistics
sequences = CFG["split"]["train"]
print("Analizing sequences", sequences)
# create content accumulator
accum = {}
total = 0.0
for key, _ in CFG["labels"].items():
accum[key] = 0
# itearate over sequences
for seq in sequences:
seq_accum = {}
seq_total = 0.0
for key, _ in CFG["labels"].items():
seq_accum[key] = 0
# make seq string
print("*" * 80)
seqstr = '{0:02d}'.format(int(seq))
print("parsing seq {}".format(seq))
# does sequence folder exist?
scan_paths = os.path.join(FLAGS.dataset, "sequences",
seqstr, "velodyne")
if os.path.isdir(scan_paths):
print("Sequence folder exists!")
else:
print("Sequence folder doesn't exist! Exiting...")
quit()
# populate the pointclouds
scan_names = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(scan_paths)) for f in fn]
scan_names.sort()
# does sequence folder exist?
label_paths = os.path.join(FLAGS.dataset, "sequences",
seqstr, "labels")
if os.path.isdir(label_paths):
print("Labels folder exists!")
else:
print("Labels folder doesn't exist! Exiting...")
quit()
# populate the pointclouds
label_names = [os.path.join(dp, f) for dp, dn, fn in os.walk(
os.path.expanduser(label_paths)) for f in fn]
label_names.sort()
# check that there are same amount of labels and scans
print(len(label_names))
print(len(scan_names))
assert(len(label_names) == len(scan_names))
# create a scan
nclasses = len(CFG["labels"])
scan = SemLaserScan(nclasses, CFG["color_map"], project=False)
for idx in range(len(scan_names)):
# open scan
scan.open_scan(scan_names[idx])
scan.open_label(label_names[idx])
# make histogram and accumulate
count = np.bincount(scan.sem_label)
seq_total += count.sum()
for key, data in seq_accum.items():
if count.size > key:
seq_accum[key] += count[key]
# zero the count
count[key] = 0
for i, c in enumerate(count):
if c > 0:
print("wrong label ", i, ", nr: ", c)
seq_accum = collections.OrderedDict(
sorted(seq_accum.items(), key=lambda t: t[0]))
# print and send to total
total += seq_total
print("seq ", seqstr, "total", seq_total)
for key, data in seq_accum.items():
accum[key] += data
print(data)
# print content to fill yaml file
print("*" * 80)
print("Content in training set")
print(accum)
accum = collections.OrderedDict(sorted(accum.items(), key=lambda t: t[0]))
for key, data in accum.items():
print(" {}: {}".format(key, data / total))