-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_MNIST.py
164 lines (147 loc) · 5.11 KB
/
main_MNIST.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
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import *
from torchvision.transforms.transforms import *
from torchvision.transforms.functional import *
from tqdm import tqdm
from torchplus.utils import Init, ClassificationAccuracy
if __name__ == "__main__":
batch_size = 128
train_epoches = 10
log_epoch = 1
class_num = 10
root_dir = "./logZZPMAIN"
h = 32
w = 32
init = Init(
seed=9970,
log_root_dir=root_dir,
backup_filename=__file__,
tensorboard=True,
comment="main MNIST",
)
output_device = init.get_device()
writer = init.get_writer()
log_dir = init.get_log_dir()
data_workers = 2
transform = Compose(
[
Grayscale(num_output_channels=1),
Resize((h, w)),
RandomHorizontalFlip(),
RandomVerticalFlip(),
ToTensor(),
]
)
mnist_train_ds = MNIST(
root="./data", train=True, transform=transform, download=True
)
mnist_test_ds = MNIST(
root="./data", train=False, transform=transform, download=True
)
mnist_train_ds_len = len(mnist_train_ds)
mnist_test_ds_len = len(mnist_test_ds)
train_ds = mnist_train_ds
test_ds = mnist_test_ds
train_ds_len = len(train_ds)
test_ds_len = len(test_ds)
print(train_ds_len)
print(test_ds_len)
# for train
train_dl = DataLoader(
dataset=train_ds,
batch_size=batch_size,
shuffle=True,
num_workers=data_workers,
drop_last=True,
pin_memory=True,
)
# for evaluate
test_dl = DataLoader(
dataset=test_ds,
batch_size=batch_size,
shuffle=False,
num_workers=data_workers,
drop_last=False,
pin_memory=True,
)
class Classifier(nn.Module):
def __init__(self, out_features):
super(Classifier, self).__init__()
self.out_features = out_features
self.conv1 = nn.Conv2d(1, 128, 3, 1, 1)
self.conv2 = nn.Conv2d(128, 256, 3, 1, 1)
self.conv3 = nn.Conv2d(256, 512, 3, 1, 1)
self.bn1 = nn.BatchNorm2d(128)
self.bn2 = nn.BatchNorm2d(256)
self.bn3 = nn.BatchNorm2d(512)
self.mp1 = nn.MaxPool2d(2, 2)
self.mp2 = nn.MaxPool2d(2, 2)
self.mp3 = nn.MaxPool2d(2, 2)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.relu3 = nn.ReLU()
self.fc1 = nn.Linear(8192, 50)
self.dropout = nn.Dropout()
self.fc2 = nn.Linear(50, self.out_features)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.mp1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.mp2(x)
x = self.relu2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.mp3(x)
x = self.relu3(x)
x = x.view(-1, 8192)
x = self.fc1(x)
x = self.dropout(x)
x = self.fc2(x)
return x
myclassifier = Classifier(class_num).train(True).to(output_device)
optimizer = optim.Adam(
myclassifier.parameters(), lr=0.0002, betas=(0.5, 0.999), amsgrad=True
)
for epoch_id in tqdm(range(1, train_epoches + 1), desc="Total Epoch"):
for i, (im, label) in enumerate(tqdm(train_dl, desc=f"epoch {epoch_id}")):
im = im.to(output_device)
label = label.to(output_device)
bs, c, h, w = im.shape
optimizer.zero_grad()
out = myclassifier.forward(im)
ce = nn.CrossEntropyLoss()(out, label)
loss = ce
loss.backward()
optimizer.step()
if epoch_id % log_epoch == 0:
train_ca = ClassificationAccuracy(class_num)
predict = torch.argmax(F.softmax(out, dim=-1), dim=-1)
train_ca.accumulate(label=label, predict=predict)
acc_train = train_ca.get()
writer.add_scalar("loss", loss, epoch_id)
writer.add_scalar("acc_train", acc_train, epoch_id)
with torch.no_grad():
myclassifier.eval()
test_ca = ClassificationAccuracy(class_num)
for i, (im, label) in enumerate(tqdm(test_dl, desc="testing ")):
im = im.to(output_device)
label = label.to(output_device)
bs, c, h, w = im.shape
out = myclassifier.forward(im)
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax, dim=-1)
test_ca.accumulate(label=label, predict=predict)
acc_test = test_ca.get()
acc_test_per_class = test_ca.get(True)
writer.add_scalar("acc_test", acc_test, epoch_id)
with open(os.path.join(log_dir, f"myclassifier_{epoch_id}.pkl"), "wb") as f:
torch.save(myclassifier.state_dict(), f)
writer.close()