-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
85 lines (62 loc) · 2.24 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
import torch
import numpy as np
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
from utils import synthesize_data
class starDataset(Dataset):
"""Star Dataset with labels
Args:
n_samples {int} -- items in dataset (default: 50000)
"""
def __init__(self, n_samples = 50000):
self.n_samples = n_samples
self.img = []
self.labels = []
for i in range(n_samples):
image, label = synthesize_data()
self.img.append(image)
self.labels.append(label)
def __len__(self):
return self.n_samples
def __getitem__(self, idx):
image, label = self.img[idx], self.labels[idx]
image = torch.tensor(image, dtype=torch.float32)
label = torch.tensor(label.reshape((label.shape[0])), dtype=torch.float32)
has_star = (~torch.isnan(label[0])).float().reshape(1)
label = torch.cat((has_star, label), dim = 0)
label[[1, 2]] /= 200.
label[3] /= 2*np.pi
label[[4,5]] /= 200.
return image[None], label
def datasetLoaders(trainSamples, valSamples, testSamples, batchSize):
"""Loads the train, val and test dataset as torch.utils.data.DataLoader
Args:
trainSamples (int): number of train samples
valSamples (int): number of validation samples
testSamples (int): number of test samples
batchSize (int): batch size for train, test and val dataloaders
Returns:
torch.utils.data.DataLoader: return train, val and test dataset objects
"""
trainDataset = starDataset(trainSamples)
trainLoader = DataLoader(
dataset=trainDataset,
batch_size = batchSize,
num_workers=8,
shuffle=True
)
valDataset = starDataset(valSamples)
valLoader = DataLoader(
dataset=valDataset,
batch_size = 1,
num_workers=8,
shuffle=True
)
testDataset = starDataset(testSamples)
testLoader = DataLoader(
dataset=testDataset,
batch_size = batchSize,
num_workers=8,
shuffle=True
)
return trainLoader, valLoader, testLoader