forked from fatemehtd/Echo-SyncNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_embeddings.py
executable file
·168 lines (135 loc) · 5.45 KB
/
extract_embeddings.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
import os
import sys
import argparse
from algorithms import get_algo
from config import CONFIG
from datasets import create_one_epoch_dataset
from utils import get_embeddings_dataset
from utils import get_lr_opt_global_step
from utils import restore_ckpt
from utils import setup_eval_dir
from utils import load_config
from utils import prepare_gpu
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
gfile = tf.io.gfile
layers = tf.keras.layers
def parse_args():
parser = argparse.ArgumentParser(
description='Runs a video through TCC and outputs the embeddings')
parser.add_argument('--logdir',
type=str,
help='Path to logs.',
default='/mnt/nas/workspace/fatemeht/projects/SyncNet/trained_model/all_view/',
required=False)
parser.add_argument('--save_path',
type=str,
help='Path to folder.',
default='/mnt/nas/workspace/fatemeht/projects/SyncNet/Stanford_embeddings/embeddings_stanford5_test.npy',
required=False)
parser.add_argument('--config',
type=str,
help='training configuration in Json format',
default='/mnt/nas/workspace/fatemeht/projects/SyncNet/trained_model/all_view/config.json',
required=False)
parser.add_argument('--path_to_tfrecords',
type=str,
help='path to tfrecords',
default='/bigdata2/dataset/stanford_tfrecords/',
required=False)
parser.add_argument('--dataset',
type=str,
default='PLAX',
required=False)
parser.add_argument('--split',
type=str,
default='val',
required=False)
parser.add_argument('--max_embs',
type=int,
help='Max number of videos to embed. 0 or less means embed all videos in dataset',
default=0,
required=False)
parser.add_argument('--visualize',
type=bool,
help='Visualize images.',
default=False,
required=False)
parser.add_argument('--keep_data',
type=bool,
help='Keep frames of video with embeddings.',
default=False,
required=False)
parser.add_argument('--optical_flow',
type=bool,
help='Seclect true if the input is opticalflow',
default=False,
required=False)
parser.add_argument('--keep_labels',
type=bool,
help='Keep per-frame labels with embeddings',
default=True,
required=False)
parser.add_argument('--sample_all_stride',
type=int,
help='Stride between frames that will be embedded.',
default=1,
required=False)
parser.add_argument('--frames_per_batch',
type=int,
help='frames_per_batchs',
default=1,
required=False)
parser.add_argument('--gpu',
type=str,
help='(optional) index of the gpu to use',
required=False,
default="-2")
parser.add_argument('--defun',
type=bool,
help='Defun functions in algo for faster training',
default=False,
required=False)
return parser.parse_args()
evaluated_last_ckpt = False
def evaluate(args):
"""Extract embeddings."""
logdir = args.logdir
setup_eval_dir(logdir)
# Can ignore frame labels if dataset doesn't have per-frame labels.
CONFIG.DATA.FRAME_LABELS = args.keep_labels
# Subsample frames in case videos are long or fps is high to save memory.
CONFIG.DATA.SAMPLE_ALL_STRIDE = args.sample_all_stride
algo = get_algo(CONFIG.TRAINING_ALGO)
_, optimizer, _ = get_lr_opt_global_step()
restore_ckpt(logdir=logdir, **algo.model)
if args.defun:
algo.call = tf.function(algo.call)
algo.compute_loss = tf.function(algo.compute_loss)
iterator, _ = create_one_epoch_dataset(args.dataset, args.split, mode='eval',
path_to_tfrecords=args.path_to_tfrecords)
max_embs = None if args.max_embs <= 0 else args.max_embs
embeddings = get_embeddings_dataset(
algo.model,
iterator,
frames_per_batch=args.frames_per_batch,
keep_data=args.keep_data,
optical_flow=args.optical_flow,
keep_labels=args.keep_labels,
max_embs=max_embs)
np.save(gfile.GFile(args.save_path, 'w'), embeddings)
return embeddings
def main(_):
tf.keras.backend.set_learning_phase(0)
args = parse_args()
config = load_config(args.config)
CONFIG.update(config)
prepare_gpu(args.gpu)
embeddings = evaluate(args)
if __name__ == '__main__':
main(sys.argv[1:])