This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·100 lines (87 loc) · 1.81 KB
/
build.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
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.initializers import RandomUniform
from keras.layers import (
Conv2D,
MaxPooling2D,
Flatten,
Dense
)
from keras.preprocessing.image import ImageDataGenerator
classifier = Sequential()
classifier.add(
Conv2D(32, (3, 3),
input_shape=(64, 64, 3),
activation='relu')
)
classifier.add(
Conv2D(64, (2, 2),
input_shape=(32, 3, 3),
activation='relu')
)
classifier.add(
MaxPooling2D(pool_size=(2, 2))
)
classifier.add(Flatten())
classifier.add(Dense(
activation='relu',
units=128,
kernel_initializer=RandomUniform(minval=0.0, maxval=0.0001)
))
classifier.add(Dense(
activation='sigmoid',
units=96
))
classifier.add(Dense(
activation='sigmoid',
units=64
))
classifier.add(Dense(
activation='sigmoid',
units=16
))
classifier.add(Dense(
activation='softmax',
units=5
))
classifier.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=[
'accuracy'
]
)
train_datagon = ImageDataGenerator(
rescale=1./255,
width_shift_range=0.05,
height_shift_range=0.05
)
test_datagon = ImageDataGenerator(rescale=1./255)
training_set = train_datagon.flow_from_directory(
'dataset/train',
target_size=(64, 64),
batch_size=32,
class_mode='categorical'
)
print(training_set.class_indices)
test_set = test_datagon.flow_from_directory(
'dataset/test',
target_size=(64, 64),
batch_size=32,
class_mode='categorical'
)
classifier.fit_generator(
training_set,
steps_per_epoch=180,
epochs=10,
validation_data=test_set,
validation_steps=500,
callbacks=[
ModelCheckpoint('./model.h5', monitor='val_loss', save_best_only=False)
]
)
scores = classifier.evaluate_generator(test_set, steps=5)
print('%s: %.2f%%' % (
classifier.metrics_names[1],
scores[1] * 100
))