-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_deep_ensemble.py
180 lines (133 loc) · 4.92 KB
/
train_deep_ensemble.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
import argparse
import pathlib
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
from utils.datasets import all_datasets
from utils.cnn_duq import SoftmaxModel as CNN
from torchvision.models import resnet18
class ResNet(nn.Module):
def __init__(self, input_size, num_classes):
super().__init__()
self.resnet = resnet18(pretrained=False, num_classes=num_classes)
# Adapted resnet from:
# https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py
self.resnet.conv1 = nn.Conv2d(
3, 64, kernel_size=3, stride=1, padding=1, bias=False
)
self.resnet.maxpool = nn.Identity()
def forward(self, x):
x = self.resnet(x)
x = F.log_softmax(x, dim=1)
return x
def train(model, train_loader, optimizer, epoch, loss_fn):
model.train()
total_loss = []
for batch_idx, (data, target) in enumerate(tqdm(train_loader)):
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
prediction = model(data)
loss = loss_fn(prediction, target)
loss.backward()
optimizer.step()
total_loss.append(loss.item())
avg_loss = torch.tensor(total_loss).mean()
print(f"Epoch: {epoch}:")
print(f"Train Set: Average Loss: {avg_loss:.2f}")
def test(models, test_loader, loss_fn):
models.eval()
loss = 0
correct = 0
for data, target in test_loader:
with torch.no_grad():
data = data.cuda()
target = target.cuda()
losses = torch.empty(len(models), data.shape[0])
predictions = []
for i, model in enumerate(models):
predictions.append(model(data))
losses[i, :] = loss_fn(predictions[i], target, reduction="sum")
predictions = torch.stack(predictions)
loss += torch.mean(losses)
avg_prediction = predictions.exp().mean(0)
# get the index of the max log-probability
class_prediction = avg_prediction.max(1)[1]
correct += (
class_prediction.eq(target.view_as(class_prediction)).sum().item()
)
loss /= len(test_loader.dataset)
percentage_correct = 100.0 * correct / len(test_loader.dataset)
print(
"Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)".format(
loss, correct, len(test_loader.dataset), percentage_correct
)
)
return loss, percentage_correct
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--epochs", type=int, default=75, help="number of epochs to train (default: 75)"
)
parser.add_argument(
"--lr", type=float, default=0.05, help="learning rate (default: 0.05)"
)
parser.add_argument(
"--ensemble", type=int, default=5, help="Ensemble size (default: 5)"
)
parser.add_argument(
"--dataset",
required=True,
choices=["FashionMNIST", "CIFAR10"],
help="Select a dataset",
)
parser.add_argument("--seed", type=int, default=1, help="random seed (default: 1)")
args = parser.parse_args()
print(args)
torch.manual_seed(args.seed)
loss_fn = F.nll_loss
ds = all_datasets[args.dataset]()
input_size, num_classes, train_dataset, test_dataset = ds
kwargs = {"num_workers": 4, "pin_memory": True}
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=128, shuffle=True, **kwargs
)
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=5000, shuffle=False, **kwargs
)
if args.dataset == "FashionMNIST":
milestones = [10, 20]
ensemble = [CNN(input_size, num_classes).cuda() for _ in range(args.ensemble)]
else:
# CIFAR-10
milestones = [25, 50]
ensemble = [
ResNet(input_size, num_classes).cuda() for _ in range(args.ensemble)
]
ensemble = torch.nn.ModuleList(ensemble)
optimizers = []
schedulers = []
for model in ensemble:
# Need different optimisers to apply weight decay and momentum properly
# when only optimising one element of the ensemble
optimizers.append(
torch.optim.SGD(
model.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4
)
)
schedulers.append(
torch.optim.lr_scheduler.MultiStepLR(
optimizers[-1], milestones=milestones, gamma=0.1
)
)
for epoch in range(1, args.epochs + 1):
for i, model in enumerate(ensemble):
train(model, train_loader, optimizers[i], epoch, loss_fn)
schedulers[i].step()
test(ensemble, test_loader, loss_fn)
pathlib.Path("saved_models").mkdir(exist_ok=True)
path = f"saved_models/{args.dataset}_{len(ensemble)}"
torch.save(ensemble.state_dict(), path + "_ensemble.pt")
if __name__ == "__main__":
main()