-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.py
249 lines (209 loc) · 10.8 KB
/
transfer.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
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from tqdm import tqdm
import os
import argparse
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from dataset.cinic10 import get_cinic10_dataloader
from dataset.cifar10 import get_cifar10_dataloaders
from models import *
def main():
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--name', default='', type=str,
help='experiment name')
parser.add_argument('--target_dataset', default='', type=str, choices=['cifar100', 'stl10', 'cinic10', 'svhn', 'tinyimagenet', 'emnist', 'fashion-mnist', 'cifar10'],
help='target dataset')
parser.add_argument('--source_dataset', default='cifar100', type=str, choices=['cifar100', 'stl10', 'cinic10', 'svhn', 'tinyimagenet', 'emnist', 'fashion-mnist', 'cifar10'],
help='source dataset')
parser.add_argument('--model', default='', type=str, choices=['wrn_28_4', 'ResNet18', 'ShuffleV2', 'resnext32_16x4d'],
help='model name')
parser.add_argument('--ckpt_path', default='', type=str,
help='the path to ckpts trained on source dataset')
parser.add_argument('--lr', default=4e-1, type=float,
help='learning rate')
parser.add_argument('--epoch', default=30, type=int,
help='number of epochs')
parser.add_argument('--batch', default=512, type=int,
help='batch size')
parser.add_argument('--wd', default=5e-4, type=float,
help='weight decay')
parser.add_argument('--start_epoch', default=10, type=int,
help='the start epoch model to be transferred')
parser.add_argument('--skip', default=10, type=int,
help='conduct transfer experiment each N epoch')
opt = parser.parse_args()
batch_size = opt.batch
if opt.target_dataset == 'cifar100':
train_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
test_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = torchvision.datasets.CIFAR100(root='./data', train=True,
download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR100(root='./data', train=False,
download=True, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
classes = 100
elif opt.target_dataset == 'stl10':
train_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
test_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = torchvision.datasets.STL10(root='./data', split='train',
download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.STL10(root='./data', split='test',
download=True, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
classes = 10
elif opt.target_dataset == 'cinic10':
trainloader, testloader = get_cinic10_dataloader(batch_size=batch_size,
num_workers=2,
is_instance=False,
augmentation='mine')
classes = 10
elif opt.target_dataset == 'cifar10':
trainloader, testloader = get_cifar10_dataloaders(batch_size=batch_size,
num_workers=2,
is_instance=False,
augmentation='mine')
classes = 10
elif opt.target_dataset == 'emnist':
train_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
test_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = torchvision.datasets.EMNIST(root='./data', split='byclass', train=True,
download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.EMNIST(root='./data', split='byclass', train=False,
download=True, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
classes = 10
elif opt.target_dataset == 'fashion-mnist':
train_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
test_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True,
download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.FashionMNIST(root='./data', train=False,
download=True, transform=test_transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
classes = 10
else:
raise NotImplementedError
root = './save/model/{}/'.format(opt.ckpt_path)
evaluated_epochs = list(range(501))[opt.start_epoch::opt.skip] # evaluate at most 500 epochs
ckpts = [root + 'ckpt_epoch_{}.pth'.format(epoch) for epoch in evaluated_epochs]
if opt.source_dataset == 'cifar100':
source_num_classes = 100
elif opt.source_dataset in ['stl10', 'cinic10', 'cifar10']:
source_num_classes = 10
else:
raise NotImplementedError
for j, ckpt in enumerate(ckpts):
net = eval(opt.model)(num_classes=source_num_classes)
try:
net.load_model(ckpt)
except:
break
if 'wrn' in opt.model:
net.fc = nn.Linear(net.fc.weight.shape[1], classes)
elif 'resnext' in opt.model:
net.classifier = nn.Linear(net.classifier.weight.shape[1], classes)
elif 'ResNet' in opt.model or 'resnet' in opt.model:
net.linear = nn.Linear(net.linear.weight.shape[1], classes)
elif 'Shuffle' in opt.model:
net.linear = nn.Linear(net.linear.weight.shape[1], classes)
else:
raise NotImplementedError
net.cuda()
net.train()
criterion = nn.CrossEntropyLoss()
trainable_params = []
for k, v in net.named_parameters():
if 'fc' in k or 'linear' in k or 'classifier' in k:
trainable_params.append(v)
optimizer = optim.SGD(trainable_params, lr=opt.lr, momentum=0.9, weight_decay=opt.wd)
scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=opt.epoch)
best_acc = 0.
for epoch in range(opt.epoch):
net.cuda()
net.train()
for i, data in tqdm(enumerate(trainloader, 0), total=len(trainloader), leave=False):
net.cuda()
if len(data) == 2:
inputs, labels = data
else:
inputs, labels, _ = data
inputs, labels = inputs.cuda(), labels.cuda()
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
correct = 0
total = 0
net.cuda()
net.eval()
with torch.no_grad():
for data in tqdm(testloader, leave=False):
if len(data) == 2:
images, labels = data
else:
images, labels, _ = data
images, labels = images.cuda(), labels.cuda()
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
acc = correct / total
if acc > best_acc:
best_acc = acc
scheduler.step()
print('Source Epoch: {}, Target Epoch: {}, Best Acc: {}'.format(j+1, epoch+1, best_acc))
print('Finished Training')
if __name__ == "__main__":
main()