Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/BatchNormalization/cifar10_cnn_100epochs.py
3119 views
1
'''Train a simple deep CNN on the CIFAR10 small images dataset.
2
It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.
3
(it's still underfitting at that point, though).
4
https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py
5
'''
6
7
from __future__ import print_function
8
import keras
9
from keras.datasets import cifar10
10
from keras.preprocessing.image import ImageDataGenerator
11
from keras.models import Sequential
12
from keras.layers import Dense, Dropout, Activation, Flatten
13
from keras.layers import Conv2D, MaxPooling2D
14
import os
15
import pickle
16
17
from numpy.random import seed
18
seed(7)
19
20
batch_size = 32
21
num_classes = 10
22
epochs = 100
23
data_augmentation = True
24
num_predictions = 20
25
save_dir = os.path.join(os.getcwd(), 'saved_models_noBn_100_s7')
26
model_name = 'keras_cifar10_trained_model.h5'
27
28
# The data, split between train and test sets:
29
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
30
print('x_train shape:', x_train.shape)
31
print(x_train.shape[0], 'train samples')
32
print(x_test.shape[0], 'test samples')
33
34
# Convert class vectors to binary class matrices.
35
y_train = keras.utils.to_categorical(y_train, num_classes)
36
y_test = keras.utils.to_categorical(y_test, num_classes)
37
38
model = Sequential()
39
model.add(Conv2D(32, (3, 3), padding='same',
40
input_shape=x_train.shape[1:]))
41
model.add(Activation('relu'))
42
model.add(Conv2D(32, (3, 3)))
43
model.add(Activation('relu'))
44
model.add(MaxPooling2D(pool_size=(2, 2)))
45
model.add(Dropout(0.25))
46
47
model.add(Conv2D(64, (3, 3), padding='same'))
48
model.add(Activation('relu'))
49
model.add(Conv2D(64, (3, 3)))
50
model.add(Activation('relu'))
51
model.add(MaxPooling2D(pool_size=(2, 2)))
52
model.add(Dropout(0.25))
53
54
model.add(Flatten())
55
model.add(Dense(512))
56
model.add(Activation('relu'))
57
model.add(Dropout(0.5))
58
model.add(Dense(num_classes))
59
model.add(Activation('softmax'))
60
61
# initiate RMSprop optimizer
62
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
63
64
# Let's train the model using RMSprop
65
model.compile(loss='categorical_crossentropy',
66
optimizer=opt,
67
metrics=['accuracy'])
68
69
x_train = x_train.astype('float32')
70
x_test = x_test.astype('float32')
71
x_train /= 255
72
x_test /= 255
73
74
if not data_augmentation:
75
print('Not using data augmentation.')
76
history = model.fit(x_train, y_train,
77
batch_size=batch_size,
78
epochs=epochs,
79
validation_data=(x_test, y_test),
80
shuffle=True)
81
else:
82
print('Using real-time data augmentation.')
83
# This will do preprocessing and realtime data augmentation:
84
datagen = ImageDataGenerator(
85
featurewise_center=False, # set input mean to 0 over the dataset
86
samplewise_center=False, # set each sample mean to 0
87
featurewise_std_normalization=False, # divide inputs by std of the dataset
88
samplewise_std_normalization=False, # divide each input by its std
89
zca_whitening=False, # apply ZCA whitening
90
zca_epsilon=1e-06, # epsilon for ZCA whitening
91
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
92
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
93
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
94
shear_range=0., # set range for random shear
95
zoom_range=0., # set range for random zoom
96
channel_shift_range=0., # set range for random channel shifts
97
fill_mode='nearest', # set mode for filling points outside the input boundaries
98
cval=0., # value used for fill_mode = "constant"
99
horizontal_flip=True, # randomly flip images
100
vertical_flip=False, # randomly flip images
101
rescale=None, # set rescaling factor (applied before any other transformation)
102
preprocessing_function=None, # set function that will be applied on each input
103
data_format=None, # image data format, either "channels_first" or "channels_last"
104
validation_split=0.0) # fraction of images reserved for validation (strictly between 0 and 1)
105
106
# Compute quantities required for feature-wise normalization
107
# (std, mean, and principal components if ZCA whitening is applied).
108
datagen.fit(x_train)
109
110
# Fit the model on the batches generated by datagen.flow().
111
history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_test, y_test), workers=4)
112
113
with open('./trainHistoryDictNoBn50', 'wb') as file_pi:
114
pickle.dump(history.history, file_pi)
115
116
# Save model and weights
117
if not os.path.isdir(save_dir):
118
os.makedirs(save_dir)
119
model_path = os.path.join(save_dir, model_name)
120
model.save(model_path)
121
print('Saved trained model at %s ' % model_path)
122
123
# Score trained model.
124
scores = model.evaluate(x_test, y_test, verbose=1)
125
print('Test loss:', scores[0])
126
print('Test accuracy:', scores[1])
127
128
129