Views
No views yet
| Metric | Score |
|---|---|
| Accuracy | 0.8778 |
| Precision | 0.9518 |
| Recall | 0.8144 |
| F1-Score | 0.8778 |
1import torch
2import torchaudio
3from torchvision import models
4from huggingface_hub import hf_hub_download
5
6# Load model
7model = models.resnet18(pretrained=False)
8model.fc = torch.nn.Linear(model.fc.in_features, 2)
9model_path = hf_hub_download(repo_id="cxlrd/engine-knock-resnet18", filename="model.pth")
10model.load_state_dict(torch.load(model_path, map_location='cpu'))
11model.eval()
12
13# Prepare audio
14waveform, sample_rate = torchaudio.load('audio.wav')
15mel_spec = torchaudio.transforms.MelSpectrogram(
16 sample_rate=16000, n_fft=1024, hop_length=512, n_mels=128
17)(waveform)
18mel_spec_db = torchaudio.transforms.AmplitudeToDB()(mel_spec)
19mel_spec_db = torch.nn.functional.interpolate(
20 mel_spec_db.unsqueeze(0), size=(224, 224), mode='bilinear'
21).repeat(1, 3, 1, 1)
22
23# Predict
24with torch.no_grad():
25 output = model(mel_spec_db)
26 prediction = torch.argmax(output, dim=1)
27 print('Clean' if prediction == 0 else 'Knocking')1@misc{engine-knock-resnet18,
2 author = {cxlrd},
3 title = {Engine Knock Detection with ResNet-18},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/cxlrd/engine-knock-resnet18}}
7}