-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_datasets.py
136 lines (101 loc) · 3.87 KB
/
custom_datasets.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
from torchvision import datasets, transforms
from torch.utils.data import Dataset, DataLoader
import torchvision
import numpy
import pandas as pd
import os
from utils import *
def simple_data_transformer():
transform = transforms.ToTensor()
def imagenet_transformer():
transform=transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def cifar10_transformer():
return torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor(),
])
def mnist_transformer():
return torchvision.transforms.Compose([
# torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor()
])
class Ring(Dataset):
def __init__(self, path, transform=None, return_idx = True, testset=False):
if not testset:
self.ring = pd.read_csv(os.path.join(path, "simple_data/ring.csv"))
else:
self.ring = pd.read_csv(os.path.join(path, "simple_data/ring_test.csv"))
self.transform = transform
self.return_idx = return_idx
self.testset = testset
def __len__(self):
return len(self.ring)
def __getitem__(self, index):
if isinstance(index, numpy.float64):
index = index.astype(numpy.int64)
data = self.ring.iloc[index, 1:].to_numpy()
target = self.ring.iloc[index, 0]
data = data.astype(numpy.float32)
target = int(target)
if self.transform:
data = self.transform(data)
if self.return_idx:
return data, target, index
else:
return data, target
class MNIST(Dataset):
def __init__(self, path):
self.mnist = datasets.MNIST(root=path,
download=True,
train=True,
transform=mnist_transformer())
def __getitem__(self, index):
if isinstance(index, numpy.float64):
index = index.astype(numpy.int64)
data, target = self.mnist[index]
return data, target, index
def __len__(self):
return len(self.mnist)
class CIFAR10(Dataset):
def __init__(self, path):
self.cifar10 = datasets.CIFAR10(root=path,
download=True,
train=True,
transform=cifar10_transformer())
def __getitem__(self, index):
if isinstance(index, numpy.float64):
index = index.astype(numpy.int64)
data, target = self.cifar10[index]
return data, target, index
def __len__(self):
return len(self.cifar10)
class CIFAR100(Dataset):
def __init__(self, path):
self.cifar100 = datasets.CIFAR100(root=path,
download=True,
train=True,
transform=cifar10_transformer())
def __getitem__(self, index):
if isinstance(index, numpy.float64):
index = index.astype(numpy.int64)
data, target = self.cifar100[index]
# Your transformations here (or set it in CIFAR10)
return data, target, index
def __len__(self):
return len(self.cifar100)
class ImageNet(Dataset):
def __init__(self, path):
self.imagenet = datasets.ImageFolder(root=path, transform=imagenet_transformer)
def __getitem__(self, index):
if isinstance(index, numpy.float64):
index = index.astype(numpy.int64)
data, target = self.imagenet[index]
return data, target, index
def __len__(self):
return len(self.imagenet)