-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
193 lines (168 loc) · 6.84 KB
/
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
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
186
187
188
189
190
191
192
193
import os
import torch
import pandas as pd
from skimage import io
from skimage import color
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from PIL import Image
import warnings
import numpy as np
warnings.filterwarnings("ignore")
class ImageNette_train(Dataset):
#mapping = {"n01440764":0, "n02102040":217, "n02979186":482, "n03000684":491, "n03028079":497, "n03394916":566, "n03417042":569, "n03425413":571, "n03445777":574, "n03888257":701}
"""
0: tench
217: English springer
482: cassette player
491: chain saw
497: church
566: French horn
569: garbage truck
571: gas pump
574: golf ball
701: parachute
"""
mapping = {"n01440764": 0, "n02102040": 1, "n02979186": 2, "n03000684": 3, "n03028079": 4, "n03394916": 5,"n03417042": 6, "n03425413": 7, "n03445777": 8, "n03888257": 9}
def __init__(self, root_dir=None, csv_file="noisy_imagenette.csv"):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.datas = pd.read_csv(os.path.join(root_dir, csv_file))
self.root_dir = root_dir
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.RandomRotation(30),
transforms.RandomHorizontalFlip(),
transforms.GaussianBlur(kernel_size=5),
transforms.ColorJitter(),
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
def __len__(self):
return len(self.datas)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.datas.iloc[idx, 0])
image = io.imread(img_name)
if len(image.shape) != 3:
image = color.gray2rgb(image)
label = self.datas.iloc[idx, 1]
label = self.mapping[label]
label = torch.tensor(label)
image = self.transform(image.copy())
sample = (image, label)
return sample
class ImageNette_test(Dataset):
#mapping = {"n01440764":0, "n02102040":217, "n02979186":482, "n03000684":491, "n03028079":497, "n03394916":566, "n03417042":569, "n03425413":571, "n03445777":574, "n03888257":701}
mapping = {"n01440764": 0, "n02102040": 1, "n02979186": 2, "n03000684": 3, "n03028079": 4, "n03394916": 5, "n03417042": 6, "n03425413": 7, "n03445777": 8, "n03888257": 9}
def __init__(self, root_dir=None, csv_file="test.csv"):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.datas = pd.read_csv(os.path.join(root_dir, csv_file))
self.root_dir = root_dir
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
def __len__(self):
return len(self.datas)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.datas.iloc[idx, 0])
image = io.imread(img_name)
if len(image.shape) != 3:
image = color.gray2rgb(image)
label = self.datas.iloc[idx, 1]
label = self.mapping[label]
label = torch.tensor(label)
image = self.transform(image.copy())
sample = (image, label)
return sample
class CUB_train(Dataset):
def __init__(self, root_dir=None, csv_file="noisy_imagenette.csv"):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.datas = pd.read_csv(os.path.join(root_dir, csv_file))
self.root_dir = root_dir
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize((600, 600), Image.BILINEAR),
transforms.RandomCrop((448, 448)),
transforms.RandomHorizontalFlip(),
])
def __len__(self):
return len(self.datas)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.datas.iloc[idx, 1])
image = io.imread(img_name)
seg_name = os.path.join(self.root_dir, self.datas.iloc[idx, 3])
seg = io.imread(seg_name)[:,:,None]
label = self.datas.iloc[idx, 2]
label = torch.tensor(label)
image = np.concatenate([image, seg], axis=2)
image = self.transform(image.copy())
seg = image[3, :,:]
image = image[:3,:,:]
image = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(image)
sample = (image, seg, label)
return sample
class CUB_test(Dataset):
def __init__(self, root_dir=None, csv_file="noisy_imagenette.csv"):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.datas = pd.read_csv(os.path.join(root_dir, csv_file))
self.root_dir = root_dir
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize((600, 600), Image.BILINEAR),
transforms.CenterCrop((448, 448)),
])
def __len__(self):
return len(self.datas)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.datas.iloc[idx, 1])
image = io.imread(img_name)
seg_name = os.path.join(self.root_dir, self.datas.iloc[idx, 3])
seg = io.imread(seg_name)[:,:,None]
label = self.datas.iloc[idx, 2]
label = torch.tensor(label)
image = np.concatenate([image, seg], axis=2)
image = self.transform(image.copy())
seg = image[3, :,:]
image = image[:3,:,:]
image = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(image)
sample = (image, seg, label)
return sample