How to transform audio to spectogram using pytorch

This recipe helps you transform audio to spectogram using pytorch

Recipe Objective

How to transform audio to spectogram using PyTorch?

This is achieved by using the torchaudio under which we have to use transformation by using .transform.spectogram function which will create the spectogram from the waveform. Lets understand this with practical implementation.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
import torchaudio
import requests
import matplotlib.pyplot as plt

Step 2 - Audio url

audio_url = "https://pytorch.org/tutorials/_static/img/steam-train-whistle-daniel_simon-converted-from-mp3.wav"
request_url = requests.get(audio_url)

Step 3 - Open the audio file

with open('steam-train-whistle-daniel_simon-converted-from-mp3.wav', 'wb') as file:
    file.write(request_url.content)
audio_file = "steam-train-whistle-daniel_simon-converted-from-mp3.wav"
data_waveform, rate_of_sample = torchaudio.load(audio_file)

Step 4 - Print shape of audio file

print("This is the shape of the audio file: {}".format(data_waveform.size()))

This is the shape of the audio file: torch.Size([2, 276858])

Step 5 - Transform the audio

audio_spectogram = torchaudio.transforms.Spectrogram()(data_waveform)
print("Shape of spectrogram: {}".format(audio_spectogram.size()))

Shape of spectrogram: torch.Size([2, 201, 1385])

Step 6 - Plot the spectogram

plt.figure()
plt.imshow(audio_spectogram.log2()[0,:,:].numpy(), cmap='gray')

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.