-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
106 lines (86 loc) · 3.74 KB
/
model.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import tensorflow as tf
from config import NUM_CLASSES
from efficient_net_b0 import EfficientNetB0
from mobile_net_v2_model import MobileNetV2
from tensorflow.keras.metrics import TopKCategoricalAccuracy
from tensorflow.keras.layers import Dropout, Dense
from tensorflow.keras import Input
from tensorflow.keras.models import Model
from tensorflow.keras.applications.densenet import DenseNet121
# from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
from tensorflow.keras.applications.nasnet import NASNetMobile
# from tensorflow.keras.applications.efficientnet import EfficientNetB0
def build_densenet121_model(input_shape=[None, 128, 3], dropout=0,
optimizer=None, pretraining=True):
# setup model
weights = 'imagenet' if pretraining else None
inputs = Input(shape=input_shape)
# inputs = tf.keras.applications.mobilenet.preprocess_input(inputs)
x = DenseNet121(input_shape=input_shape, weights=weights,
include_top=False, pooling='avg')(inputs)
x = Dropout(dropout)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
# setup the metrics
metrics = [
TopKCategoricalAccuracy(k=1, name='top_1', dtype=tf.float32)
]
# compile the model
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=metrics)
return model
def build_mobilenetv2_model(input_shape=[None, 128, 3], dropout=0,
optimizer=None, pretraining=True):
# setup model
weights = "imagenet" if pretraining else None
inputs = Input(shape=input_shape)
x = MobileNetV2(input_shape=input_shape, weights=weights,
include_top=False, pooling="avg")(inputs)
x = Dropout(dropout)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
# setup the metrics
metrics = [
TopKCategoricalAccuracy(k=1, name='top_1', dtype=tf.float32)
]
# compile the model
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=metrics)
return model
def build_nasnetmobile_model(input_shape=[None, 128, 3], dropout=0,
optimizer=None, pretraining=True):
# setup model
weights = "imagenet" if pretraining else None
inputs = Input(shape=input_shape)
x = NASNetMobile(input_shape=input_shape, weights=weights,
include_top=False, pooling="avg")(inputs)
x = Dropout(dropout)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
# setup the metrics
metrics = [
TopKCategoricalAccuracy(k=1, name='top_1', dtype=tf.float32)
]
# compile the model
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=metrics)
return model
def build_efficientnet_model(input_shape=[None, 128, 3], dropout=0,
optimizer=None, pretraining=True):
# setup model
weights = "imagenet" if pretraining else None
inputs = Input(shape=input_shape)
# inputs = tf.keras.layers.Resizing(128, 224)(inputs)
x = EfficientNetB0(input_shape=input_shape, weights=weights,
include_top=False, pooling="avg")(inputs)
x = Dropout(dropout)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
# setup the metrics
metrics = [
TopKCategoricalAccuracy(k=1, name='top_1', dtype=tf.float32)
]
# compile the model
model.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=metrics)
return model