-
Notifications
You must be signed in to change notification settings - Fork 10
/
dataset.py
168 lines (141 loc) · 6.56 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
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
import os
import math
import json
from pathlib import Path
import torch
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import pytorch_lightning as pl
from einops import rearrange
from PIL import Image
import numpy as np
import cv2
import random
import pickle
import webdataset as wds
from torch.utils.data.distributed import DistributedSampler
import matplotlib.pyplot as plt
import sys
class ObjaverseDataLoader():
def __init__(self, root_dir, batch_size, total_view=12, num_workers=4):
# super().__init__(self, root_dir, batch_size, total_view, num_workers)
self.root_dir = root_dir
self.batch_size = batch_size
self.num_workers = num_workers
self.total_view = total_view
image_transforms = [torchvision.transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])]
self.image_transforms = torchvision.transforms.Compose(image_transforms)
def train_dataloader(self):
dataset = ObjaverseData(root_dir=self.root_dir, total_view=self.total_view, validation=False,
image_transforms=self.image_transforms)
# sampler = DistributedSampler(dataset)
return wds.WebLoader(dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)
# sampler=sampler)
def val_dataloader(self):
dataset = ObjaverseData(root_dir=self.root_dir, total_view=self.total_view, validation=True,
image_transforms=self.image_transforms)
sampler = DistributedSampler(dataset)
return wds.WebLoader(dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)
class ObjaverseData(Dataset):
def __init__(self,
root_dir='.objaverse/hf-objaverse-v1/views',
image_transforms=None,
total_view=12,
validation=False
) -> None:
"""Create a dataset from a folder of images.
If you pass in a root directory it will be searched for images
ending in ext (ext can be a list)
"""
self.root_dir = root_dir
self.total_view = total_view
# todo only partial data currently downloaded
if os.path.exists(os.path.join(self.root_dir, 'valid_paths.json')):
with open(os.path.join(self.root_dir, 'valid_paths.json')) as f:
self.paths = json.load(f)
else:
self.paths = []
# include all folders
for folder in os.listdir(self.root_dir):
if os.path.isdir(os.path.join(self.root_dir, folder)):
self.paths.append(folder)
total_objects = len(self.paths)
if validation:
self.paths = self.paths[math.floor(total_objects / 100. * 99.):] # used last 1% as validation
else:
self.paths = self.paths[:math.floor(total_objects / 100. * 99.)] # used first 99% as training
print('============= length of dataset %d =============' % len(self.paths))
self.tform = image_transforms
def __len__(self):
return len(self.paths)
def cartesian_to_spherical(self, xyz):
ptsnew = np.hstack((xyz, np.zeros(xyz.shape)))
xy = xyz[:, 0] ** 2 + xyz[:, 1] ** 2
z = np.sqrt(xy + xyz[:, 2] ** 2)
theta = np.arctan2(np.sqrt(xy), xyz[:, 2]) # for elevation angle defined from Z-axis down
# ptsnew[:,4] = np.arctan2(xyz[:,2], np.sqrt(xy)) # for elevation angle defined from XY-plane up
azimuth = np.arctan2(xyz[:, 1], xyz[:, 0])
return np.array([theta, azimuth, z])
def get_T(self, target_RT, cond_RT):
R, T = target_RT[:3, :3], target_RT[:, -1]
T_target = -R.T @ T
R, T = cond_RT[:3, :3], cond_RT[:, -1]
T_cond = -R.T @ T
theta_cond, azimuth_cond, z_cond = self.cartesian_to_spherical(T_cond[None, :])
theta_target, azimuth_target, z_target = self.cartesian_to_spherical(T_target[None, :])
d_theta = theta_target - theta_cond
d_azimuth = (azimuth_target - azimuth_cond) % (2 * math.pi)
d_z = z_target - z_cond
d_T = torch.tensor([d_theta.item(), math.sin(d_azimuth.item()), math.cos(d_azimuth.item()), d_z.item()])
return d_T
def load_im(self, path, color):
'''
replace background pixel with random color in rendering
'''
try:
img = plt.imread(path)
except:
print(path)
sys.exit()
img[img[:, :, -1] == 0.] = color
img = Image.fromarray(np.uint8(img[:, :, :3] * 255.))
return img
def __getitem__(self, index):
data = {}
total_view = 12
index_target, index_cond = random.sample(range(total_view), 2) # without replacement
filename = os.path.join(self.root_dir, self.paths[index])
# print(self.paths[index])
color = [1., 1., 1., 1.]
try:
target_im = self.process_im(self.load_im(os.path.join(filename, '%03d.png' % index_target), color))
cond_im = self.process_im(self.load_im(os.path.join(filename, '%03d.png' % index_cond), color))
target_RT = np.load(os.path.join(filename, '%03d.npy' % index_target))
cond_RT = np.load(os.path.join(filename, '%03d.npy' % index_cond))
except:
# very hacky solution, sorry about this
filename = os.path.join(self.root_dir, '0a0c6d3b5f58499db8d6d649ba8de189') # this one we know is valid
target_im = self.process_im(self.load_im(os.path.join(filename, '%03d.png' % index_target), color))
cond_im = self.process_im(self.load_im(os.path.join(filename, '%03d.png' % index_cond), color))
target_RT = np.load(os.path.join(filename, '%03d.npy' % index_target))
cond_RT = np.load(os.path.join(filename, '%03d.npy' % index_cond))
target_im = torch.zeros_like(target_im)
cond_im = torch.zeros_like(cond_im)
data["image_target"] = target_im
data["image_cond"] = cond_im
data["T"] = self.get_T(target_RT, cond_RT)
return data
def process_im(self, im):
im = im.convert("RGB")
return self.tform(im)
# main
if __name__ == "__main__":
# test dataloader
dataloader = ObjaverseDataLoader(root_dir='/data/zero123/views_release', batch_size=2, num_workers=4)
train_loader = dataloader.train_dataloader()
import pdb; pdb.set_trace()
for i, data in enumerate(dataloader.train_dataloader()):
print(data)