Skip to content

Commit

Permalink
get_timestamp_embeddings: Log durations
Browse files Browse the repository at this point in the history
To see if numpy/tensorflow conversions are problematic
  • Loading branch information
jonnor committed Aug 30, 2021
1 parent ee32314 commit dacccbd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
31 changes: 28 additions & 3 deletions openl3_hear/hear2021.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
HOP_SIZE_TIMESTAMPS = 0.050 # <50 ms recommended
HOP_SIZE_SCENE = 0.5

import time

import openl3
import numpy
import structlog
import tensorflow as tf

#import tensorflow_datasets
Expand All @@ -19,6 +22,9 @@
from typing import NewType, Tuple
Tensor = NewType('Tensor', object)

log = structlog.get_logger()


class Model(tf.Module):
def __init__(self, model, sample_rate=48000, embedding_size=512):
self.sample_rate = sample_rate
Expand Down Expand Up @@ -75,16 +81,27 @@ def get_embedding(samples):
# Compute embeddings for each clip
embeddings = []
timestamps = []

# convert to Numpy
pre_convert_start = time.time()
samples = numpy.array(audio)
pre_convert_end = time.time()

compute_start = time.time()
for sound_no in range(audio.shape[0]):
samples = numpy.array(audio[sound_no, :])
emb, ts = get_embedding(samples)
emb, ts = get_embedding(samples[sound_no, :])
embeddings.append(emb)
timestamps.append(ts)
compute_end = time.time()

# convert to Tensorflow
post_convert_start = time.time()
emb = numpy.stack(embeddings)
ts = numpy.stack(timestamps)
emb = tf.convert_to_tensor(emb)
ts = tf.convert_to_tensor(ts)

post_convert_end = time.time()

# post-conditions
assert len(ts.shape) == 2
assert len(ts) >= 1
Expand All @@ -96,6 +113,14 @@ def get_embedding(samples):
if len(ts) >= 2:
assert ts[0,1] == ts[0,0] + hop_size

log.debug('get-timestamp-embeddings',
n_samples=audio.shape[0],
sample_length=audio.shape[1]/model.sample_rate,
pre_convert_duration=pre_convert_end-pre_convert_start,
post_convert_duration=post_convert_end-post_convert_start,
compute_duration=compute_end-compute_start,
)

# XXX: are timestampes centered?
# first results seems to be 0.0, which would indicate that window
# starts at -window/2 ?
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ openl3==0.4.1
tensorflow-datasets==4.3.0
tensorflow==2.4.2
hearvalidator==2021.0.2
structlog==21.1.0

0 comments on commit dacccbd

Please sign in to comment.