-
Notifications
You must be signed in to change notification settings - Fork 44
/
cifar10_wrn_16_8.py
64 lines (48 loc) · 2.16 KB
/
cifar10_wrn_16_8.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
import numpy as np
import sklearn.metrics as metrics
import wide_residual_network as wrn
from keras.datasets import cifar10
import keras.callbacks as callbacks
import keras.utils.np_utils as kutils
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
from keras import backend as K
batch_size = 100
nb_epoch = 100
img_rows, img_cols = 32, 32
(trainX, trainY), (testX, testY) = cifar10.load_data()
trainX = trainX.astype('float32')
trainX = (trainX - trainX.mean(axis=0)) / (trainX.std(axis=0))
testX = testX.astype('float32')
testX = (testX - testX.mean(axis=0)) / (testX.std(axis=0))
trainY = kutils.to_categorical(trainY)
testY = kutils.to_categorical(testY)
generator = ImageDataGenerator(rotation_range=10,
width_shift_range=5./32,
height_shift_range=5./32,)
init_shape = (3, 32, 32) if K.image_dim_ordering() == 'th' else (32, 32, 3)
# For WRN-16-8 put N = 2, k = 8
# For WRN-28-10 put N = 4, k = 10
# For WRN-40-4 put N = 6, k = 4
model = wrn.create_wide_residual_network(init_shape, nb_classes=10, N=2, k=8, dropout=0.00)
model.summary()
plot_model(model, "WRN-16-8.png", show_shapes=False)
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["acc"])
print("Finished compiling")
#model.load_weights("weights/WRN-16-8 Weights.h5")
print("Model loaded.")
model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size, epochs=nb_epoch,
callbacks=[callbacks.ModelCheckpoint("weights/WRN-16-8 Weights.h5",
monitor="val_acc",
save_best_only=True,
verbose=1)],
validation_data=(testX, testY),
validation_steps=testX.shape[0] // batch_size,)
yPreds = model.predict(testX)
yPred = np.argmax(yPreds, axis=1)
yPred = kutils.to_categorical(yPred)
yTrue = testY
accuracy = metrics.accuracy_score(yTrue, yPred) * 100
error = 100 - accuracy
print("Accuracy : ", accuracy)
print("Error : ", error)