Skip to content

Commit

Permalink
Merge pull request #24 from marl/verbosedocstrings
Browse files Browse the repository at this point in the history
Verbose option in CLI + docstrings
  • Loading branch information
justinsalamon authored Jul 13, 2018
2 parents 1a43629 + cba63af commit c3d18d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
19 changes: 14 additions & 5 deletions crepe/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def run(filename, output=None, model_capacity='full', viterbi=False,
save_activation=False, save_plot=False, plot_voicing=False,
no_centering=False, step_size=10):
no_centering=False, step_size=10, verbose=True):
"""
Collect the WAV files to process and run the model
Expand Down Expand Up @@ -44,6 +44,8 @@ def run(filename, output=None, model_capacity='full', viterbi=False,
tools and is generally not recommended.
step_size : int
The step size in milliseconds for running pitch estimation.
verbose : bool
Print status messages and keras progress (default=True).
"""

files = []
Expand All @@ -69,16 +71,18 @@ def run(filename, output=None, model_capacity='full', viterbi=False,
sys.exit(-1)

for i, file in enumerate(files):
print('CREPE: Processing {} ... ({}/{})'.format(file, i+1, len(files)),
file=sys.stderr)
if verbose:
print('CREPE: Processing {} ... ({}/{})'.format(
file, i+1, len(files)), file=sys.stderr)
process_file(file, output=output,
model_capacity=model_capacity,
viterbi=viterbi,
center=(not no_centering),
save_activation=save_activation,
save_plot=save_plot,
plot_voicing=plot_voicing,
step_size=step_size)
step_size=step_size,
verbose=verbose)


def positive_int(value):
Expand Down Expand Up @@ -150,6 +154,10 @@ def main():
parser.add_argument('--step-size', '-s', default=10, type=positive_int,
help='The step size in milliseconds for running '
'pitch estimation. The default is 10 ms.')
parser.add_argument('--quiet', '-q', default=False,
action='store_true',
help='Suppress all non-error printouts (e.g. progress '
'bar).')

args = parser.parse_args()

Expand All @@ -161,4 +169,5 @@ def main():
save_plot=args.save_plot,
plot_voicing=args.plot_voicing,
no_centering=args.no_centering,
step_size=args.step_size)
step_size=args.step_size,
verbose=not args.quiet)
44 changes: 30 additions & 14 deletions crepe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def build_and_load_model(model_capacity):
Returns
-------
The keras model loaded in memory
model : keras.models.Model
The pre-trained keras model loaded in memory
"""
from keras.layers import Input, Reshape, Conv2D, BatchNormalization
from keras.layers import MaxPool2D, Dropout, Permute, Flatten, Dense
Expand Down Expand Up @@ -151,7 +152,8 @@ def to_viterbi_cents(salience):
range(len(observations))])


def get_activation(audio, sr, model_capacity='full', center=True, step_size=10, verbose=1):
def get_activation(audio, sr, model_capacity='full', center=True, step_size=10,
verbose=1):
"""
Parameters
Expand All @@ -170,6 +172,9 @@ def get_activation(audio, sr, model_capacity='full', center=True, step_size=10,
- If `False`, then `D[:, t]` begins at `audio[t * hop_length]`
step_size : int
The step size in milliseconds for running pitch estimation.
verbose : int
Set the keras verbosity mode: 1 (default) will print out a progress bar
during prediction, 0 will suppress all non-error printouts.
Returns
-------
Expand Down Expand Up @@ -229,6 +234,9 @@ def predict(audio, sr, model_capacity='full',
- If `False`, then `D[:, t]` begins at `audio[t * hop_length]`
step_size : int
The step size in milliseconds for running pitch estimation.
verbose : int
Set the keras verbosity mode: 1 (default) will print out a progress bar
during prediction, 0 will suppress all non-error printouts.
Returns
-------
Expand All @@ -244,7 +252,8 @@ def predict(audio, sr, model_capacity='full',
The raw activation matrix
"""
activation = get_activation(audio, sr, model_capacity=model_capacity,
center=center, step_size=step_size, verbose=verbose)
center=center, step_size=step_size,
verbose=verbose)
confidence = activation.max(axis=1)

if viterbi:
Expand All @@ -262,7 +271,7 @@ def predict(audio, sr, model_capacity='full',

def process_file(file, output=None, model_capacity='full', viterbi=False,
center=True, save_activation=False, save_plot=False,
plot_voicing=False, step_size=10):
plot_voicing=False, step_size=10, verbose=True):
"""
Use the input model to perform pitch estimation on the input file.
Expand Down Expand Up @@ -293,6 +302,8 @@ def process_file(file, output=None, model_capacity='full', viterbi=False,
relevant if save_plot is True.
step_size : int
The step size in milliseconds for running pitch estimation.
verbose : bool
Print status messages and keras progress (default=True).
Returns
-------
Expand All @@ -304,26 +315,30 @@ def process_file(file, output=None, model_capacity='full', viterbi=False,
print("CREPE: Could not read %s" % file, file=sys.stderr)
raise

time, frequency, confidence, activation = predict(audio, sr,
model_capacity=model_capacity,
viterbi=viterbi,
center=center,
step_size=step_size)
time, frequency, confidence, activation = predict(
audio, sr,
model_capacity=model_capacity,
viterbi=viterbi,
center=center,
step_size=step_size,
verbose=1 * verbose)

# write prediction as TSV
f0_file = output_path(file, ".f0.csv", output)
f0_data = np.vstack([time, frequency, confidence]).transpose()
np.savetxt(f0_file, f0_data, fmt=['%.3f', '%.3f', '%.6f'], delimiter=',',
header='time,frequency,confidence', comments='')
print("CREPE: Saved the estimated frequencies and confidence values at "
"{}".format(f0_file))
if verbose:
print("CREPE: Saved the estimated frequencies and confidence values "
"at {}".format(f0_file))

# save the salience file to a .npy file
if save_activation:
activation_path = output_path(file, ".activation.npy", output)
np.save(activation_path, activation)
print("CREPE: Saved the activation matrix at {}".format(
activation_path))
if verbose:
print("CREPE: Saved the activation matrix at {}".format(
activation_path))

# save the salience visualization in a PNG file
if save_plot:
Expand All @@ -345,5 +360,6 @@ def process_file(file, output=None, model_capacity='full', viterbi=False,
inferno((confidence > 0.5).astype(np.float))[np.newaxis, :, :])

imwrite(plot_file, (255 * image).astype(np.uint8))
print("CREPE: Saved the salience plot at {}".format(plot_file))
if verbose:
print("CREPE: Saved the salience plot at {}".format(plot_file))

2 changes: 1 addition & 1 deletion crepe/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.0.6'
version = '0.0.7'

0 comments on commit c3d18d3

Please sign in to comment.