diff --git a/src/TensorFlowNET.Core/APIs/tf.array.cs b/src/TensorFlowNET.Core/APIs/tf.array.cs index 390942d2..5b0cf5b5 100644 --- a/src/TensorFlowNET.Core/APIs/tf.array.cs +++ b/src/TensorFlowNET.Core/APIs/tf.array.cs @@ -188,6 +188,9 @@ namespace Tensorflow public Tensor slice(Tensor input, Tb[] begin, Ts[] size, string name = null) => array_ops.slice(input, begin, size, name: name); + public Tensor squeeze(Tensor input, int axis, string name = null, int squeeze_dims = -1) + => array_ops.squeeze(input, new[] { axis }, name); + public Tensor squeeze(Tensor input, int[] axis = null, string name = null, int squeeze_dims = -1) => array_ops.squeeze(input, axis, name); diff --git a/src/TensorFlowNET.Core/APIs/tf.audio.cs b/src/TensorFlowNET.Core/APIs/tf.audio.cs new file mode 100644 index 00000000..573b11ec --- /dev/null +++ b/src/TensorFlowNET.Core/APIs/tf.audio.cs @@ -0,0 +1,37 @@ +/***************************************************************************** + Copyright 2021 Haiping Chen. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System.Collections.Generic; +using Tensorflow.IO; + +namespace Tensorflow +{ + public partial class tensorflow + { + public AudioAPI audio { get; } = new AudioAPI(); + + public class AudioAPI + { + audio_ops audio_ops = new audio_ops(); + + public Tensors decode_wav(Tensor contents, int desired_channels = -1, int desired_samples = -1, string name = null) + => audio_ops.decode_wav(contents, + desired_channels: desired_channels, + desired_samples: desired_samples, + name: name); + } + } +} diff --git a/src/TensorFlowNET.Core/APIs/tf.data.cs b/src/TensorFlowNET.Core/APIs/tf.data.cs index 7fe7fcf9..b4a92ce5 100644 --- a/src/TensorFlowNET.Core/APIs/tf.data.cs +++ b/src/TensorFlowNET.Core/APIs/tf.data.cs @@ -22,6 +22,7 @@ namespace Tensorflow public class DataOps { + public int AUTOTUNE = -1; public DatasetManager Dataset { get; } = new DatasetManager(); } } diff --git a/src/TensorFlowNET.Core/APIs/tf.io.cs b/src/TensorFlowNET.Core/APIs/tf.io.cs index 25d9cfe8..0c0510dd 100644 --- a/src/TensorFlowNET.Core/APIs/tf.io.cs +++ b/src/TensorFlowNET.Core/APIs/tf.io.cs @@ -26,9 +26,11 @@ namespace Tensorflow public class IoApi { io_ops ops; + public GFile gfile; public IoApi() { ops = new io_ops(); + gfile = new GFile(); } public Tensor read_file(string filename, string name = null) diff --git a/src/TensorFlowNET.Core/APIs/tf.strings.cs b/src/TensorFlowNET.Core/APIs/tf.strings.cs index 52cd96b4..ecaf775d 100644 --- a/src/TensorFlowNET.Core/APIs/tf.strings.cs +++ b/src/TensorFlowNET.Core/APIs/tf.strings.cs @@ -80,8 +80,8 @@ namespace Tensorflow public Tensor format(string template, Tensor[] inputs, string placeholder = "{}", int summarize = 3, string name = null) => ops.string_format(inputs, template: template, placeholder: placeholder, summarize: summarize, name: name); - public RaggedTensor split(Tensor input, string sep = "", int maxsplit = -1, string name = null) - => ops.string_split_v2(input, sep: sep, maxsplit : maxsplit, name : name); + public RaggedTensor split(Tensor input, char sep = ' ', int maxsplit = -1, string name = null) + => ops.string_split_v2(input, sep: sep.ToString(), maxsplit : maxsplit, name : name); public (RaggedTensor, RaggedTensor) unicode_decode_with_offsets(Tensor input, string input_encoding, string errors = "replace", int replacement_char = 0xFFFD, diff --git a/src/TensorFlowNET.Core/IO/gfile.cs b/src/TensorFlowNET.Core/IO/gfile.cs index a7303bf6..5f08702d 100644 --- a/src/TensorFlowNET.Core/IO/gfile.cs +++ b/src/TensorFlowNET.Core/IO/gfile.cs @@ -14,6 +14,7 @@ limitations under the License. ******************************************************************************/ +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -49,5 +50,18 @@ namespace Tensorflow.IO foreach (var f in walk_v2(dir, topdown)) yield return f; } + + public string[] listdir(string data_dir) + => Directory.GetDirectories(data_dir) + .Select(x => x.Split(Path.DirectorySeparatorChar).Last()) + .ToArray(); + + public string[] glob(string data_dir) + { + var dirs = new List(); + foreach(var dir in Directory.GetDirectories(data_dir)) + dirs.AddRange(Directory.GetFiles(dir)); + return dirs.ToArray(); + } } } diff --git a/src/TensorFlowNET.Core/Operations/audio_ops.cs b/src/TensorFlowNET.Core/Operations/audio_ops.cs new file mode 100644 index 00000000..4f1b5f64 --- /dev/null +++ b/src/TensorFlowNET.Core/Operations/audio_ops.cs @@ -0,0 +1,29 @@ +/***************************************************************************** + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +******************************************************************************/ + +using System.Collections.Generic; +using Tensorflow.Contexts; +using static Tensorflow.Binding; + +namespace Tensorflow +{ + public class audio_ops + { + public Tensors decode_wav(Tensor contents, int desired_channels = -1, int desired_samples = -1, string name = null) + => tf.Context.ExecuteOp("DecodeWav", name, new ExecuteOpArgs(contents) + .SetAttributes(new { desired_channels, desired_samples })); + } +} diff --git a/src/TensorFlowNET.Core/Operations/string_ops.cs b/src/TensorFlowNET.Core/Operations/string_ops.cs index 59e99960..0a4169b6 100644 --- a/src/TensorFlowNET.Core/Operations/string_ops.cs +++ b/src/TensorFlowNET.Core/Operations/string_ops.cs @@ -73,11 +73,19 @@ namespace Tensorflow } }.SetAttributes(new { template, placeholder, summarize })); - public RaggedTensor string_split_v2(Tensor input, string sep = "", int maxsplit = -1, string name = null) + public RaggedTensor string_split_v2(Tensor input, string sep = " ", int maxsplit = -1, string name = null) { return tf_with(ops.name_scope(name, "StringSplit"), scope => { var sep_tensor = ops.convert_to_tensor(sep, dtype: TF_DataType.TF_STRING); + if(input.rank == 0) + { + return string_split_v2(array_ops.stack(new[] { input }), + sep: sep, + maxsplit: maxsplit, + name: name)[0]; + } + var result = tf.Context.ExecuteOp("StringSplitV2", name, new ExecuteOpArgs(input, sep) { diff --git a/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs b/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs index 567014ab..aadba0c5 100644 --- a/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs @@ -134,7 +134,7 @@ namespace Tensorflow => new[] { _row_splits }; public override string ToString() - => $"tf.RaggedTensor: shape={shape} [{string.Join(", ", _values.StringData().Take(10))}]"; + => $"tf.RaggedTensor: shape={_values.TensorShape} [{string.Join(", ", _values.StringData().Take(10))}]"; public static implicit operator Tensor(RaggedTensor indexedSlices) => indexedSlices._to_variant(); diff --git a/src/TensorFlowNET.Core/Tensors/Tensors.cs b/src/TensorFlowNET.Core/Tensors/Tensors.cs index 3c334ea5..1f23fc44 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensors.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensors.cs @@ -92,6 +92,12 @@ namespace Tensorflow public static implicit operator Tensor[](Tensors tensors) => tensors.items.ToArray(); + public void Deconstruct(out Tensor a, out Tensor b) + { + a = items[0]; + b = items[1]; + } + public override string ToString() => items.Count() == 1 ? items.First().ToString()