-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIFAR10AugmentedHistogramOfGradients.py
149 lines (121 loc) · 4.93 KB
/
CIFAR10AugmentedHistogramOfGradients.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
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
"""Copyright (c) 2023 Ole-Christoffer Granmo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
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
def horizontal_flip(image):
return cv2.flip(image, 1)
patch_size = 0
imageSize = (
32 # The size of the original image - in pixels - assuming this is a square image
)
channels = 3 # The number of channels of the image. A RBG color image, has 3 channels
classes = 10 # The number of classes available for this dataset
winSize = imageSize
blockSize = 12
blockStride = 4
cellSize = 4
nbins = 18
derivAperture = 1
winSigma = -1.0
histogramNormType = 0
L2HysThreshold = 0.2
gammaCorrection = True
nlevels = 64
signedGradient = True
hog = cv2.HOGDescriptor(
(winSize, winSize),
(blockSize, blockSize),
(blockStride, blockStride),
(cellSize, cellSize),
nbins,
derivAperture,
winSigma,
histogramNormType,
L2HysThreshold,
gammaCorrection,
nlevels,
signedGradient,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--num_clauses", default=2000, type=int)
parser.add_argument("--T", default=50, 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=False, type=bool)
parser.add_argument("--epochs", default=250, 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)
Y_train = Y_train
Y_test = Y_test
Y_train = Y_train.reshape(Y_train.shape[0])
Y_test = Y_test.reshape(Y_test.shape[0])
fd = hog.compute(X_train_aug[0])
X_train = np.empty((X_train_aug.shape[0], fd.shape[0]), dtype=np.uint32)
for i in range(X_train_aug.shape[0]):
fd = hog.compute(X_train_aug[i])
X_train[i] = fd >= 0.1
fd = hog.compute(X_test_org[0])
X_test = np.empty((X_test_org.shape[0], fd.shape[0]), dtype=np.uint32)
for i in range(X_test_org.shape[0]):
fd = hog.compute(X_test_org[i])
X_test[i] = fd >= 0.1
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,
)
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 Augmented HoG Accuracy: %.2f%% Training: %.2fs Testing: %.2fs"
% (
epoch + 1,
result_test,
stop_training - start_training,
stop_testing - start_testing,
)
)
np.savetxt(
"class_sums/CIFAR10AugmentedHistogramOfGradients_%d_%d_%d_%.2f_%d_%d_%d.txt"
% (
epoch + 1,
args.num_clauses,
args.T,
args.s,
patch_size,
args.max_included_literals,
args.weighted_clauses,
),
Y_test_scores,
delimiter=",",
)