-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvit_train.py
More file actions
134 lines (103 loc) · 3.9 KB
/
vit_train.py
File metadata and controls
134 lines (103 loc) · 3.9 KB
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
import os
from PIL import Image
import torch
from torchvision import datasets, transforms
from utils.dataset import MNISTImageDataset
from utils.models import ViT
import torch.nn.functional as F
from tqdm import tqdm
from utils.backdoor import attack
backdoor = False
dataset_dir = "./dataset/MNIST"
wtype = 'hidden'
model_save_path = f"./models/vit_mnist_{"backdoor" if backdoor else "clean"}_{wtype}.pth"
batch_size = 128
lr = 3e-4
num_epochs = 10
img_width = 28
img_channels = 1
num_classes = 10
patch_size = 7
embedding_dim = 64
ff_dim = 2048
num_heads = 8
num_layers = 3
weight_decay = 1e-4
transform = transforms.Compose([
transforms.ToTensor(),
])
train_backdoor_imgs = []
train_backdoor_labels = []
test_backdoor_imgs = []
test_backdoor_labels = []
if backdoor:
backdoor_img_list, backdoor_label_list = attack(dataset_dir, transform, wtype=wtype)
train_backdoor_imgs = backdoor_img_list[:500]
train_backdoor_labels = backdoor_label_list[:500]
test_backdoor_imgs = backdoor_img_list[500:]
test_backdoor_labels = backdoor_label_list[500:]
backdoor_test_dataset = MNISTImageDataset(root_dir=os.path.join(dataset_dir, "test"),
transform=transform, backdoor_img_list=test_backdoor_imgs,
backdoor_label_list=test_backdoor_labels, backdoor_only=True)
backdoor_test_loader = torch.utils.data.DataLoader(backdoor_test_dataset, batch_size=batch_size, shuffle=False)
train_dataset = MNISTImageDataset(root_dir=os.path.join(
dataset_dir, "train"), transform=transform, backdoor_img_list=train_backdoor_imgs,
backdoor_label_list=train_backdoor_labels)
test_dataset = MNISTImageDataset(root_dir=os.path.join(
dataset_dir, "test"), transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(f"{device=}")
model = ViT(
img_width=img_width,
img_channels=img_channels,
patch_size=patch_size,
d_model=embedding_dim,
num_heads=num_heads,
num_layers=num_layers,
num_classes=num_classes,
ff_dim=ff_dim,
).to(device)
print(model)
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
for epoch in range(num_epochs):
losses = []
total_train = 0
correct_train = 0
model.train()
for img, label in tqdm(train_loader, desc=f"epoch-{epoch}"):
img = img.to(device)
label = label.to(device)
pred = model(img)
loss = F.cross_entropy(pred, label)
pred_class = torch.argmax(pred, dim=1)
correct_train += (pred_class == label).sum().item()
total_train += pred.shape[0]
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.append(loss.item())
print(f"epoch-{epoch}: train loss:", sum(losses))
print(f"epoch-{epoch}: train acc:", correct_train / total_train)
model.eval()
total = 0
correct = 0
with torch.no_grad():
for img, label in test_loader:
img = img.to(device)
pred = torch.argmax(model(img), dim=1).cpu()
correct += (pred == label).sum().item()
total += pred.shape[0]
print(f"epoch-{epoch}: test acc:", correct / total)
backdoor_total = 0
backdoor_correct = 0
if backdoor:
with torch.no_grad():
for img, label in backdoor_test_loader:
img = img.to(device)
pred = torch.argmax(model(img), dim=1).cpu()
backdoor_correct += (pred == label).sum().item()
backdoor_total += pred.shape[0]
print(f"epoch-{epoch}: backdoor test acc:", backdoor_correct / backdoor_total)
torch.save(model.state_dict(), model_save_path)