File size: 1,069 Bytes
6c222bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
from tensorflow.keras import layers

IMAGE_SIZE = (224, 224)
CLASS_NAMES = ["human", "non_human"]


def build_model():
    inputs = tf.keras.Input(shape=(*IMAGE_SIZE, 3), name="rgb_image")

    
    x = layers.RandomFlip("horizontal", name="augment_flip")(inputs)
   
    x = layers.RandomZoom((-0.2, 0.2), name="augment_zoom")(x)
    
    x = layers.RandomRotation(0.05, name="augment_rotation")(x)

    x = layers.Rescaling(1.0 / 127.5, offset=-1.0, name="mobilenetv2_scale")(x)

    #Pretrained Base Network
    base = tf.keras.applications.MobileNetV2(
        include_top=False,
        weights="imagenet",
        input_shape=(*IMAGE_SIZE, 3),
    )
    base.trainable = False

    #Classifier Head 
    x = base(x, training=False)
    x = layers.GlobalAveragePooling2D(name="global_average_pool")(x)
    x = layers.Dropout(0.2, name="dropout")(x)
    outputs = layers.Dense(1, activation="sigmoid", name="non_human_probability")(x)

    return tf.keras.Model(inputs, outputs, name="human_face_classifier")