-
Notifications
You must be signed in to change notification settings - Fork 428
/
extract_weights.py
106 lines (87 loc) · 3.56 KB
/
extract_weights.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
from __future__ import print_function
import os
import numpy as np
import tensorflow as tf
from keras.utils.data_utils import get_file
def get_xception_filename(key):
"""Rename tensor name to the corresponding Keras layer weight name.
# Arguments
key: tensor name in TF (determined by tf.variable_scope)
"""
filename = str(key)
filename = filename.replace('/', '_')
filename = filename.replace('xception_65_', '')
filename = filename.replace('decoder_', '', 1)
filename = filename.replace('BatchNorm', 'BN')
if 'Momentum' in filename:
return None
if 'entry_flow' in filename or 'exit_flow' in filename:
filename = filename.replace('_unit_1_xception_module', '')
elif 'middle_flow' in filename:
filename = filename.replace('_block1', '')
filename = filename.replace('_xception_module', '')
# from TF to Keras naming
filename = filename.replace('_weights', '_kernel')
filename = filename.replace('_biases', '_bias')
return filename + '.npy'
def get_mobilenetv2_filename(key):
"""Rename tensor name to the corresponding Keras layer weight name.
# Arguments
key: tensor name in TF (determined by tf.variable_scope)
"""
filename = str(key)
filename = filename.replace('/', '_')
filename = filename.replace('MobilenetV2_', '')
filename = filename.replace('BatchNorm', 'BN')
if 'Momentum' in filename:
return None
# from TF to Keras naming
filename = filename.replace('_weights', '_kernel')
filename = filename.replace('_biases', '_bias')
return filename + '.npy'
def extract_tensors_from_checkpoint_file(filename, output_folder='weights', net_name=None):
"""Extract tensors from a TF checkpoint file.
# Arguments
filename: TF checkpoint file
output_folder: where to save the output numpy array files
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
reader = tf.train.NewCheckpointReader(filename)
for key in reader.get_variable_to_shape_map():
# convert tensor name into the corresponding Keras layer weight name and save
if net_name == 'xception':
filename = get_xception_filename(key)
elif net_name == 'mobilenetv2':
filename = get_mobilenetv2_filename(key)
if filename:
path = os.path.join(output_folder, filename)
arr = reader.get_tensor(key)
np.save(path, arr)
print("tensor_name: ", key)
CKPT_URL = 'https://download.tensorflow.org/models/deeplabv3_pascal_trainval_2018_01_04.tar.gz'
CKPT_URL_MOBILE = 'https://download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz'
MODEL_DIR = 'models'
MODEL_SUBDIR = 'deeplabv3_pascal_trainval'
MODEL_SUBDIR_MOBILE = 'deeplabv3_mnv2_pascal_trainval'
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
checkpoint_tar = get_file(
'deeplabv3_pascal_trainval_2018_01_04.tar.gz',
CKPT_URL,
extract=True,
cache_subdir='',
cache_dir=MODEL_DIR)
checkpoint_tar_mobile = get_file(
'deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz',
CKPT_URL_MOBILE,
extract=True,
cache_subdir='',
cache_dir=MODEL_DIR)
checkpoint_file = os.path.join(MODEL_DIR, MODEL_SUBDIR, 'model.ckpt')
extract_tensors_from_checkpoint_file(
checkpoint_file, net_name='xception', output_folder='weights/xception')
checkpoint_file = os.path.join(
MODEL_DIR, MODEL_SUBDIR_MOBILE, 'model.ckpt-30000')
extract_tensors_from_checkpoint_file(
checkpoint_file, net_name='mobilenetv2', output_folder='weights/mobilenetv2')