-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathpredict_cifar_10.py
186 lines (136 loc) · 6.71 KB
/
predict_cifar_10.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
import numpy as np
import argparse
import sklearn.metrics as metrics
from scipy.optimize import minimize
from sklearn.metrics import log_loss
from models import wide_residual_net as WRN, dense_net as DN
from keras.datasets import cifar10
from keras import backend as K
import keras.utils.np_utils as kutils
parser = argparse.ArgumentParser(description='CIFAR 10 Ensemble Prediction')
parser.add_argument('--optimize', type=int, default=0, help='Optimization flag. Set to 1 to perform a randomized '
'search to maximise classification accuracy.\n'
'Set to -1 to get non weighted classification accuracy')
parser.add_argument('--num_tests', type=int, default=20, help='Number of tests to perform when optimizing the '
'ensemble weights for maximizing classification accuracy')
parser.add_argument('--model', type=str, default='wrn', help='Type of model to train')
# Wide ResNet Parameters
parser.add_argument('--wrn_N', type=int, default=2, help='Number of WRN blocks. Computed as N = (n - 4) / 6.')
parser.add_argument('--wrn_k', type=int, default=4, help='Width factor of WRN')
# DenseNet Parameters
parser.add_argument('--dn_depth', type=int, default=40, help='Depth of DenseNet')
parser.add_argument('--dn_growth_rate', type=int, default=12, help='Growth rate of DenseNet')
args = parser.parse_args()
# Change NUM_TESTS to larger numbers to get possibly better results
NUM_TESTS = args.num_tests
# Change to False to only predict
OPTIMIZE = args.optimize
model_type = str(args.model).lower()
assert model_type in ['wrn', 'dn'], 'Model type must be one of "wrn" for Wide ResNets or "dn" for DenseNets'
if model_type == "wrn":
n = args.wrn_N * 6 + 4
k = args.wrn_k
models_filenames = [r"weights/WRN-CIFAR10-%d-%d-Best.h5" % (n, k),
r"weights/WRN-CIFAR10-%d-%d-1.h5" % (n, k),
r"weights/WRN-CIFAR10-%d-%d-2.h5" % (n, k),
r"weights/WRN-CIFAR10-%d-%d-3.h5" % (n, k),
r"weights/WRN-CIFAR10-%d-%d-4.h5" % (n, k),
r"weights/WRN-CIFAR10-%d-%d-5.h5" % (n, k)]
else:
depth = args.dn_depth
growth_rate = args.dn_growth_rate
models_filenames = [r"weights/DenseNet-CIFAR10-%d-%d-Best.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-1.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-2.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-3.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-4.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-5.h5" % (depth, growth_rate),
r"weights/DenseNet-CIFAR10-%d-%d-6.h5" % (depth, growth_rate)]
(trainX, trainY), (testX, testY) = cifar10.load_data()
nb_classes = len(np.unique(testY))
trainX = trainX.astype('float32')
trainX /= 255.0
testX = testX.astype('float32')
testX /= 255.0
trainY_cat = kutils.to_categorical(trainY)
testY_cat = kutils.to_categorical(testY)
if K.image_dim_ordering() == "th":
init = (3, 32, 32)
else:
init = (32, 32, 3)
if model_type == "wrn":
model = WRN.create_wide_residual_network(init, nb_classes=10, N=args.wrn_N, k=args.wrn_k, dropout=0.00)
model_prefix = 'WRN-CIFAR10-%d-%d' % (args.wrn_N * 6 + 4, args.wrn_k)
else:
model = DN.create_dense_net(nb_classes=10, img_dim=init, depth=args.dn_depth, nb_dense_block=1,
growth_rate=args.dn_growth_rate, nb_filter=16, dropout_rate=0.2)
model_prefix = 'DenseNet-CIFAR10-%d-%d' % (args.dn_depth, args.dn_growth_rate)
best_acc = 0.0
best_weights = None
train_preds = []
for fn in models_filenames:
model.load_weights(fn)
print("Predicting train set values on model %s" % (fn))
yPreds = model.predict(trainX, batch_size=128, verbose=2)
train_preds.append(yPreds)
test_preds = []
for fn in models_filenames:
model.load_weights(fn)
print("Predicting test set values on model %s" % (fn))
yPreds = model.predict(testX, batch_size=128, verbose=2)
test_preds.append(yPreds)
def calculate_weighted_accuracy():
global weighted_predictions, weight, prediction, yPred, yTrue, accuracy, error
weighted_predictions = np.zeros((testX.shape[0], nb_classes), dtype='float32')
for weight, prediction in zip(prediction_weights, test_preds):
weighted_predictions += weight * prediction
yPred = np.argmax(weighted_predictions, axis=1)
yTrue = testY
accuracy = metrics.accuracy_score(yTrue, yPred) * 100
error = 100 - accuracy
print("Accuracy : ", accuracy)
print("Error : ", error)
exit()
if OPTIMIZE == 0:
with open('weights/Ensemble weights %s.json' % model_prefix, mode='r') as f:
dictionary = json.load(f)
prediction_weights = dictionary['best_weights']
calculate_weighted_accuracy()
elif OPTIMIZE == -1:
prediction_weights = [1. / len(models_filenames)] * len(models_filenames)
calculate_weighted_accuracy()
''' OPTIMIZATION REGION '''
print()
def log_loss_func(weights):
''' scipy minimize will pass the weights as a numpy array '''
final_prediction = np.zeros((trainX.shape[0], nb_classes), dtype='float32')
for weight, prediction in zip(weights, train_preds):
final_prediction += weight * prediction
return log_loss(trainY_cat, final_prediction)
for iteration in range(NUM_TESTS):
prediction_weights = np.random.random(len(models_filenames))
constraints = ({'type': 'eq', 'fun':lambda w: 1 - sum(w)})
bounds = [(0, 1)] * len(train_preds)
result = minimize(log_loss_func, prediction_weights, method='SLSQP', bounds=bounds, constraints=constraints)
print('Best Ensemble Weights: {weights}'.format(weights=result['x']))
weights = result['x']
weighted_predictions = np.zeros((testX.shape[0], nb_classes), dtype='float32')
for weight, prediction in zip(weights, test_preds):
weighted_predictions += weight * prediction
yPred = np.argmax(weighted_predictions, axis=1)
yTrue = testY
accuracy = metrics.accuracy_score(yTrue, yPred) * 100
error = 100 - accuracy
print("Iteration %d: Accuracy : " % (iteration + 1), accuracy)
print("Iteration %d: Error : " % (iteration + 1), error)
if accuracy > best_acc:
best_acc = accuracy
best_weights = weights
print()
print("Best Accuracy : ", best_acc)
print("Best Weights : ", best_weights)
with open('weights/Ensemble weights %s.json' % model_prefix, mode='w') as f:
dictionary = {'best_weights' : best_weights.tolist()}
json.dump(dictionary, f)
''' END OF OPTIMIZATION REGION '''