-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVGG_Models.py
61 lines (49 loc) · 2.14 KB
/
VGG_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
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization, Reshape, GlobalAveragePooling2D
from tensorflow.keras.utils import plot_model
from tensorflow.keras.applications import VGG19, VGG16
def model_create_and_train(model_type,data_preprocessor, callback, train_set, test_set, valid_set):
'''
argument: model_type (which is going to be the type of Vgg model to work on either VGG19 or VGG 16)
purpose: Create VGG model with necessary hyperparameters
return: Created,compiled and trained Vgg model
'''
if model_type == 'VGG19':
model_name = VGG19
elif model_type == 'VGG16':
model_name = VGG16
else:
return f'Error on Model type'
quit()
model = model_name(
weights = 'imagenet',
include_top = False,
input_shape = (224, 224, 3)
)
for layer in model.layers:
layer.trainable = False
x = Flatten()(model.output)
# Add a fully connected layer with 512 hidden units and ReLU activation
x = Dense(512, activation='relu')(x)
# Add a dropout rate of 0.5
x = Dropout(0.5)(x)
# Add a final sigmoid layer with 1 node for classification output
x = Dense(1, activation='sigmoid')(x)
predictions = Dense(1, activation='sigmoid')(x)
model_final = Model(inputs=model.input, outputs=predictions)
checkpoint = callback.model_checkpoint(model_type = f'{model_name}')
learning_reducer = callback.learning_reducer()
early_stop = callback.early_stopping()
compiled_model = callback.model_compiler(model_final)
compiled_model.summary()
trained_model = compiled_model.fit(
train_set,
epochs = data_preprocessor.EPOCHS,
steps_per_epoch = train_set.samples // data_preprocessor.BATCH_SIZE,
batch_size = data_preprocessor.BATCH_SIZE,
validation_data = valid_set,
validation_steps = test_set.samples // data_preprocessor.BATCH_SIZE - 10,
callbacks = [checkpoint, learning_reducer, early_stop]
)
model_test_eval = compiled_model.evaluate(test_set)
return trained_model, model_test_eval