-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
772 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
data: | ||
csv_file: '/data/digbose92/ads_complete_repo/ads_codes/SAIM-ADS/data/SAIM_ads_data_message_tone_train_test_val_clip_features.csv' | ||
|
||
parameters: | ||
batch_size: 16 | ||
train_shuffle: True | ||
val_shuffle: False | ||
epochs: 50 | ||
early_stop: 5 | ||
max_length: 333 | ||
fps: 4 | ||
base_fps: 24 | ||
num_workers: 4 | ||
|
||
device: | ||
is_cuda: True | ||
|
||
loss: | ||
loss_option: 'bce_cross_entropy_loss' | ||
|
||
optimizer: | ||
choice: 'Adam' | ||
lr: 1e-4 | ||
gamma: 0.5 | ||
step_size: 15 | ||
scheduler: 'step_lr' | ||
mode: 'max' | ||
decay: 0.001 | ||
patience: 5 | ||
factor: 0.5 | ||
verbose: True | ||
|
||
model: | ||
option: 'LSTM_multi_layer_social_message_model' | ||
model_type: 'LSTM' | ||
embedding_dim: 512 | ||
n_hidden: 128 | ||
n_layers: 2 | ||
n_classes: 2 | ||
batch_first: True | ||
|
||
output: | ||
model_dir: '/data/digbose92/ads_complete_repo/ads_codes/model_files/recent_models/model_dir' | ||
log_dir: '/data/digbose92/ads_complete_repo/ads_codes/model_files/recent_models/log_dir' |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#use transformers library to extract features from ASTs | ||
from transformers import AutoProcessor, ASTModel, AutoFeatureExtractor | ||
import torch | ||
from datasets import load_dataset | ||
import torchaudio | ||
import json | ||
from tqdm import tqdm | ||
import os | ||
import pickle | ||
|
||
def generate_file_list(json_data,folder): | ||
|
||
wav_file_names=[folder+"/"+i.split("/")[-1] for i in json_data] | ||
return wav_file_names | ||
|
||
#load the model | ||
model_option="MIT/ast-finetuned-audioset-10-10-0.4593" | ||
wav_file_list="/data/digbose92/ads_complete_repo/ads_codes/SAIM-ADS/data/jwt_ads_of_world_wav_files.json" | ||
folder="/data/digbose92/ads_complete_repo/ads_wav_files/cvpr_wav_files" | ||
option="cvpr_ads" | ||
save_folder="/data/digbose92/ads_complete_repo/ads_features/audio_embeddings/ast_embeddings/cvpr_ads" | ||
#save_folder="/data/digbose92/ads_complete_repo/ads_features/audio_embeddings/ast_embeddings/jwt_ads_of_world" | ||
|
||
if(option=="jwt_ads_of_world"): | ||
with open(wav_file_list) as f: | ||
wav_file_list = json.load(f) | ||
|
||
wav_file_names_json_data=[wav_file_list["data"][i]["wav"] for i in range(len(wav_file_list['data']))] | ||
wav_file_names=generate_file_list(wav_file_names_json_data,folder) | ||
|
||
elif(option=="cvpr_ads"): | ||
|
||
wav_file_names=os.listdir(folder) | ||
wav_file_names=[os.path.join(folder,i) for i in wav_file_names] | ||
#print(len(wav_file_names)) | ||
#print(wav_file_names) | ||
|
||
# wav_file="/data/digbose92/ads_complete_repo/ads_wav_files/jwt_ads_of_world_wav_files/2k_sports_never_say_never_1.wav" | ||
|
||
#define feature extractor and model | ||
feature_extractor = AutoFeatureExtractor.from_pretrained(model_option) | ||
#print(feature_extractor.max_length) | ||
model = ASTModel.from_pretrained(model_option) | ||
device=torch.device("cuda:0") | ||
model.to(device) | ||
sampling_rate=16000 | ||
file_list_failure=[] | ||
|
||
for wav_file in tqdm(wav_file_names): | ||
try: | ||
waveform, sampling_rate = torchaudio.load(wav_file) #read the audio using torchaudio | ||
#print(wav_file) | ||
waveform=waveform[0].cpu().numpy() | ||
inputs=feature_extractor(waveform, sampling_rate=sampling_rate, return_tensors="pt") #extract features using transformers | ||
inputs['input_values']=inputs['input_values'].to(device) | ||
#print(inputs.keys()) | ||
with torch.no_grad(): | ||
outputs=model(**inputs) | ||
|
||
last_hidden_state=outputs.last_hidden_state | ||
pooler_output=outputs.pooler_output | ||
|
||
#create dictionary to save | ||
save_dict={'last_hidden_state':last_hidden_state.cpu().numpy(),'pooler_output':pooler_output.cpu().numpy()} | ||
|
||
file_name_id=os.path.splitext(wav_file.split("/")[-1])[0]+".pkl" | ||
destination_filename=os.path.join(save_folder,file_name_id) | ||
|
||
with open(destination_filename, 'wb') as f: | ||
pickle.dump(save_dict, f) | ||
#dict_filename=os.path.join(save_folder,wav_file.split("/")[-1]+".npy") | ||
except: | ||
file_list_failure.append(wav_file) | ||
pass | ||
|
||
|
||
|
||
#create the save file name | ||
|
||
#save_file_name=wav_file.split("/")[-1].split(".")[0]+".npy" | ||
|
||
|
||
|
||
#the sequence length is 1214 | ||
#because the spectrogram is 128*1024 which is broken down as follows: (128-16)//10+1=12 and (1024-16)//10+1=101 and 101*12=1212 | ||
#adding two more tokens will make it 1214 which is two CLS tokens | ||
#print(last_hidden_state.shape) | ||
|
||
|
||
# waveform, sampling_rate = torchaudio.load(wav_file) | ||
# waveform=waveform[0].cpu().numpy() | ||
# #model option and model | ||
# feature_extractor = AutoFeatureExtractor.from_pretrained(model_option) | ||
# model = ASTModel.from_pretrained(model_option) | ||
# sampling_rate=16000 | ||
|
||
|
||
|
||
|
||
|
||
# # # #generate the datasets | ||
# # dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation") | ||
# # dataset = dataset.sort("id") | ||
# # print(type(dataset[0]["audio"]["array"])) | ||
# # #read the audio file | ||
# inputs = feature_extractor(waveform, sampling_rate=sampling_rate, return_tensors="pt") | ||
|
||
|
||
# #inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") | ||
|
||
# #generate outputs | ||
# with torch.no_grad(): | ||
# outputs=model(**inputs) | ||
|
||
# print(outputs.keys()) | ||
# # #last hidden state | ||
# last_hidden_state=outputs.last_hidden_state | ||
# print(last_hidden_state.shape) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import torchaudio.compliance.kaldi as ta_kaldi | ||
import torchaudio | ||
import torch | ||
|
||
file="/data/digbose92/ads_complete_repo/ads_wav_files/jwt_ads_of_world_wav_files/459528.wav" | ||
waveform,sampling_rate=torchaudio.load(file) | ||
waveform=waveform[0].cpu().numpy() | ||
|
||
waveform = torch.from_numpy(waveform).unsqueeze(0) | ||
num_mel_bins=128 | ||
max_length=1024 | ||
fbank = ta_kaldi.fbank( | ||
waveform, | ||
htk_compat=True, | ||
sample_frequency=sampling_rate, | ||
use_energy=False, | ||
window_type="hanning", | ||
num_mel_bins=num_mel_bins, | ||
dither=0.0, | ||
frame_shift=10 | ||
) | ||
|
||
n_frames = fbank.shape[0] | ||
difference = max_length - n_frames | ||
|
||
# pad or truncate, depending on difference | ||
if difference > 0: | ||
pad_module = torch.nn.ZeroPad2d((0, 0, 0, difference)) | ||
fbank = pad_module(fbank) | ||
elif difference < 0: | ||
fbank = fbank[0:max_length, :] | ||
fbank = fbank.numpy() | ||
|
||
print(fbank.shape) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ Pillow | |
ipython | ||
jupyter | ||
bertopic | ||
importlib-resources | ||
importlib-resources | ||
transformers |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.