-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_saver.py
58 lines (44 loc) · 1.74 KB
/
model_saver.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
import argparse
import importlib
import tensorflow as tf
import pathlib
def build_model(model_name, model, base_trainable, image_size, num_of_output, weights):
if (model_name in ['MobileNetV3Small', 'MobileNetV3Large']):
base_architecture = model(
include_top=False,
input_shape=image_size + (3, ),
pooling='avg',
minimalistic=True)
else:
base_architecture = model(
include_top=False,
input_shape=image_size + (3, ),
pooling='avg')
base_architecture.trainable = base_trainable
outputs = tf.keras.layers.Dense(num_of_output, activation="softmax")(base_architecture.output)
model = tf.keras.Model(base_architecture.input, outputs)
model.compile(
optimizer=tf.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
model.load_weights(weights)
return model
def main(args):
module = importlib.import_module("tensorflow.keras.applications")
backbone = getattr(module, args.model_name)
num_output = 102 if args.cfg == "oxford_flowers102" else 100
model = build_model(model=backbone,
model_name=args.model_name,
base_trainable=False,
image_size=(224, 224),
num_of_output=num_output,
weights=args.weights)
model.save(f'generated/{args.cfg}/{args.model_name}/saved-{args.num}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--model-name')
parser.add_argument('--cfg')
parser.add_argument('--weights')
parser.add_argument('--num')
args = parser.parse_args()
main(args)