forked from NeerajG03/Modified-Project-Tomato
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
90 lines (77 loc) · 3.72 KB
/
model.py
File metadata and controls
90 lines (77 loc) · 3.72 KB
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
import json
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import BatchNormalization, Dense, Flatten, Dropout
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.applications import MobileNetV2, DenseNet201, ResNet152V2, VGG19, InceptionV3
def build_model(config_file="config.json"):
config = json.load(open(config_file, "r"))
# Model Selection
backbone = None
if config["model_configuration"]["backbone_name"] == "mobilenetv2":
print(f"[INFO]: Selected Model: {config['model_configuration']['backbone_name']}")
backbone = MobileNetV2(input_shape=(config["img_width"], config["img_height"], 3),
include_top=False,
pooling="max",
weights="imagenet")
elif config["model_configuration"]["backbone_name"] == "densenet201":
print(f"[INFO]: Selected Model: {config['model_configuration']['backbone_name']}")
backbone = DenseNet201(input_shape=(config["img_width"], config["img_height"], 3),
include_top=False,
pooling="max",
weights="imagenet")
elif config["model_configuration"]["backbone_name"] == "resnet152v2":
print(f"[INFO]: Selected Model: {config['model_configuration']['backbone_name']}")
backbone = ResNet152V2(input_shape=(config["img_width"], config["img_height"], 3),
include_top=False,
pooling="max",
weights="imagenet")
elif config["model_configuration"]["backbone_name"] == "vgg19":
print(f"[INFO]: Selected Model: {config['model_configuration']['backbone_name']}")
backbone = VGG19(input_shape=(config["img_width"], config["img_height"], 3),
include_top=False,
pooling="max",
weights="imagenet")
elif config["model_configuration"]["backbone_name"] == "inceptionv3":
print(f"[INFO]: Selected Model: {config['model_configuration']['backbone_name']}")
backbone = InceptionV3(input_shape=(config["img_width"], config["img_height"], 3),
include_top=False,
pooling="max",
weights="imagenet")
else:
identifier = config["model_configuration"]["backbone_name"]
print(f"[ERROR]: No application module found with identifier: {identifier}")
# Setting the transfer learning mode
backbone.trainable = True
# Creating Sequential Model
model = Sequential()
model.add(backbone)
if config["add_dense"]:
model.add(BatchNormalization())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(64, activation="relu"))
model.add(BatchNormalization())
model.add(Flatten())
else:
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(config["n_classes"], activation='softmax'))
# Optimizer selection
opt = None
if config["model_configuration"]["optimizer"] == "adam":
print(f'[INFO]: Selecting Adam as the optimizer')
print(f'[INFO]: Learning Rate: {config["learning_rates"]["initial_lr"]}')
opt = Adam(learning_rate=config["learning_rates"]["initial_lr"])
else:
opt = SGD()
# Building the Model
model.compile(loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['acc', 'mse'])
return model
if __name__ == "__main__":
model = build_model()
print(model)
model.summary()