-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
105 lines (91 loc) · 4.24 KB
/
model.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
import tensorflow as tf
from keras.optimizers import RMSprop
from tensorflow import keras
from keras.layers import Flatten, Dense, MaxPool2D, Conv2D, Dropout, BatchNormalization
from keras.datasets import mnist
import matplotlib.pyplot as plt
from google.colab import files
from keras.optimizers import RMSprop
%matplotlib inline
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from tensorflow.keras.datasets import mnist
from keras.utils.np_utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
img_rows, img_cols = 28, 28
x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,1)
x_test =x_test.reshape(x_test.shape[0],img_rows,img_cols,1)
x_train=x_train/255
x_test=x_test/255
from keras.utils import to_categorical
num_classes = 10
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)
##model
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
#---------------------------------------------------------------------
model = tf.keras.models.Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape = (28,28,1)),
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization(),
MaxPool2D(pool_size=(2, 2)),
Dropout(0.25),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same' ),
BatchNormalization(),
MaxPool2D(pool_size=(2, 2)),
Dropout(0.25),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same' ),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same' ),
BatchNormalization(),
MaxPool2D(pool_size=(2, 2)),
Dropout(0.25),
Flatten(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.4),
Dense(64, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(10, activation = "softmax")
])
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range = 0.1, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(x_train)
callbacks1 = [
EarlyStopping(monitor = 'loss', patience = 6),
ReduceLROnPlateau(monitor = 'loss', patience = 3),
ModelCheckpoint('model.best.hdf5', save_best_only=True) # saving the best model
]
optimizer = RMSprop(lr = 0.001, rho = 0.9, epsilon=1e-08, decay =0.0)
model.summary()
model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
batch_size = 128
model.fit(datagen.flow(x_train,y_train, batch_size=batch_size), epochs = 50,
steps_per_epoch = x_train.shape[0] // batch_size,
validation_data = (x_test, y_test),
callbacks = callbacks1,
)
loss,acc = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', loss)
print('Test accuracy:', (100*acc))
model.save("test_model.h5")