-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
185 lines (150 loc) · 6.06 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import h5py
import torch
from torch.utils.data import DataLoader,Dataset
import numpy as np
from torchvision import transforms
import cv2
import matplotlib.pyplot as plt
import os
from PIL import Image
def normalize(image):
image_data = [[0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]]
image_data = np.array(image_data)
img_copy = torch.zeros(image.shape)
for i in range(3):
img_copy[ i, :, :] = (image[ i, :, :] - image_data[0, i])/image_data[1,i]
return img_copy
transform_train = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
transform_test = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
transform_CIFAR100 = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize((0.53561753,0.48983628,0.42546818), (0.26656017,0.26091456,0.27394977))
])
transform_CIFAR10C_train = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4645897160947712,0.6514782475490196,0.5637088950163399), (0.18422159112571024, 0.3151505122530825, 0.26127269383599344))
])
transform_CIFAR10C_test = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
transforms.Normalize((0.4645897160947712,0.6514782475490196,0.5637088950163399), (0.18422159112571024, 0.3151505122530825, 0.26127269383599344))
])
class dataset(Dataset):
def __init__(self, name,train=False):
super(dataset, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
label = torch.tensor(self.labels[item])
image = np.array(self.images[item, :, :, :]*255,dtype='uint8')
image = transform_train(image)
return [image,label]
class dataset1(Dataset):
def __init__(self, name,train=False):
super(dataset1, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
label = torch.tensor(self.labels[item])
image = np.array(self.images[item, :, :, :]*255,dtype='uint8')
image = transform_test(image)
return [image,label]
class dataset3(Dataset):
def __init__(self, name,train=False):
super(dataset3, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
label = torch.tensor(self.labels[item])
image = np.array(self.images[item, :, :, :]*255,dtype='uint8')
image = transform_CIFAR100(image)
return [image,label]
class dataset4(Dataset):
def __init__(self, name,train=False):
super(dataset4, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
if train == True:
self.transform = transform_CIFAR10C_train
elif train ==False:
self.transform = transform_CIFAR10C_test
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
label = torch.tensor(self.labels[item])
image = np.array(self.images[item, :, :, :]*255,dtype='uint8')
image = self.transform(image)
return [image,label]
class dataset5(Dataset):
def __init__(self, name,train=False):
super(dataset5, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
image = (np.squeeze(self.images[item,:,:,:]))
image = np.transpose(image,[2,0,1])
image = torch.tensor(image)
image = normalize(image)
label = torch.tensor(self.labels[item])
return [image,label]
class dataset6(Dataset):
def __init__(self, name,train=False):
super(dataset6, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
image = (np.squeeze(self.images[item,:,:,:]))
image = torch.tensor(image)
image = normalize(image)
label = torch.tensor(self.labels[item])
return [image,label]
class dataset7(Dataset):
def __init__(self, name,train=False):
super(dataset7, self).__init__()
self.name = name
self.data = h5py.File(os.path.join("data",name), 'r')
self.images = np.array(self.data['/data'])
self.labels = np.array(self.data['/label'])
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, item):
image = (np.squeeze(self.images[item,:,:,:]))
image = torch.tensor(image)
label = torch.tensor(self.labels[item])
return [image,label]