browser audio detection phase 3
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
"""Regenerate the self-hosted YAMNet TensorFlow.js GraphModel.
|
||||
|
||||
Run in a dedicated Python environment with tensorflow-cpu, tensorflowjs,
|
||||
TensorFlow Hub, and tf-keras installed. Uses the official TensorFlow YAMNet
|
||||
implementation and published weights; normal site traffic never contacts them.
|
||||
"""
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
SOURCE_REVISION = "c651073789719ff39dcf51406733bbcaaafb43f2"
|
||||
SOURCE_BASE = f"https://raw.githubusercontent.com/tensorflow/models/{SOURCE_REVISION}/research/audioset/yamnet"
|
||||
WEIGHTS_URL = "https://storage.googleapis.com/audioset/yamnet.h5"
|
||||
OUTPUT = Path(__file__).resolve().parents[1] / "fireworks_detect/static/browser-yamnet"
|
||||
|
||||
|
||||
def download(url, target):
|
||||
urllib.request.urlretrieve(url, target)
|
||||
|
||||
|
||||
def main():
|
||||
os.environ.setdefault("TF_USE_LEGACY_KERAS", "1")
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
directory = Path(directory)
|
||||
for name in ("yamnet.py", "features.py", "params.py", "yamnet_class_map.csv"):
|
||||
download(f"{SOURCE_BASE}/{name}", directory / name)
|
||||
download(WEIGHTS_URL, directory / "yamnet.h5")
|
||||
sys.path.insert(0, str(directory))
|
||||
import tensorflow as tf
|
||||
import params
|
||||
import yamnet
|
||||
|
||||
class Export(tf.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.model = yamnet.yamnet_frames_model(params.Params(tflite_compatible=True))
|
||||
self.model.load_weights(str(directory / "yamnet.h5"))
|
||||
|
||||
@tf.function(input_signature=[tf.TensorSpec([None], tf.float32, name="waveform")])
|
||||
def classify(self, waveform):
|
||||
scores, embeddings, spectrogram = self.model(waveform)
|
||||
return {"predictions": scores, "embeddings": embeddings, "log_mel_spectrogram": spectrogram}
|
||||
|
||||
export = Export()
|
||||
with tempfile.TemporaryDirectory() as saved_dir:
|
||||
tf.saved_model.save(export, saved_dir, signatures={"serving_default": export.classify})
|
||||
OUTPUT.mkdir(parents=True, exist_ok=True)
|
||||
# The converter imports TF Decision Forests unconditionally even
|
||||
# though YAMNet contains no decision forest ops. Keep this stub
|
||||
# scoped to the conversion subprocess rather than installing TFDF.
|
||||
(directory / "tensorflow_decision_forests.py").write_text("")
|
||||
(directory / "tensorflow_hub.py").write_text("")
|
||||
(directory / "jax/experimental").mkdir(parents=True)
|
||||
(directory / "jax/__init__.py").write_text("")
|
||||
(directory / "jax/experimental/__init__.py").write_text("")
|
||||
(directory / "jax/experimental/jax2tf.py").write_text("")
|
||||
converter_env = {**os.environ, "PYTHONPATH": str(directory)}
|
||||
subprocess.run([
|
||||
sys.executable, "-m", "tensorflowjs.converters.converter",
|
||||
"--input_format=tf_saved_model", "--output_format=tfjs_graph_model",
|
||||
"--signature_name=serving_default", saved_dir, str(OUTPUT),
|
||||
], check=True, env=converter_env)
|
||||
with (directory / "yamnet_class_map.csv").open(newline="") as source:
|
||||
rows = list(csv.DictReader(source))
|
||||
assert len(rows) == 521
|
||||
(OUTPUT / "yamnet_class_map.csv").write_bytes((directory / "yamnet_class_map.csv").read_bytes())
|
||||
(OUTPUT / "yamnet_class_map.json").write_text(json.dumps([row["display_name"] for row in rows]))
|
||||
(OUTPUT / "VERSION").write_text(
|
||||
f"google-yamnet-1\nsource_revision={SOURCE_REVISION}\ntensorflow={tf.__version__}\n"
|
||||
)
|
||||
print(f"Browser YAMNet GraphModel written to {OUTPUT}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user