-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader2d.py
164 lines (138 loc) · 4.16 KB
/
data_loader2d.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
import torch
import torch.utils.data as torchdata
import torchvision.transforms as transforms
import cv2
import hydra
import numpy as np
import os
import visdom
from utils.sunrgbd_utils import read_sunrgbd_label
import scipy.io as sio
import PIL.Image as pil
seg_list37 = {
1: 'wall',
2: 'floor',
3:'cabinet',
4: 'bed',
5: 'chair',
6:'sofa',
7: 'table',
8: 'door',
9: 'window',
10: 'bookshelf',
11:'picture',
12:'counter',
13:'blinds',
14:'desk',
15:'shelves',
16:'curtain',
17:'dresser',
18:'pillow',
19:'mirror',
20:'floor_mat',
21:'clothes',
22:'ceiling',
23:'books',
24:'fridge',
25:'tv',
26:'paper',
27:'towel',
28:'shower_curtain',
29:'box',
30:'whiteboard',
31:'person',
32:'night_stand',
33:'toilet',
34:'sink',
35:'lamp',
36:'bathtub',
37:'bag',
}
seg_list13 = {
1: 'Bed',
2:'Books',
3 :'Ceiling',
4 :'Chair',
5 :'Floor',
6:'Furniture',
7:'Objects',
8 :'Picture',
9 :'Sofa',
10 :'Table',
11 :'TV',
12 :'Wall',
13 :'Window',
}
class SunrgbdImageDataset(torchdata.Dataset):
"""
TODO: Depth Image not used currently
"""
def __init__(self, raw_datapath='/home/neil/disk/sunrgbd_trainval',
split_set='train',
class_name='lamp'):
super(SunrgbdImageDataset, self).__init__()
self.raw_datapath = raw_datapath
self.class_name = class_name
self.class_idx = None
i=1
while(i<=37):
if seg_list37[i]==self.class_name:
self.class_idx = i
break
i += 1
self.use_v1 = False
self.image_path = os.path.join(self.raw_datapath, 'image')
self.depth_path = os.path.join(self.raw_datapath, 'depth')
self.image_names = open(os.path.join(self.raw_datapath, 'data_list', split_set,'{}.txt'.format(class_name))).read().splitlines()
# self.transform = transforms.Compose(
# transforms.Resize(size=(512,512)),
# transforms.ToTensor()
# )
def augmentaion(self, image, is_label=False):
"""
TODO: add more augmentation methods
"""
if is_label:
# to image
toimg = transforms.ToPILImage()
image = toimg(image)
# gray
trans_gray = transforms.Grayscale()
image = trans_gray(image)
# resize
resize =transforms.Resize(size=(512,512))
image = resize(image)
# To tenor
totensor = transforms.ToTensor()
image = totensor(image)
return image
def __len__(self):
return len(self.image_names)
def __getitem__(self, index):
image_name = '{:06d}'.format(int(self.image_names[index]))
image_path = os.path.join(self.image_path, image_name+'.jpg')
depth_path = os.path.join(self.depth_path, image_name+'.mat')
# RGB image
image = pil.open(image_path)
image = self.augmentaion(image)
# Depth image (to be used)
depth_image = sio.loadmat(depth_path)['instance']
depth_image = torch.tensor(depth_image)
objects = read_sunrgbd_label(os.path.join(self.raw_datapath, 'label_v1' if self.use_v1 else 'label', '{}.txt'.format(image_name)))
objects = [obj for obj in objects if obj.classname == self.class_name]
bbox2d = [ obj.box2d for obj in objects] # (xmin, ymin, xmax, ymax)
# segmentation label
seg_label = sio.loadmat(os.path.join(self.raw_datapath, 'seg_label', image_name+'.mat'))['instance']
seg_label = torch.tensor(seg_label)
mask = seg_label==self.class_idx
seg_label[:] = 0
seg_label[mask] = 1
seg_label = self.augmentaion(image, is_label=True)
return {'image_name':image_name,'image':image, 'seg_label':seg_label, 'bbox2d':bbox2d, 'depth_image':depth_image}
def show_image(self, image):
#server = visdom.Visdom(env='Sun RGBD Image')
cv2.imwrite("./demo/image.jpg", image)
if __name__=='__main__':
dataset = SunrgbdImageDataset(split_set='train')
#print(dataset.image_names)
print(dataset[6]['image'])