-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaskmodel.py
53 lines (41 loc) · 1.61 KB
/
maskmodel.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
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define the path to your dataset
dataset_path = "E:\Face Mask Dataset"
# Use ImageDataGenerator for data augmentation
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
# Load and preprocess training data
train_generator = datagen.flow_from_directory(
dataset_path,
target_size=(224, 224),
batch_size=32,
class_mode='binary', # or 'categorical' if you have more than two classes
subset='training'
)
# Load and preprocess validation data
validation_generator = datagen.flow_from_directory(
dataset_path,
target_size=(224, 224),
batch_size=32,
class_mode='binary',
subset='validation'
)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid')) # Use 'softmax' if more than two classes
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(
train_generator,
epochs=10, # Adjust the number of epochs based on your dataset and resources
validation_data=validation_generator
)
model.save('face_mask_detection_model.h5')