-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIFAR10AdaptiveThresholdingMean.py
100 lines (83 loc) · 3 KB
/
CIFAR10AdaptiveThresholdingMean.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
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
import argparse
from tmu.models.classification.vanilla_classifier import TMClassifier
import numpy as np
from keras.datasets import cifar10
import cv2
from time import time
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--num_clauses", default=2000, type=int)
parser.add_argument("--T", default=500, type=int)
parser.add_argument("--s", default=10.0, type=float)
parser.add_argument("--max_included_literals", default=32, type=int)
parser.add_argument("--device", default="GPU", type=str)
parser.add_argument("--weighted_clauses", default=True, type=bool)
parser.add_argument("--epochs", default=250, type=int)
parser.add_argument("--patch_size", default=10, type=int)
args = parser.parse_args()
(X_train_org, Y_train), (X_test_org, Y_test) = cifar10.load_data()
X_train = np.copy(X_train_org)
X_test = np.copy(X_test_org)
Y_train = Y_train.reshape(Y_train.shape[0])
Y_test = Y_test.reshape(Y_test.shape[0])
print(len(Y_train))
for i in range(X_train.shape[0]):
for j in range(X_train.shape[3]):
X_train[i, :, :, j] = cv2.adaptiveThreshold(
X_train_org[i, :, :, j],
1,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
11,
2,
)
for i in range(X_test.shape[0]):
for j in range(X_test.shape[3]):
X_test[i, :, :, j] = cv2.adaptiveThreshold(
X_test_org[i, :, :, j],
1,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
11,
2,
)
tm = TMClassifier(
number_of_clauses=args.num_clauses,
T=args.T,
s=args.s,
max_included_literals=args.max_included_literals,
platform=args.device,
weighted_clauses=args.weighted_clauses,
patch_dim=(args.patch_size, args.patch_size),
)
for epoch in range(args.epochs):
start_training = time()
tm.fit(X_train, Y_train)
stop_training = time()
start_testing = time()
Y_test_predicted, Y_test_scores = tm.predict(X_test, return_class_sums=True)
stop_testing = time()
result_test = 100 * (Y_test_scores.argmax(axis=1) == Y_test).mean()
print(
"#%d Adaptive Thresholding Mean Accuracy: %.2f%% Training: %.2fs Testing: %.2fs"
% (
epoch + 1,
result_test,
stop_training - start_training,
stop_testing - start_testing,
)
)
np.savetxt(
"class_sums/CIFAR10AdaptiveThresholdingMean_%d_%d_%d_%.1f_%d_%d_%d.txt"
% (
epoch + 1,
args.num_clauses,
args.T,
args.s,
args.patch_size,
args.max_included_literals,
args.weighted_clauses,
),
Y_test_scores,
delimiter=",",
)