-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
146 lines (97 loc) · 3.04 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Importing libraries
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
from keras import backend as K
import numpy as np
from keras.preprocessing import image
from keras.callbacks import History
import matplotlib.pyplot as plt
# Model / data parameters
img_width, img_height = 300, 200
num_classes = 3
batch_size = 2
inputShape = (img_width, img_height, 3)
train_data_dir = "../dataset/RockPaperScissors"
validation_data_dir = "../dataset/RockPaperScissors"
# Data Augmentation
datagen = ImageDataGenerator(
rescale = 1. / 255,
validation_split = 0.25
)
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
subset = "training",
class_mode = "categorical"
)
val_datagen = ImageDataGenerator(
rescale = 1. / 255
)
validation_generator = datagen.flow_from_directory(
validation_data_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
subset = "validation",
class_mode = "categorical"
)
#THE MODEL
model = keras.Sequential()
# Set image shape: input_shape=(32, 32, 3)
model.add(Conv2D(32, kernel_size = (3, 3), input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.3))
model.add(Conv2D(32, kernel_size = (3, 3), input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size = (3, 3), input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = (3, 3), input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(3, activation='softmax'))
# Training
rms = keras.optimizers.RMSprop(learning_rate= 0.5, rho = 0.9)
model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['categorical_accuracy'])
history = History()
# fit wda
#batch size = 2
model.fit_generator(
train_generator,
steps_per_epoch = 821,
epochs = 3, callbacks = [history],
validation_data = validation_generator,
validation_steps = 656
)
# Saving Model
model.save_weights("model.h5")
print("Saved model to disk")
model.summary()
print(history.history.keys())
plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()