-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify_sequential.py
More file actions
162 lines (130 loc) · 4.28 KB
/
Copy pathclassify_sequential.py
File metadata and controls
162 lines (130 loc) · 4.28 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
# customized CNN
import CNN_MNIST as CNN
# For image pixel
import numpy as np
# Plotting
import matplotlib.pyplot as plt
import matplotlib.colors
# Additional libraries
import random
import time
from multiprocessing import Pool
# Return base_image
def build_base(meta_data, images):
image_rows = []
for i in range(meta_data["num_rows"]):
start = meta_data["num_columns"] * i
end = (meta_data["num_columns"]) * (i + 1)
image_row = np.concatenate((images[start:end]), axis=-1)
image_rows.append(image_row)
image_grid = np.concatenate((image_rows), axis=2)
base_image = np.squeeze(image_grid)
return base_image
# Return mask image
def build_mask(meta_data, results):
mask_cell = np.full((28, 28), np.nan)
mask_rows = []
current_idx = 0
for row in range(meta_data["num_rows"]):
mask_row = []
for column in range(meta_data["num_columns"]):
if current_idx in results:
mask_cell = np.full((28, 28), results[current_idx])
else:
mask_cell = np.full((28, 28), np.nan)
mask_row.append(mask_cell)
current_idx += 1
# Merge row of columns
mask_row = np.array(mask_row)
merged_mask_row = np.concatenate((mask_row), axis=-1)
mask_rows.append(merged_mask_row)
# Merge rows
mask_image = np.concatenate((mask_rows), axis=0)
return mask_image
# Refresh mask image
def animate_mask(meta_data, results):
# Plot mask image
mask_image = build_mask(meta_data, results)
mask = plt.imshow(
mask_image, cmap=meta_data["custom_cmap"], norm=meta_data["norm"], alpha=0.5
)
plt.pause(1e-10)
return mask
# Return dict of dl and list of idx
def rand_batch(meta_data, dl, rand):
# Batch dictionary
batch_dict = {}
batch_array = []
for idx, batch in enumerate(dl):
if idx > (meta_data["num_cells"]):
break
batch_dict[idx] = batch
batch_array.append(batch)
rand_idx = [*range(0, meta_data["num_cells"])]
if rand:
random.shuffle(rand_idx)
return batch_dict, batch_array, rand_idx
# Initiate plt
def init_plt():
# Initiate plt
plt.subplots()
# Configure plt
plt.tight_layout(pad=1, w_pad=0, h_pad=0)
custom_cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
"custom", ["red", "white", "green"]
)
norm = plt.Normalize(-1, 1)
return custom_cmap, norm
def initiate_model():
meta_data = {}
# Define batch_size
meta_data["batch_size"] = 1
meta_data["num_rows"] = 200
meta_data["num_columns"] = 300
meta_data["num_cells"] = meta_data["num_rows"] * meta_data["num_columns"]
meta_data["threads"] = 1
# init
dl, images = CNN.dl_images()
# Initiate plt
custom_cmap, norm = init_plt()
meta_data["custom_cmap"] = custom_cmap
meta_data["norm"] = norm
# Plot base image
base_image = build_base(meta_data, images)
base = plt.imshow(base_image, cmap="gray_r")
# Initiate model
model = CNN.init_model(meta_data["batch_size"])
# Get dictonary of batches and random index
batch_dict, batch_array, rand_idx = rand_batch(meta_data, dl, False)
return meta_data, model, batch_dict, batch_array, rand_idx
# main only run if called directly (prevent multiprocessing crash)
if __name__ == "__main__":
# initiate CNN model with MNIST images
meta_data, model, batch_dict, batch_array, rand_idx = initiate_model()
# Time before run
start_time = time.time()
results = {}
# Simple for loop
for i in rand_idx:
# Make prediction
result = model.run(i, batch_dict[i])
results.update(result)
# Time after run
end_time = time.time()
# Plot result in matplotlib
animate_mask(meta_data, results)
# Calculate Accuracy
neg_list = list(results.values())
count = 0
# iterating each number in list
for num in neg_list:
# checking condition
if num <= 0:
count += 1
# Print Logs
print(f"Thread(s) used: {meta_data['threads']}")
print(f"Total Item: {meta_data['num_cells']}")
print(f"Total Runtime: {(end_time - start_time):.4f} sec")
print(f"Accuracy: {((meta_data['num_cells']-count)/meta_data['num_cells'])*100:.2f}%")
# Stay open
plt.show()