-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdataset.py
130 lines (100 loc) · 3.38 KB
/
dataset.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
# -*- coding:utf-8 -*-
# Created Time: 2018/05/10 17:22:38
# Author: Taihong Xiao <xiaotaihong@126.com>
import os
import scipy.ndimage as nd
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
class Config:
@property
def data_dir(self):
# data_dir = '/home/xiaoth/datasets/ModelNet/volumetric_data'
data_dir = '/gpfs/share/home/1501210096/datasets/ModelNet/volumetric_data'
if not os.path.exists(data_dir):
os.makedirs(data_dir)
return data_dir
@property
def exp_dir(self):
exp_dir = os.path.join('train_log')
if not os.path.exists(exp_dir):
os.makedirs(exp_dir)
return exp_dir
@property
def model_dir(self):
model_dir = os.path.join(self.exp_dir, 'model')
if not os.path.exists(model_dir):
os.makedirs(model_dir)
return model_dir
@property
def log_dir(self):
log_dir = os.path.join(self.exp_dir, 'log')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return log_dir
@property
def img_dir(self):
img_dir = os.path.join(self.exp_dir, 'img')
if not os.path.exists(img_dir):
os.makedirs(img_dir)
return img_dir
nchw = [32,64,64,64]
G_lr = 2.5e-3
D_lr = 1e-5
step_size = 2000
gamma = 0.95
shuffle = True
num_workers = 0
max_iter = 20000
config = Config()
class Single(Dataset):
def __init__(self, filenames, config):
self.filenames = filenames
self.config = config
def __len__(self):
return len(self.filenames)
def __getitem__(self, idx):
voxel = sio.loadmat(self.filenames[idx])['instance']
voxel = np.pad(voxel, (1,1), 'constant', constant_values=(0,0))
if self.config.nchw[-1] != 32:
ratio = self.config.nchw[-1] / 32.
voxel = nd.zoom(voxel, (ratio, ratio, ratio), mode='constant', order=0)
return np.expand_dims(voxel.astype(np.float32), 0)
def gen(self):
dataloader = DataLoader(self, batch_size=self.config.nchw[0], shuffle=self.config.shuffle, num_workers=self.config.num_workers, drop_last=True)
while True:
for data in dataloader:
yield data
class ShapeNet(object):
def __init__(self, category, config=config):
self.category = category
self.config = config
self.dict = {True: None, False: None}
for is_train in [True, False]:
prefix = os.path.join(self.config.data_dir, category, '30')
data_dir = prefix + '/train' if is_train else prefix + '/test'
filenames = [os.path.join(data_dir, name) for name in os.listdir(data_dir) if name.endswith('.mat')]
self.dict[is_train] = Single(filenames, self.config).gen()
def gen(self, is_train):
data_gen = self.dict[is_train]
return data_gen
def test():
dataset = ShapeNet('chair')
import cProfile
pr = cProfile.Profile()
pr.enable()
for i in range(10):
if 1 % 2 == 0:
voxel = next(dataset.gen(True))
else:
voxel = next(dataset.gen(False))
print(i)
print(voxel.shape)
pr.disable()
pr.print_stats()
if __name__ == "__main__":
test()