Skip to content

Commit

Permalink
Add kmodel converter
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnycase committed Jan 10, 2019
1 parent e65ae8a commit 5ad8fc5
Show file tree
Hide file tree
Showing 21 changed files with 1,042 additions and 30 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ nncase
## NNCase Converter
A tool to convert models between many formats.
### Usage
`ncc -i <input format> -o <output format> [--dataset <dataset path>] <input path> <output path>`
`ncc -i <input format> -o <output format> [--dataset <dataset path>] [--postprocess <dataset postprocess>] <input path> <output path>`

Input formats can be one of `tflite` and `paddle`.

Output formats can be one of `tf`, `tflite` and `k210code`.
Output formats can be one of `tf`, `tflite`, `k210code` and `k210model`.

Dataset postprocess can be one of `0to1` (normalize images from 0 to 1) and `n1to1` (normalize images from -1 to 1).

### Examples
- Convert TFLite model to K210 code.

`ncc -i tflite -o k210code --dataset ./images ./mbnetv1.tflite ./mbnetv1.c`

- Convert TFLite model to K210 model.

`ncc -i tflite -o k210model --dataset ./images ./mbnetv1.tflite ./mbnetv1.kmodel`

- Convert PaddlePaddle model to TensorFlow model.

`ncc -i paddle -o tf ./MobileNetV1_pretrained ./mbnetv1.pb`
Expand Down
15 changes: 12 additions & 3 deletions src/NnCase.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Options
[Option("dataset", Required = false, HelpText = "Dataset path")]
public string Dataset { get; set; }

[Option("postprocess", Required = false, HelpText = "Dataset postprocess")]
public string Postprocess { get; set; }

[Value(0, MetaName = "input", HelpText = "Input path")]
public string Input { get; set; }

Expand Down Expand Up @@ -97,7 +100,8 @@ static async Task Main(string[] args)
throw new ArgumentException("input-format");
}

switch (options.OutputFormat.ToLowerInvariant())
var outputFormat = options.OutputFormat.ToLowerInvariant();
switch (outputFormat)
{
case "tf":
{
Expand All @@ -114,7 +118,12 @@ static async Task Main(string[] args)
break;
}
case "k210code":
case "k210model":
{
PostprocessMethods pm = PostprocessMethods.Normalize0To1;
if (options.Postprocess == "n1to1")
pm = PostprocessMethods.NormalizeMinus1To1;

if (options.InputFormat.ToLowerInvariant() != "tflite")
{
var tmpTflite = Path.GetTempFileName();
Expand Down Expand Up @@ -143,13 +152,13 @@ static async Task Main(string[] args)
var ctx = new GraphPlanContext();
graph.Plan(ctx);
var dim = graph.Inputs.First().Output.Dimensions.ToArray();
var k210c = new GraphToK210Converter(graph);
var k210c = new GraphToK210Converter(graph, outputFormat == "k210code" ? K210ConvertType.Code : K210ConvertType.KModel);
await k210c.ConvertAsync(new ImageDataset(
options.Dataset,
new[] { dim[1], dim[2], dim[3] },
1,
PreprocessMethods.None,
PostprocessMethods.Normalize0To1),
pm),
ctx,
Path.GetDirectoryName(options.Output),
Path.GetFileNameWithoutExtension(options.Output));
Expand Down
2 changes: 1 addition & 1 deletion src/NnCase.Converter/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task ExportK210Code(string modelPath, string datasetDir, str
var ctx = new GraphPlanContext();
graph.Plan(ctx);
var dim = graph.Inputs.First().Output.Dimensions.ToArray();
var k210c = new GraphToK210Converter(graph);
var k210c = new GraphToK210Converter(graph, K210ConvertType.Code);
await k210c.ConvertAsync(new ImageDataset(
datasetDir,
new[] { dim[1], dim[2], dim[3] },
Expand Down
Loading

0 comments on commit 5ad8fc5

Please sign in to comment.