-
Notifications
You must be signed in to change notification settings - Fork 3
/
classifier_main.py
158 lines (133 loc) · 4.35 KB
/
classifier_main.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
import os
from PIL import Image
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import transforms
from torch.optim import lr_scheduler
from torch.utils.tensorboard import SummaryWriter
from Datasets.AFAD.AFADClassifierDataset import AFADClassifierDataset
from Datasets.Morph2.Morph2_coral_Dataset import Morph2_coral_Dataset
from Models.AgeClassifier import AgeClassifier
from Training.train_classification_model import train_classification_model
from Optimizers.RangerLars import RangerLars
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
torch.cuda.empty_cache()
# classes = [(0, 10), (5, 15), (10, 20), (15, 25), (20, 30), (25, 35), (30, 40), (35, 45), (40, 50),
# (45, 55), (50, 60), (55, 65), (60, 70), (65, 75), (70, 80)]
age_interval = 5
min_age = 15
max_age = 80
BATCH_SIZE = 128
NUM_EPOCHS = 70
NumLabels = int(max_age / age_interval - min_age / age_interval + 1)
# Load data
# data_parser = DataParser()
# data_parser.initialize_data()
#
# train_ds = Morph2ClassifierDataset(
# data_parser.x_train,
# data_parser.y_train,
# MinAge,
# AgeIntareval,
# transform=transforms.Compose([
# # RandomCrop((160, 160), 10),
# # transforms.Normalize((103.939, 116.779, 123.68), (1, 1, 1)),
# transforms.RandomResizedCrop(160, (0.95, 1.0)),
# transforms.RandomHorizontalFlip(),
# transforms.ToTensor()
# ])
# )
#
# test_ds = Morph2ClassifierDataset(
# data_parser.x_test,
# data_parser.y_test,
# MinAge,
# AgeIntareval,
# transform=transforms.Compose([
# # transforms.Normalize((103.939, 116.779, 123.68), (1, 1, 1)),
# transforms.ToTensor()
# ])
# )
# train_ds = AFADClassifierDataset(
# './Datasets/AFAD/aligned_data/afad_train.h5',
# min_age=min_age,
# max_age=max_age,
# age_interval=age_interval,
# transform=transforms.Compose([
# # transforms.Normalize((103.939, 116.779, 123.68), (1, 1, 1)),
# transforms.RandomResizedCrop(160, (0.9, 1.0)),
# transforms.RandomHorizontalFlip(),
# transforms.RandomRotation(degrees=10, resample=Image.BICUBIC),
# transforms.ToTensor()
# ])
# )
# test_ds = AFADClassifierDataset(
# './Datasets/AFAD/aligned_data/afad_test.h5',
# min_age=min_age,
# max_age=max_age,
# age_interval=age_interval,
# transform=transforms.Compose([
# transforms.ToTensor()
# ])
# )
train_ds = Morph2_coral_Dataset(
'./Datasets/Morph2/coral/coral_morph2_train.h5',
min_age,
age_interval,
transform=transforms.Compose([
# transforms.Normalize((103.939, 116.779, 123.68), (1, 1, 1)),
transforms.RandomResizedCrop(160, (0.9, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(degrees=10, resample=Image.BICUBIC),
transforms.ToTensor()
])
)
test_ds = Morph2_coral_Dataset(
'./Datasets/Morph2/coral/coral_morph2_test.h5',
min_age,
age_interval,
transform=transforms.Compose([
transforms.ToTensor()
])
)
image_datasets = {
'train': train_ds,
'val': test_ds
}
data_loaders = {x: DataLoader(image_datasets[x], batch_size=BATCH_SIZE, shuffle=True, num_workers=0) for x in ['train', 'val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
### create model and parameters ###
classification_model = AgeClassifier(NumLabels)
# classification_model = ArcMarginAgeClassifier(NumLabels, margin_m=0.1)
classification_model.to(device)
classification_model.freeze_base_cnn(True)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = RangerLars(classification_model.parameters(), lr=1e-2)
scheduler = lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
# scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=2, verbose=True)
### Train ###
writer = SummaryWriter('logs/Morph2_coral/RangerLars_unfreeze_at_15_lr_1e2_steplr_01_256_epochs_70')
best_classification_model = train_classification_model(
classification_model,
criterion,
optimizer,
scheduler,
data_loaders,
dataset_sizes,
device,
writer,
num_epochs=NUM_EPOCHS
)
print('saving best model')
model_path = 'weights/Morph2_coral/RangerLars_unfreeze_at_15_lr_1e2_steplr_01_256_epochs_70'
if not os.path.exists(model_path):
os.makedirs(model_path)
FINAL_MODEL_FILE = os.path.join(model_path, "weights.pt")
torch.save(best_classification_model.state_dict(), FINAL_MODEL_FILE)
print('exiting')
# model = AgeClassifier(NumLabels)
# best_classification_model.to(device)
# model.load_state_dict(torch.load(PATH))
# model.eval()