-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prediction Output #131
Open
chadlagore
wants to merge
5
commits into
master
Choose a base branch
from
chad/126-prediction-output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−69
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c4883e9
Add prediction output
chadlagore a5e0e1e
Refactored conversation and base to work maybe more harmoniously
chadlagore 13b9a6b
Need my fixture
chadlagore efa19f0
Fix non-deterministic test; update README to talk about determinism
chadlagore e0a4cb3
Typo; move preprocessing params to Audio class
chadlagore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .minutes import Minutes # noqa | ||
from .speaker import Speaker # noqa | ||
from .conversation import Conversation # noqa | ||
from minutes.minutes import Minutes # noqa | ||
from minutes.speaker import Speaker # noqa | ||
from minutes.conversation import Phrase, Conversation # noqa |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import json | ||
import os | ||
import pickle | ||
|
||
from keras import backend as K | ||
from keras.models import Sequential, load_model | ||
|
@@ -24,6 +23,19 @@ class BaseModel: | |
'random_state', | ||
} | ||
|
||
@property | ||
def preprocessing_params(self): | ||
"""Returns a mapping of parameters that are required to do preprocessing | ||
of audio data suitable for this model. Useful as kwargs to audio | ||
manipulation classes. | ||
""" | ||
return { | ||
i: getattr(self, i) for i in self.intialization_params | ||
if i in { | ||
'ms_per_observation', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to declare this set as a constant somewhere and refer to it by name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course! |
||
} | ||
} | ||
|
||
@property | ||
def fitted(self): | ||
return self.model is not None | ||
|
@@ -63,7 +75,7 @@ def load_model(cls, name): | |
def __init__(self, name, ms_per_observation=3000, test_size=0.33, | ||
random_state=42): | ||
self.name = name | ||
self.speakers = set() | ||
self.speakers = [] | ||
self.test_size = test_size | ||
self.random_state = random_state | ||
self.ms_per_observation = ms_per_observation | ||
|
@@ -78,7 +90,7 @@ def add_speaker(self, speaker): | |
""" | ||
if speaker in self.speakers: | ||
raise LookupError(f'Speaker {speaker.name} already added.') | ||
self.speakers.add(speaker) | ||
self.speakers.append(speaker) | ||
|
||
def add_speakers(self, speakers): | ||
"""Add a collection of speakers to the model. | ||
|
@@ -98,8 +110,12 @@ def _generate_training_data(self): | |
y -- a categorical one-hot encoding of different speakers | ||
numbered 1..k. | ||
""" | ||
obs = [s.get_observations(self.ms_per_observation) | ||
for s in self.speakers] | ||
obs = [] | ||
for s in self.speakers: | ||
_, processed = s.get_observations(**self.preprocessing_params) | ||
obs += processed, | ||
|
||
# Generate and flatten labels. | ||
labels = [[i] * len(o) for i, o in enumerate(obs)] | ||
flattened_labels = [j for i in labels for j in i] | ||
|
||
|
@@ -153,3 +169,21 @@ def save_model(self): | |
# Save internal model. | ||
if self.model is not None: | ||
self.model.save(os.path.join(self.home, 'keras.h5')) | ||
|
||
def predict(self, observations): | ||
"""Predict against a table of audio observations. | ||
|
||
Arguments: | ||
observations {np.array} -- A table of processed audio observations. | ||
|
||
Returns: | ||
np.array -- An array of predicted speakers. | ||
""" | ||
result = self.model.predict(observations) | ||
y_hat_indices = np.argmax(result, axis=1) | ||
|
||
# Index into the speaker array using the predicted speaker indicies. | ||
return np.array(self.speakers)[y_hat_indices] | ||
|
||
def __str__(self): | ||
return self.name |
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 |
---|---|---|
@@ -1,30 +1,36 @@ | ||
from minutes.audio import Audio | ||
|
||
|
||
class Conversation: | ||
class Phrase: | ||
def __init__(self, observation, speaker): | ||
"""A phrase in a conversation, characterized by an audio segment | ||
and a speaker. | ||
|
||
def __init__(self, audio_loc, speakers): | ||
Arguments: | ||
observation {np.array} -- 1 dimensional audio sample. | ||
speaker {Speaker} -- The inferred speaker for the audio segment. | ||
""" | ||
self.observation = observation | ||
self.speaker = speaker | ||
|
||
|
||
class Conversation(Audio): | ||
|
||
def __init__(self, audio_loc, model): | ||
"""Create a new conversation from audio sample. | ||
|
||
Arguments: | ||
audio_loc {str} -- The absolute location of an audio conversation | ||
sample. | ||
speakers {List[Speaker]} -- A list of speakers included in this | ||
model {Minutes} -- A model trained on speakers within this | ||
conversation. | ||
""" | ||
self.speakers = speakers | ||
self.audio = Audio(audio_loc) | ||
|
||
def get_observations(self, ms_per_observation, verbose=False): | ||
"""Converts the conversation audio sample into an n x d matrix of | ||
observations. | ||
self.model = model | ||
super().__init__(audio_loc) | ||
|
||
Keyword Arguments: | ||
verbose {bool} -- (default: {False}) | ||
ms_per_observation {int} -- (default: {False}) | ||
|
||
Returns: | ||
np.array -- An n x d matrix of observations. | ||
""" | ||
return self.audio.get_spectrograms(ms_per_observation, verbose) | ||
# Predict against the conversation spectrograms. | ||
raw, X_hat = self.get_observations(**model.preprocessing_params) | ||
y_hat = model.predict(X_hat) | ||
|
||
# Convert to a list of phrases. | ||
self.phrases = [Phrase(o, speaker) for o, speaker in zip(raw, y_hat)] |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
s/maninpulation/manipulation/g