-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_model.py
90 lines (68 loc) · 2.4 KB
/
build_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
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense, Dropout
model = Sequential()
# conv layer 1
model.add(Convolution2D(filters = 32,
kernel_size = (3,3),
activation = "relu",
input_shape = (28,28,1)))
# conv layer 2
model.add(Convolution2D(filters = 32,
kernel_size = (3,3),
activation = "relu"))
# pooling layer 1
model.add(MaxPooling2D(pool_size=(2,2)))
# deactivating some elements
model.add(Dropout(.20))
# conv layer 3
model.add(Convolution2D(filters = 64,
kernel_size = (3,3),
activation = "relu"))
# conv layer 4
model.add(Convolution2D(filters = 64,
kernel_size = (3,3),
activation = "relu"))
# pooling layer 2
model.add(MaxPooling2D(pool_size=(2,2)))
# deactivating some elements
model.add(Dropout(.1))
# flatten layer
model.add(Flatten())
# fully connected layer
model.add(Dense(256, activation="relu"))
model.add(Dense(output_dim=5, activation="softmax"))
model.compile(optimizer = "adam", loss="categorical_crossentropy", metrics=["accuracy"])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=False,
fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(
'data/train_set',
target_size=(28, 28),
color_mode="grayscale",
batch_size=128,
class_mode='categorical')
test_set = test_datagen.flow_from_directory(
'data/test_set',
target_size=(28, 28),
color_mode="grayscale",
batch_size=128,
class_mode='categorical')
model.fit_generator(
training_set,
steps_per_epoch=12000,
epochs=5,
validation_data=test_set,
validation_steps=3000)
model.save("model_new.h5")
model_json = model.to_json()
with open("model_new.json", "w") as json_file:
json_file.write(model_json)