forked from JiarunLiu/Co-Correcting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicTrainer.py
259 lines (230 loc) · 12.7 KB
/
BasicTrainer.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
import os
import copy
import json
import datetime
import numpy as np
from os.path import join
import torch
import torchvision
from dataset.cifar import CIFAR10, CIFAR100
from dataset.mnist import MNIST
from dataset.ISIC import ISIC
from dataset.clothing1m import Clothing1M
from dataset.PatchCamelyon import PatchCamelyon
from models.densenet import densenet121, densenet161, densenet169, densenet201
from models.resnet import resnet18, resnet34, resnet50, resnet101, resnet152
from models.preact_resnet import PreActResNet18, PreActResNet34, PreActResNet50, PreActResNet101, PreActResNet152
from models.coteaching_model import MLPNet, CNN_small, CNN
class BasicTrainer(object):
def __init__(self, args):
self._get_args(args)
if self.args.random_seed is not None:
torch.manual_seed(self.args.random_seed)
def _save_meta(self):
# save meta data
print(vars(self.args))
nowTime = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
with open(join(self.args.dir, "settings-{}.json".format(nowTime)), 'w') as f:
json.dump(vars(self.args), f, indent=4, sort_keys=True)
def _get_args(self, args):
self.args = args
# addition args
self.args.checkpoint_dir = join(self.args.dir, "checkpoint.pth.tar")
self.args.modelbest_dir = join(self.args.dir, "model_best.pth.tar")
self.args.record_dir = join(self.args.dir, 'record.json')
self.args.y_file = join(self.args.dir, "y.npy")
self.best_prec1 = 0
def _get_model(self, backbone):
if backbone == 'resnet18':
model = resnet18(pretrained=True, num_classes=self.args.classnum).to(self.args.device)
elif backbone == 'resnet34':
model = resnet34(pretrained=True, num_classes=self.args.classnum).to(self.args.device)
elif backbone == 'resnet50':
model = resnet50(pretrained=True, num_classes=self.args.classnum).to(self.args.device)
elif backbone == 'resnet101':
model = resnet101(pretrained=True, num_classes=self.args.classnum).to(self.args.device)
elif backbone == 'resnet152':
model = resnet152(pretrained=True, num_classes=self.args.classnum).to(self.args.device)
elif backbone == 'preact_resnet18':
model = PreActResNet18(num_classes=self.args.classnum, input_size=self.args.image_size,
input_dim=self.args.input_dim).to(self.args.device)
elif backbone == 'preact_resnet34':
model = PreActResNet34(num_classes=self.args.classnum, input_size=self.args.image_size,
input_dim=self.args.input_dim).to(self.args.device)
elif backbone == 'preact_resnet50':
model = PreActResNet50(num_classes=self.args.classnum, input_size=self.args.image_size,
input_dim=self.args.input_dim).to(self.args.device)
elif backbone == 'preact_resnet101':
model = PreActResNet101(num_classes=self.args.classnum, input_size=self.args.image_size,
input_dim=self.args.input_dim).to(self.args.device)
elif backbone == 'preact_resnet152':
model = PreActResNet152(num_classes=self.args.classnum, input_size=self.args.image_size,
input_dim=self.args.input_dim).to(self.args.device)
elif backbone == 'densenet121':
model = densenet121(num_classes=self.args.classnum, pretrained=True).to(self.args.device)
elif backbone == 'densenet161':
model = densenet161(num_classes=self.args.classnum, pretrained=True).to(self.args.device)
elif backbone == 'densenet169':
model = densenet169(num_classes=self.args.classnum, pretrained=True).to(self.args.device)
elif backbone == 'densenet201':
model = densenet201(num_classes=self.args.classnum, pretrained=True).to(self.args.device)
elif backbone == 'mlp':
model = MLPNet().to(self.args.device)
elif backbone == 'cnn_small' or backbone == "CNN_SMALL":
model = CNN_small(self.args.classnum).to(self.args.device)
elif backbone == "cnn" or backbone == "CNN":
model = CNN(n_outputs=self.args.classnum, input_channel=self.args.input_dim, linear_num=self.args.linear_num).to(self.args.device)
else:
print("No matched backbone. Using ResNet50...")
model = resnet50(pretrained=True, num_classes=self.args.classnum,
input_size=self.args.image_size).to(self.args.device)
return model
def _get_optim(self, parm, optim="SGD", scheduler=None, lr=None):
if optim == "SGD" or optim == "sgd":
optimizer = torch.optim.SGD(parm, lr=lr if lr else self.args.lr, momentum=self.args.momentum, weight_decay=self.args.weight_decay)
elif optim == "adam" or optim == "Adam" or optim == "ADAM":
optimizer = torch.optim.Adam(parm, lr=lr if lr else self.args.lr)
elif optim == "adamw" or optim == "AdamW":
optimizer = torch.optim.AdamW(parm, lr=lr if lr else self.args.lr)
elif optim == "RMSprop" or optim == "rmsprop":
optimizer = torch.optim.RMSprop(parm, lr=lr if lr else self.args.lr, momentum=self.args.momentum, weight_decay=self.args.weight_decay)
elif optim == "Adadelta":
optimizer = torch.optim.Adadelta(parm, lr=lr if lr else self.args.lr)
elif optim == "Adagrad":
optimizer = torch.optim.Adagrad(parm, lr=lr if lr else self.args.lr)
else:
NotImplementedError("No Such Optimizer Implemented: {}".format(optim))
return optimizer
def _get_dataset_isic(self):
transform = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(p=0.5),
torchvision.transforms.RandomVerticalFlip(p=0.5),
torchvision.transforms.RandomRotation(degrees=[-180, 180]),
torchvision.transforms.Resize(self.args.image_size),
torchvision.transforms.ToTensor(),
])
transform1 = torchvision.transforms.Compose([
torchvision.transforms.Resize(self.args.image_size),
torchvision.transforms.ToTensor(),
])
trainset = ISIC(root=self.args.root,
train=0,
transform=transform,
noise_type=self.args.noise_type,
noise_rate=self.args.noise,
device=self.args.data_device,
redux=self.args.train_redux,
image_size=self.args.image_size)
testset = ISIC(root=self.args.root,
train=1,
transform=transform1,
noise_type='clean',
noise_rate=self.args.noise,
device=self.args.data_device,
redux=self.args.test_redux,
image_size=self.args.image_size)
valset = ISIC(root=self.args.root,
train=2,
transform=transform1,
noise_type='clean',
noise_rate=self.args.noise,
device=self.args.data_device,
redux=self.args.val_redux,
image_size=self.args.image_size)
return trainset, testset, valset
def _get_dataset_pcam(self):
transform = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(p=0.5),
torchvision.transforms.RandomVerticalFlip(p=0.5),
torchvision.transforms.RandomRotation(degrees=[-90, 90]),
torchvision.transforms.ColorJitter(0.2, 0.75, 0.25, 0.04),
torchvision.transforms.Resize(self.args.image_size),
torchvision.transforms.ToTensor(),
])
transform1 = torchvision.transforms.Compose([
torchvision.transforms.Resize(self.args.image_size),
torchvision.transforms.ToTensor(),
])
trainset = PatchCamelyon(root=self.args.root,
train=0,
transform=transform,
noise_type=self.args.noise_type,
noise_rate=self.args.noise,
redux=self.args.train_redux,
random_ind_redux=self.args.random_ind_redux)
testset = PatchCamelyon(root=self.args.root,
train=1,
transform=transform1,
noise_type='clean',
noise_rate=0,
redux=self.args.test_redux,
random_ind_redux = self.args.random_ind_redux)
valset = PatchCamelyon(root=self.args.root,
train=2,
transform=transform1,
noise_type='clean',
noise_rate=0,
redux=self.args.val_redux,
random_ind_redux=self.args.random_ind_redux)
return trainset, testset, valset
def _get_dataset_mnist(self):
transform1 = torchvision.transforms.Compose([
torchvision.transforms.RandomPerspective(),
torchvision.transforms.ColorJitter(0.2, 0.75, 0.25, 0.04),
torchvision.transforms.ToTensor(),
])
transform = torchvision.transforms.ToTensor()
trainset = MNIST(root=self.args.root,
download=True,
train=0,
transform=transform1,
noise_type=self.args.noise_type,
noise_rate=self.args.noise,
redux=self.args.train_redux,
)
testset = MNIST(root=self.args.root,
download=True,
train=1,
transform=transform,
noise_type='clean',
noise_rate=0,
redux=self.args.test_redux,
full_test=self.args.full_test,
)
valset = MNIST(root=self.args.root,
download=True,
train=2,
transform=transform,
noise_type='clean',
noise_rate=0,
redux=self.args.val_redux,
)
return trainset, testset, valset
def _load_data(self):
if self.args.dataset == 'isic':
trainset, testset, valset = self._get_dataset_isic()
elif self.args.dataset == 'mnist':
trainset, testset, valset = self._get_dataset_mnist()
elif self.args.dataset == 'pcam':
trainset, testset, valset = self._get_dataset_pcam()
else:
NotImplementedError("Dataset [{}] Was Not Been Implemented".format(self.args.dataset))
trainloader = torch.utils.data.DataLoader(trainset, batch_size=self.args.batch_size,
shuffle=True, num_workers=self.args.workers,
pin_memory=True if self.args.data_device == 1 else False)
testloader = torch.utils.data.DataLoader(testset, batch_size=self.args.batch_size,
shuffle=False, num_workers=self.args.workers,
pin_memory=True if self.args.data_device == 1 else False)
valloader = torch.utils.data.DataLoader(valset, batch_size=self.args.batch_size,
shuffle=False, num_workers=self.args.workers,
pin_memory=True if self.args.data_device == 1 else False)
self.train_batch_num = len(trainloader)
self.test_batch_num = len(testloader)
self.val_batch_num = len(valloader)
self.train_data_num = len(trainset)
self.test_data_num = len(testset)
self.val_data_num = len(valset)
self.noise_or_not = trainset.noise_or_not
self.clean_labels = trainset.labels
print("Train num: {}\tTest num: {}\tVal num: {}".format(len(trainset), len(testset), len(valset)))
return trainloader, testloader, valloader