-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexperiment.py
220 lines (171 loc) · 7.27 KB
/
experiment.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from __future__ import print_function
import sys
import os
import getopt
import pickle
import numpy as np
from keras.utils import to_categorical
from keras.optimizers import Adagrad
from keras import backend as K
from noise import (noisify_with_P, noisify_binary_asymmetric,
noisify_cifar10_asymmetric, noisify_mnist_asymmetric,
noisify_cifar100_asymmetric)
from models import MNISTModel, CIFAR10Model, CIFAR100Model
from models import IMDBModel, LSTMModel
from models import NoiseEstimator
np.random.seed(1337) # for reproducibility
def build_file_name(loc, dataset, loss, noise, asymmetric, run):
return (os.path.dirname(os.path.realpath(__file__)) +
'/output/' + loc +
dataset + '_' +
loss + '_' +
str(noise) + '_' +
str(asymmetric) + '_' +
str(run))
def train_and_evaluate(dataset, loss, noise, run=0, num_batch=32,
asymmetric=0):
val_split = 0.1
if dataset == 'mnist':
kerasModel = MNISTModel(num_batch=num_batch)
kerasModel.optimizer = Adagrad()
elif dataset == 'cifar10_deep':
kerasModel = CIFAR10Model(num_batch=num_batch, type='deep')
elif dataset[8:-1] == 'resnet':
kerasModel = CIFAR10Model(num_batch=num_batch, type=dataset[8:])
elif dataset == 'cifar100':
kerasModel = CIFAR100Model(num_batch=num_batch)
elif dataset == 'imdb':
kerasModel = IMDBModel(num_batch=num_batch)
kerasModel.optimizer = Adagrad()
elif dataset == 'lstm':
kerasModel = LSTMModel(num_batch=num_batch)
kerasModel.optimizer = Adagrad(lr=0.001)
else:
ValueError('No dataset given.')
import sys
sys.exit()
# an important data-dependent configuration
if dataset == 'cifar100':
filter_outlier = False
else:
filter_outlier = True
# the data, shuffled and split between train and test sets
print('Loading %s ...' % dataset)
X_train, X_test, y_train, y_test = kerasModel.get_data()
print('Done.')
# apply label noise
if asymmetric == 0:
y_train, P = noisify_with_P(y_train, kerasModel.classes, noise,
random_state=run)
elif asymmetric == 1:
if dataset == 'mnist':
y_train, P = noisify_mnist_asymmetric(y_train, noise,
random_state=run)
elif dataset == 'cifar100':
y_train, P = noisify_cifar100_asymmetric(y_train, noise,
random_state=run)
elif dataset[:7] == 'cifar10':
y_train, P = noisify_cifar10_asymmetric(y_train, noise,
random_state=run)
else: # binary classes
y_train, P = noisify_binary_asymmetric(y_train, noise,
random_state=run)
print('T: \n', P)
# convert class vectors to binary class matrices
Y_train = to_categorical(y_train, kerasModel.classes)
Y_test = to_categorical(y_test, kerasModel.classes)
# keep track of the best model
model_file = build_file_name('tmp_model/', dataset, loss, noise,
asymmetric, run)
# this is the case when we post-train changing the loss
if loss == 'est_backward':
vanilla_file = build_file_name('tmp_model/', dataset, 'crossentropy',
noise, asymmetric, run)
if not os.path.isfile(vanilla_file):
ValueError('Need to train with crossentropy first !')
# first compile the vanilla_crossentropy model with the saved weights
kerasModel.build_model('crossentropy', P=None)
kerasModel.load_model(vanilla_file)
# estimate P
est = NoiseEstimator(classifier=kerasModel, alpha=0.0,
filter_outlier=filter_outlier)
# use all X_train
P_est = est.fit(X_train).predict()
print('Condition number:', np.linalg.cond(P_est))
print('T estimated: \n', P)
# compile the model with the new estimated loss
kerasModel.build_model('backward', P=P_est)
elif loss == 'est_forward':
vanilla_file = build_file_name('tmp_model/', dataset, 'crossentropy',
noise, asymmetric, run)
if not os.path.isfile(vanilla_file):
ValueError('Need to train with crossentropy first !')
# first compile the vanilla_crossentropy model with the saved weights
kerasModel.build_model('crossentropy', P=None)
kerasModel.load_model(vanilla_file)
# estimate P
est = NoiseEstimator(classifier=kerasModel, alpha=0.0,
filter_outlier=filter_outlier)
# use all X_train
P_est = est.fit(X_train).predict()
print('T estimated:', P)
# compile the model with the new estimated loss
kerasModel.build_model('forward', P=P_est)
else:
# compile the model
kerasModel.build_model(loss, P)
# fit the model
history = kerasModel.fit_model(model_file, X_train, Y_train,
validation_split=val_split)
history_file = build_file_name('history/', dataset, loss,
noise, asymmetric, run)
# decomment for writing history
with open(history_file, 'wb') as f:
pickle.dump(history, f)
print('History dumped at ' + str(history_file))
# test
score = kerasModel.evaluate_model(X_test, Y_test)
# clean models, unless it is vanilla_crossentropy --to be used by P_est
if loss != 'crossentropy':
os.remove(model_file)
return score
if __name__ == "__main__":
def error_and_exit():
print('Usage: ' + str(__file__) + ' -d dataset -l loss '
'-n noise_rate [-a asymmetric_noise -r n_runs]')
sys.exit()
opts, args = getopt.getopt(sys.argv[1:], "d:l:n:a:r:")
# dataset/model cases: mnist, imdb, lstm, cifa100
# cifar10_resnet{n} with a digit. See the call to cifar10 models
# loss: crossentropy, backward, forward, unhinged, sigmoid, ramp, savage, boot_soft
n_runs = 1
num_batch = 128
asymmetric = 0 # symmetric noise by default
dataset, loss, noise = None, None, None
for opt, arg in opts:
if opt == '-d':
dataset = arg
elif opt == '-l':
loss = arg
elif opt == '-n':
noise = np.array(arg).astype(np.float)
elif opt == '-a':
asymmetric = np.array(arg).astype(np.int)
elif opt == '-r':
n_runs = np.array(arg).astype(np.int)
else:
error_and_exit()
# compulsory params
if dataset is None or loss is None or noise is None:
error_and_exit()
print("Params: dataset=%s, loss=%s, noise=%s, asymmetric=%d, "
% (dataset, loss, noise, asymmetric))
accuracies = []
# implicit random initialization
for i in range(n_runs):
accuracies.append(train_and_evaluate(dataset, loss, noise, i,
num_batch, asymmetric))
print("*** # RUN %d: accuracy=%.2f" % (i, accuracies[i]))
print(accuracies)
print(np.mean(accuracies), np.std(accuracies))
K.clear_session()