-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIFAR103x3AugmentedAdaptiveColorThermometerScoring.py
121 lines (95 loc) · 3.62 KB
/
CIFAR103x3AugmentedAdaptiveColorThermometerScoring.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import argparse
from tmu.models.classification.vanilla_classifier import TMClassifier
import numpy as np
from keras.datasets import cifar10
from time import time
from MultilevelThresholdingThermometers import mlt_temp
import cv2
import random
def horizontal_flip(image):
return cv2.flip(image, 1)
def shuffle_dataset(image_array, label_array):
pairs = list(zip(image_array, label_array))
random.shuffle(pairs)
image_array_rand = []
label_array_rand = []
for i in range(len(pairs)):
image_array_rand.append(pairs[i][0])
label_array_rand.append(pairs[i][1])
return (np.asarray(image_array_rand), label_array_rand)
def batch_train(tm, batchsize, x_train, y_train):
(x_train, y_train) = shuffle_dataset(x_train, y_train)
x_train = np.asarray(x_train)
for i in range(0, len(x_train), batchsize):
X_batch = x_train[i : i + batchsize]
Y_batch = y_train[i : i + batchsize]
tm.fit(X_batch, Y_batch)
batchsize = 1000
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--num_clauses", default=2000, type=int)
parser.add_argument("--T", default=3000, type=int)
parser.add_argument("--s", default=5.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=3, type=int)
augmented_images = []
augmented_labels = []
args = parser.parse_args()
(X_train_org, Y_train), (X_test_org, Y_test) = cifar10.load_data()
for i in range(len(X_train_org)):
image = X_train_org[i]
label = Y_train[i]
# Original image and label
augmented_images.append(image)
augmented_labels.append(label)
augmented_images.append(horizontal_flip(image))
augmented_labels.append(label)
X_train_aug = np.array(augmented_images)
Y_train = np.array(augmented_labels).reshape(-1, 1)
X_train = mlt_temp(X_train_aug)
X_test = mlt_temp(X_test_org)
Y_train = Y_train.reshape(Y_train.shape[0])
Y_test = Y_test.reshape(Y_test.shape[0])
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()
batch_train(tm, batchsize, 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 Augmented Adaptive Color Accuracy: %.2f%% Training: %.2fs Testing: %.2fs"
% (
epoch + 1,
result_test,
stop_training - start_training,
stop_testing - start_testing,
)
)
np.savetxt(
"class_sums/CIFAR10AugmentedAdaptiveColorThermometers_%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=",",
)