Exemplo n.º 1
0
 def file_access(fn):
     return resolve_path(config.DATASET_PATH, "audio", fn + ".wav")
Exemplo n.º 2
0
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Reshape, GlobalAveragePooling1D
from keras.layers import Conv2D, MaxPooling2D, Conv1D, GlobalMaxPooling1D
from keras.utils import to_categorical, plot_model

import config
from src.utils import resolve_path, pretty_duration
from src.sound_actions import make_file_spectrogram, file_to_fft
from src.dataset_actions import load_dataset_json, extract_tracks_by_family, make_labels_and_spectrograms
from src.metrics import plot_confusion_matrix


if __name__ == "__main__":
    logging.basicConfig(level=config.LOG_LEVEL, format=config.LOG_FORMAT, datefmt=config.LOG_DATE_FMT)

    data_path = resolve_path(config.DATA_PATH)
    dataset_path = resolve_path(config.DATASET_PATH)
    cache_path = resolve_path(config.CACHE_PATH)

    os.makedirs(cache_path, exist_ok=True)

    # # FFT of some files
    # file_to_fft(os.path.join(dataset_path, "audio", "guitar_acoustic_030-076-127.wav"), plot=True)
    # file_to_fft(os.path.join(dataset_path, "audio", "guitar_acoustic_030-077-075.wav"), plot=True)
    # file_to_fft(os.path.join(dataset_path, "audio", "guitar_acoustic_030-078-050.wav"), plot=True)
    # file_to_fft(os.path.join(dataset_path, "audio", "mallet_acoustic_047-094-127.wav"), plot=True)
    # file_to_fft(os.path.join(dataset_path, "audio", "bass_synthetic_134-097-127.wav"), plot=True)
    # file_to_fft(os.path.join(data_path, "broken_clarinet.wav"), plot=True)

    # # Spectrograms of some files
    # make_file_spectrogram(os.path.join(dataset_path, "audio", "guitar_acoustic_030-076-127.wav"), plot=True)
Exemplo n.º 3
0
import os
import re
from glob import glob
import shlex
from pprint import pprint
from typing import Dict, Any

from jinja2 import Environment, FileSystemLoader

from src.utils import resolve_path, get_docker_templates
from src.choices import all_questions_dict

# Search templates in project files
dockerfile_template_files = glob(
    resolve_path("templates_dockerfile", "*.jinja2"))
docker_compose_template_files = glob(
    resolve_path("templates_docker_compose", "*.yml.jinja2"))

# Only keep the base name (no extension)
known_dockerfile_templates = [
    os.path.split(fn)[1][:-7] for fn in dockerfile_template_files
]
known_docker_compose_templates = [
    os.path.split(fn)[1][:-11] for fn in docker_compose_template_files
]

# Prepare the jinja2 FileSystemLoader
env_dockerfile = Environment(
    loader=FileSystemLoader(resolve_path("templates_dockerfile")))
env_docker_compose = Environment(
Exemplo n.º 4
0
def make_model(evaluate=False, **kwargs):
    logging.info("Making labels and ffts...")
    labels, ffts, _ = make_labels_and_fft(local_data_store, **kwargs)

    # Shuffle dataset
    logging.info("Shuffling...")
    indices = np.arange(len(labels))
    np.random.shuffle(indices)

    labels = np.asarray(labels)[indices]
    ffts = np.asarray(ffts)[indices]

    labels = to_categorical(np.asarray(labels))
    num_classes = labels.shape[1]

    logging.info(
        f"{labels.shape[0]} entries, {labels.shape[1]} classes, ffts shape {ffts.shape}"
    )

    # Model
    logging.info("Making model...")
    model = Sequential()

    model.add(Dense(256, input_shape=(256, ), activation="relu"))
    model.add(Dropout(0.2))
    model.add(Dense(128, activation="relu"))
    model.add(Dropout(0.2))
    # model.add(Dense(128, activation="relu"))
    # model.add(Dropout(0.1))
    model.add(Dense(num_classes, activation="softmax"))

    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])
    plot_model(model,
               to_file=resolve_path("Cache", "MLP_realtime.png"),
               show_shapes=True,
               show_layer_names=True)

    if evaluate:
        logging.info("Model evaluation...")

        # Cut dataset into train and validation sets
        cut = int(labels.shape[0] * 0.8)

        labels_train, labels_valid = labels[:cut], labels[cut:]
        ffts_train, ffts_valid = ffts[:cut], ffts[cut:]

        # Train the model on train data
        model.fit(ffts_train,
                  labels_train,
                  batch_size=64,
                  epochs=20,
                  validation_data=(ffts_valid, labels_valid))

        logging.info(f"Done training model for evaluation")

        labels_pred = model.predict(ffts_valid)

        labels_valid_flat = np.argmax(labels_valid, axis=1)
        labels_pred_flat = np.argmax(labels_pred, axis=1)

        acc = accuracy_score(labels_valid_flat, labels_pred_flat)
        cm = confusion_matrix(labels_valid_flat, labels_pred_flat)
        logging.info(f"Got {acc:.5f} accuracy.\nConfusion matrix:\n{cm}")

    logging.info("Model real train...")

    t0 = time.perf_counter()
    model.fit(ffts, labels, batch_size=64, epochs=N_EPOCH)

    logging.info(
        f"Done training model in {pretty_duration(time.perf_counter() - t0)} ({N_EPOCH} epochs)"
    )
    return model
Exemplo n.º 5
0
from scipy.signal import windows as ssw
from scipy.fftpack import fft
from sklearn.metrics import accuracy_score, confusion_matrix
import sounddevice as sd
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.utils import to_categorical, plot_model

import config
from src.utils import resolve_path, pretty_duration
from src.sound_actions import wave_file_to_time_data

N_EPOCH = 30

local_data_store = {
    0: [resolve_path("Data", "aymeric.wav")],
    1: [resolve_path("Data", "amelie.wav")],
}
local_classes_i2n = {
    0: "aymeric",
    1: "amelie",
}
SAMPLE_DURATION = 40


def give_many_fft(
    fs: int,
    time_data: np.ndarray,
    threshold: float = 0.01,
    sample_duration: int = config.SAMPLE_DURATION,
    block_duration: int = config.BLOCK_DURATION,
Exemplo n.º 6
0
import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth

from src.utils import resolve_path, pretty_duration
from src.videostream import WebcamVideoStream
from src.playsounds import PlaySounds
import src.cyvisord.visord as cyvo

# Configuration parameters
LOG_FORMAT = "%(asctime)s [%(name)s/%(levelname)s] %(message)s"
LOG_DATE_FMT = "%H:%M:%S"
DARK_THRESHOLD = 92
DEBUG = False

# Directory where the path to the samples will be based on
SAMPLES_DIRECTORY = resolve_path("samples")

# The ratio width/height of the instrument will be computed, the first match above it will be used
SOUND_NAMES_BY_RATIOS = [
    (0.0, ["whistle_A.wav", "whistle_B.wav", "whistle_C.wav"]),
    (0.5, ["flam-01.wav", "ophat-05.wav", "kick-08.wav", "sdst-02.wav"]),
    (10.0, ["piano_A.wav", "piano_B.wav", "piano_C.wav"]),
]


def detect_zones(img: np.ndarray) -> List[Tuple[int, int]]:
    """Will detect the best black zones un the image"""

    # Threshold to adapt to ambient luminosity
    threshold = np.average(img) // 2
    logging.debug(f"Zones threshold: {threshold}")