Skip to content

Commit

Permalink
included prediction speed experiment in the main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmpercussion committed Jun 11, 2024
1 parent 686c84d commit 21dba2c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@ datasets
*.hdf5
*.no
*.npz
*.csv
3 changes: 2 additions & 1 deletion impsy/impsy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .dataset import dataset
from .train import train
from .interaction import run
from .tests import test_mdrnn
from .tests import test_mdrnn, prediction_speed_test

@click.group()
def cli():
Expand All @@ -18,5 +18,6 @@ def main():
cli.add_command(train)
cli.add_command(run)
cli.add_command(test_mdrnn)
cli.add_command(prediction_speed_test)
# runs the command line interface
cli()
69 changes: 68 additions & 1 deletion impsy/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import click
import time
from .utils import mdrnn_config
import pandas as pd


pd.set_option('display.float_format', lambda x: '%.2f' % x)


@click.command(name="test-mdrnn")
Expand All @@ -25,7 +29,6 @@ def build_network(sess, dimension, units, mixes, layers):
n_hidden_units=units,
n_mixtures=mixes,
layers=layers)
print("MDRNN Loaded.")
return net

start_build = time.time()
Expand All @@ -34,3 +37,67 @@ def build_network(sess, dimension, units, mixes, layers):
sess = tf.Session()
build_network(sess, 4, model_config["units"], model_config["mixes"], model_config["layers"])
print("Done. That took", time.time() - start_build, "seconds.")


@click.command(name="test-speed")
def prediction_speed_test():
"""This command runs a speed test experiment with different sized MDRNN models. The output is written to a CSV file."""
start_import = time.time()
import impsy.mdrnn as mdrnn
import tensorflow.compat.v1 as tf
print("Done. That took", time.time() - start_import, "seconds.")

def build_network(sess, compute_graph, net_config):
"""Build the MDRNN."""
mdrnn.MODEL_DIR = "./models/"
tf.keras.backend.set_session(sess)
with compute_graph.as_default():
net = mdrnn.PredictiveMusicMDRNN(mode=mdrnn.NET_MODE_RUN,
dimension=net_config['dimension'],
n_hidden_units=net_config['units'],
n_mixtures=net_config['mixes'],
layers=net_config['layers'])
return net

def request_rnn_prediction(input_value, net):
""" Accesses a single prediction from the RNN. """
start = time.time()
output_value = net.generate_touch(input_value)
time_delta = time.time() - start
return output_value, time_delta

def run_test(tests, config):
times = []
compute_graph = tf.Graph()
with compute_graph.as_default():
sess = tf.Session()
net = build_network(sess, compute_graph, config)
for i in range(tests):
## Predictions.
item = mdrnn.random_sample(out_dim=config['dimension'])
tf.keras.backend.set_session(sess)
with compute_graph.as_default():
rnn_output, t = request_rnn_prediction(item, net)
out_dict = {
'time': t,
'mixes': config['mixes'],
'layers': config['layers'],
'units': config['units'],
'dimension': config['dimension']}
times.append(out_dict)
# clean up
tf.keras.backend.clear_session()
sess.close()
return times

experiments = []
mdrnn_units = [64, 128, 256, 512]
dimensions = [2, 3, 4, 5, 6, 7, 8, 9]
for un in mdrnn_units:
for dim in dimensions:
net_config = {"mixes": 5, "layers": 2, "units": un, "dimension": dim}
times = run_test(100, net_config)
experiments.extend(times)
total_experiment = pd.DataFrame.from_records(experiments)
total_experiment.to_csv("total_exp.csv")
print(total_experiment.describe())
84 changes: 0 additions & 84 deletions utils/test_prediction_speed.py

This file was deleted.

0 comments on commit 21dba2c

Please sign in to comment.