Skip to content
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

[WIP] NNで変換 #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Networks/nn_mcep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

W = tf.Variable(tf.random_normal([hidden_size, output_size], stddev=0.1), name='W4')
b = tf.Variable(tf.zeros([output_size]), name='b4')
Converted = tf.add(tf.add(tf.matmul(h3, W), b), X, name='Converted')
Converted = tf.add(tf.matmul(h3, W), b, name='Converted')

loss = tf.nn.l2_loss(tf.subtract(Converted, Y), name='Loss')
loss = tf.reduce_mean(tf.square(Y - Converted), name='Loss')
optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss, name='Optimizer')

init = tf.global_variables_initializer()
Expand Down
41 changes: 41 additions & 0 deletions Networks/nn_mcep_res.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tensorflow as tf


tf.set_random_seed(1)

# Constant
input_size = 40 # Passed feature size
output_size = 40 # Number of outputs
hidden_size = 500

# Parameters
lr = tf.placeholder(tf.float32, name='learning_rate')

# Todo: Batch input...

# Graph Input
X = tf.placeholder(tf.float32, [None, input_size], name='X')
# Graph Label
Y = tf.placeholder(tf.float32, [None, output_size], name='Y')

h1 = tf.layers.dense(inputs=X, units=hidden_size, activation=tf.nn.leaky_relu)
h2 = tf.layers.dense(inputs=h1, units=hidden_size, activation=tf.nn.leaky_relu)
h3 = tf.layers.dense(inputs=h2, units=hidden_size, activation=tf.nn.leaky_relu)

W = tf.Variable(tf.random_normal([hidden_size, output_size], stddev=0.1), name='W4')
b = tf.Variable(tf.zeros([output_size]), name='b4')
Converted = tf.add(tf.add(tf.matmul(h3, W), b), X, name='Converted')

loss = tf.reduce_mean(tf.square(Y - Converted), name='Loss')
optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss, name='Optimizer')

init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
tf.saved_model.simple_save(
sess,
'../Models/McepNN_Res/model',
inputs={'X': X, 'Y': Y},
outputs={'Converted': Converted}
)
2 changes: 1 addition & 1 deletion VoiceConversionStarter.Common/Util/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void SaveAsNPY(Array arr, string path)
{
var dirSeparate = path.Split(new[] { System.IO.Path.DirectorySeparatorChar });
var dir = string.Join(System.IO.Path.DirectorySeparatorChar.ToString(), dirSeparate.Take(dirSeparate.Length - 1));
System.IO.Directory.CreateDirectory(dir);
if (dir != "") CreateDirectory(dir);
np.Save(arr, path);
}

Expand Down
38 changes: 38 additions & 0 deletions VoiceConversionStarter.Common/Util/Matrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Linq;
using NumSharp;

namespace VoiceConversionStarter.Common.Util
{
public static class Matrix
{
public static T[,] To2DimArray<T>(T[][] arr)
{
return np.array(arr).ToMuliDimArray<T>() as T[,];
}

public static T[][] To2JaggedArray<T>(T[,] arr)
{
return np.array(arr).ToJaggedArray<T>() as T[][];
}

public static T[] Add<T>(T[] first, T[] second)
{
return (np.array(first) + np.array(second)).Cast<T>().ToArray();
}

public static T[] Sub<T>(T[] first, T[] second)
{
return (np.array(first) - np.array(second)).Cast<T>().ToArray();
}

public static T[] Mul<T>(T[] first, T[] second)
{
return (np.array(first) * np.array(second)).Cast<T>().ToArray();
}

public static T[] Div<T>(T[] first, T[] second)
{
return (np.array(first) / np.array(second)).Cast<T>().ToArray();
}
}
}
55 changes: 54 additions & 1 deletion VoiceConversionStarter.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ class TrainMcapOptions
public int Epoch { get; set; }
}

[Verb("eval-mcep", HelpText = "eval mcep-model")]
class EvalMcapOptions
{
[Option("model", Required = true, HelpText = "trained model.")]
public string Model { get; set; }

[Option("source-dir", Required = true, HelpText = "source statistics dir.")]
public string SourceParam { get; set; }

[Option("target-dir", Required = true, HelpText = "target statistics dir.")]
public string TargetParam { get; set; }

[Option('i', "input", Required = true, HelpText = "input npy.")]
public string Input { get; set; }

[Option('o', "output", Default = "out.npy", HelpText = "converted npy.")]
public string Output { get; set; }
}

static int Train(TrainMcapOptions opts)
{
System.Console.WriteLine("Run Train");
Expand Down Expand Up @@ -110,11 +129,45 @@ static int Train(TrainMcapOptions opts)
return 0;
}

static int Eval(EvalMcapOptions opts)
{
var mlContext = new MLContext(seed: 555);

var sep = System.IO.Path.DirectorySeparatorChar;

var sourceMean = Common.Util.IO.LoadNPY<float[]>($"{opts.SourceParam}{sep}Means.npy");
var sourceVar = Common.Util.IO.LoadNPY<float[]>($"{opts.SourceParam}{sep}Vars.npy");

var targetMean = Common.Util.IO.LoadNPY<float[]>($"{opts.TargetParam}{sep}Means.npy");
var targetVar = Common.Util.IO.LoadNPY<float[]>($"{opts.TargetParam}{sep}Vars.npy");

var tfModel = mlContext.Model.Load(opts.Model, out var inputSchema);

var schema = SchemaDefinition.Create(typeof(SourceFrame));
var schemaInputCol = inputSchema.Single((v) => v.Name == nameof(SourceFrame.X));
schema[schemaInputCol.Name].ColumnType = schemaInputCol.Type;

var predictor = mlContext.Model.CreatePredictionEngine<SourceFrame, TargetFrame>(tfModel, inputSchemaDefinition: schema);

var data = Common.Util.IO.LoadNPY<float[,]>(opts.Input);
var sourceFrames = Common.Util.Matrix.To2JaggedArray(data).Select(v => new SourceFrame { X = v });

var converted = sourceFrames
.Select(v => Common.Util.Matrix.Mul(Common.Util.Matrix.Sub(v.X, sourceMean), sourceVar))
.Select(v => predictor.Predict(new SourceFrame { X = v }))
.Select(v => Common.Util.Matrix.Div(Common.Util.Matrix.Add(v.Converted, targetMean), targetVar));

Common.Util.IO.SaveAsNPY(Common.Util.Matrix.To2DimArray(converted.ToArray()), opts.Output);

return 0;
}

static int Main(string[] args)
{
return CommandLine.Parser.Default.ParseArguments<TrainMcapOptions>(args)
return CommandLine.Parser.Default.ParseArguments<TrainMcapOptions, EvalMcapOptions>(args)
.MapResult(
(TrainMcapOptions opts) => Train(opts),
(EvalMcapOptions opts) => Eval(opts),
errs => 1
);
}
Expand Down