-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticlass_functions1.py
More file actions
122 lines (100 loc) · 4.02 KB
/
Copy pathmulticlass_functions1.py
File metadata and controls
122 lines (100 loc) · 4.02 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
import torch
import numpy as np # confusion matrix 사용시
import matplotlib.pyplot as plt
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
def Train(model, train_DL, criterion, optimizer, EPOCH):
NoT=len(train_DL.dataset) # Number of training data
loss_history = []
model.train() # train mode로!
for ep in range(EPOCH):
rloss = 0
for x_batch, y_batch in train_DL:
x_batch = x_batch.to(DEVICE)
y_batch = y_batch.to(DEVICE)
# inference
y_hat = model(x_batch)
# cross entropy loss
loss = criterion(y_hat, y_batch)
# update
optimizer.zero_grad() # gradient 누적을 막기 위한 초기화
loss.backward() # backpropagation
optimizer.step() # weight update
# loss accumulation
loss_b = loss.item() * x_batch.shape[0] # batch loss # BATCH_SIZE 로 하면 마지막 18개도 32개로 계산해버림
rloss += loss_b # running loss
# print loss
loss_e = rloss/NoT # epoch loss
loss_history += [loss_e]
print(f"Epoch: {ep+1}, train loss: {loss_e:.3f}")
print("-"*20)
return loss_history
def Test(model,test_DL):
model.eval()
with torch.no_grad():
rcorrect = 0
# rloss = 0
for x_batch, y_batch in test_DL:
x_batch = x_batch.to(DEVICE)
y_batch = y_batch.to(DEVICE)
# inference
y_hat = model(x_batch)
# corrects accumulation
pred = y_hat.argmax(dim=1)
corrects_b = torch.sum(pred == y_batch).item()
rcorrect += corrects_b
accuracy_e = rcorrect/len(test_DL.dataset)*100
print(f"Test accuracy: {rcorrect}/{len(test_DL.dataset)} ({accuracy_e:.1f} %)")
return round(accuracy_e,1)
def Test_plot(model, test_DL):
model.eval()
with torch.no_grad():
x_batch, y_batch = next(iter(test_DL))
x_batch = x_batch.to(DEVICE)
y_hat = model(x_batch)
pred = y_hat.argmax(dim=1)
x_batch = x_batch.to("cpu")
plt.figure(figsize=(8,4))
for idx in range(6):
plt.subplot(2,3, idx+1, xticks=[], yticks=[])
plt.imshow(x_batch[idx].permute(1,2,0), cmap="gray")
pred_class = test_DL.dataset.classes[pred[idx]]
true_class = test_DL.dataset.classes[y_batch[idx]]
plt.title(f"{pred_class} ({true_class})", color = "g" if pred_class==true_class else "r")
def count_params(model):
num = sum([p.numel() for p in model.parameters() if p.requires_grad])
return num
def get_conf(model, test_DL):
N = len(test_DL.dataset.classes)
model.eval()
with torch.no_grad():
confusion = torch.zeros(N,N)
for x_batch, y_batch in test_DL:
x_batch = x_batch.to(DEVICE)
y_batch = y_batch.to(DEVICE)
# inference
y_hat = model(x_batch)
# accuracy
pred = y_hat.argmax(dim=1)
confusion += torch.bincount(N * y_batch.cpu() + pred.cpu(), minlength=N**2).reshape(N, N)
confusion = confusion.numpy()
return confusion
def plot_confusion_matrix(confusion, classes=None):
N = confusion.shape[0]
accuracy=np.trace(confusion)/np.sum(confusion) * 100
plt.figure(figsize=(10,7))
plt.imshow(confusion, cmap="Blues")
plt.title("confusion matrix")
plt.colorbar()
for i in range(N):
for j in range(N):
plt.text(j,i, round(confusion[i,j]),
horizontalalignment="center", fontsize=10,
color="white" if confusion[i,j] > np.max(confusion) / 1.5 else "black")
if classes is not None:
plt.xticks(range(N), classes)
plt.yticks(range(N), classes)
else:
plt.xticks(range(N))
plt.yticks(range(N))
plt.xlabel(f"Predicted label \n accuracy = {accuracy:.1f} %")
plt.ylabel("True label")