Das erste, was wir brauchen, ist ein Datensatz (Beispiel von Fotos) von Personen mit und ohne Maske fĂŒr das zusĂ€tzliche Training des öffentlich zugĂ€nglichen neuronalen Netzwerks MobileNetV2. Ich hatte einen Datensatz von 981 Fotos von maskierten Personen und dieselbe Anzahl ohne dieselben Personen.
Ich möchte einen wichtigen Punkt erwĂ€hnen, dass das neuronale MobileNetV2-Netzwerk fĂŒr fast jede Klassifizierung verwendet werden kann. Beispielsweise war es möglich, es neu zu trainieren, um das Geschlecht zu bestimmen, oder zu versuchen, automatisch eine Person zu bestimmen, die eine Brille trĂ€gt oder nicht, weshalb wir Frieren Sie alle Basisebenen des Modells ein, und in der oberen Ebene wird das bereitgestellt, was klassifiziert werden muss. Wir werden uns jedoch auf die Suche nach einer medizinischen Maske konzentrieren, die derzeit am relevantesten ist.
Platzieren wir also unseren Datensatz mit 1962 Fotos in zwei Verzeichnissen im Datensatzordner mit Masken in "WithMask" bzw. ohne Maske in "Withoutmask". Jedes enthÀlt 981 Fotografien. Ein weiterer wichtiger Hinweis ist, dass wir es auf den Gesichtern neu trainieren und nicht nur, dass die Person auf dem Bild eine Maske trÀgt oder nicht, obwohl dies hÀtte sein können.
Als nÀchstes importieren wir die erforderlichen Bibliotheken:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import os
# Geben Sie die anfÀnglichen Hyperparameter an
â , ,
INIT_LR = 0,004
â ,
EPOCHS = 20
â , .
BS = 32
, , , .
imagePaths = list(paths.list_images (r'C:\dataset')) #
data , labels = [] , []
for imagePath in imagePaths:
# ( )
label = imagePath.split(os.path.sep)[-2]
# 224224
image = load_img(imagePath, target_size = (224, 224))
image = img_to_array(image)
image = preprocess_input(image)
#
data.append(image)
labels.append(label)
# NumPy
data = np.array(data, dtype="float32")
labels = np.array(labels)
# , .. 0 1
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
# 80% 20%;
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size = 0.20, stratify = labels, random_state = 42)
#
aug = ImageDataGenerator(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")
# c
path_weights = âmobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5'
baseModel = MobileNetV2(weights=path_weights,
include_top=False, input_tensor=Input(shape=(224, 224, 3))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size = (7, 7))(headModel)
headModel = Flatten(name = "flatten")(headModel)
headModel = Dense(128, activation = "relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation = "softmax")(headModel)
model = Model(inputs = baseModel.input, outputs = headModel)
#
for layer in baseModel.layers:
layer.trainable = False
#
opt = Adam(lr = INIT_LR, decay = INIT_LR / EPOCHS)
model.compile(loss = "binary_crossentropy", optimizer = opt, metrics = ["accuracy"])
#
H = model.fit( aug.flow(trainX, trainY, batch_size = BS), steps_per_epoch = len(trainX) // BS,
validation_data = (testX, testY), validation_steps = len(testX) // BS, epochs = EPOCHS)
#
predIdxs = model.predict(testX, batch_size = BS)
# ,
predIdxs = np.argmax(predIdxs, axis=1)
#
print(classification_report(testY.argmax(axis = 1), predIdxs, target_names = lb.classes_))
#
model.save('model_mask_FACE', save_format = "h5")
model_mask = tf.keras.models.load_model('model_mask_FACE)
,
# , MTCNN
frame = cv2.cvtColor(cv2.imread(âhouse.png'), cv2.COLOR_BGR2RGB)
frame_image = Image.fromarray(frame)
boxes, probs, landmarks = mtcnn.detect(frame_image, landmarks = True)
x1, y1, x2, y2 = [int(bx) for bx in boxes[0]]
image = Image.fromarray(frame[y1:y2, x1:x2]).resize((224,224))
face = img_to_array(image)
#
face = preprocess_input(face)
face = np.expand_dims(face, axis=0)
#
(mask, withoutMask) = model_mask.predict(face)[0]
image = cv2.imread(âhouse.pngâ)
#
if mask > withoutMask and max(mask, withoutMask) > 0.8: #
label = "Mask" if mask > withoutMask else "No Mask"
color = (0, 122, 0) if label == "Mask" else (0, 0, 122)
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
cv2.putText(image, label, (x1, y1 - 10),cv2.FONT_HERSHEY_SIMPLEX, 2, color, 5)
cv2.rectangle(image, (x1, y1), (x2, y2), color, 5)
y = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
In diesem Artikel haben wir gezeigt, wie das neuronale MobileNetV2-Netzwerk neu trainiert wird, um die Bilder von Personen mit und ohne medizinische Masken zu klassifizieren.