-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
160 lines (133 loc) · 5.4 KB
/
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
import torch
import torch.nn as nn
import pandas as pd
import torch.nn.functional as F
from simsiam import *
from tqdm import tqdm
import time
import os
class Classifier(nn.Module):
def __init__(self, input_dim=4096, num_classes=6, dropout_prob=0.2):
super(Classifier, self).__init__()
self.fc = nn.Sequential(
nn.Dropout(dropout_prob),
nn.Linear(input_dim, 2048),
nn.ReLU(),
nn.Linear(2048, num_classes)
)
def forward(self, x):
x = self.fc(x)
#return F.softmax(x, dim=1)
return x
class ConvClassifier(nn.Module):
def __init__(self, image_height, image_width, in_channels, out_channels=4, num_classes=6, dropout_prob=0.5):
super(ConvClassifier, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.ReLU(),
)
self.fc = nn.Sequential(
nn.Linear(image_height * image_width * out_channels, 512),
nn.ReLU(),
nn.Dropout(dropout_prob),
nn.Linear(512, num_classes)
)
def forward(self, x):
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return F.softmax(x, dim=1)
def calculate_accuracy(predictions, labels):
'''
quick and dirty function to calculate accuracy given two one hot label vectors (predicted and true)
'''
_, predicted = torch.max(predictions, 1)
_, true = torch.max(labels,1)
correct = (predicted == true).type(torch.DoubleTensor).mean().item()
return correct
def train_classifier(encoder, classifier, train_loader, val_loader, num_epochs=10, learning_rate=0.001, run_id=None):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
criterion = nn.CrossEntropyLoss()
criterion = criterion.to(device)
optimizer = torch.optim.Adam(classifier.parameters(), lr=learning_rate)
# create directory to store outputs
if "classifier_test_results" not in os.listdir():
os.makedirs("classifier_test_results")
epoch_losses = []
val_epoch_losses = []
accuracies = []
print("Training classifier...")
overall_start_time = time.time()
for epoch in range(num_epochs):
epoch_start_time = time.time()
running_loss = 0.0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# train
encoder = encoder.to(device)
classifier = classifier.to(device)
classifier.train()
for images, y in tqdm(train_loader):
inputs, _,_= images
inputs = inputs.to(device)
optimizer.zero_grad()
embeddings = encoder.encoder(inputs)
y = y.float()
y = y.to(device)
outputs = classifier(embeddings)
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = round(running_loss / len(train_loader), 5)
epoch_losses.append(epoch_loss)
# validate
classifier.eval()
val_loss = 0.0
total_accuracy = 0.0
with torch.no_grad():
for images, y in val_loader:
inputs, _,_= images
inputs = inputs.to(device)
embeddings = encoder.encoder(inputs)
y = y.float()
y = y.to(device)
outputs = classifier(embeddings)
loss = criterion(outputs, y)
val_loss += criterion(outputs, y).item()
total_accuracy += calculate_accuracy(outputs, y)
epoch_val_loss = round(val_loss / len(val_loader), 5)
val_loss = round(val_loss / len(val_loader), 5)
accuracy = round(total_accuracy / len(val_loader),4)*100
val_epoch_losses.append(val_loss)
accuracies.append(accuracy)
print(f"Epoch {epoch+1}/{num_epochs}, Train Loss: {epoch_loss}, Val Loss: {epoch_val_loss}, Val Acc: {accuracy}%, Epoch Time: {round(time.time()-epoch_start_time,2)} seconds")
# save model after every epoch
if run_id is not None:
torch.save(classifier, f"experiments/models/{run_id}/classifier_epoch_{epoch}.pth")
else:
torch.save(classifier, f"classifier_test_results/classifier_epoch_{epoch}.pth")
data = {'epoch': range(1, len(epoch_losses) + 1),
'train_loss': epoch_losses,
'val_loss': val_epoch_losses,
'accuracy':accuracies}
df = pd.DataFrame(data)
if run_id is not None:
df.to_csv(f"experiments/models/{run_id}/classifier_loss.csv")
else:
df.to_csv("classifier_test_results/loss.csv", index=False)
print(f"Finished Training - Total Train Time = {round(time.time() - overall_start_time, 2)}")
return
def evaluate_accuracy(encoder, classifier, test_loader):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
classifier.eval()
total_accuracy = 0.0
with torch.no_grad():
for images, y in test_loader:
inputs, _,_= images
inputs = inputs.to(device)
embeddings = encoder.encoder(inputs)
y = y.float().to(device)
outputs = classifier(embeddings).to(device)
total_accuracy += calculate_accuracy(outputs, y)
accuracy = round(total_accuracy / len(test_loader),4)*100
return accuracy