help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
environmental-sounds
RSK World
environmental-sounds
Environmental Sound Dataset - Audio Classification + Sound Event Detection + Deep Learning + Machine Learning
environmental-sounds
  • environmental-sounds
  • examples
  • .gitignore844 B
  • ADVANCED_FEATURES.md8.3 KB
  • CONTRIBUTING.md1.7 KB
  • CREATE_RELEASE.md5.1 KB
  • DATASET_STRUCTURE.md3.5 KB
  • LICENSE1.5 KB
  • PROJECT_SUMMARY.md5 KB
  • README.md5.9 KB
  • RELEASE_NOTES.md4.8 KB
  • analyze.py6.9 KB
  • api_server.py8.1 KB
  • audio_quality.py10.2 KB
  • audio_similarity.py10.4 KB
  • augment_audio.py10.9 KB
  • batch_processing.py11.3 KB
  • create_dataset_structure.py4.8 KB
  • create_sample_data.py4.3 KB
  • create_zip.py3.8 KB
  • deep_learning_models.py13.2 KB
  • environmental-sounds.zip50.1 KB
  • example_usage.py5 KB
  • index.html26.9 KB
  • load_data.py4.9 KB
  • model_interpretability.py10.3 KB
  • realtime_classification.py9.6 KB
  • requirements.txt475 B
  • setup.py1.7 KB
  • train_model.py7.8 KB
  • verify_project.py5.3 KB
build_local_dictionary.pyload_data.pysetup.pyinteractive.godeep_learning_models.py
load_data.py
Raw Download
Find: Go to:
"""
Environmental Sound Dataset - Data Loading Module

Project: Environmental Sound Dataset
Website: https://rskworld.in
Founded by: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import os
import numpy as np
import librosa
import pandas as pd
from pathlib import Path
from typing import Tuple, List, Optional


def load_environmental_sounds(
    split: str = 'train',
    data_dir: str = './environmental-sounds',
    sample_rate: int = 22050,
    duration: Optional[float] = None
) -> Tuple[List[np.ndarray], List[str]]:
    """
    Load environmental sound audio files from the dataset.
    
    Args:
        split: 'train' or 'test' to specify which split to load
        data_dir: Root directory of the dataset
        sample_rate: Target sample rate for audio files
        duration: Maximum duration in seconds (None for full audio)
    
    Returns:
        Tuple of (audio_data, labels) where:
        - audio_data: List of numpy arrays containing audio waveforms
        - labels: List of class labels (strings)
    """
    split_dir = os.path.join(data_dir, split)
    
    if not os.path.exists(split_dir):
        raise ValueError(f"Directory {split_dir} does not exist")
    
    audio_data = []
    labels = []
    
    # Iterate through class directories
    for class_name in os.listdir(split_dir):
        class_dir = os.path.join(split_dir, class_name)
        
        if not os.path.isdir(class_dir):
            continue
        
        # Load all audio files in this class
        for audio_file in os.listdir(class_dir):
            if audio_file.endswith(('.wav', '.mp3', '.flac')):
                audio_path = os.path.join(class_dir, audio_file)
                
                try:
                    # Load audio file
                    y, sr = librosa.load(audio_path, sr=sample_rate, duration=duration)
                    audio_data.append(y)
                    labels.append(class_name)
                except Exception as e:
                    print(f"Error loading {audio_path}: {e}")
                    continue
    
    return audio_data, labels


def load_metadata(metadata_path: str = './environmental-sounds/metadata.csv') -> pd.DataFrame:
    """
    Load dataset metadata from CSV file.
    
    Args:
        metadata_path: Path to the metadata CSV file
    
    Returns:
        DataFrame containing metadata
    """
    if os.path.exists(metadata_path):
        return pd.read_csv(metadata_path)
    else:
        print(f"Metadata file not found at {metadata_path}")
        return pd.DataFrame()


def get_class_distribution(labels: List[str]) -> dict:
    """
    Get the distribution of classes in the dataset.
    
    Args:
        labels: List of class labels
    
    Returns:
        Dictionary with class names as keys and counts as values
    """
    from collections import Counter
    return dict(Counter(labels))


def prepare_features(
    audio_data: List[np.ndarray],
    feature_type: str = 'mfcc',
    n_mfcc: int = 13,
    n_mels: int = 128
) -> np.ndarray:
    """
    Extract features from audio data.
    
    Args:
        audio_data: List of audio waveforms
        feature_type: Type of features to extract ('mfcc', 'mel', 'chroma', 'spectral')
        n_mfcc: Number of MFCC coefficients
        n_mels: Number of mel bands
    
    Returns:
        Numpy array of extracted features
    """
    features = []
    
    for audio in audio_data:
        if feature_type == 'mfcc':
            feat = librosa.feature.mfcc(y=audio, n_mfcc=n_mfcc)
            feat = np.mean(feat, axis=1)  # Average over time
        elif feature_type == 'mel':
            feat = librosa.feature.melspectrogram(y=audio, n_mels=n_mels)
            feat = np.mean(feat, axis=1)
        elif feature_type == 'chroma':
            feat = librosa.feature.chroma_stft(y=audio)
            feat = np.mean(feat, axis=1)
        elif feature_type == 'spectral':
            feat = librosa.feature.spectral_centroid(y=audio)
            feat = np.mean(feat)
        else:
            raise ValueError(f"Unknown feature type: {feature_type}")
        
        features.append(feat)
    
    return np.array(features)


if __name__ == '__main__':
    # Example usage
    print("Loading Environmental Sound Dataset...")
    
    try:
        train_data, train_labels = load_environmental_sounds('train')
        print(f"Loaded {len(train_data)} training samples")
        print(f"Classes: {set(train_labels)}")
        print(f"Class distribution: {get_class_distribution(train_labels)}")
        
        test_data, test_labels = load_environmental_sounds('test')
        print(f"Loaded {len(test_data)} test samples")
        
    except Exception as e:
        print(f"Error: {e}")
        print("Please ensure the dataset is properly structured in ./environmental-sounds/")

161 lines•4.9 KB
python
setup.py
Raw Download
Find: Go to:
"""
Setup script for Environmental Sound Dataset

Project: Environmental Sound Dataset
Website: https://rskworld.in
Founded by: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Phone: +91 93305 39277
"""

from setuptools import setup, find_packages

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

with open("requirements.txt", "r", encoding="utf-8") as fh:
    requirements = [line.strip() for line in fh if line.strip() and not line.startswith("#")]

setup(
    name="environmental-sound-dataset",
    version="1.0.0",
    author="RSK World",
    author_email="help@rskworld.in",
    description="Environmental sound classification dataset with audio samples for sound event detection",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://rskworld.in",
    packages=find_packages(),
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: Science/Research",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Topic :: Multimedia :: Sound/Audio",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
    ],
    python_requires=">=3.8",
    install_requires=requirements,
    keywords="audio, sound, classification, machine learning, environmental sounds, dataset",
    project_urls={
        "Homepage": "https://rskworld.in",
        "Contact": "mailto:help@rskworld.in",
    },
)

51 lines•1.7 KB
python
deep_learning_models.py
Raw Download
Find: Go to:
"""
Environmental Sound Dataset - Deep Learning Models Module

Project: Environmental Sound Dataset
Website: https://rskworld.in
Founded by: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Phone: +91 93305 39277

This module provides deep learning models (CNN, LSTM, Transformer) for audio classification.
"""

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models, callbacks
from typing import Tuple, Optional, List
import librosa
import os


class AudioCNN:
    """
    Convolutional Neural Network for audio classification.
    """
    
    def __init__(self, input_shape: Tuple[int, int], num_classes: int):
        """
        Initialize CNN model.
        
        Args:
            input_shape: Shape of input (time_steps, features)
            num_classes: Number of output classes
        """
        self.input_shape = input_shape
        self.num_classes = num_classes
        self.model = self._build_model()
    
    def _build_model(self) -> keras.Model:
        """Build CNN architecture."""
        model = models.Sequential([
            layers.Input(shape=self.input_shape),
            
            # First Conv Block
            layers.Conv2D(32, (3, 3), activation='relu', padding='same'),
            layers.BatchNormalization(),
            layers.MaxPooling2D((2, 2)),
            layers.Dropout(0.25),
            
            # Second Conv Block
            layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
            layers.BatchNormalization(),
            layers.MaxPooling2D((2, 2)),
            layers.Dropout(0.25),
            
            # Third Conv Block
            layers.Conv2D(128, (3, 3), activation='relu', padding='same'),
            layers.BatchNormalization(),
            layers.MaxPooling2D((2, 2)),
            layers.Dropout(0.25),
            
            # Fourth Conv Block
            layers.Conv2D(256, (3, 3), activation='relu', padding='same'),
            layers.BatchNormalization(),
            layers.GlobalAveragePooling2D(),
            layers.Dropout(0.5),
            
            # Dense layers
            layers.Dense(512, activation='relu'),
            layers.BatchNormalization(),
            layers.Dropout(0.5),
            layers.Dense(256, activation='relu'),
            layers.Dropout(0.5),
            
            # Output layer
            layers.Dense(self.num_classes, activation='softmax')
        ])
        
        model.compile(
            optimizer=keras.optimizers.Adam(learning_rate=0.001),
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
        )
        
        return model
    
    def train(
        self,
        X_train: np.ndarray,
        y_train: np.ndarray,
        X_val: Optional[np.ndarray] = None,
        y_val: Optional[np.ndarray] = None,
        epochs: int = 50,
        batch_size: int = 32,
        callbacks_list: Optional[List] = None
    ) -> keras.callbacks.History:
        """Train the model."""
        if callbacks_list is None:
            callbacks_list = [
                callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True),
                callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5),
                callbacks.ModelCheckpoint('best_cnn_model.h5', save_best_only=True, monitor='val_loss')
            ]
        
        validation_data = (X_val, y_val) if X_val is not None else None
        
        history = self.model.fit(
            X_train, y_train,
            validation_data=validation_data,
            epochs=epochs,
            batch_size=batch_size,
            callbacks=callbacks_list,
            verbose=1
        )
        
        return history
    
    def predict(self, X: np.ndarray) -> np.ndarray:
        """Make predictions."""
        return self.model.predict(X)
    
    def save(self, filepath: str):
        """Save the model."""
        self.model.save(filepath)
    
    def load(self, filepath: str):
        """Load a saved model."""
        self.model = keras.models.load_model(filepath)


class AudioLSTM:
    """
    LSTM model for audio sequence classification.
    """
    
    def __init__(self, input_shape: Tuple[int, int], num_classes: int):
        """
        Initialize LSTM model.
        
        Args:
            input_shape: Shape of input (time_steps, features)
            num_classes: Number of output classes
        """
        self.input_shape = input_shape
        self.num_classes = num_classes
        self.model = self._build_model()
    
    def _build_model(self) -> keras.Model:
        """Build LSTM architecture."""
        model = models.Sequential([
            layers.Input(shape=self.input_shape),
            
            # Bidirectional LSTM layers
            layers.Bidirectional(layers.LSTM(128, return_sequences=True)),
            layers.Dropout(0.3),
            layers.BatchNormalization(),
            
            layers.Bidirectional(layers.LSTM(64, return_sequences=True)),
            layers.Dropout(0.3),
            layers.BatchNormalization(),
            
            layers.Bidirectional(layers.LSTM(32)),
            layers.Dropout(0.3),
            
            # Dense layers
            layers.Dense(128, activation='relu'),
            layers.Dropout(0.5),
            layers.Dense(64, activation='relu'),
            layers.Dropout(0.5),
            
            # Output layer
            layers.Dense(self.num_classes, activation='softmax')
        ])
        
        model.compile(
            optimizer=keras.optimizers.Adam(learning_rate=0.001),
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
        )
        
        return model
    
    def train(
        self,
        X_train: np.ndarray,
        y_train: np.ndarray,
        X_val: Optional[np.ndarray] = None,
        y_val: Optional[np.ndarray] = None,
        epochs: int = 50,
        batch_size: int = 32
    ) -> keras.callbacks.History:
        """Train the model."""
        callbacks_list = [
            callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True),
            callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5),
            callbacks.ModelCheckpoint('best_lstm_model.h5', save_best_only=True, monitor='val_loss')
        ]
        
        validation_data = (X_val, y_val) if X_val is not None else None
        
        history = self.model.fit(
            X_train, y_train,
            validation_data=validation_data,
            epochs=epochs,
            batch_size=batch_size,
            callbacks=callbacks_list,
            verbose=1
        )
        
        return history
    
    def predict(self, X: np.ndarray) -> np.ndarray:
        """Make predictions."""
        return self.model.predict(X)
    
    def save(self, filepath: str):
        """Save the model."""
        self.model.save(filepath)
    
    def load(self, filepath: str):
        """Load a saved model."""
        self.model = keras.models.load_model(filepath)


class AudioTransformer:
    """
    Transformer model for audio classification.
    """
    
    def __init__(self, input_shape: Tuple[int, int], num_classes: int, d_model: int = 128):
        """
        Initialize Transformer model.
        
        Args:
            input_shape: Shape of input (time_steps, features)
            num_classes: Number of output classes
            d_model: Model dimension
        """
        self.input_shape = input_shape
        self.num_classes = num_classes
        self.d_model = d_model
        self.model = self._build_model()
    
    def _transformer_encoder(self, inputs, head_size, num_heads, ff_dim, dropout=0):
        """Transformer encoder block."""
        # Multi-head attention
        attention_output = layers.MultiHeadAttention(
            key_dim=head_size, num_heads=num_heads, dropout=dropout
        )(inputs, inputs)
        attention_output = layers.Dropout(dropout)(attention_output)
        out1 = layers.LayerNormalization(epsilon=1e-6)(inputs + attention_output)
        
        # Feed forward
        ffn_output = layers.Dense(ff_dim, activation="relu")(out1)
        ffn_output = layers.Dense(inputs.shape[-1])(ffn_output)
        ffn_output = layers.Dropout(dropout)(ffn_output)
        out2 = layers.LayerNormalization(epsilon=1e-6)(out1 + ffn_output)
        
        return out2
    
    def _build_model(self) -> keras.Model:
        """Build Transformer architecture."""
        inputs = layers.Input(shape=self.input_shape)
        
        # Embedding
        x = layers.Dense(self.d_model)(inputs)
        x = layers.LayerNormalization(epsilon=1e-6)(x)
        
        # Transformer blocks
        x = self._transformer_encoder(x, head_size=64, num_heads=4, ff_dim=256, dropout=0.3)
        x = self._transformer_encoder(x, head_size=64, num_heads=4, ff_dim=256, dropout=0.3)
        
        # Global pooling
        x = layers.GlobalAveragePooling1D()(x)
        x = layers.Dropout(0.5)(x)
        
        # Dense layers
        x = layers.Dense(128, activation='relu')(x)
        x = layers.Dropout(0.5)(x)
        x = layers.Dense(64, activation='relu')(x)
        
        # Output
        outputs = layers.Dense(self.num_classes, activation='softmax')(x)
        
        model = keras.Model(inputs, outputs)
        model.compile(
            optimizer=keras.optimizers.Adam(learning_rate=0.001),
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
        )
        
        return model
    
    def train(
        self,
        X_train: np.ndarray,
        y_train: np.ndarray,
        X_val: Optional[np.ndarray] = None,
        y_val: Optional[np.ndarray] = None,
        epochs: int = 50,
        batch_size: int = 32
    ) -> keras.callbacks.History:
        """Train the model."""
        callbacks_list = [
            callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True),
            callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5),
            callbacks.ModelCheckpoint('best_transformer_model.h5', save_best_only=True, monitor='val_loss')
        ]
        
        validation_data = (X_val, y_val) if X_val is not None else None
        
        history = self.model.fit(
            X_train, y_train,
            validation_data=validation_data,
            epochs=epochs,
            batch_size=batch_size,
            callbacks=callbacks_list,
            verbose=1
        )
        
        return history
    
    def predict(self, X: np.ndarray) -> np.ndarray:
        """Make predictions."""
        return self.model.predict(X)
    
    def save(self, filepath: str):
        """Save the model."""
        self.model.save(filepath)
    
    def load(self, filepath: str):
        """Load a saved model."""
        self.model = keras.models.load_model(filepath)


def prepare_features_for_dl(
    audio_list: List[np.ndarray],
    feature_type: str = 'mel',
    n_mels: int = 128,
    hop_length: int = 512
) -> np.ndarray:
    """
    Prepare features for deep learning models.
    
    Args:
        audio_list: List of audio waveforms
        feature_type: Type of features ('mel', 'mfcc', 'spectrogram')
        n_mels: Number of mel bands
        hop_length: Hop length for STFT
    
    Returns:
        Feature array with shape (n_samples, time_steps, features)
    """
    features = []
    
    for audio in audio_list:
        if feature_type == 'mel':
            feat = librosa.feature.melspectrogram(
                y=audio, n_mels=n_mels, hop_length=hop_length
            )
            feat = librosa.power_to_db(feat, ref=np.max)
        elif feature_type == 'mfcc':
            feat = librosa.feature.mfcc(y=audio, n_mfcc=13, hop_length=hop_length)
        elif feature_type == 'spectrogram':
            stft = librosa.stft(audio, hop_length=hop_length)
            feat = np.abs(stft)
            feat = librosa.power_to_db(feat, ref=np.max)
        else:
            raise ValueError(f"Unknown feature type: {feature_type}")
        
        # Transpose to (time_steps, features)
        feat = feat.T
        features.append(feat)
    
    # Pad sequences to same length
    max_len = max(f.shape[0] for f in features)
    padded_features = []
    
    for feat in features:
        if feat.shape[0] < max_len:
            pad_width = max_len - feat.shape[0]
            feat = np.pad(feat, ((0, pad_width), (0, 0)), mode='constant')
        padded_features.append(feat)
    
    return np.array(padded_features)


if __name__ == '__main__':
    print("Deep Learning Models Module")
    print("=" * 50)
    print("Available models:")
    print("  - AudioCNN: Convolutional Neural Network")
    print("  - AudioLSTM: Long Short-Term Memory network")
    print("  - AudioTransformer: Transformer-based model")
    print()
    print("Example usage:")
    print("  model = AudioCNN(input_shape=(128, 128), num_classes=10)")
    print("  model.train(X_train, y_train, X_val, y_val)")

401 lines•13.2 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer