-
Notifications
You must be signed in to change notification settings - Fork 1
/
mobilenetv3l.py
80 lines (66 loc) · 3.2 KB
/
mobilenetv3l.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# AIM: To create MobileNetV3Large(transfer learning) class.
# NOTE: For MobileNetV3Large, input preprocessing is part of the model by default. As we use
# the preprocessing layer separately, it just acts as pass through function and has no
# effect whatsoever.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras.applications import MobileNetV3Large
from tensorflow.keras.applications.mobilenet_v3 import preprocess_input
from tensorflow.keras import layers, Model, Input
from tensorflow.keras import backend as K
class MobileNetV3L:
'''
Class to construct MobileNetV3Large model.
'''
@staticmethod
def build(width, height, depth, classes, data_aug, dense_layer):
'''
Static method to build the MobileNetV3Large model architecture.
Parameters:
width (int): Width of the input image.
height (int): Height of the input image.
depth (int): Depth of the input image.
classes (int): Number of output classes to learn to predict.
data_aug (boolean): If value set to True, then add data augmentation layer; else do not add.
dense_layer (boolean): If value set to True, then add dense layer; else do not add.
Returns:
model: Constructed mobilenetv3large network architecture.
'''
# initialize the model along with the input shape to be "channels last" and the channels dimension itself
inputShape = (height, width, depth)
seed = 42 # set seed
# if we are using "channels first", update the input shape and channels dimension
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
# add data augmentation layer if boolean value of dataAugmentation variable is set to 1
data_augmentation = Sequential([
layers.RandomFlip("horizontal", input_shape=inputShape, seed=seed),
layers.RandomZoom(height_factor=-0.4, seed=seed),
layers.RandomTranslation(0.3, 0.2, seed=seed)
])
# defining mobilenetv3large network
base_model = MobileNetV3Large(include_top=False,
input_shape=inputShape,
weights='imagenet')
base_model.trainable=False
# input layer
inputs = Input(shape=inputShape)
# data augmentation layer to be added if data_aug is set to True
if data_aug==True:
x = data_augmentation(inputs) # data augmentation layer
x = preprocess_input(x) # preprocessing layer
else:
x = preprocess_input(inputs) # preprocessing layer
# base model
x = base_model(x, training=False)
x = GlobalAveragePooling2D()(x)
# dense layer to be added if dense_layer is set to True
if dense_layer==True:
x = Dense(128, activation="relu")(x)
x = Dropout(0.2)(x)
# output layer
outputs = Dense(classes, activation="softmax")(x)
model = Model(inputs, outputs)
# model name
model._name = 'MobileNetV3Large'
return model