-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderDataset.py
82 lines (80 loc) · 3.14 KB
/
RenderDataset.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
import torch
from torch.utils.data import Dataset
import json
from torch import nn
from torch.nn import functional as F
class RenderDataset(Dataset):
def __init__(self,data_dir="datas/a.json",transform=None) -> None:
super().__init__()
with open(data_dir) as f:
self.datas = json.load(f)
self.transform = transform
def __getitem__(self, index):
para = torch.Tensor(self.datas[index]["point"] + self.datas[index]["dir"])
label = torch.Tensor(self.datas[index]["rgb"])
if self.transform is not None:
para = self.transform(para)
label = self.transform(label)
return para,label
def __len__(self)->int:
return len(self.datas)
class RenderDatasetSph(Dataset):
def __init__(self,data_dir="datas/sph_1.json",transform=None) -> None:
super().__init__()
with open(data_dir) as f:
self.datas = json.load(f)
self.transform = transform
def __getitem__(self, index):
para = torch.Tensor(self.datas[index]["point_sph"]+self.datas[index]["dir_sph"])
label = torch.Tensor(self.datas[index]["rgb"])
if self.transform is not None:
para = self.transform(para)
label = self.transform(label)
return para,label
def __len__(self)->int:
return len(self.datas)
class RenderDatasetB(Dataset):
def __init__(self,data_dir="datas/sph_1.json",transform=None) -> None:
super().__init__()
with open(data_dir) as f:
self.datas = json.load(f)
self.transform = transform
def __getitem__(self, index):
para = torch.Tensor(self.datas[index]["point_sph"]+self.datas[index]["dir_sph"])
label = torch.Tensor(self.datas[index]["rgb"])
if(torch.allclose(label, torch.tensor([0,0,1],dtype=torch.float32))):
label = torch.Tensor([0])
else:
label = torch.Tensor([1])
if self.transform is not None:
para = self.transform(para)
label = self.transform(label)
return para,label
def __len__(self)->int:
return len(self.datas)
class RenderDatasetM(Dataset):
def __init__(self,data_dir="datas/sph_1.json",transform=None) -> None:
super().__init__()
with open(data_dir) as f:
self.datas = json.load(f)
self.transform = transform
def __getitem__(self, index):
para = torch.Tensor(self.datas[index]["point_sph"]+self.datas[index]["dir_sph"])
label = torch.Tensor(self.datas[index]["rgb"])
hit_value = self.datas[index]["hit"]
hit = torch.tensor(hit_value, dtype=torch.float)
# dis = torch.Tensor(self.datas[index]["dis"])
if self.transform is not None:
para = self.transform(para)
label = self.transform(label)
hit = self.transform(hit)
return para,label,hit
def __len__(self)->int:
return len(self.datas)
## test
if __name__ == "__main__":
from torch.utils.data import DataLoader
dataset = RenderDataset()
dataset_loader = DataLoader(dataset,batch_size=4,shuffle=True)
for batch in dataset_loader:
para,label = batch