forked from Servir-Mekong/surface-water-map-unet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkpnt_2_h5_2_tf_model.py
40 lines (30 loc) · 1.15 KB
/
chkpnt_2_h5_2_tf_model.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
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
load_dotenv('.env')
import ast
import os
import tensorflow as tf
from pathlib import Path
from model import model
# specify directory as data io info
BASEDIR = Path('/mnt/hydrafloods/output/jrc_adjusted_LR_2020_07_13_V1/model/sentinel1-surface-water')
MODEL_DIR = BASEDIR / 'trial_6ba0bc0ef8458bf43280b5814775bd2b'
CHECKPOINT_DIR = MODEL_DIR / 'checkpoints' / 'epoch_0' / 'checkpoint'
H5_MODEL = MODEL_DIR / 'tf-model-h5'
TF_MODEL_DIR = MODEL_DIR / 'tf-model'
try:
os.mkdir(TF_MODEL_DIR)
except FileExistsError:
print(f'> {TF_MODEL_DIR} exists, skipping..')
# specify some data structure
FEATURES = ast.literal_eval(os.getenv('FEATURES'))
in_shape = (None, None) + (len(FEATURES),)
out_classes = int(os.getenv('OUT_CLASSES_NUM'))
this_model = model.get_model(in_shape, out_classes)
print(this_model.summary())
# open and save model as h5
this_model.load_weights(f'{str(CHECKPOINT_DIR)}')
tf.keras.models.save_model(this_model, str(H5_MODEL), save_format='h5')
# open and save model as tf
this_model.load_weights(f'{str(H5_MODEL)}')
tf.keras.models.save_model(this_model, str(TF_MODEL_DIR), save_format='tf')