-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build_Dataset.py
164 lines (127 loc) · 6.28 KB
/
Build_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
import torch
import torch.nn as nn
import torch.utils.data as data
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as dataset
from autoaugment import CIFAR10Policy
from auto_augment import AutoAugment
import numpy as np
#=========================================================== dataset ==========================================
# autoaugment: https://github.com/DeepVoltaire/AutoAugment
# auto_augment: https://github.com/4uiiurz1/pytorch-auto-augment
#==============================
class Cutout(object):
def __init__(self, length=16):
self.length = length
def __call__(self, img):
h, w = img.size(1), img.size(2)
mask = np.ones((h, w), np.float32)
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1: y2, x1: x2] = 0.
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img *= mask
return img
def _data_transforms_cifar10(cutout_size, autoaugment=False):
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
if autoaugment:
# 2
# train_transform = transforms.Compose([
# transforms.RandomCrop(32, padding=4),
# transforms.RandomHorizontalFlip(),
# AutoAugment(),
# transforms.ToTensor(),
# transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
# ])
# 1
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
CIFAR10Policy(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
else:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
if cutout_size is not None:
train_transform.transforms.append(Cutout(cutout_size))
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
return train_transform, valid_transform
def build_search_cifar10(args, ratio=0.9,cutout_size=None, autoaugment=False, num_workers = 10):
#used for searching process, so valid_data "train=True"
train_transform, valid_transform = _data_transforms_cifar10(cutout_size,autoaugment)
train_data = dataset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
valid_data = dataset.CIFAR10(root=args.data, train=True, download=True, transform=valid_transform)
num_train = len(train_data)
assert num_train == len(valid_data)
indices = list(range(num_train))
split = int(np.floor(ratio * num_train))
np.random.shuffle(indices)
train_queue = torch.utils.data.DataLoader(
train_data, batch_size=args.search_train_batch_size,
sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
pin_memory=True, num_workers=num_workers)#16
valid_queue = torch.utils.data.DataLoader(
valid_data, batch_size=args.search_eval_batch_size,
sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[split:num_train]),
pin_memory=True, num_workers=num_workers)
return train_queue, valid_queue
def build_train_cifar10(args, cutout_size=None, autoaugment=False):
# used for training process, so valid_data "train=False"
train_transform, valid_transform = _data_transforms_cifar10(cutout_size, autoaugment)
train_data = dataset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
valid_data = dataset.CIFAR10(root=args.data, train=False, download=True, transform=valid_transform)
train_queue = torch.utils.data.DataLoader(
train_data, batch_size=args.train_batch_size, shuffle=True, pin_memory=True, num_workers=16)
valid_queue = torch.utils.data.DataLoader(
valid_data, batch_size=args.eval_batch_size, shuffle=False, pin_memory=True, num_workers=16)
return train_queue, valid_queue
def build_train_cifar100(args, cutout_size=None, autoaugment=False):
train_transform, valid_transform = _data_transforms_cifar10(cutout_size, autoaugment)
train_data = dataset.CIFAR100(root=args.data, train=True, download=True, transform=train_transform)
valid_data = dataset.CIFAR100(root=args.data, train=False, download=True, transform=valid_transform)
train_queue = torch.utils.data.DataLoader(
train_data, batch_size=args.train_batch_size, shuffle=True, pin_memory=True, num_workers=16)
valid_queue = torch.utils.data.DataLoader(
valid_data, batch_size=args.eval_batch_size, shuffle=False, pin_memory=True, num_workers=16)
return train_queue, valid_queue
# =========================================================== Optimizer_Loss ==========================================
def build_search_Optimizer_Loss(model, args, last_epoch=-1):
model.cuda()
train_criterion = nn.CrossEntropyLoss().cuda()
eval_criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(
model.parameters(),
args.search_lr_max,
momentum=args.search_momentum,
weight_decay=args.search_l2_reg,
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.search_epochs, args.search_lr_min, last_epoch)
return train_criterion, eval_criterion, optimizer, scheduler
def build_train_Optimizer_Loss(model, args, epoch=-1):
model.cuda()
train_criterion = nn.CrossEntropyLoss().cuda()
eval_criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(
model.parameters(),
args.lr_max,
momentum=args.momentum,
weight_decay=args.l2_reg,
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epochs, args.lr_min, epoch)
return train_criterion, eval_criterion, optimizer, scheduler