forked from ZhugeKongan/torch-template-for-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloder.py
277 lines (260 loc) · 15 KB
/
dataloder.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding:UTF-8 -*-
"""
dataset and data reading
"""
import os
# import glob
# import json
# import functools
import numpy as np
# import pandas as pd
# from osgeo import gdal
# import albumentations as albu
# from skimage.color import gray2rgb
# from matplotlib import pyplot as plt
# from sklearn.model_selection import train_test_split
#
#
# from utils.arg_utils import *
# from utils.data_utils import *
# from utils.algorithm_utils import *
from autoaug.augmentations import Augmentation
from autoaug.archive import fa_reduced_cifar10,autoaug_paper_cifar10,fa_reduced_imagenet
import autoaug.aug_transforms as aug
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import TensorDataset, DataLoader,Dataset
from dataset_loder.scoliosis_dataloder import ScoliosisDataset
from autoaug.cutout import Cutout
def training_transforms():
return transforms.Compose([
# transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],std=[0.2471, 0.2435, 0.2616]),
# Cutout()
#[125.3, 123.0, 113.9],[63.0, 62.1, 66.7]
])
def validation_transforms():
return transforms.Compose([
# transforms.RandomCrop(32, padding=4),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465],std=[0.2471, 0.2435, 0.2616]),
])
def load_dataset(data_config):
if data_config.dataset == 'cifar10':
training_transform=training_transforms()
if data_config.autoaug:
print('auto Augmentation the data !')
training_transform.transforms.insert(0, Augmentation(fa_reduced_cifar10()))
train_dataset = torchvision.datasets.CIFAR10(root=data_config.data_path,
train=True,
transform=training_transform,
download=True)
val_dataset = torchvision.datasets.CIFAR10(root=data_config.data_path,
train=False,
transform=validation_transforms(),
download=True)
return train_dataset,val_dataset
elif data_config.dataset == 'cifar100':
train_dataset = torchvision.datasets.CIFAR100(root=data_config.data_path,
train=True,
transform=training_transforms(),
download=True)
val_dataset = torchvision.datasets.CIFAR100(root=data_config.data_path,
train=False,
transform=validation_transforms(),
download=True)
return train_dataset, val_dataset
elif data_config.dataset == 'tiny_imagenet':
data_path='/disks/disk2/lishengyan/dataset/tiny-imagenet-200'
traindir = data_path + '/train'
valdir = data_path + '/val'
testdir=data_path + '/test'
normalize = transforms.Normalize(mean=[0.4802, 0.4481, 0.3975],
std=[0.2302, 0.2265, 0.2262])
train_dataset = torchvision.datasets.ImageFolder(traindir,
transforms.Compose([
# transforms.RandomResizedCrop(64),
# transforms.RandomCrop(64, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize ]))
val_dataset = torchvision.datasets.ImageFolder(testdir,
transforms.Compose([
# transforms.Resize(64),
# transforms.RandomResizedCrop(224),
transforms.ToTensor(),
normalize ]))
return train_dataset, val_dataset
elif data_config.dataset == 'imagenet':
traindir = data_config.data_path+'/ILSVRC/train'
valdir =data_config.data_path+'/ILSVRC/val'
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
jittering =aug.ColorJitter(brightness=0.4, contrast=0.4,
saturation=0.4)
lighting = aug.Lighting(alphastd=0.1,
eigval=[0.2175, 0.0188, 0.0045],
eigvec=[[-0.5675, 0.7192, 0.4009],
[-0.5808, -0.0045, -0.8140],
[-0.5836, -0.6948, 0.4203]])
train_dataset = torchvision.datasets.ImageFolder(traindir,
transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
jittering, lighting, normalize, ]))
val_dataset = torchvision.datasets.ImageFolder(valdir,
transforms.Compose([
transforms.Resize(256),
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
normalize, ]))
return train_dataset, val_dataset
elif data_config.dataset == 'scoliosis':
# traindir = data_config.data_path + '/train'
# valdir = data_config.data_path + '/test'
normalize = transforms.Normalize(mean=[0.64, 0.53, 0.43],
std=[0.20, 0.19, 0.19])
jittering = aug.ColorJitter(brightness=0.4, contrast=0.4,
saturation=0.4)
lighting = aug.Lighting(alphastd=0.1,
eigval=[0.2175, 0.0188, 0.0045],
eigvec=[[-0.5675, 0.7192, 0.4009],
[-0.5808, -0.0045, -0.8140],
[-0.5836, -0.6948, 0.4203]])
train_transforms = transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize])
test_transforms = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
normalize])
train_transforms.transforms.insert(0, Augmentation(autoaug_paper_cifar10()))#fa_reduced_cifar10,autoaug_paper_cifar10,fa_reduced_imagenet
train_dataset =ScoliosisDataset(data_config.data_path,
transform=train_transforms,#,jittering, lighting,transforms.RandomHorizontalFlip(),
train=True)
val_dataset = ScoliosisDataset(data_config.data_path,
target_transform=test_transforms,
train=False)
return train_dataset, val_dataset
elif data_config.dataset == 'SCUT-FBP5500':
data_path=data_config.data_path
trainfile = data_config.label_file + '/train.txt'
valfile = data_config.label_file + '/test.txt'
normalize = transforms.Normalize(mean=[0.22, 0.37, 0.73],
std=[1.61, 1.75, 1.80])
train_dataset =FacialAttractionDataset(data_path,trainfile,
transform=transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),normalize]),
)
val_dataset = FacialAttractionDataset(data_path,valfile ,
transform=transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),normalize]),
)
return train_dataset, val_dataset
elif data_config.dataset == 'sco_fa':
source_dir=data_config.source_dir
taget_dir=data_config.taget_dir
trainfile = data_config.label_file + '/train.txt'
valfile = data_config.label_file + '/test.txt'
source_normalize = transforms.Normalize(mean=[0.22, 0.37, 0.73],
std=[1.61, 1.75, 1.80])
target_normalize = transforms.Normalize(mean=[0.64, 0.53, 0.43],
std=[0.20, 0.19, 0.19])
source_transforms=transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
source_normalize])
target_transforms =transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
target_normalize])
# source_transforms.transforms.insert(0, Augmentation(autoaug_paper_cifar10()))
target_transforms.transforms.insert(0, Augmentation(autoaug_paper_cifar10()))
train_dataset =ScoandFaDataset(source_dir=source_dir,
taget_dir=taget_dir+'train',
label_file=trainfile,
source_transform=source_transforms,
target_transform=target_transforms
)
val_dataset = ScoandFaDataset(source_dir=source_dir,
taget_dir=taget_dir+'test',
label_file=valfile,
source_transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor(),source_normalize]),
target_transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor(),target_normalize]),
)
return train_dataset, val_dataset
elif data_config.dataset == 'scofa':
source_dir=data_config.source_dir
taget_dir=data_config.taget_dir
source_normalize = transforms.Normalize(mean=[0.22, 0.37, 0.73],
std=[1.61, 1.75, 1.80])
target_normalize = transforms.Normalize(mean=[0.64, 0.53, 0.43],
std=[0.20, 0.19, 0.19])
source_transforms=transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
source_normalize])
target_transforms =transforms.Compose([
transforms.Resize(224),
# transforms.RandomResizedCrop(224),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
target_normalize])
# source_transforms.transforms.insert(0, Augmentation(autoaug_paper_cifar10()))
target_transforms.transforms.insert(0, Augmentation(autoaug_paper_cifar10()))
train_dataset =ScoandFaNshotDataset(source_dir=source_dir+'train',
taget_dir=taget_dir+'train',
source_transform=source_transforms,
target_transform=target_transforms
)
val_dataset = ScoandFaNshotDataset(source_dir=source_dir+'test',
taget_dir=taget_dir+'test',
source_transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor(),source_normalize]),
target_transform=transforms.Compose([transforms.Resize(224),transforms.ToTensor(),target_normalize]),
)
return train_dataset, val_dataset
elif data_config.dataset == 'megaage_asian':
train_path = data_config.data_path+'train'
val_path = data_config.data_path+'test'
trainfile = data_config.label_file + 'train_age.txt'
valfile = data_config.label_file + 'test_age.txt'
normalize = transforms.Normalize(mean=[0.54, 0.47, 0.44],
std=[0.29, 0.28, 0.28])
train_dataset = MegaAsiaAgeDataset(train_path, trainfile,
transform=transforms.Compose([
# transforms.Resize(224),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize]),
)
val_dataset = MegaAsiaAgeDataset(val_path, valfile,
transform=transforms.Compose([
transforms.Resize(256),
transforms.RandomResizedCrop(224),
# transforms.Resize(224),
transforms.ToTensor(),
normalize]),
)
return train_dataset, val_dataset
else:
raise Exception('unknown dataset: {}'.format(data_config.dataset))