개요
마스크를 착용한 사람과 착용하지 않은 사람을 인식하는 실험입니다.
- 데이터셋 : https://github.com/prajnasb/observations
- 참고소스 : https://pyimagesearch.com/2020/05/04/covid-19-face-mask-detector-with-opencv-keras-tensorflow-and-deep-learning/
구현
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import AveragePooling2D, Dropout, Flatten, Dense, 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, 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
import cv2
tf.__version__
'2.3.0'
gpus = tf.config.experimental.list_logical_devices('GPU')
gpus
[LogicalDevice(name='/device:GPU:0', device_type='GPU')]
BASE_DIR = os.getcwd()
DATASET_DIR = os.path.join(BASE_DIR, 'datasets')
WITH_MASK_DIR = os.path.join(DATASET_DIR, 'with_mask')
WITHOUT_MASK_DIR = os.path.join(DATASET_DIR, 'without_mask')
INIT_LR = 1e-4 # 0.0001
EPOCHS = 20
BS = 32
with_mask_paths = list(paths.list_images(WITH_MASK_DIR))
without_mask_paths = list(paths.list_images(WITHOUT_MASK_DIR))
image_paths = with_mask_paths + without_mask_paths
data = []
labels = []
for image_path in image_paths:
label = image_path.split(os.path.sep)[-2]
image = load_img(image_path, target_size=(224, 224))
image = img_to_array(image)
image = preprocess_input(image)
data.append(image)
labels.append(label)
data = np.array(data, dtype="float32")
labels = np.array(labels)
def show_images(sample_images):
_,ax = plt.subplots(2,2, figsize=(6,8))
for i in range(len(sample_images)):
img = cv2.imread(str(sample_images[i]))
print("Shape of image: ", img.shape)
ax[i//2, i%2].imshow(img)
ax[i//2, i%2].axis('off')
print('>>> With Mask')
show_images(with_mask_paths[:4])
>>> With Mask
Shape of image: (350, 525, 3)
Shape of image: (350, 525, 3)
Shape of image: (350, 233, 3)
Shape of image: (428, 320, 3)
print('>> Without Mask')
show_images(without_mask_paths[:4])
>> Without Mask
Shape of image: (270, 180, 3)
Shape of image: (225, 154, 3)
Shape of image: (366, 274, 3)
Shape of image: (428, 320, 3)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
labels.shape
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.20, stratify=labels, random_state=42)
trainX.shape, trainY.shape, testX.shape, testY.shape
((1100, 224, 224, 3), (1100, 2), (276, 224, 224, 3), (276, 2))
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")
def build_model():
baseModel = MobileNetV2(weights="imagenet", 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)
# False로 하면 layer weight가 변하지 않는다.
for layer in baseModel.layers:
layer.trainable = False
return model
model = build_model()
model.summary()
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 224, 224, 3) 0
__________________________________________________________________________________________________
Conv1_pad (ZeroPadding2D) (None, 225, 225, 3) 0 input_1[0][0]
__________________________________________________________________________________________________
Conv1 (Conv2D) (None, 112, 112, 32) 864 Conv1_pad[0][0]
__________________________________________________________________________________________________
bn_Conv1 (BatchNormalization) (None, 112, 112, 32) 128 Conv1[0][0]
__________________________________________________________________________________________________
Conv1_relu (ReLU) (None, 112, 112, 32) 0 bn_Conv1[0][0]
__________________________________________________________________________________________________
expanded_conv_depthwise (Depthw (None, 112, 112, 32) 288 Conv1_relu[0][0]
__________________________________________________________________________________________________
expanded_conv_depthwise_BN (Bat (None, 112, 112, 32) 128 expanded_conv_depthwise[0][0]
__________________________________________________________________________________________________
expanded_conv_depthwise_relu (R (None, 112, 112, 32) 0 expanded_conv_depthwise_BN[0][0]
__________________________________________________________________________________________________
expanded_conv_project (Conv2D) (None, 112, 112, 16) 512 expanded_conv_depthwise_relu[0][0
__________________________________________________________________________________________________
expanded_conv_project_BN (Batch (None, 112, 112, 16) 64 expanded_conv_project[0][0]
__________________________________________________________________________________________________
block_1_expand (Conv2D) (None, 112, 112, 96) 1536 expanded_conv_project_BN[0][0]
__________________________________________________________________________________________________
block_1_expand_BN (BatchNormali (None, 112, 112, 96) 384 block_1_expand[0][0]
__________________________________________________________________________________________________
block_1_expand_relu (ReLU) (None, 112, 112, 96) 0 block_1_expand_BN[0][0]
__________________________________________________________________________________________________
block_1_pad (ZeroPadding2D) (None, 113, 113, 96) 0 block_1_expand_relu[0][0]
__________________________________________________________________________________________________
block_1_depthwise (DepthwiseCon (None, 56, 56, 96) 864 block_1_pad[0][0]
__________________________________________________________________________________________________
block_1_depthwise_BN (BatchNorm (None, 56, 56, 96) 384 block_1_depthwise[0][0]
__________________________________________________________________________________________________
block_1_depthwise_relu (ReLU) (None, 56, 56, 96) 0 block_1_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_1_project (Conv2D) (None, 56, 56, 24) 2304 block_1_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_1_project_BN (BatchNormal (None, 56, 56, 24) 96 block_1_project[0][0]
__________________________________________________________________________________________________
block_2_expand (Conv2D) (None, 56, 56, 144) 3456 block_1_project_BN[0][0]
__________________________________________________________________________________________________
block_2_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_2_expand[0][0]
__________________________________________________________________________________________________
block_2_expand_relu (ReLU) (None, 56, 56, 144) 0 block_2_expand_BN[0][0]
__________________________________________________________________________________________________
block_2_depthwise (DepthwiseCon (None, 56, 56, 144) 1296 block_2_expand_relu[0][0]
__________________________________________________________________________________________________
block_2_depthwise_BN (BatchNorm (None, 56, 56, 144) 576 block_2_depthwise[0][0]
__________________________________________________________________________________________________
block_2_depthwise_relu (ReLU) (None, 56, 56, 144) 0 block_2_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_2_project (Conv2D) (None, 56, 56, 24) 3456 block_2_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_2_project_BN (BatchNormal (None, 56, 56, 24) 96 block_2_project[0][0]
__________________________________________________________________________________________________
block_2_add (Add) (None, 56, 56, 24) 0 block_1_project_BN[0][0]
block_2_project_BN[0][0]
__________________________________________________________________________________________________
block_3_expand (Conv2D) (None, 56, 56, 144) 3456 block_2_add[0][0]
__________________________________________________________________________________________________
block_3_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_3_expand[0][0]
__________________________________________________________________________________________________
block_3_expand_relu (ReLU) (None, 56, 56, 144) 0 block_3_expand_BN[0][0]
__________________________________________________________________________________________________
block_3_pad (ZeroPadding2D) (None, 57, 57, 144) 0 block_3_expand_relu[0][0]
__________________________________________________________________________________________________
block_3_depthwise (DepthwiseCon (None, 28, 28, 144) 1296 block_3_pad[0][0]
__________________________________________________________________________________________________
block_3_depthwise_BN (BatchNorm (None, 28, 28, 144) 576 block_3_depthwise[0][0]
__________________________________________________________________________________________________
block_3_depthwise_relu (ReLU) (None, 28, 28, 144) 0 block_3_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_3_project (Conv2D) (None, 28, 28, 32) 4608 block_3_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_3_project_BN (BatchNormal (None, 28, 28, 32) 128 block_3_project[0][0]
__________________________________________________________________________________________________
block_4_expand (Conv2D) (None, 28, 28, 192) 6144 block_3_project_BN[0][0]
__________________________________________________________________________________________________
block_4_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_4_expand[0][0]
__________________________________________________________________________________________________
block_4_expand_relu (ReLU) (None, 28, 28, 192) 0 block_4_expand_BN[0][0]
__________________________________________________________________________________________________
block_4_depthwise (DepthwiseCon (None, 28, 28, 192) 1728 block_4_expand_relu[0][0]
__________________________________________________________________________________________________
block_4_depthwise_BN (BatchNorm (None, 28, 28, 192) 768 block_4_depthwise[0][0]
__________________________________________________________________________________________________
block_4_depthwise_relu (ReLU) (None, 28, 28, 192) 0 block_4_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_4_project (Conv2D) (None, 28, 28, 32) 6144 block_4_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_4_project_BN (BatchNormal (None, 28, 28, 32) 128 block_4_project[0][0]
__________________________________________________________________________________________________
block_4_add (Add) (None, 28, 28, 32) 0 block_3_project_BN[0][0]
block_4_project_BN[0][0]
__________________________________________________________________________________________________
block_5_expand (Conv2D) (None, 28, 28, 192) 6144 block_4_add[0][0]
__________________________________________________________________________________________________
block_5_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_5_expand[0][0]
__________________________________________________________________________________________________
block_5_expand_relu (ReLU) (None, 28, 28, 192) 0 block_5_expand_BN[0][0]
__________________________________________________________________________________________________
block_5_depthwise (DepthwiseCon (None, 28, 28, 192) 1728 block_5_expand_relu[0][0]
__________________________________________________________________________________________________
block_5_depthwise_BN (BatchNorm (None, 28, 28, 192) 768 block_5_depthwise[0][0]
__________________________________________________________________________________________________
block_5_depthwise_relu (ReLU) (None, 28, 28, 192) 0 block_5_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_5_project (Conv2D) (None, 28, 28, 32) 6144 block_5_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_5_project_BN (BatchNormal (None, 28, 28, 32) 128 block_5_project[0][0]
__________________________________________________________________________________________________
block_5_add (Add) (None, 28, 28, 32) 0 block_4_add[0][0]
block_5_project_BN[0][0]
__________________________________________________________________________________________________
block_6_expand (Conv2D) (None, 28, 28, 192) 6144 block_5_add[0][0]
__________________________________________________________________________________________________
block_6_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_6_expand[0][0]
__________________________________________________________________________________________________
block_6_expand_relu (ReLU) (None, 28, 28, 192) 0 block_6_expand_BN[0][0]
__________________________________________________________________________________________________
block_6_pad (ZeroPadding2D) (None, 29, 29, 192) 0 block_6_expand_relu[0][0]
__________________________________________________________________________________________________
block_6_depthwise (DepthwiseCon (None, 14, 14, 192) 1728 block_6_pad[0][0]
__________________________________________________________________________________________________
block_6_depthwise_BN (BatchNorm (None, 14, 14, 192) 768 block_6_depthwise[0][0]
__________________________________________________________________________________________________
block_6_depthwise_relu (ReLU) (None, 14, 14, 192) 0 block_6_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_6_project (Conv2D) (None, 14, 14, 64) 12288 block_6_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_6_project_BN (BatchNormal (None, 14, 14, 64) 256 block_6_project[0][0]
__________________________________________________________________________________________________
block_7_expand (Conv2D) (None, 14, 14, 384) 24576 block_6_project_BN[0][0]
__________________________________________________________________________________________________
block_7_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_7_expand[0][0]
__________________________________________________________________________________________________
block_7_expand_relu (ReLU) (None, 14, 14, 384) 0 block_7_expand_BN[0][0]
__________________________________________________________________________________________________
block_7_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_7_expand_relu[0][0]
__________________________________________________________________________________________________
block_7_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_7_depthwise[0][0]
__________________________________________________________________________________________________
block_7_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_7_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_7_project (Conv2D) (None, 14, 14, 64) 24576 block_7_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_7_project_BN (BatchNormal (None, 14, 14, 64) 256 block_7_project[0][0]
__________________________________________________________________________________________________
block_7_add (Add) (None, 14, 14, 64) 0 block_6_project_BN[0][0]
block_7_project_BN[0][0]
__________________________________________________________________________________________________
block_8_expand (Conv2D) (None, 14, 14, 384) 24576 block_7_add[0][0]
__________________________________________________________________________________________________
block_8_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_8_expand[0][0]
__________________________________________________________________________________________________
block_8_expand_relu (ReLU) (None, 14, 14, 384) 0 block_8_expand_BN[0][0]
__________________________________________________________________________________________________
block_8_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_8_expand_relu[0][0]
__________________________________________________________________________________________________
block_8_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_8_depthwise[0][0]
__________________________________________________________________________________________________
block_8_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_8_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_8_project (Conv2D) (None, 14, 14, 64) 24576 block_8_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_8_project_BN (BatchNormal (None, 14, 14, 64) 256 block_8_project[0][0]
__________________________________________________________________________________________________
block_8_add (Add) (None, 14, 14, 64) 0 block_7_add[0][0]
block_8_project_BN[0][0]
__________________________________________________________________________________________________
block_9_expand (Conv2D) (None, 14, 14, 384) 24576 block_8_add[0][0]
__________________________________________________________________________________________________
block_9_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_9_expand[0][0]
__________________________________________________________________________________________________
block_9_expand_relu (ReLU) (None, 14, 14, 384) 0 block_9_expand_BN[0][0]
__________________________________________________________________________________________________
block_9_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_9_expand_relu[0][0]
__________________________________________________________________________________________________
block_9_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_9_depthwise[0][0]
__________________________________________________________________________________________________
block_9_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_9_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_9_project (Conv2D) (None, 14, 14, 64) 24576 block_9_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_9_project_BN (BatchNormal (None, 14, 14, 64) 256 block_9_project[0][0]
__________________________________________________________________________________________________
block_9_add (Add) (None, 14, 14, 64) 0 block_8_add[0][0]
block_9_project_BN[0][0]
__________________________________________________________________________________________________
block_10_expand (Conv2D) (None, 14, 14, 384) 24576 block_9_add[0][0]
__________________________________________________________________________________________________
block_10_expand_BN (BatchNormal (None, 14, 14, 384) 1536 block_10_expand[0][0]
__________________________________________________________________________________________________
block_10_expand_relu (ReLU) (None, 14, 14, 384) 0 block_10_expand_BN[0][0]
__________________________________________________________________________________________________
block_10_depthwise (DepthwiseCo (None, 14, 14, 384) 3456 block_10_expand_relu[0][0]
__________________________________________________________________________________________________
block_10_depthwise_BN (BatchNor (None, 14, 14, 384) 1536 block_10_depthwise[0][0]
__________________________________________________________________________________________________
block_10_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_10_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_10_project (Conv2D) (None, 14, 14, 96) 36864 block_10_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_10_project_BN (BatchNorma (None, 14, 14, 96) 384 block_10_project[0][0]
__________________________________________________________________________________________________
block_11_expand (Conv2D) (None, 14, 14, 576) 55296 block_10_project_BN[0][0]
__________________________________________________________________________________________________
block_11_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_11_expand[0][0]
__________________________________________________________________________________________________
block_11_expand_relu (ReLU) (None, 14, 14, 576) 0 block_11_expand_BN[0][0]
__________________________________________________________________________________________________
block_11_depthwise (DepthwiseCo (None, 14, 14, 576) 5184 block_11_expand_relu[0][0]
__________________________________________________________________________________________________
block_11_depthwise_BN (BatchNor (None, 14, 14, 576) 2304 block_11_depthwise[0][0]
__________________________________________________________________________________________________
block_11_depthwise_relu (ReLU) (None, 14, 14, 576) 0 block_11_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_11_project (Conv2D) (None, 14, 14, 96) 55296 block_11_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_11_project_BN (BatchNorma (None, 14, 14, 96) 384 block_11_project[0][0]
__________________________________________________________________________________________________
block_11_add (Add) (None, 14, 14, 96) 0 block_10_project_BN[0][0]
block_11_project_BN[0][0]
__________________________________________________________________________________________________
block_12_expand (Conv2D) (None, 14, 14, 576) 55296 block_11_add[0][0]
__________________________________________________________________________________________________
block_12_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_12_expand[0][0]
__________________________________________________________________________________________________
block_12_expand_relu (ReLU) (None, 14, 14, 576) 0 block_12_expand_BN[0][0]
__________________________________________________________________________________________________
block_12_depthwise (DepthwiseCo (None, 14, 14, 576) 5184 block_12_expand_relu[0][0]
__________________________________________________________________________________________________
block_12_depthwise_BN (BatchNor (None, 14, 14, 576) 2304 block_12_depthwise[0][0]
__________________________________________________________________________________________________
block_12_depthwise_relu (ReLU) (None, 14, 14, 576) 0 block_12_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_12_project (Conv2D) (None, 14, 14, 96) 55296 block_12_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_12_project_BN (BatchNorma (None, 14, 14, 96) 384 block_12_project[0][0]
__________________________________________________________________________________________________
block_12_add (Add) (None, 14, 14, 96) 0 block_11_add[0][0]
block_12_project_BN[0][0]
__________________________________________________________________________________________________
block_13_expand (Conv2D) (None, 14, 14, 576) 55296 block_12_add[0][0]
__________________________________________________________________________________________________
block_13_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_13_expand[0][0]
__________________________________________________________________________________________________
block_13_expand_relu (ReLU) (None, 14, 14, 576) 0 block_13_expand_BN[0][0]
__________________________________________________________________________________________________
block_13_pad (ZeroPadding2D) (None, 15, 15, 576) 0 block_13_expand_relu[0][0]
__________________________________________________________________________________________________
block_13_depthwise (DepthwiseCo (None, 7, 7, 576) 5184 block_13_pad[0][0]
__________________________________________________________________________________________________
block_13_depthwise_BN (BatchNor (None, 7, 7, 576) 2304 block_13_depthwise[0][0]
__________________________________________________________________________________________________
block_13_depthwise_relu (ReLU) (None, 7, 7, 576) 0 block_13_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_13_project (Conv2D) (None, 7, 7, 160) 92160 block_13_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_13_project_BN (BatchNorma (None, 7, 7, 160) 640 block_13_project[0][0]
__________________________________________________________________________________________________
block_14_expand (Conv2D) (None, 7, 7, 960) 153600 block_13_project_BN[0][0]
__________________________________________________________________________________________________
block_14_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_14_expand[0][0]
__________________________________________________________________________________________________
block_14_expand_relu (ReLU) (None, 7, 7, 960) 0 block_14_expand_BN[0][0]
__________________________________________________________________________________________________
block_14_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_14_expand_relu[0][0]
__________________________________________________________________________________________________
block_14_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_14_depthwise[0][0]
__________________________________________________________________________________________________
block_14_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_14_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_14_project (Conv2D) (None, 7, 7, 160) 153600 block_14_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_14_project_BN (BatchNorma (None, 7, 7, 160) 640 block_14_project[0][0]
__________________________________________________________________________________________________
block_14_add (Add) (None, 7, 7, 160) 0 block_13_project_BN[0][0]
block_14_project_BN[0][0]
__________________________________________________________________________________________________
block_15_expand (Conv2D) (None, 7, 7, 960) 153600 block_14_add[0][0]
__________________________________________________________________________________________________
block_15_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_15_expand[0][0]
__________________________________________________________________________________________________
block_15_expand_relu (ReLU) (None, 7, 7, 960) 0 block_15_expand_BN[0][0]
__________________________________________________________________________________________________
block_15_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_15_expand_relu[0][0]
__________________________________________________________________________________________________
block_15_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_15_depthwise[0][0]
__________________________________________________________________________________________________
block_15_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_15_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_15_project (Conv2D) (None, 7, 7, 160) 153600 block_15_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_15_project_BN (BatchNorma (None, 7, 7, 160) 640 block_15_project[0][0]
__________________________________________________________________________________________________
block_15_add (Add) (None, 7, 7, 160) 0 block_14_add[0][0]
block_15_project_BN[0][0]
__________________________________________________________________________________________________
block_16_expand (Conv2D) (None, 7, 7, 960) 153600 block_15_add[0][0]
__________________________________________________________________________________________________
block_16_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_16_expand[0][0]
__________________________________________________________________________________________________
block_16_expand_relu (ReLU) (None, 7, 7, 960) 0 block_16_expand_BN[0][0]
__________________________________________________________________________________________________
block_16_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_16_expand_relu[0][0]
__________________________________________________________________________________________________
block_16_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_16_depthwise[0][0]
__________________________________________________________________________________________________
block_16_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_16_depthwise_BN[0][0]
__________________________________________________________________________________________________
block_16_project (Conv2D) (None, 7, 7, 320) 307200 block_16_depthwise_relu[0][0]
__________________________________________________________________________________________________
block_16_project_BN (BatchNorma (None, 7, 7, 320) 1280 block_16_project[0][0]
__________________________________________________________________________________________________
Conv_1 (Conv2D) (None, 7, 7, 1280) 409600 block_16_project_BN[0][0]
__________________________________________________________________________________________________
Conv_1_bn (BatchNormalization) (None, 7, 7, 1280) 5120 Conv_1[0][0]
__________________________________________________________________________________________________
out_relu (ReLU) (None, 7, 7, 1280) 0 Conv_1_bn[0][0]
__________________________________________________________________________________________________
average_pooling2d (AveragePooli (None, 1, 1, 1280) 0 out_relu[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (None, 1280) 0 average_pooling2d[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 128) 163968 flatten[0][0]
__________________________________________________________________________________________________
dropout (Dropout) (None, 128) 0 dense[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 2) 258 dropout[0][0]
==================================================================================================
Total params: 2,422,210
Trainable params: 164,226
Non-trainable params: 2,257,984
__________________________________________________________________________________________________
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
es = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
history = 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,
callbacks=[es])
Epoch 1/20
34/34 [==============================] - ETA: 0s - loss: 0.5160 - accuracy: 0.7566WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 31s 920ms/step - loss: 0.5160 - accuracy: 0.7566 - val_loss: 0.1297 - val_accuracy: 0.9783
Epoch 2/20
34/34 [==============================] - ETA: 0s - loss: 0.1358 - accuracy: 0.9654WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 27s 798ms/step - loss: 0.1358 - accuracy: 0.9654 - val_loss: 0.0545 - val_accuracy: 0.9928
Epoch 3/20
34/34 [==============================] - ETA: 0s - loss: 0.0776 - accuracy: 0.9860WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 31s 914ms/step - loss: 0.0776 - accuracy: 0.9860 - val_loss: 0.0404 - val_accuracy: 0.9928
Epoch 4/20
34/34 [==============================] - ETA: 0s - loss: 0.0540 - accuracy: 0.9850WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 27s 783ms/step - loss: 0.0540 - accuracy: 0.9850 - val_loss: 0.0277 - val_accuracy: 0.9928
Epoch 5/20
34/34 [==============================] - ETA: 0s - loss: 0.0465 - accuracy: 0.9878WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 27s 795ms/step - loss: 0.0465 - accuracy: 0.9878 - val_loss: 0.0263 - val_accuracy: 0.9928
Epoch 6/20
34/34 [==============================] - ETA: 0s - loss: 0.0363 - accuracy: 0.9897WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 29s 866ms/step - loss: 0.0363 - accuracy: 0.9897 - val_loss: 0.0205 - val_accuracy: 0.9928
Epoch 7/20
34/34 [==============================] - ETA: 0s - loss: 0.0284 - accuracy: 0.9944WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 27s 780ms/step - loss: 0.0284 - accuracy: 0.9944 - val_loss: 0.0183 - val_accuracy: 0.9928
Epoch 8/20
34/34 [==============================] - ETA: 0s - loss: 0.0252 - accuracy: 0.9944WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 30s 879ms/step - loss: 0.0252 - accuracy: 0.9944 - val_loss: 0.0176 - val_accuracy: 0.9928
Epoch 9/20
34/34 [==============================] - ETA: 0s - loss: 0.0315 - accuracy: 0.9906WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 27s 802ms/step - loss: 0.0315 - accuracy: 0.9906 - val_loss: 0.0157 - val_accuracy: 0.9928
Epoch 10/20
34/34 [==============================] - ETA: 0s - loss: 0.0202 - accuracy: 0.9934WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 14s 412ms/step - loss: 0.0202 - accuracy: 0.9934 - val_loss: 0.0150 - val_accuracy: 0.9964
Epoch 11/20
34/34 [==============================] - ETA: 0s - loss: 0.0210 - accuracy: 0.9925WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 270ms/step - loss: 0.0210 - accuracy: 0.9925 - val_loss: 0.0151 - val_accuracy: 0.9964
Epoch 12/20
34/34 [==============================] - ETA: 0s - loss: 0.0131 - accuracy: 0.9981WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 273ms/step - loss: 0.0131 - accuracy: 0.9981 - val_loss: 0.0134 - val_accuracy: 0.9964
Epoch 13/20
34/34 [==============================] - ETA: 0s - loss: 0.0178 - accuracy: 0.9934WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 273ms/step - loss: 0.0178 - accuracy: 0.9934 - val_loss: 0.0134 - val_accuracy: 0.9928
Epoch 14/20
34/34 [==============================] - ETA: 0s - loss: 0.0124 - accuracy: 0.9963WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 12s 365ms/step - loss: 0.0124 - accuracy: 0.9963 - val_loss: 0.0120 - val_accuracy: 0.9964
Epoch 15/20
34/34 [==============================] - ETA: 0s - loss: 0.0114 - accuracy: 0.9981WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 10s 290ms/step - loss: 0.0114 - accuracy: 0.9981 - val_loss: 0.0115 - val_accuracy: 0.9964
Epoch 16/20
34/34 [==============================] - ETA: 0s - loss: 0.0150 - accuracy: 0.9934WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 272ms/step - loss: 0.0150 - accuracy: 0.9934 - val_loss: 0.0113 - val_accuracy: 0.9964
Epoch 17/20
34/34 [==============================] - ETA: 0s - loss: 0.0144 - accuracy: 0.9953WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 274ms/step - loss: 0.0144 - accuracy: 0.9953 - val_loss: 0.0112 - val_accuracy: 0.9964
Epoch 18/20
34/34 [==============================] - ETA: 0s - loss: 0.0120 - accuracy: 0.9944WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 273ms/step - loss: 0.0120 - accuracy: 0.9944 - val_loss: 0.0111 - val_accuracy: 0.9964
Epoch 19/20
34/34 [==============================] - ETA: 0s - loss: 0.0085 - accuracy: 0.9981WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 273ms/step - loss: 0.0085 - accuracy: 0.9981 - val_loss: 0.0110 - val_accuracy: 0.9964
Epoch 20/20
34/34 [==============================] - ETA: 0s - loss: 0.0123 - accuracy: 0.9963WARNING:tensorflow:Can save best model only with val_acc available, skipping.
34/34 [==============================] - 9s 277ms/step - loss: 0.0123 - accuracy: 0.9963 - val_loss: 0.0136 - val_accuracy: 0.9928
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
checkpoint_save_path = os.path.join(BASE_DIR, 'checkpoint', 'mask_model.h5')
model.save(checkpoint_save_path, save_format="h5")
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_))
precision recall f1-score support
with_mask 0.99 0.99 0.99 138
without_mask 0.99 0.99 0.99 138
accuracy 0.99 276
macro avg 0.99 0.99 0.99 276
weighted avg 0.99 0.99 0.99 276
sample_X = testX[:9]
sample_pred_idxs = model.predict(sample_X)
sample_pred_idxs = np.argmax(sample_pred_idxs, axis=1)
_,ax = plt.subplots(3,3, figsize=(10,8))
for i in range(len(sample_pred_idxs)):
sample_pred = sample_pred_idxs[i]
ax[i//3, i%3].set_title(lb.classes_[sample_pred])
ax[i//3, i%3].imshow(sample_X[i])
ax[i//3, i%3].axis('off')
'AI 인공지능 > Research' 카테고리의 다른 글
월리를 찾아라 (0) | 2022.03.01 |
---|---|
바코드, QR 코드 인식할 수 있을까? (0) | 2022.02.16 |
지문을 인식할 수 있을까? (0) | 2022.02.15 |
이미지 캡차를 인식할 수 있을까? (0) | 2022.02.14 |