Upload 3 files
Browse files- config.json +51 -0
- model.py +38 -0
- pytorch_model.bin +3 -0
config.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "small_audio_classifier_mlp",
|
| 3 |
+
"torch_dtype": "float32",
|
| 4 |
+
"input_dim": 6144,
|
| 5 |
+
"num_labels": 10,
|
| 6 |
+
"hidden_dims": [
|
| 7 |
+
512,
|
| 8 |
+
256
|
| 9 |
+
],
|
| 10 |
+
"dropout": 0.2,
|
| 11 |
+
"activation": "gelu",
|
| 12 |
+
"task": "audio_classification",
|
| 13 |
+
"outputs": {
|
| 14 |
+
"logits": "float32[batch, num_labels]"
|
| 15 |
+
},
|
| 16 |
+
"preprocessing": {
|
| 17 |
+
"expected_input": "flattened log-mel spectrogram or other fixed-length audio features",
|
| 18 |
+
"example_logmel": {
|
| 19 |
+
"n_mels": 64,
|
| 20 |
+
"n_frames": 96,
|
| 21 |
+
"flatten": true,
|
| 22 |
+
"input_dim": 6144
|
| 23 |
+
},
|
| 24 |
+
"note": "compute features outside the model; this model is intentionally tiny for assignments"
|
| 25 |
+
},
|
| 26 |
+
"id2label": {
|
| 27 |
+
"0": "class_0",
|
| 28 |
+
"1": "class_1",
|
| 29 |
+
"2": "class_2",
|
| 30 |
+
"3": "class_3",
|
| 31 |
+
"4": "class_4",
|
| 32 |
+
"5": "class_5",
|
| 33 |
+
"6": "class_6",
|
| 34 |
+
"7": "class_7",
|
| 35 |
+
"8": "class_8",
|
| 36 |
+
"9": "class_9"
|
| 37 |
+
},
|
| 38 |
+
"label2id": {
|
| 39 |
+
"class_0": 0,
|
| 40 |
+
"class_1": 1,
|
| 41 |
+
"class_2": 2,
|
| 42 |
+
"class_3": 3,
|
| 43 |
+
"class_4": 4,
|
| 44 |
+
"class_5": 5,
|
| 45 |
+
"class_6": 6,
|
| 46 |
+
"class_7": 7,
|
| 47 |
+
"class_8": 8,
|
| 48 |
+
"class_9": 9
|
| 49 |
+
},
|
| 50 |
+
"version": "0.1.0"
|
| 51 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
class SmallAudioClassifierMLP(nn.Module):
|
| 6 |
+
def __init__(self, input_dim=6144, num_labels=10, hidden_dims=(512, 256), dropout=0.2, activation="gelu"):
|
| 7 |
+
super().__init__()
|
| 8 |
+
act = {"relu": nn.ReLU, "gelu": nn.GELU, "silu": nn.SiLU}[activation]
|
| 9 |
+
dims = [input_dim] + list(hidden_dims)
|
| 10 |
+
layers = []
|
| 11 |
+
for i in range(len(dims) - 1):
|
| 12 |
+
layers += [
|
| 13 |
+
nn.Linear(dims[i], dims[i+1]),
|
| 14 |
+
nn.LayerNorm(dims[i+1]),
|
| 15 |
+
act(),
|
| 16 |
+
nn.Dropout(dropout),
|
| 17 |
+
]
|
| 18 |
+
self.mlp = nn.Sequential(*layers)
|
| 19 |
+
self.classifier = nn.Linear(dims[-1], num_labels)
|
| 20 |
+
|
| 21 |
+
def forward(self, x):
|
| 22 |
+
h = self.mlp(x)
|
| 23 |
+
return self.classifier(h)
|
| 24 |
+
|
| 25 |
+
def load_pretrained(model_dir: str, map_location="cpu"):
|
| 26 |
+
with open(f"{model_dir}/config.json", "r", encoding="utf-8") as f:
|
| 27 |
+
cfg = json.load(f)
|
| 28 |
+
m = SmallAudioClassifierMLP(
|
| 29 |
+
input_dim=cfg["input_dim"],
|
| 30 |
+
num_labels=cfg["num_labels"],
|
| 31 |
+
hidden_dims=tuple(cfg["hidden_dims"]),
|
| 32 |
+
dropout=cfg["dropout"],
|
| 33 |
+
activation=cfg["activation"],
|
| 34 |
+
)
|
| 35 |
+
sd = torch.load(f"{model_dir}/pytorch_model.bin", map_location=map_location)
|
| 36 |
+
m.load_state_dict(sd)
|
| 37 |
+
m.eval()
|
| 38 |
+
return m, cfg
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3d2758b33b878bcca1883b88f69b5dcce7f905a8dd5ed17df8a093af16a36996
|
| 3 |
+
size 13130572
|