-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
54 lines (33 loc) · 1.68 KB
/
train_model.py
File metadata and controls
54 lines (33 loc) · 1.68 KB
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
import tensorflow as tf
from tensorflow import keras
import numpy as np
TRAIN_PATH = "images/train"
VALIDATION_PATH = "images/validate"
# labels
TRAIN_LABELS = np.array([0, 1], dtype='float')
VALIDATION_LABELS = np.array([0, 1], dtype='float')
# TRAINING
training_data = keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest")
train = training_data.flow_from_directory(directory=TRAIN_PATH, shuffle=True, target_size=(226, 226), class_mode="binary")
# VALIDATION
validation_data = keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest")
validation = training_data.flow_from_directory(directory=VALIDATION_PATH, shuffle=False, target_size=(226, 226), class_mode="binary")
model = keras.Sequential([
keras.layers.Conv2D(64, (24, 24), activation="relu", input_shape=(226, 226, 3)),
keras.layers.MaxPooling2D(2, 2),
keras.layers.Conv2D(64, (24, 24), activation="relu", input_shape=(226, 226, 3)),
keras.layers.MaxPooling2D(2, 2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dense(2, activation="softmax")
])
# COMPILING THE MODEL
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.summary()
# TRAINING THE MODEL
model.fit(train, epochs=2)
# EVALUATING THE MODEL
model.evaluate(validation)
model.save("mask_detect.model")
print("MODEL SAVED")