<jupyter_start><jupyter_text>Train Model This colab demonstrates how to extract the AudioSet embeddings, using a VGGish deep neural network (DNN). Based on the directions at: https://github.com/tensorflow/models/tree/master/research/audioset<jupyter_code>!lscpu
!nvidia-smi
#Google drive access
import os
from google.colab import drive
drive.mount('/content/gdrive',force_remount = True)

#Directory
root_path = 'gdrive/My Drive/SoundEventDetection/modelTraining'
os.chdir(root_path)
%%capture
#Install necessary software
!pip install six
!pip install h5py
!pip install pydub
!pip install numpy
!pip install scipy
!pip install keras
!pip install future
!pip install resampy
!pip install ipython
!pip install soundfile
!pip install pysoundfile
!pip install scikit-learn
!apt-get install libsndfile1

!pip install python==3.6
!pip install matplotlib
!pip install cudnn==7.1.2
!pip install cudatoolkit==9
!pip install tensorflow-gpu==1.12.0

#Install: cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
!apt-get update
!apt-get install cuda=9.0.176-1
#OR
#!wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb

#VGGish model checkpoint, in TensorFlow checkpoint format.
import os 
os.chdir("trained_models")
!wget https://storage.googleapis.com/audioset/vggish_model.ckpt
os.chdir("..")
%%capture
#Important DO NOT DELETE
#import six
import sys
#import h5py
#import math
#import glob
#import h5py
#import time
import numpy as np
import pandas as pd
import soundfile as sf
import tensorflow as tf
import matplotlib.pyplot as plt
#from pydub.playback import play
#from pydub import AudioSegment
#from scipy.io import wavfile
#from scipy.io.wavfile import write

sys.path.insert(1, os.path.join(sys.path[0], '../'))

#ML imports
#from keras.layers import Input, Dense, BatchNormalization, Dropout, Activation, Concatenate
from keras.layers import Lambda
#from keras.optimizers import Adam
from keras.models import load_model
from keras.models import Model
import keras.backend as K

#External .py scripts
from lib import mel_features
from lib import vggish_input
from lib import vggish_params
from lib import vggish_postprocess
from lib import vggish_slim
from lib import utilities
from lib import data_generator
from lib.train_functions import evaluateCore, trainCore, average_pooling, max_pooling, attention_pooling, pooling_shape, train, writeToFile
#Download dataset
!wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=0B49XSFgf-0yVQk01eG92RHg4WTA' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=0B49XSFgf-0yVQk01eG92RHg4WTA" -O packed_features.zip && rm -rf /tmp/cookies.txt
!unzip packed_features.zip
#Set args
args = {
  "data_dir" : "packed_features/",
  "workspace" : "workspace/",
  "mini_data" : False,
  "balance_type" : "balance_in_batch", #'no_balance', 'balance_in_batch'
  "model_type" : 'decision_level_single_attention', #'decision_level_max_pooling', 'decision_level_average_pooling', 'decision_level_single_attention', 'decision_level_multi_attention', 'feature_level_attention'
  "learning_rate" : 1e-3,
}

args["filename"] = utilities.get_filename("work/")

#Logs
logs_dir = os.path.join(args["workspace"], 'logs', args["filename"])
utilities.create_folder(logs_dir)
logging = utilities.create_logging(logs_dir, filemode='w')
#Train Model
def trainModel():
  if True:
      train(args, 0)
  else:
      args["bgn_iteration"] = 10000
      args["fin_iteration"] = 50001
      args["interval_iteration"] = 5000
      utilities.get_avg_stats(args)

trainModel()<jupyter_output>Model: "model_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            (None, 10, 128)      0                                            
__________________________________________________________________________________________________
dense_11 (Dense)                (None, 10, 1024)     132096      input_3[0][0]                    
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 10, 1024)     4096        dense_11[0][0]                   
__________________________________________________________________________________________________
activation_7 (Activation)       (None, 10, 1024)     0           batch_normalization_7[0][0][...]
{'fraction_non_alphanumeric': 0.12209660842754368, 'fraction_numerical': 0.033093525179856115, 'mean_word_length': 4.827544910179641, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '510659', 'license': 'Apache-2.0', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1510, 'n_words': 366, 'path': 'modelTraining/0_Train_Model.ipynb', 'repo_name': 'PatrickLewandowski/SoundEventDetection', 'size': '4865'}