跳转到主要内容

在Keras中使用VGGish。

项目描述

VGGish:一个类似VGG的音频分类模型

该仓库提供了一个VGGish模型,使用Keras实现,后端为tensorflow(由于tf.slim已被弃用,我认为我们应该有一个最新的接口)。该仓库是基于AudioSet的模型开发的。有关更多详细信息,请访问slim版本

安装

pip install vggish-keras

权重将在首次请求时下载。您还可以运行python -m vggish_keras.download_helpers.download_weights来下载它们。

用法

基本 - 简单且高效的方法

import librosa
import numpy as np
import vggish_keras as vgk

# loads the model once and provides a simple function that takes in `filename` or `y, sr`
compute = vgk.get_embedding_function(hop_duration=0.25)
# model, pump, and sampler are available as attributes
compute.model.summary() # take a peak at the model

# compute from filename
Z, ts = compute(librosa.util.example_audio_file())

# compute from pcm
y, sr = librosa.load(librosa.util.example_audio_file())
Z, ts = compute(y=y, sr=sr)

替代方案 - 使用底层的辅助函数

# get the embeddings - WARNING: it instantiates a new model each time
Z, ts = vgk.get_embeddings(librosa.util.example_audio_file(), hop_duration=0.25)

# create model, pump, sampler once and pass to vgk.get_embeddings
# - more typing :'(
model, pump, sampler = vgk.get_embedding_model(hop_duration=0.25)
Z, ts = vgk.get_embeddings(
    librosa.util.example_audio_file(),
    model=model, pump=pump, sampler=sampler)

手动使用keras模型和pump直接

import librosa
import numpy as np
import vggish_keras as vgk

# define the model
pump = vgk.get_pump()
model = vgk.VGGish(pump)
sampler = vgk.get_sampler(pump)

# transform audio into VGGish embeddings
filename = librosa.util.example_audio_file()
X = np.concatenate([
    x[vgk.params.PUMP_INPUT]
    for x in sampler(pump(filename))])
Z = model.predict(X)

# calculate timestamps
ts = vgk.get_timesteps(Z, pump, sampler)
assert Z.shape == (13, 512)

参考

我在download_helpers/convert_ckpt.py中包含了一个权重转换脚本,展示了如何将.ckpt格式的权重转换为.h5格式,供感兴趣的人参考。

待办事项

  • 目前,可以通过vgk.params全局更改参数(采样率、跳步大小等) - 我希望允许将参数覆盖传递给vgk.VGGish
  • 目前它依赖于https://github.com/bmcfee/pumpp/pull/123。一旦合并,将移除自定义GitHub安装位置

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分布

vggish-keras-0.1.1.tar.gz (8.1 kB 查看散列

上传时间 源代码

由以下支持