amroa commited on
Commit
d967830
·
1 Parent(s): 283eb10
Files changed (5) hide show
  1. app.py +189 -0
  2. config.py +25 -0
  3. label_mapping.csv +729 -0
  4. model.py +39 -0
  5. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ import os
6
+ import requests
7
+ from config import Config
8
+ from model import BirdAST
9
+ import torch
10
+ import librosa
11
+ import noisereduce as nr
12
+ import pandas as pd
13
+ import torch.nn.functional as F
14
+ import random
15
+ from torchaudio.compliance import kaldi
16
+ from torchaudio.functional import resample
17
+ from transformers import ASTFeatureExtractor
18
+
19
+ #TAG = "gaunernst/vit_base_patch16_1024_128.audiomae_as2m_ft_as20k"
20
+ #MODEL = timm.create_model(f"hf_hub:{TAG}", pretrained=True).eval()
21
+
22
+ #LABEL_URL = "https://huggingface.co/datasets/huggingface/label-files/raw/main/audioset-id2label.json"
23
+ #AUDIOSET_LABELS = list(json.loads(requests.get(LABEL_URL).content).values())
24
+
25
+ FEATURE_EXTRACTOR = ASTFeatureExtractor()
26
+
27
+ def plot_mel(sr, x):
28
+ mel_spec = librosa.feature.melspectrogram(y=x, sr=sr, n_mels=224, fmax=10000)
29
+ mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)
30
+ mel_spec_db = (mel_spec_db - mel_spec_db.min()) / (mel_spec_db.max() - mel_spec_db.min()) # normalize spectrogram to [0,1]
31
+ mel_spec_db = np.stack([mel_spec_db, mel_spec_db, mel_spec_db], axis=-1) # Convert to 3-channel
32
+ fig, ax = plt.subplots(nrows=1, ncols=1, sharex=True)
33
+ librosa.display.specshow(mel_spec_db[:, :, 0], sr=sr, x_axis='time', y_axis='mel', fmin = 0, fmax=10000, ax = ax)
34
+ return fig
35
+
36
+ def plot_wave(sr, x):
37
+ ry = nr.reduce_noise(y=x, sr=sr)
38
+ fig, ax = plt.subplots(2, 1, figsize=(12, 8))
39
+
40
+ # Plot the original waveform
41
+ librosa.display.waveshow(x, sr=sr, ax=ax[0])
42
+ ax[0].set(title='Original Waveform')
43
+ ax[0].set_xlabel('Time (s)')
44
+ ax[0].set_ylabel('Amplitude')
45
+
46
+ # Plot the noise-reduced waveform
47
+ librosa.display.waveshow(ry, sr=sr, ax=ax[1])
48
+ ax[1].set(title='Noise Reduced Waveform')
49
+ ax[1].set_xlabel('Time (s)')
50
+ ax[1].set_ylabel('Amplitude')
51
+
52
+ plt.tight_layout()
53
+ return fig
54
+
55
+ def predict(audio, start, end):
56
+ sr, x = audio
57
+ x = np.array(x, dtype=np.float64)
58
+ res = preprocess_for_inference(x, sr)
59
+
60
+ if start >= end:
61
+ raise gr.Error(f"`start` ({start}) must be smaller than end ({end}s)")
62
+
63
+ if x.shape[0] < start * sr:
64
+ raise gr.Error(f"`start` ({start}) must be smaller than audio duration ({x.shape[0] / sr:.0f}s)")
65
+
66
+ if x.shape[0] > end * sr:
67
+ end = x.shape[0]/(1.0*sr)
68
+
69
+ fig1 = plot_mel(sr, x)
70
+ fig2 = plot_wave(sr, x)
71
+
72
+
73
+ return res, res, fig1, fig2
74
+
75
+ def download_model(url, model_path):
76
+ if not os.path.exists(model_path):
77
+ response = requests.get(url)
78
+ response.raise_for_status() # Ensure the request was successful
79
+ with open(model_path, 'wb') as f:
80
+ f.write(response.content)
81
+
82
+ # Model URL and path
83
+ model_url = 'https://huggingface.co/shiyi-li/BirdAST/resolve/main/BirdAST_Baseline_fold_1.pth'
84
+ model_path = 'BirdAST_Baseline_fold_1.pth'
85
+ download_model(model_url, model_path)
86
+
87
+ # Load the model (assumes you have the model architecture defined)
88
+ eval_model = BirdAST(Config().backbone_name, Config().n_classes, n_mlp_layers=1, activation='silu')
89
+ state_dict = torch.load('BirdAST_Baseline_fold_1.pth', map_location='cpu')
90
+ eval_model.load_state_dict(state_dict)
91
+
92
+ # Set to evaluation mode
93
+ eval_model.eval()
94
+
95
+ # Load the species mapping
96
+ label_mapping = pd.read_csv('label_mapping.csv')
97
+ species_id_to_name = {row['species_id']: row['scientific_name'] for index, row in label_mapping.iterrows()}
98
+
99
+
100
+ def preprocess_for_inference(audio_arr, sr):
101
+ spec = FEATURE_EXTRACTOR(audio_arr, sampling_rate=sr, padding="max_length", return_tensors="pt")
102
+ input_values = spec['input_values'] #.squeeze(0)
103
+ #print(input)
104
+ #print(input.shape)
105
+ results = []
106
+ with torch.no_grad():
107
+ # Perform model evaluation
108
+ output = eval_model(input_values)
109
+ predict_score = F.softmax(output['logits'], dim=1)
110
+
111
+ # Get the top 10 predictions
112
+ topk_values, topk_indices = torch.topk(predict_score, 10, dim=1)
113
+
114
+ # Map indices to species names and probabilities
115
+ for idx, scores in zip(topk_indices[0], topk_values[0]):
116
+ species_name = species_id_to_name[idx.item()]
117
+ probability = scores.item()
118
+ results.append([species_name, probability])
119
+
120
+ return results
121
+
122
+ DESCRIPTION = """
123
+ Bird audio classification using SOTA Voice of Jungle Technology.
124
+ """
125
+
126
+ """
127
+ with gr.Blocks() as demo:
128
+ submit_btn = gr.Button("Submit")
129
+ demo = gr.Interface(
130
+ title="Bird audio classification",
131
+ description=DESCRIPTION,
132
+ fn=predict,
133
+ inputs=["audio", "number", "number"],
134
+ outputs=[
135
+ gr.Dataframe(headers=["class", "score"], row_count=10, label="prediction"),
136
+ gr.Plot(label="waveform"),
137
+ gr.Plot(label="spectrogram"),
138
+ ],
139
+ examples=[
140
+ ["312_Cissopis_leverinia_1.wav", 0, 5],
141
+ ["1094_Pionus_fuscus_2.wav", 0, 10],
142
+ ],
143
+ )
144
+ """
145
+
146
+
147
+ css = """
148
+ .number-input {
149
+ height: 100%;
150
+ padding-bottom: 60px; /* Adust the value as needed for more or less space */
151
+ }
152
+ .full-height {
153
+ height: 100%;
154
+ }
155
+ .column-container {
156
+ height: 100%;
157
+ }
158
+ """
159
+ with gr.Blocks(css = css) as demo:
160
+ gr.Markdown("# Bird audio classification")
161
+ gr.Markdown(DESCRIPTION)
162
+
163
+ with gr.Row():
164
+ with gr.Column(elem_classes="column-container"):
165
+ start_time_input = gr.Number(label="Start Time", value=0, elem_classes="number-input full-height")
166
+ end_time_input = gr.Number(label="End Time", value=1, elem_classes="number-input full-height")
167
+ with gr.Column():
168
+ audio_input = gr.Audio(label="Input Audio", elem_classes="full-height")
169
+
170
+
171
+ with gr.Row():
172
+ raw_class_output = gr.Dataframe(headers=["class", "score"], row_count=10, label="Class Prediction")
173
+ species_output = gr.Dataframe(headers=["class", "score"], row_count=10, label="Species Prediction")
174
+
175
+ with gr.Row():
176
+ waveform_output = gr.Plot(label="Waveform")
177
+ spectrogram_output = gr.Plot(label="Spectrogram")
178
+
179
+ gr.Examples(
180
+ examples=[
181
+ ["312_Cissopis_leverinia_1.wav", 0, 5],
182
+ ["1094_Pionus_fuscus_2.wav", 0, 10],
183
+ ],
184
+ inputs=[audio_input, start_time_input, end_time_input]
185
+ )
186
+
187
+ gr.Button("Predict").click(predict, [audio_input, start_time_input, end_time_input], [raw_class_output, species_output, waveform_output, spectrogram_output])
188
+
189
+ demo.launch(share = True)
config.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Config:
2
+ model_name = "BirdAST_Baseline_5folds"
3
+ backbone_name = "MIT/ast-finetuned-audioset-10-10-0.4593"
4
+ n_classes = 728 # number of classes in the dataset
5
+ audio_sr = 16000 #Hz
6
+ segment_length = 10 #s
7
+ fft_window = 0.025 #s
8
+ hop_window_length = 0.01 #s
9
+ n_mels = 128
10
+ low_cut = 1000 #Hz
11
+ high_cut = 8000 #Hz
12
+ top_db = 100
13
+ batch_size = 4
14
+ num_workers = 0
15
+ n_splits = 5
16
+ max_lr = 1e-5
17
+ epochs = 15
18
+ weight_decay = 0.01
19
+ lr_final_div = 1000
20
+ amp = True
21
+ grad_accum_steps = 1
22
+ max_grad_norm = 1e7
23
+ print_epoch_freq = 1
24
+ print_freq = 500
25
+ random_seed = 2046
label_mapping.csv ADDED
@@ -0,0 +1,729 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ scientific_name,species_id
2
+ Accipiter_superciliosus,0
3
+ Akletos_melanoceps,1
4
+ Amazona_amazonica,2
5
+ Amazona_diadema,3
6
+ Amazona_farinosa,4
7
+ Amazona_festiva,5
8
+ Amblycercus_holosericeus,6
9
+ Ammodramus_aurifrons,7
10
+ Ammonastes_pelzelni,8
11
+ Ampelion_rufaxilla,9
12
+ Anabazenops_dorsalis,10
13
+ Anairetes_parulus,11
14
+ Andigena_hypoglauca,12
15
+ Anhima_cornuta,13
16
+ Anisognathus_lacrymosus,14
17
+ Anthracothorax_viridigula,15
18
+ Antrostomus_rufus,16
19
+ Ara_ararauna,17
20
+ Ara_chloropterus,18
21
+ Ara_severus,19
22
+ Aramides_cajaneus,20
23
+ "Aramus_Vieillot,",21
24
+ Aratinga_leucophthalma,22
25
+ Ardea_cocoi,23
26
+ Arremon_abeillei,24
27
+ Arremon_assimilis,25
28
+ Arremon_taciturnus,26
29
+ Asthenes_flammulata,27
30
+ Atlapetes_latinuchus,28
31
+ Atticora_tibialis,29
32
+ Attila_bolivianus,30
33
+ Attila_cinnamomeus,31
34
+ Attila_citriniventris,32
35
+ Attila_spadiceus,33
36
+ Automolus_infuscatus,34
37
+ Automolus_ochrolaemus,35
38
+ Automolus_paraensis,36
39
+ Automolus_rufipileatus,37
40
+ Automolus_subulatus,38
41
+ Basileuterus_tristriatus,39
42
+ Berlepschia_rikeri,40
43
+ Boissonneaua_matthewsii,41
44
+ Brotogeris_chrysoptera,42
45
+ Brotogeris_cyanoptera,43
46
+ Brotogeris_sanctithomae,44
47
+ Brotogeris_versicolurus,45
48
+ Bucco_capensis,46
49
+ Buteo_nitidus,47
50
+ Buteogallus_schistaceus,48
51
+ Buteogallus_urubitinga,49
52
+ Buthraupis_montana,50
53
+ Cacicus_cela,51
54
+ Cacicus_chrysonotus,52
55
+ Cacicus_haemorrhous,53
56
+ Cacicus_leucoramphus,54
57
+ Calidris_fuscicollis,55
58
+ Campephilus_melanoleucos,56
59
+ Campephilus_pollens,57
60
+ Campephilus_rubricollis,58
61
+ Camptostoma_obsoletum,59
62
+ Campylorhamphus_procurvoides,60
63
+ Campylorhynchus_fasciatus,61
64
+ Cantorchilus_leucotis,62
65
+ Capito_auratus,63
66
+ Capito_aurovirens,64
67
+ Capito_niger,65
68
+ Capsiempis_flaveola,66
69
+ Carduelis_magellanica,67
70
+ Caryothraustes_canadensis,68
71
+ Catharus_fuscater,69
72
+ Celeus_elegans,70
73
+ Celeus_flavus,71
74
+ Celeus_grammicus,72
75
+ Celeus_torquatus,73
76
+ Celeus_undatus,74
77
+ Cephalopterus_ornatus,75
78
+ Ceratopipra_erythrocephala,76
79
+ Ceratotriccus_granadensis,77
80
+ Cercomacra_cinerascens,78
81
+ Cercomacra_manu,79
82
+ Cercomacra_nigrescens,80
83
+ Cercomacroides_fuscicauda,81
84
+ Cercomacroides_serva,82
85
+ Cercomacroides_tyrannina,83
86
+ Certhiasomus_stictolaemus,84
87
+ Certhiaxis_cinnamomeus,85
88
+ Certhiaxis_mustelinus,86
89
+ Chaetura_brachyura,87
90
+ Chaetura_spinicaudus,88
91
+ Chamaeza_mollissima,89
92
+ Chamaeza_nobilis,90
93
+ Charadrius_collaris,91
94
+ Chelidoptera_tenebrosa,92
95
+ Chionomesa_fimbriata,93
96
+ Chiroxiphia_pareola,94
97
+ Chlorestes_cyanus,95
98
+ Chloroceryle_americana,96
99
+ Chlorophanes_spiza,97
100
+ Chlorophonia_pyrrhophrys,98
101
+ Chlorornis_riefferii,99
102
+ Chlorospingus_flavopectus,100
103
+ Chlorostilbon_mellisugus,101
104
+ Chlorostilbon_olivaresi,102
105
+ Chordeiles_pusillus,103
106
+ Chrysomus_icterocephalus,104
107
+ Ciccaba_albitarsis,105
108
+ Cinnycerthia_peruana,106
109
+ Cissopis_leverianus,107
110
+ Cistothorus_platensis,108
111
+ Cnemoscopus_rubrirostris,109
112
+ Cnemotriccus_fuscatus,110
113
+ Cnipodectes_subbrunneus,111
114
+ Coccycua_minuta,112
115
+ Coccyzus_melacoryphus,113
116
+ Coeligena_violifer,114
117
+ "Coereba_Vieillot,",115
118
+ Colaptes_atricollis,116
119
+ Colibri_coruscans,117
120
+ Columba_speciosa,118
121
+ Columbina_cruziana,119
122
+ Columbina_minuta,120
123
+ Conirostrum_bicolor,121
124
+ Conirostrum_margaritae,122
125
+ Conopias_parvus,123
126
+ Conopias_trivirgatus,124
127
+ Conopophaga_aurita,125
128
+ Contopus_fumigatus,126
129
+ Contopus_punensis,127
130
+ Corythopis_torquatus,128
131
+ Cranioleuca_antisiensis,129
132
+ Cranioleuca_berlepschi,130
133
+ Cranioleuca_gutturata,131
134
+ Cranioleuca_muelleri,132
135
+ Cranioleuca_vulpecula,133
136
+ Cranioleuca_vulpina,134
137
+ Crax_alector,135
138
+ Crax_globulosa,136
139
+ Crotophaga_ani,137
140
+ Crotophaga_major,138
141
+ Crypturellus_bartletti,139
142
+ Crypturellus_casiquiare,140
143
+ Crypturellus_cinereus,141
144
+ Crypturellus_duidae,142
145
+ Crypturellus_soui,143
146
+ Crypturellus_undulatus,144
147
+ Crypturellus_variegatus,145
148
+ Cyanerpes_caeruleus,146
149
+ Cyanicterus_cyanicterus,147
150
+ Cyanocorax_heilprini,148
151
+ Cyanocorax_violaceus,149
152
+ Cyanocorax_yncas,150
153
+ Cyanoloxia_rothschildii,151
154
+ Cyanolyca_viridicyanus,152
155
+ Cyclarhis_gujanensis,153
156
+ Cymbilaimus_lineatus,154
157
+ Cyphorhinus_arada,155
158
+ Cyphorhinus_thoracicus,156
159
+ Cyphos_macrodactylus,157
160
+ Dacnis_albiventris,158
161
+ Daptrius_ater,159
162
+ Deconychura_longicauda,160
163
+ Dendrexetastes_rufigula,161
164
+ Dendrocincla_fuliginosa,162
165
+ Dendrocincla_merula,163
166
+ Dendrocolaptes_certhia,164
167
+ Dendrocolaptes_hoffmannsi,165
168
+ Dendrocolaptes_picumnus,166
169
+ Dendrocygna_autumnalis,167
170
+ Dendroplex_kienerii,168
171
+ Deroptyus_accipitrinus,169
172
+ Dichrozona_cincta,170
173
+ Diglossa_cyanea,171
174
+ Diglossa_mystacalis,172
175
+ Diglossa_sittoides,173
176
+ Donacobius_atricapilla,174
177
+ Dromococcyx_phasianellus,175
178
+ Drymophila_devillei,176
179
+ Drymophila_striaticeps,177
180
+ Dryocopus_lineatus,178
181
+ Egretta_alba,179
182
+ Egretta_thula,180
183
+ Elaenia_flavogaster,181
184
+ Elaenia_pallatangae,182
185
+ Elaenia_pelzelni,183
186
+ Elaenia_ruficeps,184
187
+ Elliotomyia_chionogaster,185
188
+ Empidonomus_varius,186
189
+ Epinecrophylla_amazonica,187
190
+ Epinecrophylla_gutturalis,188
191
+ Epinecrophylla_haematonota,189
192
+ Epinecrophylla_leucophthalma,190
193
+ Epinecrophylla_ornata,191
194
+ Eriocnemis_alinae,192
195
+ Eubucco_richardsoni,193
196
+ Euchrepomis_humeralis,194
197
+ Eucometis_penicillata,195
198
+ Euphonia_cayennensis,196
199
+ Euphonia_chlorotica,197
200
+ Euphonia_chrysopasta,198
201
+ Euphonia_cyanocephala,199
202
+ Euphonia_minuta,200
203
+ Euphonia_plumbea,201
204
+ Euphonia_rufiventris,202
205
+ Euphonia_xanthogaster,203
206
+ Eupsittula_pertinax,204
207
+ Eurypygidae,205
208
+ Falco_deiroleucus,206
209
+ Falco_femoralis,207
210
+ Falco_magnirostris,208
211
+ Falco_rufigularis,209
212
+ Florisuga_mellivora,210
213
+ Formicarius_analis,211
214
+ Formicarius_colma,212
215
+ Formicivora_grisea,213
216
+ Forpus_crassirostris,214
217
+ Forpus_xanthops,215
218
+ Frederickena_unduliger,216
219
+ Frederickena_viridis,217
220
+ Fringilla_capensis,218
221
+ Fringilla_psaltria,219
222
+ Furnarius_figulus,220
223
+ Furnarius_leucopus,221
224
+ Furnarius_minor,222
225
+ Furnarius_torridus,223
226
+ Galbacyrhynchus_leucotis,224
227
+ Galbalcyrhynchus_purusianus,225
228
+ Galbula_cyanescens,226
229
+ Galbula_cyanicollis,227
230
+ Galbula_dea,228
231
+ Galbula_galbula,229
232
+ Galbula_leucogastra,230
233
+ Galbula_ruficauda,231
234
+ Galbula_tombacea,232
235
+ Gallinula_galeata,233
236
+ Geothlypis_aequinoctialis,234
237
+ Geranospiza_caerulescens,235
238
+ Glaucidium_bolivianum,236
239
+ Glaucidium_brasilianum,237
240
+ Glaucidium_hardyi,238
241
+ Glyphorynchus_spirurus,239
242
+ Grallaria_blakei,240
243
+ Grallaria_carrikeri,241
244
+ Grallaria_obscura,242
245
+ Grallaria_occabambae,243
246
+ Grallaria_przewalskii,244
247
+ Grallaria_quitensis,245
248
+ Grallaria_ruficapilla,246
249
+ Grallaria_squamigera,247
250
+ Grallaricula_leymebambae,248
251
+ Grallaricula_ochraceifrons,249
252
+ Graydidascalus_brachyurus,250
253
+ Gymnomystax_mexicanus,251
254
+ Gymnopithys_leucaspis,252
255
+ Gymnopithys_rufigula,253
256
+ Gymnopithys_salvini,254
257
+ Habia_rubica,255
258
+ Hafferia_fortis,256
259
+ Haplospiza_rustica,257
260
+ Harpia_harpyja,258
261
+ Heliangelus_amethysticollis,259
262
+ Helicolestes_hamatus,260
263
+ Heliornis_fulica,261
264
+ Hemithraupis_flavicollis,262
265
+ Hemitriccus_griseipectus,263
266
+ Hemitriccus_inornatus,264
267
+ Hemitriccus_josephinae,265
268
+ Hemitriccus_minimus,266
269
+ Hemitriccus_minor,267
270
+ Hemitriccus_zosterops,268
271
+ Henicorhina_leucophrys,269
272
+ Henicorhina_leucoptera,270
273
+ Henicorhina_leucosticta,271
274
+ Herpetotheres_cachinnans,272
275
+ Herpsilochmus_axillaris,273
276
+ Herpsilochmus_dorsimaculatus,274
277
+ Herpsilochmus_dugandi,275
278
+ Herpsilochmus_parkeri,276
279
+ Herpsilochmus_praedictus,277
280
+ Herpsilochmus_stotzi,278
281
+ Heterocercus_aurantiivertex,279
282
+ Heterocercus_flavivertex,280
283
+ Hoploxypterus_cayanus,281
284
+ Hylexetastes_perrotii,282
285
+ Hylexetastes_uniformis,283
286
+ Hylocharis_sapphirina,284
287
+ Hylopezus_macularius,285
288
+ Hylophilus_brunneiceps,286
289
+ Hylophilus_muscicapinus,287
290
+ Hylophilus_olivaceus,288
291
+ Hylophilus_pectoralis,289
292
+ Hylophilus_semicinereus,290
293
+ Hylophilus_thoracicus,291
294
+ Hylophylax_naevius,292
295
+ Hylophylax_punctulatus,293
296
+ Hypnelus_bicinctus,294
297
+ Hypocnemis_cantator,295
298
+ Hypocnemis_flavescens,296
299
+ Hypocnemis_hypoxantha,297
300
+ Hypocnemis_peruviana,298
301
+ Hypocnemis_rondoni,299
302
+ Hypocnemis_striata,300
303
+ Hypocnemoides_maculicauda,301
304
+ Hypocnemoides_melanopogon,302
305
+ Ibycter_americanus,303
306
+ Icterus_cayanensis,304
307
+ Ictinia_plumbea,305
308
+ Incaspiza_laeta,306
309
+ Incaspiza_watkinsi,307
310
+ Inezia_subflava,308
311
+ Iodopleura_isabellae,309
312
+ Iridosornis_reinhardti,310
313
+ Isleria_guttata,311
314
+ Islerothraupis_luctuosa,312
315
+ Jacamerops_aureus,313
316
+ Jacana_jacana,314
317
+ Kleinothraupis_atropileus,315
318
+ Knipolegus_orenocensis,316
319
+ Lampropsar_tanagrinus,317
320
+ Lamprospiza_melanoleuca,318
321
+ Lanio_fulvus,319
322
+ Lanio_versicolor,320
323
+ Laniocera_hypopyrra,321
324
+ Larus_serranus,322
325
+ Laterallus_exilis,323
326
+ Lathrotriccus_euleri,324
327
+ Leistes_bellicosus,325
328
+ Lepidocolaptes_duidae,326
329
+ Lepidocolaptes_fatimalimae,327
330
+ Lepidocolaptes_fuscicapillus,328
331
+ Lepidothrix_coronata,329
332
+ Lepidothrix_nattereri,330
333
+ Lepidothrix_serena,331
334
+ Leptodon_cayanensis,332
335
+ Leptopogon_taczanowskii,333
336
+ Leptotila_rufaxilla,334
337
+ Leptotila_verreauxi,335
338
+ Leuconotopicus_fumigatus,336
339
+ Leucopternis_kuhli,337
340
+ Liosceles_thoracicus,338
341
+ Lipaugus_vociferans,339
342
+ Loddigesia_mirabilis,340
343
+ Lophostrix_cristata,341
344
+ Lophotriccus_galeatus,342
345
+ Lophotriccus_vitiosus,343
346
+ Lurocalis_rufiventris,344
347
+ Malacoptila_fusca,345
348
+ Manacus_manacus,346
349
+ Margarornis_squamiger,347
350
+ Mazaria_propinqua,348
351
+ Mecocerculus_stictopterus,349
352
+ Megascops_albogularis,350
353
+ Megascops_choliba,351
354
+ Megascops_ingens,352
355
+ Megascops_petersoni,353
356
+ Megascops_watsonii,354
357
+ Megastictus_margaritatus,355
358
+ Melanerpes_cruentatus,356
359
+ Melanopareia_maranonica,357
360
+ Micrastur_gilvicollis,358
361
+ Micrastur_mintoni,359
362
+ Micrastur_mirandollei,360
363
+ Micrastur_ruficollis,361
364
+ Micrastur_semitorquatus,362
365
+ Microbates_collaris,363
366
+ Microcerculus_bambla,364
367
+ Microcerculus_marginatus,365
368
+ Microrhopias_quixensis,366
369
+ Milvago_chimachima,367
370
+ Mimus_longicaudatus,368
371
+ Mionectes_oleagineus,369
372
+ Mionectes_striaticollis,370
373
+ Mitrephanes_olivaceus,371
374
+ Monasa_atra,372
375
+ Monasa_morphoeus,373
376
+ Monasa_nigrifrons,374
377
+ Muscicapa_similis,375
378
+ Myadestes_ralloides,376
379
+ Myiarchus_cephalotes,377
380
+ Myiarchus_swainsoni,378
381
+ Myiarchus_tuberculifer,379
382
+ Myiarchus_tyrannulus,380
383
+ Myioborus_melanocephalus,381
384
+ Myioborus_miniatus,382
385
+ Myiodynastes_maculatus,383
386
+ Myiopagis_caniceps,384
387
+ Myiopagis_flavivertex,385
388
+ Myiopagis_gaimardii,386
389
+ Myiophobus_cryptoxanthus,387
390
+ Myiornis_ecaudatus,388
391
+ Myiotheretes_fumigatus,389
392
+ Myiothlypis_coronata,390
393
+ Myiothlypis_luteoviridis,391
394
+ Myiothlypis_nigrocristata,392
395
+ Myiozetetes_cayanensis,393
396
+ Myiozetetes_luteiventris,394
397
+ Myornis_senilis,395
398
+ Myrmeciza_atrothorax,396
399
+ Myrmeciza_longipes,397
400
+ Myrmelastes_humaythae,398
401
+ Myrmelastes_hyperythrus,399
402
+ Myrmelastes_leucostigma,400
403
+ Myrmelastes_rufifacies,401
404
+ Myrmoborus_leucophrys,402
405
+ Myrmoborus_lugubris,403
406
+ Myrmoborus_melanurus,404
407
+ Myrmoborus_myotherinus,405
408
+ Myrmochanes_hemileucus,406
409
+ Myrmoderus_ferruginea,407
410
+ Myrmornis_torquata,408
411
+ Myrmothera_campanisona,409
412
+ Myrmotherula_ambigua,410
413
+ Myrmotherula_assimilis,411
414
+ Myrmotherula_axillaris,412
415
+ Myrmotherula_brachyura,413
416
+ Myrmotherula_cherriei,414
417
+ Myrmotherula_iheringi,415
418
+ Myrmotherula_klagesi,416
419
+ Myrmotherula_longipennis,417
420
+ Myrmotherula_menetriesii,418
421
+ Nasica_longirostris,419
422
+ Nemosia_pileata,420
423
+ Neoctantes_niger,421
424
+ Neopelma_chrysocephalum,422
425
+ Neopipo_cinnamomea,423
426
+ Nesotriccus_tumbezanus,424
427
+ Nonnula_sclateri,425
428
+ Notharchus_hyperrhynchus,426
429
+ Notharchus_ordii,427
430
+ Notharchus_tectus,428
431
+ Nothocrax_urumutum,429
432
+ Nyctalops_stygius,430
433
+ Nyctibius_grandis,431
434
+ Nyctibius_leucopterus,432
435
+ Nyctidromus_albicollis,433
436
+ Nyctiprogne_leucopyga,434
437
+ Nystactes_tamatia,435
438
+ Nystalus_obamai,436
439
+ Ochthoeca_cinnamomeiventris,437
440
+ Ochthoeca_diadema,438
441
+ Ochthoeca_pulchella,439
442
+ Ochthoeca_thoracica,440
443
+ Ocreatus_underwoodii,441
444
+ Odontophorus_gujanensis,442
445
+ Onychorhynchus_coronatus,443
446
+ Opisthocomidae,444
447
+ Ornithion_inerme,445
448
+ Ortalis_guttata,446
449
+ Ortalis_motmot,447
450
+ Pachyramphus_castaneus,448
451
+ Pachyramphus_marginatus,449
452
+ Pachyramphus_minor,450
453
+ Pachyramphus_polychopterus,451
454
+ Pachyramphus_rufus,452
455
+ Pachyramphus_surinamus,453
456
+ Pachyramphus_versicolor,454
457
+ Pachysylvia_hypoxantha,455
458
+ Pardirallus_sanguinolentus,456
459
+ Paroaria_gularis,457
460
+ Patagioenas_cayennensis,458
461
+ Patagioenas_plumbea,459
462
+ Patagioenas_subvinacea,460
463
+ Penelope_jacquacu,461
464
+ Penelope_montagnii,462
465
+ Percnostola_rufifrons,463
466
+ Perissocephalus_tricolor,464
467
+ Phacellodomus_rufifrons,465
468
+ Phaeomyias_murina,466
469
+ Phaeothlypis_fulvicauda,467
470
+ Phaeothlypis_rivularis,468
471
+ Phaethornis_guy,469
472
+ Phaethornis_hispidus,470
473
+ Phaethornis_malaris,471
474
+ Phaethornis_ruber,472
475
+ Phaethornis_rupurumii,473
476
+ Phaethornis_superciliosus,474
477
+ Phaetusa_simplex,475
478
+ Pharomachrus_auriceps,476
479
+ Pharomachrus_pavoninus,477
480
+ Pheucticus_chrysogaster,478
481
+ Pheugopedius_coraya,479
482
+ Pheugopedius_euophrys,480
483
+ Pheugopedius_genibarbis,481
484
+ Pheugopedius_paucimaculatus,482
485
+ Philydor_erythropterum,483
486
+ Philydor_pyrrhodes,484
487
+ Phlegopsis_erythroptera,485
488
+ Phlegopsis_nigromaculata,486
489
+ Phoenicircus_carnifex,487
490
+ Phoenicircus_nigricollis,488
491
+ Phyllaemulor_bracteatus,489
492
+ Phyllomyias_plumbeiceps,490
493
+ Phylloscartes_gualaquizae,491
494
+ Phylloscartes_ventralis,492
495
+ Piaya_cayana,493
496
+ Piaya_melanogaster,494
497
+ Piculus_chrysochloros,495
498
+ Piculus_flavigula,496
499
+ Picumnus_castelnau,497
500
+ Picumnus_exilis,498
501
+ Picumnus_lafresnayi,499
502
+ Picumnus_steindachneri,500
503
+ Picumnus_varzeae,501
504
+ Pionites_melanocephalus,502
505
+ Pionus_fuscus,503
506
+ Pionus_menstruus,504
507
+ Pipile_cumanensis,505
508
+ Pipra_aureola,506
509
+ Pipra_pipra,507
510
+ Pipraeidea_bonariensis,508
511
+ Pipreola_arcuata,509
512
+ Piprites_chloris,510
513
+ Piranga_hepatica,511
514
+ Pitangus_lictor,512
515
+ Pitangus_sulphuratus,513
516
+ Pithys_albifrons,514
517
+ Platyrinchus_coronatus,515
518
+ Platyrinchus_platyrhynchos,516
519
+ Poecilotriccus_calopterus,517
520
+ Poecilotriccus_capitalis,518
521
+ Poecilotriccus_latirostris,519
522
+ Poecilotriccus_luluae,520
523
+ Poecilotriccus_senex,521
524
+ Polioptila_attenboroughi,522
525
+ Polioptila_guianensis,523
526
+ Polioptila_plumbea,524
527
+ Polytmus_theresiae,525
528
+ Progne_subis,526
529
+ Psarocolius_angustifrons,527
530
+ Psarocolius_bifasciatus,528
531
+ Psarocolius_decumanus,529
532
+ Psarocolius_viridis,530
533
+ Pseudastur_albicollis,531
534
+ Pseudocolaptes_boissonneauii,532
535
+ Pseudotriccus_ruficeps,533
536
+ Psittacara_frontatus,534
537
+ Psittacara_mitratus,535
538
+ Psophia_crepitans,536
539
+ Pteroglossus_aracari,537
540
+ Pteroglossus_azara,538
541
+ Pteroglossus_beauharnaisii,539
542
+ Pteroglossus_castanotis,540
543
+ Pulsatrix_perspicillata,541
544
+ Pygiptila_stellaris,542
545
+ Pygochelidon_melanoleuca,543
546
+ Pyriglena_maura,544
547
+ Pyrilia_aurantiocephala,545
548
+ Pyrilia_barrabandi,546
549
+ Pyrilia_caica,547
550
+ Pyrrhomyias_cinnamomeus,548
551
+ Pyrrhura_roseifrons,549
552
+ Querula_purpurata,550
553
+ Ramphastos_sulfuratus,551
554
+ Ramphastos_tucanus,552
555
+ Ramphastos_vitellinus,553
556
+ Ramphocaenus_melanurus,554
557
+ Ramphocelus_carbo,555
558
+ Ramphocelus_nigrogularis,556
559
+ Ramphotrigon_megacephalum,557
560
+ Ramphotrigon_ruficauda,558
561
+ Rhegmatorhina_berlepschi,559
562
+ Rhegmatorhina_cristata,560
563
+ Rhegmatorhina_hoffmannsi,561
564
+ Rhegmatorhina_melanosticta,562
565
+ Rhynchocyclus_olivaceus,563
566
+ Rhytipterna_immunda,564
567
+ Rhytipterna_simplex,565
568
+ Rostrhamus_sociabilis,566
569
+ Rufirallus_castaneiceps,567
570
+ Rufirallus_viridis,568
571
+ Rupicola_rupicola,569
572
+ Rynchops_niger,570
573
+ Sakesphorus_canadensis,571
574
+ Sakesphorus_luctuosus,572
575
+ Saltator_coerulescens,573
576
+ Saltator_grossus,574
577
+ Saltator_maximus,575
578
+ Saltator_striatipectus,576
579
+ Schiffornis_major,577
580
+ Schiffornis_turdina,578
581
+ Schistocichla_schistacea,579
582
+ Schizoeaca_fuliginosa,580
583
+ Sciaphylax_castanea,581
584
+ Sciaphylax_hemimelaena,582
585
+ Sclateria_naevia,583
586
+ Sclerurus_obscurior,584
587
+ Sclerurus_rufigularis,585
588
+ Scytalopus_altirostris,586
589
+ Scytalopus_atratus,587
590
+ Scytalopus_femoralis,588
591
+ Scytalopus_intermedius,589
592
+ Scytalopus_parvirostris,590
593
+ Selenidera_gouldii,591
594
+ Selenidera_nattereri,592
595
+ Selenidera_piperivora,593
596
+ Selenidera_reinwardtii,594
597
+ Sericossypha_albocristata,595
598
+ Serpophaga_hypoleuca,596
599
+ Serpophaga_subcristata,597
600
+ Sicalis_columbiana,598
601
+ Sirystes_sibilator,599
602
+ Sirystes_subcanescens,600
603
+ Sittasomus_griseicapillus,601
604
+ Spinus_olivaceus,602
605
+ Spizaetus_ornatus,603
606
+ Sporophila_angolensis,604
607
+ Sporophila_bouvronides,605
608
+ Sporophila_castaneiventris,606
609
+ Sporophila_fringilloides,607
610
+ Sporophila_luctuosa,608
611
+ Sporophila_simplex,609
612
+ Sternula_superciliaris,610
613
+ Stilpnia_viridicollis,611
614
+ Sturnella_militaris,612
615
+ Sublegatus_obscurior,613
616
+ Synallaxis_albescens,614
617
+ Synallaxis_albigularis,615
618
+ Synallaxis_azarae,616
619
+ Synallaxis_gujanensis,617
620
+ Synallaxis_hypospodia,618
621
+ Synallaxis_maranonica,619
622
+ Synallaxis_rutilans,620
623
+ Synallaxis_unirufa,621
624
+ Syndactyla_rufosuperciliata,622
625
+ Syndactyla_ucayalae,623
626
+ Tachycineta_albiventer,624
627
+ Tachyphonus_cristatus,625
628
+ Tachyphonus_phoenicius,626
629
+ Tachyphonus_surinamus,627
630
+ Talaphorus_chlorocercus,628
631
+ Tangara_cayana,629
632
+ Tangara_chilensis,630
633
+ Tangara_nigroviridis,631
634
+ Tangara_punctata,632
635
+ Tangara_varia,633
636
+ Tangara_velia,634
637
+ Tapera_naevia,635
638
+ Taraba_major,636
639
+ Terenura_spodioptila,637
640
+ Tersina_viridis,638
641
+ Thamnomanes_ardesiacus,639
642
+ Thamnomanes_caesius,640
643
+ Thamnomanes_saturninus,641
644
+ Thamnomanes_schistogynus,642
645
+ Thamnophilus_aethiops,643
646
+ Thamnophilus_amazonicus,644
647
+ Thamnophilus_bernardi,645
648
+ Thamnophilus_cryptoleucus,646
649
+ Thamnophilus_doliatus,647
650
+ Thamnophilus_murinus,648
651
+ Thamnophilus_nigrocinereus,649
652
+ Thamnophilus_punctatus,650
653
+ Thamnophilus_ruficapillus,651
654
+ Thamnophilus_schistaceus,652
655
+ Thamnophilus_stictocephalus,653
656
+ Thamnophilus_unicolor,654
657
+ Thaumasius_taczanowskii,655
658
+ Thlypopsis_inornata,656
659
+ Thlypopsis_superciliaris,657
660
+ Thraupis_episcopus,658
661
+ Thripadectes_scrutator,659
662
+ Thripophaga_fusciceps,660
663
+ Thryothorus_griseus,661
664
+ Tiaris_obscurus,662
665
+ Tinamus_major,663
666
+ Tityra_cayana,664
667
+ Tityra_semifasciata,665
668
+ Todirostrum_chrysocrotaphum,666
669
+ Todirostrum_maculatum,667
670
+ Todirostrum_pictum,668
671
+ Tolmomyias_assimilis,669
672
+ Tolmomyias_flaviventris,670
673
+ Tolmomyias_poliocephalus,671
674
+ Tolmomyias_sulphurescens,672
675
+ Tolmomyias_traylori,673
676
+ Tolmomyias_viridiceps,674
677
+ Topaza_pella,675
678
+ Topaza_pyra,676
679
+ Touit_purpuratus,677
680
+ Troglodytes_aedon,678
681
+ Troglodytes_solstitialis,679
682
+ Trogon_collaris,680
683
+ Trogon_curucui,681
684
+ Trogon_melanurus,682
685
+ Trogon_ramonianus,683
686
+ Trogon_rufus,684
687
+ Trogon_violaceus,685
688
+ Trogon_viridis,686
689
+ Tunchiornis_ochraceiceps,687
690
+ Turdus_albicollis,688
691
+ Turdus_hauxwelli,689
692
+ Turdus_ignobilis,690
693
+ Turdus_lawrencii,691
694
+ Turdus_leucomelas,692
695
+ Turdus_maranonicus,693
696
+ Turdus_nigriceps,694
697
+ Turdus_sanchezorum,695
698
+ Turdus_serranus,696
699
+ Tyranneutes_stolzmanni,697
700
+ Tyranneutes_virescens,698
701
+ Tyranniscus_cinereiceps,699
702
+ Tyranniscus_uropygialis,700
703
+ Tyrannulus_elatus,701
704
+ Tyrannus_melancholicus,702
705
+ Tyrannus_savana,703
706
+ Uranomitra_franciae,704
707
+ Uromyias_agraphia,705
708
+ Uropsalis_segmentata,706
709
+ Veniliornis_cassini,707
710
+ Vireo_chivi,708
711
+ Vireolanius_leucotis,709
712
+ Volatinia_jacarina,710
713
+ Wetmorethraupis_sterrhopteron,711
714
+ Willisornis_poecilinotus,712
715
+ Xenoglaux_loweryi,713
716
+ Xenopipo_atronitens,714
717
+ Xenops_milleri,715
718
+ Xenops_minutus,716
719
+ Xiphocolaptes_promeropirhynchus,717
720
+ Xipholena_punicea,718
721
+ Xiphorhynchus_elegans,719
722
+ Xiphorhynchus_guttatus,720
723
+ Xiphorhynchus_obsoletus,721
724
+ Xiphorhynchus_ocellatus,722
725
+ Xiphorhynchus_pardalotus,723
726
+ Xiphorhynchus_picus,724
727
+ Zimmerius_acer,725
728
+ Zimmerius_gracilipes,726
729
+ Zimmerius_viridiflavus,727
model.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils.data import Dataset, DataLoader
3
+ from torch import nn
4
+ from transformers import ASTConfig, ASTFeatureExtractor, ASTModel
5
+
6
+ class BirdAST(nn.Module):
7
+
8
+ def __init__(self, backbone_name, n_classes, n_mlp_layers=1, activation='silu'):
9
+ super(BirdAST, self).__init__()
10
+
11
+ # pre-trained backbone
12
+ backbone_config = ASTConfig.from_pretrained(backbone_name)
13
+ self.ast = ASTModel.from_pretrained(backbone_name, config=backbone_config)
14
+ self.hidden_size = backbone_config.hidden_size
15
+
16
+ # set activation functions
17
+ if activation == 'relu':
18
+ self.activation = nn.ReLU()
19
+ elif activation == 'silu':
20
+ self.activation = nn.SiLU()
21
+ else:
22
+ raise ValueError("Unsupported activation function. Choose 'relu' or 'silu'.")
23
+
24
+ # define MLP layers with activation
25
+ layers = []
26
+ for _ in range(n_mlp_layers):
27
+ layers.append(nn.Linear(self.hidden_size, self.hidden_size))
28
+ layers.append(self.activation)
29
+ layers.append(nn.Linear(self.hidden_size, n_classes))
30
+ self.mlp = nn.Sequential(*layers)
31
+
32
+ def forward(self, spectrogram):
33
+ # spectrogram: (batch_size, n_mels, n_frames)
34
+ # output: (batch_size, n_classes)
35
+
36
+ ast_output = self.ast(spectrogram, output_hidden_states=False)
37
+ logits = self.mlp(ast_output.last_hidden_state[:, 0, :]) # Use the CLS token
38
+
39
+ return {'logits': logits}
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ matplotlib
4
+ numpy
5
+ requests
6
+ timm
7
+ pandas
8
+ torch
9
+ librosa
10
+ noisereduce
11
+ torchaudio