forked from rohitgirdhar/ActionVLAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_first_layer_for_flow.py
49 lines (40 loc) · 1.82 KB
/
convert_first_layer_for_flow.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
# ------------------------------------------------------------------------------
# ActionVLAD: Learning spatio-temporal aggregation for action classification
# Copyright (c) 2017 Carnegie Mellon University and Adobe Systems Incorporated
# Please see LICENSE on https://github.com/rohitgirdhar/ActionVLAD/ for details
# ------------------------------------------------------------------------------
"""A simple script for write out the first layer with 20 channels"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import h5py
import numpy as np
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string("file_name", "", "Checkpoint filename")
tf.app.flags.DEFINE_string("tensor_name", "", "Name of the tensor to modify")
tf.app.flags.DEFINE_string("output_file_name", "", "Path to write out the"
"first layer weights")
def get_modified_weights(file_name, tensor_name):
try:
reader = tf.train.NewCheckpointReader(file_name)
T = reader.get_tensor(tensor_name)
return np.repeat(np.mean(T, axis=2, keepdims=True), 20, axis=2)
except Exception as e: # pylint: disable=broad-except
print(str(e))
if "corrupted compressed block contents" in str(e):
print("It's likely that your checkpoint file has been compressed "
"with SNAPPY.")
def main(unused_argv):
if not FLAGS.file_name:
print("Usage: inspect_checkpoint --file_name=checkpoint_file_name "
"[--tensor_name=tensor_to_modify]")
sys.exit(1)
else:
W = get_modified_weights(FLAGS.file_name, FLAGS.tensor_name)
with h5py.File(FLAGS.output_file_name, 'w') as fout:
fout.create_dataset('feat', data=W, compression='gzip',
compression_opts=9)
if __name__ == "__main__":
tf.app.run()