CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C5 - Sequence Models/Week 3/Trigger word detection/generateTestCases.py
Views: 4819
1
# New Generate Test Cases
2
import numpy as np
3
import math
4
import os,sys
5
sys.path.append('../')
6
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
7
8
from td_utils import *
9
import warnings
10
warnings.filterwarnings("ignore")
11
12
from pydub import AudioSegment
13
14
class suppress_stdout_stderr(object):
15
'''
16
A context manager for doing a "deep suppression" of stdout and stderr in
17
Python, i.e. will suppress all print, even if the print originates in a
18
compiled C/Fortran sub-function.
19
This will not suppress raised exceptions, since exceptions are printed
20
to stderr just before a script exits, and after the context manager has
21
exited (at least, I think that is why it lets exceptions through).
22
23
'''
24
def __init__(self):
25
# Open a pair of null files
26
self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
27
# Save the actual stdout (1) and stderr (2) file descriptors.
28
self.save_fds = [os.dup(1), os.dup(2)]
29
30
def __enter__(self):
31
# Assign the null pointers to stdout and stderr.
32
os.dup2(self.null_fds[0],1)
33
os.dup2(self.null_fds[1],2)
34
35
def __exit__(self, *_):
36
# Re-assign the real stdout/stderr back to (1) and (2)
37
os.dup2(self.save_fds[0],1)
38
os.dup2(self.save_fds[1],2)
39
# Close all file descriptors
40
for fd in self.null_fds + self.save_fds:
41
os.close(fd)
42
43
with suppress_stdout_stderr():
44
from solutions import *
45
46
# import copy
47
# from keras.callbacks import History
48
# import tensorflow as tf
49
sys.path.append('../../')
50
51
from grader_support import stdout_redirector
52
from grader_support import util
53
54
55
56
57
# This grader is for the Emojify assignment
58
59
mFiles = [
60
"is_overlapping.py",
61
"insert_audio_clip.py",
62
"insert_ones.py",
63
"create_training_example.py",
64
"model.py"
65
]
66
67
np.random.seed(3)
68
69
# generating the testCases for is_overlapping
70
overlap1 = is_overlapping((900, 2500), [(2000, 2550), (260, 949)])
71
overlap2 = is_overlapping((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)])
72
73
# generating the test cases for the insert_audio_clip
74
# (2732, 3452), (4859, 5579)
75
a = AudioSegment.from_wav("activate.wav")
76
b = AudioSegment.from_wav("background.wav")
77
audio_clip, segment_time = insert_audio_clip(b, a, [(3790, 4400)])
78
audio_clip.export('test.wav', format = 'wav')
79
80
inserted = graph_spectrogram('test.wav')
81
82
# generate the testCases for insert_ones
83
arr1 = insert_ones(np.zeros((1, Ty)), 9)
84
85
# generate the test Cases for create_training_example
86
87
n = AudioSegment.from_wav("negative.wav")
88
89
A = []
90
N = []
91
A.append(a)
92
N.append(n)
93
94
with stdout_redirector.stdout_redirected():
95
a, s = create_training_example(b, A, N)
96
97
98
# generating the test cases for the model
99
with suppress_stdout_stderr():
100
model = model(input_shape = (Tx, n_freq))
101
ml = len(model.layers)
102
cp = model.count_params()
103
mi = len(model.inputs)
104
mo = len(model.outputs)
105
106
def generateTestCases():
107
testCases = {
108
'is_overlapping': {
109
'partId': 'S8DvY',
110
'testCases': [
111
{
112
'testInput': ((900, 2500), [(2000, 2550), (260, 949)]),
113
'testOutput': overlap1
114
},
115
{
116
'testInput': ((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)]),
117
'testOutput': overlap2
118
}
119
]
120
},
121
'insert_audio_clip': {
122
'partId': 'BSIWi',
123
'testCases': [
124
{
125
'testInput': ("activate.wav", "background.wav"),
126
'testOutput': inserted
127
}
128
]
129
},
130
'insert_ones': {
131
'partId': '2Kdnr',
132
'testCases': [
133
{
134
'testInput': (np.zeros((1, Ty)), 9) ,
135
'testOutput': arr1
136
}
137
]
138
},
139
'create_training_example': {
140
'partId': 'v097u',
141
'testCases': [
142
{
143
'testInput': (b, A, N),
144
'testOutput': (a,s)
145
}
146
]
147
},
148
'model': {
149
'partId': '0Txcd',
150
'testCases': [
151
{
152
'testInput': (Tx, n_freq),
153
'testOutput': np.asarray([cp, ml, mi, mo])
154
}
155
]
156
}
157
}
158
return testCases
159
160
161