-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
45 lines (39 loc) · 1.36 KB
/
util.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
import torch
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import torchvision.transforms as transforms
from PIL import Image
from sklearn.metrics import classification_report, confusion_matrix
# plot loss curve for a training progress
def plot_loss_curve(loss_list) -> None:
# plot the trend of loss
plt.plot(loss_list)
plt.title('Loss versus Epochs for DAGMM')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.grid(True)
# save the figures
plt.savefig('results/loss_figure.png', dpi=600)
plt.show()
def plot_confusion_matrix(cm, state):
# use confusion matrix to show the prediction result directly
plt.figure(figsize=(6, 6))
sns.heatmap(cm, annot=True, fmt='.0f')
plt.xlabel('predictions')
plt.ylabel('labels')
plt.title('Confusion Matrix for {}'.format(state))
# automatically save the figures
plt.savefig('results/martix.png', dpi=600)
plt.show()
def get_classification_report(true, pred):
report = classification_report(true, pred, output_dict=True)
# print(report)
tmp = pd.DataFrame(report).transpose()
tmp.to_csv('results/report.csv', index=True)
return report
def init_weights(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
torch.nn.init.xavier_uniform_(m.weight, gain=1)
torch.nn.init.constant_(m.bias, 0)