-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
223 lines (198 loc) · 6.32 KB
/
train.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
"""This module contains functions to train and test models using CIFAR10."""
from typing import Union
import numpy as np
import torch
import torch.nn as nn
from PIL import Image
from sklearn.base import BaseEstimator
from sklearn.random_projection import GaussianRandomProjection
from sklearnex.decomposition import PCA
from torch.utils.data.dataloader import DataLoader
from torchvision import transforms
from torchvision.datasets import CIFAR10
from tqdm import tqdm
from model.metric import compute_knn, compute_lp, compute_tsne
from model.ndr import AE, DAE, VAE, SimCLR
class _AECIFAR10(CIFAR10):
def __getitem__(self, index: int) -> torch.Tensor:
img = self.data[index]
img = Image.fromarray(img)
return self.transform(img)
class _SimCLRCIFAR10(CIFAR10):
def __getitem__(self, index: int) -> tuple[torch.Tensor, torch.Tensor]:
img = self.data[index]
img = Image.fromarray(img)
x_i = self.transform(img)
x_j = self.transform(img)
return x_i, x_j
def _get_transform(is_simclr: bool) -> transforms.Compose:
return (
transforms.Compose(
[
# Randomly resize and crop to 32x32.
transforms.RandomResizedCrop(32),
# Horizontally flip the image with probability 0.5
transforms.RandomHorizontalFlip(p=0.5),
# With a probability of 0.8, apply color jitter
transforms.RandomApply(
[transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)], p=0.8
),
# With a probability of 0.2, convert the image to grayscale
transforms.RandomGrayscale(p=0.2),
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
),
]
)
if is_simclr
else transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
),
]
)
)
def _get_loader(
batch_size: int,
is_train: bool,
model: str = "",
) -> DataLoader:
return DataLoader(
{
**dict.fromkeys(["", "rp", "pca"], CIFAR10),
**dict.fromkeys(["ae", "dae", "vae"], _AECIFAR10),
"simclr": _SimCLRCIFAR10,
}[model](
root="./data",
train=is_train,
download=True,
transform=_get_transform(model == "simclr"),
),
batch_size=batch_size,
shuffle=is_train,
)
def _get_model(
model: str,
n_components: int,
hidden_dim: int,
sigma: float,
beta: float,
) -> Union[BaseEstimator, nn.Module]:
return {
"rp": lambda: GaussianRandomProjection(n_components),
"pca": lambda: PCA(n_components),
"ae": lambda: AE(n_components, hidden_dim),
"dae": lambda: DAE(n_components, hidden_dim, sigma),
"vae": lambda: VAE(n_components, hidden_dim, beta),
"simclr": lambda: SimCLR(n_components, hidden_dim),
}[model]()
def _train_model(
net: nn.Module,
trainloader: DataLoader,
n_epochs: int,
):
net.cuda().train()
opt = torch.optim.AdamW(net.parameters())
for _ in range(n_epochs):
with tqdm(trainloader) as t:
for x in t:
loss = net.criterion(
x.cuda()
if isinstance(x, torch.Tensor)
else tuple(_.cuda() for _ in x)
)
opt.zero_grad()
loss.backward()
opt.step()
t.set_description(f"E:{_+1}/{n_epochs}|L:{loss.item():.2f}")
@torch.no_grad()
def _get_features(
net: Union[BaseEstimator, nn.Module],
loader: DataLoader,
) -> tuple[np.ndarray, np.ndarray]:
if isinstance(net, nn.Module):
net.cuda().eval()
z_list, y_list = [], []
for x, y in tqdm(loader):
if isinstance(net, nn.Module):
z = net(x.cuda()).cpu().numpy()
else:
x = torch.flatten(x, start_dim=1).numpy()
z = net.transform(x)
z_list.append(z)
y_list.append(y)
return np.concatenate(z_list), np.concatenate(y_list)
def _test_model(
net: Union[BaseEstimator, nn.Module],
trainloader: DataLoader,
testloader: DataLoader,
) -> tuple[float, float, np.ndarray]:
z_tr, y_tr = _get_features(net, trainloader)
z_te, y_te = _get_features(net, testloader)
return (
compute_lp(z_tr, y_tr, z_te, y_te),
compute_knn(z_tr, y_tr, z_te, y_te),
compute_tsne(z_te, y_te),
)
def train(
model: str,
n_components: int = 128,
hidden_dim: int = 128,
batch_size: int = 512,
n_epochs: int = 20,
sigma: float = 0.1,
beta: float = 1e-3,
) -> tuple[float, float, np.ndarray]:
"""Performs training and testing of specified model.
Parameters
----------
model
Model to be trained. Supports Random Projection (rp),
Principle Component Analysis (pca), Autoencoder (ae),
Denosing Autoencoder (dae), Variantional Autoencoder (vae) and
Contrastive Learning (simclr).
n_components
Dimensionality reduction feature dimension.
hidden_dim
Number of hidden channels for model.
batch_size
Batch size for training and testing.
n_epochs
Number of epochs for training.
sigma
Noise standard deviation for Denosing Autoencoder.
beta
Beta value for Variantional Autoencoder.
Returns
-------
tuple[float, float, np.ndarray]
Linear Probe accuracy, Nearest Neighbor accuracy, t-SNE embeddings.
"""
trainloader = _get_loader(
batch_size,
is_train=True,
model=model,
)
net = _get_model(
model,
n_components,
hidden_dim,
sigma=sigma,
beta=beta,
)
(
_train_model(net, trainloader, n_epochs)
if isinstance(net, nn.Module)
else net.fit(trainloader.dataset.data.reshape(-1, 3072))
)
return _test_model(
net,
_get_loader(batch_size, is_train=True),
_get_loader(batch_size, is_train=False),
)
if __name__ == "__main__":
lp, knn, _ = train("ae", n_components=128, n_epochs=10)
print(f"lp-acc: {lp} knn-acc: {knn}")