<jupyter_start><jupyter_text>Importing librariesProject will require tensorflow and numpy. Tensorflow will be used to load the dataset and preprocess the image to fit into a numpy array.<jupyter_code>import tensorflow as tf
import numpy as np
tf.__version__<jupyter_output><empty_output><jupyter_text>Importing dataset<jupyter_code>!wget https://www.dropbox.com/s/o3t9lfelgtgmdkw/Day%202%20-%20Mask%20Detection.zip
!unzip "Day 2 - Mask Detection.zip"
!rm "Day 2 - Mask Detection.zip"
width = 224
height = 224
#height and width are (224,224) since we will use the mobileNetv2 network
batch_size = 32
data_dir = "/content/dataset"
# /content/dataset
training = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.3,
    subset='training',
    seed=123,
    image_size=(height, width),
    batch_size=batch_size
)
validation = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.3,
    subset='validation',
    seed=123,
    image_size=(height, width),
    batch_size=batch_size
)
classes = training.class_names
classes
training<jupyter_output><empty_output><jupyter_text>Visualize images form datasetverfiy the progress is by loading the dataset into an image using matplotlib<jupyter_code>import matplotlib.pyplot as plt
for images, labels in training.take(1):
    plt.imshow(images[1].numpy().astype('uint8'))
    plt.title(classes[labels[1]])<jupyter_output><empty_output><jupyter_text>Loading the MobileNetv2 modelMobileNetv2 model is a pretained model that can used for object detection.Use keras to load the network.<jupyter_code>from tensorflow.keras.applications import MobileNetV2
model = MobileNetV2(weights='imagenet')<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5
14540800/14536120 [==============================] - 0s 0us/step
14548992/14536120 [==============================] - 0s 0us/step<jupyter_text>Combining the modelAfter modeling the mobilenetv2 architecture, the compilation process starts where the we are using the adam optimizer and SparCategoricalCrossentropy as loss. Whereas, metrics is accuracy.<jupyter_code>model.compile(optimizer='adam', 
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), 
              metrics=['accuracy'])
model.summary()<jupyter_output>Model: "mobilenetv2_1.00_224"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 224, 224, 3) 0                                            
__________________________________________________________________________________________________
Conv1 (Conv2D)                  (None, 112, 112, 32) 864         input_1[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][...]<jupyter_text>Training the model<jupyter_code>face_mask_detection = model.fit(training,validation_data=validation,epochs=4)<jupyter_output>Epoch 1/4
81/81 [==============================] - ETA: 0s - loss: 0.1810 - accuracy: 0.9660<jupyter_text>Predicting on a new unknown image<jupyter_code>#load the image
img = tf.keras.preprocessing.image.load_img('face.jpg', target_size=(height, width))
#convert to array
image_array = tf.keras.preprocessing.image.img_to_array(img)
#expand dimensions to fit in the model
image_array = tf.expand_dims(image_array,0)
#check the shape of the image
image_array.shape
predictions = model.predict(image_array)
#using softmax to get the results
score = tf.nn.softmax(predictions[0])
import numpy
print(classes[numpy.argmax(score)])
#load the image
img = tf.keras.preprocessing.image.load_img('/content/dataset/without_mask/0_0_aidai_0084.jpg', target_size=(height, width))
#convert to array
image_array = tf.keras.preprocessing.image.img_to_array(img)
#expand dimensions to fit in the model
image_array = tf.expand_dims(image_array,0)

predictions = model.predict(image_array)
#using softmax to get the results
score = tf.nn.softmax(predictions[0])

print(classes[numpy.argmax(score)])
#load the image
img = tf.keras.preprocessing.image.load_img('/content/dataset/with_mask/0_0_0 copy 17.jpg', target_size=(height, width))
#convert to array
image_array = tf.keras.preprocessing.image.img_to_array(img)
#expand dimensions to fit in the model
image_array = tf.expand_dims(image_array,0)

predictions = model.predict(image_array)
#using softmax to get the results
score = tf.nn.softmax(predictions[0])

print(classes[numpy.argmax(score)])<jupyter_output>with_mask<jupyter_text>New Section<jupyter_code>model.save('dummy.model', save_format="h5")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1261226829734378, 'fraction_numerical': 0.03573476017580737, 'mean_word_length': 5.485749690210657, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '584414', 'license': 'Apache-2.0', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1552, 'n_words': 338, 'path': 'mask detection/mask_detection .ipynb', 'repo_name': 'Ritikpal/Basic_ML_Projetcs', 'size': '5233'}