Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C5 - Sequence Models/Week 3/Trigger word detection/generateTestCases.py
Views: 4819
# New Generate Test Cases1import numpy as np2import math3import os,sys4sys.path.append('../')5os.environ['TF_CPP_MIN_LOG_LEVEL']='2'67from td_utils import *8import warnings9warnings.filterwarnings("ignore")1011from pydub import AudioSegment1213class suppress_stdout_stderr(object):14'''15A context manager for doing a "deep suppression" of stdout and stderr in16Python, i.e. will suppress all print, even if the print originates in a17compiled C/Fortran sub-function.18This will not suppress raised exceptions, since exceptions are printed19to stderr just before a script exits, and after the context manager has20exited (at least, I think that is why it lets exceptions through).2122'''23def __init__(self):24# Open a pair of null files25self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]26# Save the actual stdout (1) and stderr (2) file descriptors.27self.save_fds = [os.dup(1), os.dup(2)]2829def __enter__(self):30# Assign the null pointers to stdout and stderr.31os.dup2(self.null_fds[0],1)32os.dup2(self.null_fds[1],2)3334def __exit__(self, *_):35# Re-assign the real stdout/stderr back to (1) and (2)36os.dup2(self.save_fds[0],1)37os.dup2(self.save_fds[1],2)38# Close all file descriptors39for fd in self.null_fds + self.save_fds:40os.close(fd)4142with suppress_stdout_stderr():43from solutions import *4445# import copy46# from keras.callbacks import History47# import tensorflow as tf48sys.path.append('../../')4950from grader_support import stdout_redirector51from grader_support import util5253545556# This grader is for the Emojify assignment5758mFiles = [59"is_overlapping.py",60"insert_audio_clip.py",61"insert_ones.py",62"create_training_example.py",63"model.py"64]6566np.random.seed(3)6768# generating the testCases for is_overlapping69overlap1 = is_overlapping((900, 2500), [(2000, 2550), (260, 949)])70overlap2 = is_overlapping((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)])7172# generating the test cases for the insert_audio_clip73# (2732, 3452), (4859, 5579)74a = AudioSegment.from_wav("activate.wav")75b = AudioSegment.from_wav("background.wav")76audio_clip, segment_time = insert_audio_clip(b, a, [(3790, 4400)])77audio_clip.export('test.wav', format = 'wav')7879inserted = graph_spectrogram('test.wav')8081# generate the testCases for insert_ones82arr1 = insert_ones(np.zeros((1, Ty)), 9)8384# generate the test Cases for create_training_example8586n = AudioSegment.from_wav("negative.wav")8788A = []89N = []90A.append(a)91N.append(n)9293with stdout_redirector.stdout_redirected():94a, s = create_training_example(b, A, N)959697# generating the test cases for the model98with suppress_stdout_stderr():99model = model(input_shape = (Tx, n_freq))100ml = len(model.layers)101cp = model.count_params()102mi = len(model.inputs)103mo = len(model.outputs)104105def generateTestCases():106testCases = {107'is_overlapping': {108'partId': 'S8DvY',109'testCases': [110{111'testInput': ((900, 2500), [(2000, 2550), (260, 949)]),112'testOutput': overlap1113},114{115'testInput': ((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)]),116'testOutput': overlap2117}118]119},120'insert_audio_clip': {121'partId': 'BSIWi',122'testCases': [123{124'testInput': ("activate.wav", "background.wav"),125'testOutput': inserted126}127]128},129'insert_ones': {130'partId': '2Kdnr',131'testCases': [132{133'testInput': (np.zeros((1, Ty)), 9) ,134'testOutput': arr1135}136]137},138'create_training_example': {139'partId': 'v097u',140'testCases': [141{142'testInput': (b, A, N),143'testOutput': (a,s)144}145]146},147'model': {148'partId': '0Txcd',149'testCases': [150{151'testInput': (Tx, n_freq),152'testOutput': np.asarray([cp, ml, mi, mo])153}154]155}156}157return testCases158159160161