-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEfficientNet_models.py
51 lines (40 loc) · 1.82 KB
/
EfficientNet_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
from tensorflow.keras.applications.efficientnet import EfficientNetB0
from tensorflow.keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization, Reshape, GlobalAveragePooling2D
from tensorflow.keras.models import Model
def model_create_and_train(model_type, data_preprocessor, callback, train_set, test_set, valid_set):
'''
argument:
purpose: Create VGG model with necessary hyperparameters
return: Created,compiled and trained Vgg model
'''
model = EfficientNetB0(
input_shape = (224, 224, 3),
include_top = False,
weights = 'imagenet'
)
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(1024, 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 = 'EfficientNetB0')
learning_reducer = callback.learning_reducer()
early_stop = callback.early_stopping()
compiled_model = callback.model_compiler(model_final)
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