-
Notifications
You must be signed in to change notification settings - Fork 4
/
kitti_dataloader.py
185 lines (139 loc) · 6.4 KB
/
kitti_dataloader.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
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import division
import tensorflow as tf
import numpy as np
import scipy
from sklearn.feature_extraction import image
import os
import random
class Dataloader(object):
def __init__(self,params):
self.__params=params
self.__contents = os.listdir(self.__params.disp_path)
self.__contents.sort()
self.__training_samples=len(self.__contents)
self.__sample_index=0
self.epoch=0
self.maxwidth=0
self.maxheight=0
self.configure_input_size()
self.__widthresize =self.maxwidth+ (self.__params.down_sample_ratio - self.maxwidth%self.__params.down_sample_ratio)%self.__params.down_sample_ratio
self.__heightresize =self.maxheight+( self.__params.down_sample_ratio - self.maxheight%self.__params.down_sample_ratio)%self.__params.down_sample_ratio
self.max_disp=256
self.__val_num=40
def get_data_size(self):
return self.__heightresize,self.__widthresize,2
def get_sample_index(self):
return self.__sample_index
def init_sample_index(self,val):
self.__sample_index=val
def get_sample_size(self):
return self.__training_samples
def get_training_data_size(self):
return 256,256,2
def configure_input_size(self):
for i in range(len(self.__contents)):
img = scipy.misc.imread( self.__params.left_path+self.__contents[i]).astype(float);
s = img.shape
if self.maxheight < s[0]:
self.maxheight = s[0]
if self.maxwidth < s[1]:
self.maxwidth = s[1]
def load_training_sample(self):
if self.__sample_index >= self.__training_samples-self.__val_num:
self.__sample_index=0
self.epoch+=1
img = scipy.misc.imread( self.__params.left_path+self.__contents[self.__sample_index]).astype(float);
disp = scipy.misc.imread( self.__params.disp_path+self.__contents[self.__sample_index]).astype(float)/256;
gt = scipy.misc.imread( self.__params.gt_path+self.__contents[self.__sample_index]).astype(float)/256;
gt_noc = scipy.misc.imread( self.__params.gt_path_noc+self.__contents[self.__sample_index]).astype(float)/256;
height,width = img.shape
s = img.shape
maxheight = s[0]-256
maxwidth = s[1]-256
x = random.randint(0,maxheight)
y = random.randint(0,maxwidth)
disp = disp[x:x+256,y:y+256]
img = img[x:x+256,y:y+256]
gt = gt[x:x+256,y:y+256]
gt_noc = gt_noc[x:x+256,y:y+256]
data = np.stack([disp,img],axis=2)
data = np.reshape(data,[1,data.shape[0],data.shape[1],data.shape[2]])
gt = np.reshape(gt,[1,gt.shape[0],gt.shape[1],1])
gt_noc = np.reshape(gt_noc,[1,gt_noc.shape[0],gt_noc.shape[1],1])
self.__sample_index+=1
return data,gt,gt_noc,self.__sample_index
def load_validation_sample(self):
if self.__sample_index >= self.__training_samples:
self.__sample_index=self.__training_samples-40
self.epoch+=1
img = scipy.misc.imread( self.__params.left_path+self.__contents[self.__sample_index]).astype(float);
disp = scipy.misc.imread( self.__params.disp_path+self.__contents[self.__sample_index]).astype(float)/256;
gt = scipy.misc.imread( self.__params.gt_path+self.__contents[self.__sample_index]).astype(float)/256;
gt_noc = scipy.misc.imread( self.__params.gt_path_noc+self.__contents[self.__sample_index]).astype(float)/256;
s = img.shape
if s[0] <self.__heightresize:
padding= self.__heightresize - s[0]
img = np.lib.pad(img,[(padding,0),(0,0)],'edge')
disp = np.lib.pad(disp,[(padding,0),(0,0)],'edge')
gt = np.lib.pad(gt,[(padding,0),(0,0)],'edge')
gt_noc = np.lib.pad(gt_noc,[(padding,0),(0,0)],'edge')
if s[1] <self.__widthresize:
padding= self.__widthresize-s[1]
img = np.lib.pad(img,[(0,0),(padding,0)],'edge')
disp = np.lib.pad(disp,[(0,0),(padding,0)],'edge')
gt = np.lib.pad(gt,[(0,0),(padding,0)],'edge')
gt_noc = np.lib.pad(gt_noc,[(0,0),(padding,0)],'edge')
data = np.stack([disp,img],axis=2)
data = np.reshape(data,[1,data.shape[0],data.shape[1],data.shape[2]])
gt = np.reshape(gt,[1,gt.shape[0],gt.shape[1],1])
gt_noc = np.reshape(gt_noc,[1,gt_noc.shape[0],gt_noc.shape[1],1])
self.__sample_index+=1
return data,gt,gt_noc,self.__sample_index
def load_verify_sample(self):
if self.__sample_index >= self.__training_samples:
self.__sample_index=0
self.epoch+=1
img = scipy.misc.imread( self.__params.left_path+self.__contents[self.__sample_index]).astype(float);
disp = scipy.misc.imread( self.__params.disp_path+self.__contents[self.__sample_index]).astype(float)/256;
gt = scipy.misc.imread( self.__params.gt_path+self.__contents[self.__sample_index]).astype(float)/256;
gt_noc = scipy.misc.imread( self.__params.gt_path_noc+self.__contents[self.__sample_index]).astype(float)/256;
s = img.shape
height,width= img.shape;
if s[0] <self.__heightresize:
padding= self.__heightresize - s[0]
img = np.lib.pad(img,[(padding,0),(0,0)],'edge')
disp = np.lib.pad(disp,[(padding,0),(0,0)],'edge')
gt = np.lib.pad(gt,[(padding,0),(0,0)],'edge')
gt_noc = np.lib.pad(gt_noc,[(padding,0),(0,0)],'edge')
if s[1] <self.__widthresize:
padding= self.__widthresize-s[1]
img = np.lib.pad(img,[(0,0),(padding,0)],'edge')
disp = np.lib.pad(disp,[(0,0),(padding,0)],'edge')
gt = np.lib.pad(gt,[(0,0),(padding,0)],'edge')
gt_noc = np.lib.pad(gt_noc,[(0,0),(padding,0)],'edge')
data = np.stack([disp,img],axis=2)
data = np.reshape(data,[1,data.shape[0],data.shape[1],data.shape[2]])
gt = np.reshape(gt,[1,gt.shape[0],gt.shape[1],1])
gt_noc = np.reshape(gt_noc,[1,gt_noc.shape[0],gt_noc.shape[1],1])
self.__sample_index+=1
return data,gt,gt_noc,self.__sample_index,height,width,self.__contents[self.__sample_index-1]
def load_test_sample(self):
if self.__sample_index >= self.__training_samples:
self.__sample_index=0
img = scipy.misc.imread( self.__params.left_path+self.__contents[self.__sample_index]).astype(float);
disp = scipy.misc.imread( self.__params.disp_path+self.__contents[self.__sample_index]).astype(float)/256;
height,width = img.shape
s = img.shape
if s[0] <self.__heightresize:
padding= self.__heightresize - s[0]
img = np.lib.pad(img,[(padding,0),(0,0)],'edge')
disp = np.lib.pad(disp,[(padding,0),(0,0)],'edge')
if s[1] <self.__widthresize:
padding= self.__widthresize-s[1]
img = np.lib.pad(img,[(0,0),(padding,0)],'edge')
disp = np.lib.pad(disp,[(0,0),(padding,0)],'edge')
data = np.stack([disp,img],axis=2)
data = np.reshape(data,[1,data.shape[0],data.shape[1],data.shape[2]])
name = self.__contents[self.__sample_index]
self.__sample_index+=1
return data,self.__sample_index,height,width,name