forked from ditwoo/pytorch-delg-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_local_features.py
171 lines (137 loc) · 5.22 KB
/
train_local_features.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
import os
from pathlib import Path
import albumentations as albu
import albumentations.pytorch
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from utils import t2d, seed_all, ImagesDataset
from models import EfficientNetEncoder, EncoderLocalFeatures
torch.autograd.set_detect_anomaly(False)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
DATA_DIR = "imagewoof2-320"
def main():
if not torch.cuda.is_available():
raise ValueError("No CUDA available")
device = torch.device("cuda:0")
has_multiple_devices = torch.cuda.device_count() > 1
train_batch_size = 128
valid_batch_size = 128
n_epochs = 35
# loaders
train_transforms = albu.Compose(
[
albu.Resize(256, 256),
albu.RandomCrop(224, 224),
albu.HorizontalFlip(),
albu.Normalize(),
albu.pytorch.ToTensorV2(),
]
)
valid_transforms = albu.Compose(
[albu.Resize(224, 224), albu.Normalize(), albu.pytorch.ToTensorV2()]
)
classes = sorted(set(os.listdir(os.path.join(DATA_DIR, "train"))))
if set(classes) != set(os.listdir(os.path.join(DATA_DIR, "val"))):
raise ValueError("Different classes in train & valid folders!")
class2index = {item: num for num, item in enumerate(classes)}
def _images_and_classes(folder):
files, targets = [], []
for file in folder.glob("*/*.JPEG"):
image_folder = str(file).split("/")[-2]
files.append(str(file))
targets.append(class2index[image_folder])
return files, targets
train_dataset = ImagesDataset(
*_images_and_classes(Path(DATA_DIR) / "train"), transforms=train_transforms
)
train_loader = DataLoader(
train_dataset,
batch_size=train_batch_size,
shuffle=True,
num_workers=os.cpu_count(),
drop_last=True,
)
valid_dataset = ImagesDataset(
*_images_and_classes(Path(DATA_DIR) / "val"), transforms=valid_transforms
)
valid_loader = DataLoader(
valid_dataset,
batch_size=valid_batch_size,
shuffle=False,
num_workers=os.cpu_count(),
drop_last=False,
)
encoder = EfficientNetEncoder("efficientnet-b1")
encoder.load_state_dict(
torch.load("encoder.pth", map_location="cpu")["encoder_state_dict"]
)
model = EncoderLocalFeatures(encoder, 10)
for param in model.encoder.parameters():
param.requires_grad = False
model = model.to(device)
if has_multiple_devices:
model = nn.DataParallel(model)
optimizer = optim.SGD(model.parameters(), lr=1e-3, momentum=0.9, weight_decay=1e-5)
reconstruction_criterion = nn.MSELoss()
attention_criterion = nn.CrossEntropyLoss()
for epoch in range(1, n_epochs + 1):
model.train()
train_loss = 0.0
train_attention_loss = 0.0
train_reconstruction_loss = 0.0
for idx, batch in enumerate(train_loader):
x, y = t2d(batch, device)
# with autograd.detect_anomaly():
emb, rec, cls, _ = model(x)
attention_loss = attention_criterion(cls, y)
train_attention_loss += attention_loss.item()
reconstruction_loss = reconstruction_criterion(emb, rec)
train_reconstruction_loss += reconstruction_loss.item()
loss = attention_loss + reconstruction_loss
train_loss += loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss /= idx + 1
train_attention_loss /= idx + 1
train_reconstruction_loss /= idx + 1
model.eval()
valid_loss = 0.0
valid_attention_loss = 0.0
valid_reconstruction_loss = 0.0
accuracy = 0.0
with torch.no_grad():
for batch in valid_loader:
x, y = t2d(batch, device)
emb, rec, cls, _ = model(x)
attention_loss = attention_criterion(cls, y)
valid_attention_loss += attention_loss.item()
reconstruction_loss = reconstruction_criterion(emb, rec)
valid_reconstruction_loss += reconstruction_loss.item()
loss = attention_loss + reconstruction_loss
valid_loss += loss.item()
acc = (torch.argmax(cls, -1) == y).sum().detach().item()
acc /= y.size(0)
accuracy += acc
valid_loss /= idx + 1
valid_attention_loss /= idx + 1
valid_reconstruction_loss /= idx + 1
accuracy /= idx + 1
print(f"Epoch {epoch}/{n_epochs}")
print(
"train - {:.5f} (attention - {:.5f}, reconstruction - {:.5f})".format(
train_loss, train_attention_loss, train_reconstruction_loss
)
)
print(
"valid - {:.5f} (attention - {:.5f}, reconstruction - {:.5f}), accuracy - {:.5f}".format(
valid_loss, valid_attention_loss, valid_reconstruction_loss, accuracy
)
)
torch.save({"model_state_dict": model.module.state_dict()}, "local_features.pth")
if __name__ == "__main__":
main()