-
Notifications
You must be signed in to change notification settings - Fork 14
/
DataLoader_DIW.py
166 lines (132 loc) · 5.74 KB
/
DataLoader_DIW.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
import os
import h5py
import math
import random
import torch
import csv
from common.NYU_params import *
from DataPointer import DataPointer
from torchvision import transforms
from PIL import Image
from math import floor
class DataLoader(object):
"""docstring for DataLoader"""
def __init__(self, relative_depth_filename):
super(DataLoader, self).__init__()
print(">>>>>>>>>>>>>>>>> Using DataLoader")
self.parse_depth(relative_depth_filename)
self.data_ptr_relative_depth = DataPointer(self.n_relative_depth_sample)
print("DataLoader init: \n \t{} relative depth samples \n ".format(self.n_relative_depth_sample))
def parse_DIW_csv(self, _filename):
f = open(_filename, 'r')
csv_file_handle = list(csv.reader(f))
_n_lines = len(csv_file_handle)
_handle = {}
_sample_idx = 0
_line_idx = 0
while _line_idx < _n_lines:
_handle[_sample_idx] = {}
_handle[_sample_idx]['img_filename'] = csv_file_handle[_line_idx][0]
_handle[_sample_idx]['n_point'] = 1
_handle[_sample_idx]['img_filename_line_idx'] = _line_idx
_line_idx += _handle[_sample_idx]['n_point']
_line_idx += 1
_sample_idx += 1
_handle['csv_file_handle'] = csv_file_handle
print("{}: number of sample = {}".format(_filename, _sample_idx))
return _handle
def parse_one_coordinate_line(self, csv_file_handle, _line_idx):
orig_img_width = float(csv_file_handle[_line_idx][5])
orig_img_height = float(csv_file_handle[_line_idx][6])
y_A_float_orig = (float(csv_file_handle[_line_idx][0])-1)/orig_img_height
x_A_float_orig = (float(csv_file_handle[_line_idx][1])-1)/orig_img_width
y_B_float_orig = (float(csv_file_handle[_line_idx][2])-1)/orig_img_height
x_B_float_orig = (float(csv_file_handle[_line_idx][3])-1)/orig_img_width
y_A = min(g_input_height-1, max(0, floor(y_A_float_orig * g_input_height )))
x_A = min(g_input_width -1, max(0, floor(x_A_float_orig * g_input_width )))
y_B = min(g_input_height-1, max(0, floor(y_B_float_orig * g_input_height )))
x_B = min(g_input_width -1, max(0, floor(x_B_float_orig * g_input_width )))
if (y_A == y_B) and (x_A == x_B):#check this
if y_A_float_orig > y_B_float_orig:
y_A+=1
elif y_A_float_orig > y_B_float_orig:
y_A-=1
if x_A_float_orig > x_B_float_orig:
x_A+=1
elif x_A_float_orig < x_B_float_orig:
x_B-=1
ordi = csv_file_handle[_line_idx][4][0]
if ordi == '>':
ordi = 1
elif ordi == '<':
ordi = -1
elif ordi == '=':
print('Error in _read_one_sample()! The ordinal_relationship should never be = !!!!')
assert(False)
else:
print(ordi)
print('Error in _read_one_sample()! The ordinal_relationship does not read correctly!!!!')
assert(False)
# print("Original:{}, {}, {}, {}".format(int(csv_file_handle[_line_idx][0])-1, int(csv_file_handle[_line_idx][1])-1, int(csv_file_handle[_line_idx][2])-1, int(csv_file_handle[_line_idx][3])-1))
# print("Size : height:{}, width{}".format(orig_img_height, orig_img_width))
# print("Float :{}, {}, {}, {}".format(y_A_float_orig, x_A_float_orig, y_B_float_orig, x_B_float_orig))
# print("Scaled: {}, {}, {}, {}".format(y_A, x_A, y_B, x_B, ordi))
# print("relationship: {}".format(ordi))
return y_A, x_A, y_B, x_B, ordi
def parse_depth(self, relative_depth_filename):
if relative_depth_filename is not None:
self.relative_depth_handle = self.parse_DIW_csv(relative_depth_filename)
else:
self.relative_depth_handle = {}
self.n_relative_depth_sample = len(self.relative_depth_handle)-1
def close(self):
pass
def mixed_sample_strategy1(self, batch_size):
n_depth = random.randint(0, batch_size-1)
return n_depth, batch_size - n_depth
def mixed_sample_strategy2(self, batch_size):
n_depth = floor(batch_size/2)
return n_depth, batch_size - n_depth
def load_indices(self, depth_indices):
if depth_indices is not None:
n_depth = len(depth_indices)
else:
n_depth = 0
batch_size = n_depth
color = torch.Tensor(batch_size, 3, g_input_height, g_input_width)
_batch_target_relative_depth_gpu = {}
_batch_target_relative_depth_gpu['n_sample'] = n_depth
loader = transforms.Compose([
transforms.Scale((g_input_width, g_input_height)),
transforms.ToTensor(),
# transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)) # may not need this
])
csv_file_handle = self.relative_depth_handle['csv_file_handle']
for i in range(0,n_depth):
chosen_idx = depth_indices[i]
_batch_target_relative_depth_gpu[i] = {}
img_name = self.relative_depth_handle[chosen_idx]['img_filename']
n_point = self.relative_depth_handle[chosen_idx]['n_point']
img = Image.open(img_name)
img = loader(img).float()
if img.size()[0] == 1:
print(img_name, ' is gray')
color[i,0,:,:].copy_(img)
color[i,1,:,:].copy_(img)
color[i,2,:,:].copy_(img)
else:
color[i,:,:,:].copy_(img)
_line_idx = self.relative_depth_handle[chosen_idx]['img_filename_line_idx']+1
y_A, x_A, y_B, x_B, ordi = self.parse_one_coordinate_line(csv_file_handle, _line_idx)
_batch_target_relative_depth_gpu[i]['y_A'] = torch.autograd.Variable(torch.Tensor([y_A])).cuda()
_batch_target_relative_depth_gpu[i]['x_A'] = torch.autograd.Variable(torch.Tensor([x_A])).cuda()
_batch_target_relative_depth_gpu[i]['y_B'] = torch.autograd.Variable(torch.Tensor([y_B])).cuda()
_batch_target_relative_depth_gpu[i]['x_B'] = torch.autograd.Variable(torch.Tensor([x_B])).cuda()
_batch_target_relative_depth_gpu[i]['ordianl_relation'] = torch.autograd.Variable(torch.Tensor([ordi])).cuda()
_batch_target_relative_depth_gpu[i]['n_point'] = n_point
return torch.autograd.Variable(color.cuda()), _batch_target_relative_depth_gpu
def load_next_batch(self, batch_size):
depth_indices = self.data_ptr_relative_depth.load_next_batch(batch_size)
return self.load_indices(depth_indices)
def reset(self):
self.current_pos = 1