-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_cifar.py
66 lines (48 loc) · 1.88 KB
/
train_cifar.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
# coding: utf-8
from sklearn import metrics
import torch
from tqdm import tqdm
from dataloader import get_cifar10_dataloader
from ffmodel import FFClassifier
from utils import AverageMeter, create_pos_data, create_neg_data, create_test_data
torch.manual_seed(2999)
def main() -> None:
# Settings
num_epochs = 80
batch_size = 64
# DataLoader
train_dataloader = get_cifar10_dataloader(_mode="train", batch_size=batch_size)
val_dataloader = get_cifar10_dataloader(_mode="val", batch_size=1)
# Device
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# FFModel
model = FFClassifier([32*32*3, 2000, 2000, 2000, 2000], device=device)
torch.compile(model)
# Loss Logger
loss_logger = AverageMeter()
# Train
for epoch in range(1, num_epochs+1):
model.train()
pbar = tqdm(train_dataloader, desc=f"Train - Epoch [{epoch}/{num_epochs}] Loss: {loss_logger.avg:.4f}")
loss_logger.reset()
for inputs, labels in pbar:
pos_inputs = create_pos_data(inputs, labels).to(device)
neg_inputs = create_neg_data(inputs, labels).to(device)
loss = model(pos_inputs=pos_inputs, neg_inputs=neg_inputs)
loss_logger.update(loss, inputs.shape[0])
pbar.set_description(f"Train - Epoch [{epoch}/{num_epochs}] Loss: {loss_logger.avg:.4f}")
torch.save(model, f"./models/epoch{epoch}.ckpt")
# Validation
model.eval()
# Evaluation
predicts = []
targets = []
for inputs, labels in tqdm(val_dataloader):
inputs = create_test_data(inputs).to(device)
predict = model.predict(inputs)
predicts.append(predict.item())
targets.append(labels.item())
print(metrics.classification_report(targets, predicts))
print()
if __name__ == "__main__":
main()