-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_visuals.py
More file actions
152 lines (109 loc) · 3.96 KB
/
model_visuals.py
File metadata and controls
152 lines (109 loc) · 3.96 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
#!/usr/bin/env python
# coding: utf-8
# In[4]:
import os
import glob
import random
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import cv2
import sklearn.metrics
def data_visualization(img_dir, samples=5, size=25):
"""
The function takes in the path to image directory, along with number of samples
figure size to be displayed the image as input.
Parameters
-----------------
img_dir = path to image files
samples = number of images to be displayed
size = matlplotlib figure size
Returns
---------------
An image grid with specified number of samples and size.
Notes
--------------
The function is customized to show images in grayscale and labels them according to
its directory name. It uses libraries not commonly used and needs to be installed
for it to work properly.
"""
path = os.path.join(img_dir,'*g')
all_images = glob.glob(path)
selected_images = random.sample(all_images, samples)
images = []
for i in selected_images:
img = cv2.imread(i)
images.append(img)
plt.figure(figsize=(size, size))
for i in range(len(images)):
plt.subplot(5, 5, i + 1)
plt.imshow(images[i], cmap='gray', vmin=0, vmax=1)
plt.title(os.path.basename(img_dir), color='black', fontsize=14)
plt.axis('off')
def plot_acc_loss(history):
"""
The function plots accuracy and loss curves from the history
file saved in the model output
Parameters
--------------
history = history file from model output
Returns
--------------
A matplotlib subplot of model accuracy and loss side by side
for both training and test data
Notes
---------------
The fucntion has default settings of image size and styles that can be
changed internally if needed. Purpose was to maintain consistency among
different model evaluations.
"""
fig, ax = plt.subplots(1,2,figsize=(8,4))
sns.set_style("white")
ax[0].set_title('Accuracy')
ax[0].plot(history.history['accuracy'], color = 'cornflowerblue')
ax[0].plot(history.history['val_accuracy'], color = 'crimson')
ax[0].set_ylabel('Accuracy')
ax[0].set_xlabel('Epoch')
ax[0].legend(['Train', 'Test'], loc='best')
ax[1].set_title('Loss')
ax[1].plot(history.history['loss'], color = 'cornflowerblue')
ax[1].plot(history.history['val_loss'], color = 'crimson')
ax[1].set_ylabel('Loss')
ax[1].set_xlabel('Epoch')
ax[1].legend(['Train', 'Test'], loc='best')
plt.subplots_adjust(wspace=0.4)
plt.show()
def plot_rec_prec(history):
"""
The function plots recall and precision curves from the history
file saved in the model output
Parameters
--------------
history = history file from model output
Returns
--------------
A matplotlib subplot of model precision and recall side by side
for both training and test data
Notes
---------------
The fucntion has default settings of image size and styles that can be
changed internally if needed. Purpose was to maintain consistency among
different model evaluations.
"""
fig, ax = plt.subplots(1,2,figsize=(8,4))
sns.set_style("white")
ax[0].set_title('Recall')
ax[0].plot(history.history['recall'], color = 'cornflowerblue')
ax[0].plot(history.history['val_recall'], color = 'crimson')
ax[0].set_ylabel('Recall')
ax[0].set_xlabel('Epoch')
ax[0].legend(['Train', 'Test'], loc='best')
ax[1].set_title('Precision')
ax[1].plot(history.history['precision'], color = 'cornflowerblue')
ax[1].plot(history.history['val_precision'], color = 'crimson')
ax[1].set_ylabel('Precision')
ax[1].set_xlabel('Epoch')
ax[1].legend(['Train', 'Test'], loc='best')
plt.subplots_adjust(wspace=0.4)
plt.show()
# In[ ]: