-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_CelebA.py
211 lines (185 loc) · 7.01 KB
/
main_CelebA.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
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, random_split
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 = 50
log_epoch = 4
class_num = 291
root_dir = "./logZZPMAIN"
dataset_dir = "./datasets/img_align_celeba_common_crop"
h = 64
w = 64
init = Init(
seed=9970,
log_root_dir=root_dir,
backup_filename=__file__,
tensorboard=True,
comment=f"main CelebA colourful flip",
)
output_device = init.get_device()
writer = init.get_writer()
log_dir = init.get_log_dir()
data_workers = 2
transform = Compose(
[
Resize((h, w)),
RandomHorizontalFlip(),
ToTensor(),
]
)
ds = ImageFolder(root=dataset_dir, transform=transform)
ds_len = len(ds)
train_ds, test_ds = random_split(ds, [ds_len * 6 // 7, ds_len - ds_len * 6 // 7])
train_ds_len = len(train_ds)
test_ds_len = len(test_ds)
print(train_ds_len)
print(test_ds_len)
train_dl = DataLoader(
dataset=train_ds,
batch_size=batch_size,
shuffle=True,
num_workers=data_workers,
drop_last=True,
)
test_dl = DataLoader(
dataset=test_ds,
batch_size=batch_size,
shuffle=False,
num_workers=data_workers,
drop_last=False,
)
train_dl_len = len(train_dl)
test_dl_len = len(test_dl)
class FeatureExtracter(nn.Module):
def __init__(self):
super(FeatureExtracter, self).__init__()
self.conv1 = nn.Conv2d(3, 128, 3, 1, 1)
self.conv2 = nn.Conv2d(128, 256, 3, 1, 1)
self.conv3 = nn.Conv2d(256, 512, 3, 1, 1)
self.conv4 = nn.Conv2d(512, 1024, 3, 1, 1)
self.bn1 = nn.BatchNorm2d(128)
self.bn2 = nn.BatchNorm2d(256)
self.bn3 = nn.BatchNorm2d(512)
self.bn4 = nn.BatchNorm2d(1024)
self.mp1 = nn.MaxPool2d(2, 2)
self.mp2 = nn.MaxPool2d(2, 2)
self.mp3 = nn.MaxPool2d(2, 2)
self.mp4 = nn.MaxPool2d(2, 2)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.relu3 = nn.ReLU()
self.relu4 = nn.ReLU()
def forward(self, x: Tensor):
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 = self.conv4(x)
x = self.bn4(x)
x = self.mp4(x)
x = self.relu4(x)
x = x.view(-1, 16384)
return x
class CLS(nn.Module):
def __init__(self, in_dim, out_dim, bottle_neck_dim=256):
super(CLS, self).__init__()
self.bottleneck = nn.Linear(in_dim, bottle_neck_dim)
self.fc = nn.Linear(bottle_neck_dim, out_dim)
def forward(self, x):
x = self.bottleneck(x)
x = self.fc(x)
return x
class Classifier(nn.Module):
def __init__(self) -> None:
super(Classifier, self).__init__()
self.feature_extractor = FeatureExtracter()
self.cls = CLS(16384, class_num, bottle_neck_dim=2650)
def forward(self, x):
x = self.feature_extractor(x)
x = self.cls(x)
return x
myclassifier = Classifier().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)
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax, 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_training", acc_train, epoch_id)
with open(
os.path.join(log_dir, f"feature_extractor_{epoch_id}.pkl"), "wb"
) as f:
torch.save(myclassifier.feature_extractor.state_dict(), f)
with open(os.path.join(log_dir, f"cls_{epoch_id}.pkl"), "wb") as f:
torch.save(myclassifier.cls.state_dict(), f)
with torch.no_grad():
myclassifier.eval()
r = 0
celoss = 0
test_ca = ClassificationAccuracy(class_num)
for i, (im, label) in enumerate(tqdm(train_dl, desc="testing train")):
r += 1
im = im.to(output_device)
label = label.to(output_device)
bs, c, h, w = im.shape
out = myclassifier.forward(im)
ce = nn.CrossEntropyLoss()(out, label)
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax, dim=-1)
test_ca.accumulate(label=label, predict=predict)
celoss += ce
celossavg = celoss / r
acc_test = test_ca.get()
writer.add_scalar("train loss", celossavg, epoch_id)
writer.add_scalar("acc_train", acc_test, epoch_id)
r = 0
celoss = 0
test_ca = ClassificationAccuracy(class_num)
for i, (im, label) in enumerate(tqdm(test_dl, desc="testing test")):
r += 1
im = im.to(output_device)
label = label.to(output_device)
bs, c, h, w = im.shape
out = myclassifier.forward(im)
ce = nn.CrossEntropyLoss()(out, label)
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax, dim=-1)
test_ca.accumulate(label=label, predict=predict)
celoss += ce
celossavg = celoss / r
acc_test = test_ca.get()
writer.add_scalar("test loss", celossavg, epoch_id)
writer.add_scalar("acc_test", acc_test, epoch_id)
writer.close()