-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine_tune_eff_net.py
176 lines (136 loc) · 4.92 KB
/
fine_tune_eff_net.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse, os
import numpy as np
import tensorflow as tf
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import callbacks
from tensorflow.keras.utils import multi_gpu_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras import applications
# from subprocess import call
# call("pip install efficientnet==1.1.1".split(" "))
# import efficientnet.tfkeras as efn
NETS = {
# "EfficientNetB0": efn.EfficientNetB0,
"InceptionV3": applications.InceptionV3,
"MobileNetV2": applications.MobileNetV2,
"ResNet50": applications.ResNet50,
}
def get_model(Net, image_shape):
inputs = layers.Input(shape=(*image_shape, 3))
base_efficient_net = Net(weights='imagenet', input_tensor=inputs, include_top=False)
base_efficient_net.trainable = False
x = base_efficient_net.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.BatchNormalization()(x)
top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
predictions = layers.Dense(2, activation='softmax')(x)
efficient_net = models.Model(inputs=base_efficient_net.input, outputs=predictions)
efficient_net.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
return efficient_net
def get_train_generator(directory, image_shape, batch_size):
train_datagen = image.ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
train_generator = train_datagen.flow_from_directory(
directory, # this is the target directory
target_size=image_shape, # all images will be resized
batch_size=batch_size,
class_mode='categorical'
)
return train_generator
def get_validation_generator(directory, image_shape, batch_size):
test_datagen = image.ImageDataGenerator(rescale=1. / 255)
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
directory,
target_size=image_shape,
batch_size=batch_size,
class_mode='categorical')
return validation_generator
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--learning-rate', type=float, default=0.01)
parser.add_argument('--batch-size', type=int, default=16)
parser.add_argument('--gpu-count', type=int, default=os.environ['SM_NUM_GPUS'])
parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR'])
parser.add_argument('--training', type=str, default=os.environ['SM_CHANNEL_TRAINING'])
parser.add_argument('--validation', type=str, default=os.environ['SM_CHANNEL_VALIDATION'])
parser.add_argument('--log-dir', type=str)
parser.add_argument('--steps-per-epoch', type=int, default=10)
parser.add_argument('--model', type=str)
args, _ = parser.parse_known_args()
epochs = args.epochs
lr = args.learning_rate
batch_size = args.batch_size
gpu_count = args.gpu_count
model_dir = args.model_dir
training_dir = args.training
validation_dir = args.validation
log_dir = args.log_dir
steps_per_epoch = args.steps_per_epoch
Net = NETS[args.model]
# input image dimensions
image_shape = (224, 224)
train_generator = get_train_generator(
directory=training_dir,
image_shape=image_shape,
batch_size=batch_size
)
validation_generator = get_validation_generator(
directory=validation_dir,
image_shape=image_shape,
batch_size=batch_size
)
model = get_model(
image_shape=image_shape,
Net=Net
)
print(model.summary())
if gpu_count > 1:
model = multi_gpu_model(model, gpus=gpu_count)
tensorboard_cb = callbacks.TensorBoard(
log_dir=log_dir,
histogram_freq=1,
)
early_stopping_cb = callbacks.EarlyStopping(
monitor='val_loss',
patience=10,
verbose=0,
mode='min'
)
checkpoint_cb = callbacks.ModelCheckpoint(
'model-{epoch:03d}-{val_accuracy:03f}.h5',
save_best_only=True,
monitor='val_accuracy'
)
model.fit(
train_generator,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
validation_data=validation_generator,
validation_steps=1,
callbacks=[
tensorboard_cb,
early_stopping_cb,
checkpoint_cb
]
)
save_path = model_dir + '/model'
if not os.path.exists(save_path):
print('save directories...', flush=True)
os.makedirs(save_path)
model.save(save_path + '/mymodel.h5')