-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
101 lines (75 loc) · 2.44 KB
/
models.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
import tensorflow as tf
import keras
from keras import layers, optimizers
def load_resnet_based(input_shape=(100, 100, 3), learning_rate=1e-3):
"""
Load pretrained ResNet-50 + Dense model
Parameters
----------
input_shape : tuple
Input image shape (height, width, channels)
learning_rate : float
Model learning rate
Returns
-------
Tensorflow model
"""
from tensorflow.keras.applications import ResNet50
resnet = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
# Make last 4 ResNet-50 layers trainable
resnet.trainable = False
for x in resnet.layers[-4:]:
x.trainable = True
# print(f'Layer: {x.name} | Parameters: {x.count_params()}')
# Add Dense layers
model = tf.keras.Sequential([
resnet,
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(100, activation='elu'),
layers.Dropout(0.5),
layers.Dense(50, activation='elu'),
layers.Dropout(0.5),
layers.Dense(10, activation='elu'),
layers.Dropout(0.5),
layers.Dense(1)
])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
loss='mse',
metrics=['mse']
)
return model
def load_nvidia(input_shape=(100, 100, 3), learning_rate=1e-3):
"""
Load NVIDIA model
Parameters
----------
input_shape : tuple
Input image shape (height, width, channels)
learning_rate : float
Model learning rate
Returns
-------
Tensorflow model
"""
model = keras.Sequential([
layers.Conv2D(24, kernel_size=5, strides=2, padding='same', input_shape=input_shape, activation='relu'),
layers.Conv2D(36, kernel_size=5, strides=2, padding='same', activation='relu'),
layers.Conv2D(48, kernel_size=5, strides=2, padding='same', activation='relu'),
layers.Conv2D(64, kernel_size=3, strides=2, padding='same', activation='relu'),
layers.Conv2D(64, kernel_size=3, strides=2, padding='same', activation='relu'),
layers.Flatten(),
layers.Dense(100, activation='relu'),
layers.Dense(50, activation='relu'),
layers.Dense(10, activation='relu'),
layers.Dense(1)
])
model.compile(
loss='mse',
optimizer=optimizers.Adam(learning_rate=learning_rate),
metrics=['mse']
)
return model
if __name__ == '__main__':
pass