forked from themightyoarfish/deepVO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_data.py
executable file
·119 lines (91 loc) · 3.79 KB
/
preprocess_data.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
#!/usr/bin/env python
'''
.. module:: preprocess_data
Contains functions for normalizing and converting the raw data.
.. moduleauthor:: Rasmus Diederichsen
'''
from argparse import ArgumentParser
import numpy as np
import sys
from utils import compute_rgb_mean
from data_manager import DataManager
def main():
parser = ArgumentParser('Preprocess robot data vor DeepVO, This process is destructive.')
parser.add_argument('-d', '--data', type=str, required=True,
help='Path to dataset (a folder with "images" and "poses" subfolders.)')
parser.add_argument('-f', '--to-float', required=False, default=False,
action='store_true', help='Convert images array to float')
parser.add_argument('-m', '--mean-normalize', required=False, default=False,
action='store_true', help='Subtract rgb mean from images')
parser.add_argument('-s', '--show', required=False, default=False,
action='store_true', help='Show the images')
parser.add_argument('-p', '--pose', required=False, default=False,
action='store_true', help='Add pi to poses (for range 0-2pi)')
parser.add_argument('-sp', '--subpi', required=False, default=False,
action='store_true', help='Add pi to poses (for range -pi - +pi)')
args = parser.parse_args()
data_manager = DataManager(args.data, dtype=np.float32, batch_size=1, sequence_length=1)
if args.to_float:
to_float(data_manager)
if args.mean_normalize:
mean_normalize(data_manager)
if args.show:
show_imgs(data_manager)
if args.pose:
add_pi_to_poses(data_manager)
if args.subpi:
sub_pi_from_poses(data_manager)
def show_imgs(data_manager):
from matplotlib import pyplot as plt
N = len(data_manager)
for idx in range(N):
img = data_manager.loadImage(idx)
minimum, maximum = img.min(), img.max()
print(f'Range: ({minimum}, {maximum})')
plt.imshow((img - minimum) / (maximum - minimum))
plt.show()
def to_float(data_manager):
'''Convert dataset to range (0, 1)'''
N = len(data_manager)
print(f'Converting {data_manager.dataset_path}images/*.npy fo float ...')
for idx in range(N):
# print progress
if idx % 10 == 0:
print(f'\r{idx+1:4d}/{N}', end='')
img = data_manager.loadImage(idx) / 255.
data_manager.saveImage(idx, img.astype(np.float32))
print('\nDone')
def mean_normalize(data_manager):
'''Normalize data to the range -1 to 1'''
assert data_manager.dtype == np.float32
N = len(data_manager)
print(f'Mean-normalizing {data_manager.dataset_path}/images/*.npy ...')
mean_accumlator = np.zeros((3,), dtype=np.float32)
# run over entire dataset to compute mean (fucking inefficient but I have other shit to do)
for idx in range(N):
img = data_manager.loadImage(idx)
mean_accumlator += compute_rgb_mean(img)
mean_accumlator /= N
print(f'Mean: {mean_accumlator}')
for idx in range(N):
if idx % 10 == 0:
print(f'\r{idx+1:4d}/{N}', end='')
img = data_manager.loadImage(idx)
data_manager.saveImage(idx, (img - mean_accumlator))
print('\nDone')
def add_pi_to_poses(data_manager):
'''Add Pi to every pose angle.'''
N = len(data_manager)
for idx in range(N):
pose = data_manager.loadPose(idx)
pose[..., 3:6] = pose[..., 3:6] + np.pi
data_manager.savePose(idx, pose)
def sub_pi_from_poses(data_manager):
'''Subtract Pi from every pose angle.'''
N = len(data_manager)
for idx in range(N):
pose = data_manager.loadPose(idx)
pose[..., 3:6] = pose[..., 3:6] - np.pi
data_manager.savePose(idx, pose)
if __name__ == '__main__':
main()