C5 Week 3 A2 Exercise 4 Trigger word detection v2a

UNQ_C4

GRADED FUNCTION: create_training_example

{mentor edit: code removed}

UNIT TEST

def create_training_example_test(target):
np.random.seed(18)
x, y = target(backgrounds[0], activates, negatives, 1375)

assert type(x) == np.ndarray, "Wrong type for x"
assert type(y) == np.ndarray, "Wrong type for y"
assert tuple(x.shape) == (101, 5511), "Wrong shape for x"
assert tuple(y.shape) == (1, 1375), "Wrong shape for y"
assert np.all(x > 0), "All x values must be higher than 0"
assert np.all(y >= 0), "All y values must be higher or equal than 0"
assert np.all(y < 51), "All y values must be smaller than 51"
assert np.sum(y) % 50 == 0, "Sum of activate marks must be a multiple of 50"
assert np.isclose(np.linalg.norm(x), 39745552.52075), "Spectrogram is wrong. Check the parameters passed to the insert_audio_clip function"

print("\033[92m All tests passed!")

create_training_example_test(create_training_example)

AttributeError Traceback (most recent call last)
in
16 print("\033[92m All tests passed!")
17
—> 18 create_training_example_test(create_training_example)

in create_training_example_test(target)
2 def create_training_example_test(target):
3 np.random.seed(18)
----> 4 x, y = target(backgrounds[0], activates, negatives, 1375)
5
6 assert type(x) == np.ndarray, “Wrong type for x”

in create_training_example(background, activates, negatives, Ty)
36 for random_activate in random_activates: # @KEEP
37 # Insert the audio clip on the background
—> 38 background, segment_time = insert_audio_clip(background, random_activates, previous_segments)
39 # Retrieve segment_start and segment_end from segment_time
40 segment_start, segment_end = segment_time

in insert_audio_clip(background, audio_clip, previous_segments)
41 ### END CODE HERE ###
42 # Step 4: Superpose audio segment and background
—> 43 new_background = background.overlay(audio_clip, position = segment_time[0])
44 else:
45 #print(“Timeouted”)

/opt/conda/lib/python3.7/site-packages/pydub/audio_segment.py in overlay(self, seg, position, loop, times, gain_during_overlay)
1151 output = StringIO()
1152
→ 1153 seg1, seg2 = AudioSegment._sync(self, seg)
1154 sample_width = seg1.sample_width
1155 spawn = seg1._spawn

/opt/conda/lib/python3.7/site-packages/pydub/audio_segment.py in _sync(cls, *segs)
434 @classmethod
435 def _sync(cls, *segs):
→ 436 channels = max(seg.channels for seg in segs)
437 frame_rate = max(seg.frame_rate for seg in segs)
438 sample_width = max(seg.sample_width for seg in segs)

/opt/conda/lib/python3.7/site-packages/pydub/audio_segment.py in (.0)
434 @classmethod
435 def _sync(cls, *segs):
→ 436 channels = max(seg.channels for seg in segs)
437 frame_rate = max(seg.frame_rate for seg in segs)
438 sample_width = max(seg.sample_width for seg in segs)

AttributeError: ‘list’ object has no attribute ‘channels’

I am unable to understand this error. I have checked the insert_audio_clip() function too.

Your insert_audio_clip() function does not work correctly for the data used in the create_training_example_test().

UNQ_C2

GRADED FUNCTION: insert_audio_clip

{mentor edit: code removed}

UNIT TEST

def insert_audio_clip_test(target):
np.random.seed(5)
audio_clip, segment_time = target(backgrounds[0], activates[0], [(0, 4400)])
duration = segment_time[1] - segment_time[0]
assert segment_time[0] > 4400, “Error: The audio clip is overlaping with the first segment”
assert duration + 1 == len(activates[0]) , “The segment length must match the audio clip length”
assert audio_clip != backgrounds[0] , “The audio clip must be different than the pure background”

# Not possible to insert clip into background
audio_clip, segment_time = target(backgrounds[0], activates[0], [(0, 9999)])
assert segment_time[0] == 10000 and segment_time[1] == 10000, "Segment must match the out by max-retry mark"
assert audio_clip == backgrounds[0], "output audio clip must be exactly the same input background"

print("\033[92m All tests passed!")

insert_audio_clip_test(insert_audio_clip)

(2254, 3169)
(4079, 4994)
(3046, 3961)
(7286, 8201)
(1032, 1947)
(740, 1655)
(1982, 2897)
(2459, 3374)
(2672, 3587)
(2718, 3633)
All tests passed!

I have passed all the tests

@TMosh, getting same issue …could you be little more specific about what could be possibly wrong with the insert_audio_clip(). Thanks in advance

I recommend you draw a little diagram of the timeline for the audio clips used in the test case. Then see if your code can handle it.

1 Like

Thanks @TMosh , I was able to trace my error…
I was passing random_activates instead of random_activate in the insert_audio_clip().
Now I am able to pass the unit test but in the grader it is throwing
“cell_UNQ_C4 function create_training_example failed test 1”
Could you please guide.
Thanks in advance

It appears there is an error in your create_training_example() function.

1 Like