<jupyter_start><jupyter_text>Load data train path<jupyter_code>path = "/drive/My Drive/FaceMask/data/train/"
train_csv_df = pd.DataFrame(pd.read_csv("/drive/My Drive/FaceMask/data/train.csv"))
train_csv_df.head()
imgPath = loadData(path,train_csv_df)<jupyter_output><empty_output><jupyter_text>Load data test path<jupyter_code>testPath = "/drive/My Drive/FaceMask/data/test/"
test_csv_df = pd.DataFrame(pd.read_csv("/drive/My Drive/FaceMask/data/test.csv"))
test_csv_df.head()
imgTest = loadData(testPath,test_csv_df)<jupyter_output><empty_output><jupyter_text>Get data train and data test<jupyter_code>train_images_array = loadImages(imgPath, (300,300))
test_images_array = loadImages(imgTest, (224,224))<jupyter_output><empty_output><jupyter_text>Get the labels<jupyter_code>train_labels_array = loadLabels(train_csv_df)
test_labels_array = loadLabels(test_csv_df)<jupyter_output><empty_output><jupyter_text>Augmentasi data train<jupyter_code>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")
# Loading the MobileNetV2 network, with the topmost layer removed

base_model = MobileNetV2(weights="imagenet", include_top=False,
                        input_tensor=Input(shape=(224, 224, 3)))


# Freeze the layer of the base model to make them untrainable.
# This ensures that their weights are not updated when we train the model.

for layer in base_model.layers:
    layer.trainable = False
    
# Construct head of the model that will be attached on top of the base model:

head_model = base_model.output
head_model = AveragePooling2D(pool_size=(7, 7))(head_model)
head_model = Flatten(name="flatten")(head_model)
head_model = Dense(128, activation="relu")(head_model)
head_model = Dropout(0.5)(head_model)
head_model = Dense(2, activation="softmax")(head_model)


# Combine the head and base of the models together:

my_model = Model(inputs=base_model.input, outputs=head_model)
my_model.summary()
INIT_LR = 1e-4
EPOCHS = 20
BATCH_SIZE = 32
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
my_model.compile(loss="binary_crossentropy", optimizer=opt,
                metrics=["accuracy"])
history = my_model.fit(
    aug.flow(train_images_array, train_labels_array, batch_size=BATCH_SIZE),
    steps_per_epoch=len(train_images_array) // BATCH_SIZE,
    validation_data=(test_images_array, test_labels_array),
    validation_steps=len(test_images_array)//BATCH_SIZE,
    epochs=EPOCHS)
my_model.save("/drive/My Drive/FaceMask/model.h5")<jupyter_output><empty_output><jupyter_text>EVALUATE MODEL<jupyter_code>results = my_model.evaluate(test_images_array, test_labels_array, batch_size=128)<jupyter_output>2/2 [==============================] - 0s 174ms/step - loss: 0.1100 - accuracy: 0.9845<jupyter_text>PLOT RESULT<jupyter_code>train_acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

train_loss = history.history['loss']
val_loss = history.history['val_loss']
plt.rcParams['figure.figsize'] = [10, 5]

# Plot training & validation accuracy values
plt.plot(train_acc)
plt.plot(val_acc)
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.savefig('/drive/My Drive/FaceMask/accuracy.png')
plt.legend(['Train', 'Test'])
# Plot training & validation loss values
plt.plot(train_loss)
plt.plot(val_loss)
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.savefig('/drive/My Drive/FaceMask/lost.png')
plt.legend(['Train', 'Test'])<jupyter_output><empty_output><jupyter_text>PREDICT<jupyter_code>my_model = load_model('D:\PROGRAMING\Python\Face Mask/model.h5')
img = image.load_img('D:\PROGRAMING\Python\Face Mask/1.jpg',target_size=(224,224))
plt.imshow(img)
plt.axis('off')
plt.show()
img = np.array(img, dtype='float')
img = img.reshape(1, 224, 224, 3)
prediksi = my_model.predict(img)
idx = np.argmax(prediksi)
percentage = "%.2f" % (prediksi[0][idx] * 100)
print(str(percentage)+" %")
if (idx):
    print("Wearing Masker")
else:
    print("Not Wearing Masker")<jupyter_output>64.68 %
Wearing Masker
{'fraction_non_alphanumeric': 0.14091019712825506, 'fraction_numerical': 0.023363348746653686, 'mean_word_length': 6.682242990654205, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '9308', 'license': 'MIT', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1493, 'n_words': 248, 'path': '.ipynb_checkpoints/FaceMask-checkpoint.ipynb', 'repo_name': 'evanezcent/Face-Mask-Detection', 'size': '4109'}