-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
119 lines (93 loc) · 3.75 KB
/
main.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
""" Adapting ImageNet-scale models to complex distribution shifts with self-learning
Run with:
❯ docker pull pytorch/pytorch
❯ DATADIR="/path/to/imagenetc"
❯ curl -s https://stes.io/gce.py > gce.py
❯ docker run --gpus 1 -v ${DATADIR}:/data/imagenetc:ro \
-v $(pwd):/app -w /app -u $(id -u) \
--tmpfs /.cache --tmpfs /.local \
-it pytorch/pytorch python gce.py
Reference:
Web: https://domainadaptation.org/selflearning/
Paper: https://arxiv.org/abs/2104.12928
---
Copyright 2021 Evgenia Rusak and Steffen Schneider
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
This software is not an Amazon product.
"""
import torch
from torchvision import models, datasets, transforms
def get_dataset_loader(valdir, batch_size, shuffle):
val_dataset = datasets.ImageFolder(valdir, transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
),
]))
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=batch_size, shuffle=shuffle
)
return val_loader
def gce(logits, target, q = 0.8):
""" Generalized cross entropy.
Reference: https://arxiv.org/abs/1805.07836
"""
probs = torch.nn.functional.softmax(logits, dim=1)
probs_with_correct_idx = probs.index_select(-1, target).diag()
loss = (1. - probs_with_correct_idx**q) / q
return loss.mean()
def adapt_batchnorm(model):
model.eval()
parameters = []
for module in model.modules():
if isinstance(module, torch.nn.BatchNorm2d):
parameters.extend(module.parameters())
module.train()
return parameters
# ---
def evaluate(
datadir = '/data/imagenetc/gaussian_blur/3',
num_epochs = 5,
batch_size = 96,
learning_rate = 0.75e-3,
gce_q = 0.8
):
model = models.resnet50(pretrained = True).cuda()
parameters = adapt_batchnorm(model)
val_loader = get_dataset_loader(
datadir,
batch_size = batch_size,
shuffle = True
)
optimizer = torch.optim.SGD(
model.parameters(), lr = learning_rate
)
num_correct, num_samples = 0., 0.
for epoch in range(num_epochs):
predictions = []
for images, targets in val_loader:
logits = model(images.cuda())
predictions = logits.argmax(dim = 1)
loss = gce(logits, predictions, q = gce_q)
optimizer.zero_grad()
loss.backward()
optimizer.step()
num_correct += (predictions.detach().cpu() == targets).float().sum()
num_samples += len(targets)
print(f"Correct: {num_correct:#5.0f}/{num_samples:#5.0f} ({100 * num_correct / num_samples:.2f} %)")
evaluate()