-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_extract_model_from_checkpoint.py
57 lines (40 loc) · 1.48 KB
/
6_extract_model_from_checkpoint.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
from pathlib import Path
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from sklearn.utils import class_weight
import config
# init variables
IMAGE_SIZE = (config.MODEL_INPUT_SIZE, config.MODEL_INPUT_SIZE)
print(f'Using {config.MODEL_URL} with input size {IMAGE_SIZE}')
class_paths = list(config.DIRPATH_DATASET.glob('*/'))
class_count = len(class_paths)
# build model
print("Building model with", config.MODEL_URL)
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),
hub.KerasLayer(config.MODEL_URL, trainable=config.DO_FINE_TUNING),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(class_count,
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True),
metrics=['accuracy'])
# load weights from checkpoint
checkpoint_dir = Path('./checkpoint')
checkpoint_path = checkpoint_dir / 'cp.ckpt'
if checkpoint_dir.exists():
model.load_weights(checkpoint_path)
print('loaded from checkpoint')
else:
exit('checkpoint does not exist!')
# save model
model.save(config.FILEPATH_SAVED_MODEL)
print('-------------- DONE --------------')