-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain_classifier.py
182 lines (118 loc) · 5.1 KB
/
train_classifier.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
import torch
import torch.nn as nn
import numpy as np
import os
import shutil
import datetime
import sys
import torch.nn.functional as F
import torchvision
from torch.optim import lr_scheduler
from models import model_classifier
from models import model_projection
from utils.utils import EarlyStopping
import config
from utils import EarlyStopping, WarmUpExponentialLR
if config.ESC_10:
import dataset_ESC10 as dataset
elif config.ESC_50:
import dataset_ESC50 as dataset
elif config.US8K:
import dataset_US8K as dataset
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
main_path = config.supCon_path_for_classifier
state_dict = torch.load( main_path + 'checkpoint.pt' )
pretrained_model =torchvision.models.resnet50(pretrained=True).to(device)
pretrained_model.fc = nn.Sequential(nn.Identity())
#to use multiple GPU cores for the model
pretrained_model = nn.DataParallel(pretrained_model, device_ids=[0, 1])
pretrained_model = pretrained_model.to(device)
pretrained_model.load_state_dict(state_dict)
pretrained_model = pretrained_model.eval()
classifier = model_classifier.Classifier().to(device)
train_loader, val_loader = dataset.create_generators()
optimizer = torch.optim.AdamW(list(classifier.parameters()), lr=config.lr, weight_decay=1e-3)
scheduler = WarmUpExponentialLR(optimizer, cold_epochs= 0, warm_epochs= config.warm_epochs, gamma=0.995)
# to save the parameters for the classifier
classifier_path = main_path + 'classifier/'
if not os.path.exists(classifier_path):
os.mkdir(classifier_path)
def hotEncoder(v):
ret_vec = torch.zeros(v.shape[0], config.class_numbers ).to(device)
for s in range(v.shape[0]):
ret_vec[s][v[s]] = 1
return ret_vec
def cross_entropy_one_hot(input, target):
_, labels = target.max(dim=1)
ls = nn.CrossEntropyLoss()(input, labels)
return ls
def train_classifier():
num_epochs = 800
with open(main_path + '/classifier_results.txt','w', 1) as output_file:
classifier_stopping = EarlyStopping(patience=300, verbose=True, log_path=classifier_path, output_file=output_file)
print('*****', file=output_file)
print('classifier after sup_contrastive', file=output_file)
if config.ESC_10:
print('ESC_10', file=output_file)
print('train folds are {} and test fold is {}'.format(config.train_folds, config.test_fold), file=output_file)
elif config.ESC_50:
print('ESC_10', file=output_file)
print('train folds are {} and test fold is {}'.format(config.train_folds, config.test_fold), file=output_file)
elif config.US8K:
print('US8K', file=output_file)
print('train folds are {} and test fold is {}'.format(config.us8k_train_folds, config.us8k_test_fold), file=output_file)
print('number of freq masks are {} and their max length is {}'.format(config.freq_masks, config.freq_masks_width), file=output_file)
print('number of time masks are {} and their max length is {}'.format(config.time_masks, config.time_masks_width), file=output_file)
print('*****', file=output_file)
for epoch in range(num_epochs):
classifier.train()
train_loss = []
train_corrects = 0
train_samples_count = 0
for _, x, label in train_loader:
loss = 0
optimizer.zero_grad()
x = x.float().to(device)
label = label.to(device).unsqueeze(1)
label_vec = hotEncoder(label)
y_rep = pretrained_model(x)
y_rep = F.normalize(y_rep, dim=1)
out = classifier(y_rep)
loss = cross_entropy_one_hot(out, label_vec)
loss.backward()
train_loss.append(loss.item() )
optimizer.step()
train_corrects += (torch.argmax(out, dim=1) == torch.argmax(label_vec, dim=1)).sum().item()
train_samples_count += x.shape[0]
val_loss = []
val_acc = []
val_corrects = 0
val_samples_count = 0
classifier.eval()
with torch.no_grad():
for _, val_x, val_label in val_loader:
val_x = val_x.float().to(device)
label = val_label.to(device).unsqueeze(1)
label_vec = hotEncoder(label)
y_rep = pretrained_model(val_x)
y_rep = F.normalize(y_rep, dim=1)
out = classifier(y_rep)
temp = cross_entropy_one_hot(out, label_vec)
val_loss.append(temp.item())
val_corrects += (torch.argmax(out, dim=1) == torch.argmax(label_vec, dim=1)).sum().item()
val_samples_count += val_x.shape[0]
train_acc = train_corrects / train_samples_count
val_acc = val_corrects / val_samples_count
scheduler.step()
print('\n', file=output_file)
print("Epoch: {}/{}...".format(epoch+1, num_epochs),
"Loss: {:.4f}...".format(np.mean(train_loss)),
"Val Loss: {:.4f}".format(np.mean(val_loss)), file=output_file)
print('train_acc is {:.4f} and val_acc is {:.4f}'.format(train_acc, val_acc), file=output_file)
classifier_stopping(-val_acc, classifier, epoch+1)
if classifier_stopping.early_stop:
print("Early stopping", file=output_file)
return
if __name__ == "__main__":
train_classifier()