-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha3.py
More file actions
186 lines (158 loc) · 6.09 KB
/
Copy patha3.py
File metadata and controls
186 lines (158 loc) · 6.09 KB
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
import numpy as np
import pandas as pd
import torch
import os
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
# "ConcatDataset" and "Subset" are possibly useful when doing semi-supervised learning.
from torch.utils.data import ConcatDataset, DataLoader, Subset, Dataset
from torchvision.datasets import DatasetFolder, VisionDataset
# This is for the progress bar.
from tqdm.auto import tqdm
import random
import matplotlib.pyplot as plt
import torchvision
label_mapping = {
'InsideCity':0,
'OpenCountry':1,
'Mountain':2,
'Highway':3,
'LivingRoom':4,
'Suburb':5,
'Bedroom':6,
'Kitchen':7,
'Industrial':8,
'Coast':9,
'TallBuilding':10,
'Street':11,
'Office':12,
'Forest':13,
'Store':14
}
import random
from collections import Counter
from sklearn.model_selection import train_test_split
def trainer(classifier, train_loader, valid_loader, model_name):
device = "cuda" if torch.cuda.is_available() else "cpu"
print(device)
n_epochs = 500
early_stop = 50
model = classifier.to(device)
#for name, param in model.named_parameters():
#model = torchvision.models.vit_b_16(image_size = 256,dropout=0.2, num_classes = 15).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.0002, weight_decay = 1e-5)
stale = 0
best_acc = 0
best_model = None
for epoch in range(n_epochs):
model.train()
train_loss = []
train_accs = []
for batch in tqdm(train_loader):
imgs, labels = batch
logits = model(imgs.to(device))
loss = criterion(logits, labels.to(device))
optimizer.zero_grad()
loss.backward()
grad_norm = nn.utils.clip_grad_norm_(model.parameters(),max_norm = 10)
optimizer.step()
acc = (logits.argmax(dim=-1)==labels.to(device)).float().mean()
train_loss.append(loss.item())
train_accs.append(acc)
train_loss = sum(train_loss) / len(train_loss)
train_acc = sum(train_accs) / len(train_accs)
print(f"[ Train | {epoch + 1:03d}/{n_epochs:03d} ] loss = {train_loss:.5f}, acc = {train_acc:.5f}")
model.eval()
valid_loss = []
valid_accs = []
for batch in tqdm(valid_loader):
imgs , labels = batch
with torch.no_grad():
logits = model(imgs.to(device))
loss = criterion(logits, labels.to(device))
acc = (logits.argmax(dim=-1) == labels.to(device)).float().mean()
valid_loss.append(loss)
valid_accs.append(acc)
valid_loss = sum(valid_loss)/len(valid_loss)
valid_acc = sum(valid_accs)/len(valid_accs)
print(f"[ Valid | {epoch + 1:03d}/{n_epochs:03d} ] loss = {valid_loss:.5f}, acc = {valid_acc:.5f}")
if valid_acc > best_acc:
print(f"Best model found at epoch {epoch}, saving model")
torch.save(model.state_dict(), f"./models/{model_name}.cpkt") # only save best to prevent output memory exceed error
best_acc = valid_acc
stale = 0
else:
stale += 1
if stale > early_stop:
print(f"No improvment {early_stop} consecutive epochs, early stopping")
break
def evaluation(test_loader, classifier, model_name):
device = "cuda" if torch.cuda.is_available() else "cpu"
model_best = classifier.to(device)
model_best.load_state_dict(torch.load(f"models/{model_name}.cpkt"))
model_best.eval()
criterion = nn.CrossEntropyLoss()
test_loss = []
test_accs = []
for batch in tqdm(test_loader):
imgs , labels = batch
with torch.no_grad():
logits = model_best(imgs.to(device))
loss = criterion(logits, labels.to(device))
acc = (logits.argmax(dim=-1) == labels.to(device)).float().mean()
test_loss.append(loss)
test_accs.append(acc)
test_loss = sum(test_loss)/len(test_loss)
test_acc = sum(test_accs)/len(test_accs)
print(f" loss = {test_loss:.5f}, acc = {test_acc:.5f}")
file = open("./log/results.txt", "a")
file.write(f"{model_name} loss = {test_loss:.5f}, acc = {test_acc:.5f}\n")
# #plot the confusion matrix
# from sklearn.metrics import confusion_matrix
# import seaborn as sns
# import pandas as pd
# y_true = []
# y_pred = []
# for batch in tqdm(test_loader):
# imgs , labels = batch
# with torch.no_grad():
# logits = model_best(imgs.to(device))
# y_true.extend(labels)
# y_pred.extend(logits.argmax(dim=-1).cpu())
# cm = confusion_matrix(y_true, y_pred)
# #normalize the confusion matrix
# cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
# # label_mapping reverse
# label_mapping = {
# 'InsideCity':0,
# 'OpenCountry':1,
# 'Mountain':2,
# 'Highway':3,
# 'LivingRoom':4,
# 'Suburb':5,
# 'Bedroom':6,
# 'Kitchen':7,
# 'Industrial':8,
# 'Coast':9,
# 'TallBuilding':10,
# 'Street':11,
# 'Office':12,
# 'Forest':13,
# 'Store':14
# }
# label_mapping = {v:k for k,v in label_mapping.items()}
# df_cm = pd.DataFrame(cm, index = [label_mapping[i] for i in range(15)],
# columns = [label_mapping[i] for i in range(15)])
# plt.figure(figsize = (10,7))
# sns.heatmap(df_cm, annot=True)
# plt.savefig(f"./log/confusion_matrix_{model_name}.png")
# plt.close()
from utils import prepare_dataset, prepare_test
from models import ResNet18, CNN, get_res_50, getvgg16, DictionaryLearning, getvit16, getResnet152V2, getResnet152V4
model = getResnet152V4()
train_loader, valid_loader, test_loader = prepare_dataset()
test_loader = prepare_test()
#trainer(model, train_loader, valid_loader, "resnet152v4")
evaluation(test_loader, model, "resnet152v4")