Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 899d15e

Browse files
committed
add audio.decode_wav
1 parent 773a3ba commit 899d15e

File tree

10 files changed

+104
-4
lines changed

10 files changed

+104
-4
lines changed

src/TensorFlowNET.Core/APIs/tf.array.cs

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ public Tensor rank(Tensor input, string name = null)
188188
public Tensor slice<Tb, Ts>(Tensor input, Tb[] begin, Ts[] size, string name = null)
189189
=> array_ops.slice(input, begin, size, name: name);
190190

191+
public Tensor squeeze(Tensor input, int axis, string name = null, int squeeze_dims = -1)
192+
=> array_ops.squeeze(input, new[] { axis }, name);
193+
191194
public Tensor squeeze(Tensor input, int[] axis = null, string name = null, int squeeze_dims = -1)
192195
=> array_ops.squeeze(input, axis, name);
193196

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*****************************************************************************
2+
Copyright 2021 Haiping Chen. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System.Collections.Generic;
18+
using Tensorflow.IO;
19+
20+
namespace Tensorflow
21+
{
22+
public partial class tensorflow
23+
{
24+
public AudioAPI audio { get; } = new AudioAPI();
25+
26+
public class AudioAPI
27+
{
28+
audio_ops audio_ops = new audio_ops();
29+
30+
public Tensors decode_wav(Tensor contents, int desired_channels = -1, int desired_samples = -1, string name = null)
31+
=> audio_ops.decode_wav(contents,
32+
desired_channels: desired_channels,
33+
desired_samples: desired_samples,
34+
name: name);
35+
}
36+
}
37+
}

src/TensorFlowNET.Core/APIs/tf.data.cs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public partial class tensorflow
2222

2323
public class DataOps
2424
{
25+
public int AUTOTUNE = -1;
2526
public DatasetManager Dataset { get; } = new DatasetManager();
2627
}
2728
}

src/TensorFlowNET.Core/APIs/tf.io.cs

+2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public partial class tensorflow
2626
public class IoApi
2727
{
2828
io_ops ops;
29+
public GFile gfile;
2930
public IoApi()
3031
{
3132
ops = new io_ops();
33+
gfile = new GFile();
3234
}
3335

3436
public Tensor read_file(string filename, string name = null)

src/TensorFlowNET.Core/APIs/tf.strings.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public Tensor string_length(Tensor input, string name = null, string unit = "BYT
8080
public Tensor format(string template, Tensor[] inputs, string placeholder = "{}", int summarize = 3, string name = null)
8181
=> ops.string_format(inputs, template: template, placeholder: placeholder, summarize: summarize, name: name);
8282

83-
public RaggedTensor split(Tensor input, string sep = "", int maxsplit = -1, string name = null)
84-
=> ops.string_split_v2(input, sep: sep, maxsplit : maxsplit, name : name);
83+
public RaggedTensor split(Tensor input, char sep = ' ', int maxsplit = -1, string name = null)
84+
=> ops.string_split_v2(input, sep: sep.ToString(), maxsplit : maxsplit, name : name);
8585

8686
public (RaggedTensor, RaggedTensor) unicode_decode_with_offsets(Tensor input, string input_encoding,
8787
string errors = "replace", int replacement_char = 0xFFFD,

src/TensorFlowNET.Core/IO/gfile.cs

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using System;
1718
using System.Collections.Generic;
1819
using System.IO;
1920
using System.Linq;
@@ -49,5 +50,18 @@ public class GFile
4950
foreach (var f in walk_v2(dir, topdown))
5051
yield return f;
5152
}
53+
54+
public string[] listdir(string data_dir)
55+
=> Directory.GetDirectories(data_dir)
56+
.Select(x => x.Split(Path.DirectorySeparatorChar).Last())
57+
.ToArray();
58+
59+
public string[] glob(string data_dir)
60+
{
61+
var dirs = new List<string>();
62+
foreach(var dir in Directory.GetDirectories(data_dir))
63+
dirs.AddRange(Directory.GetFiles(dir));
64+
return dirs.ToArray();
65+
}
5266
}
5367
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System.Collections.Generic;
18+
using Tensorflow.Contexts;
19+
using static Tensorflow.Binding;
20+
21+
namespace Tensorflow
22+
{
23+
public class audio_ops
24+
{
25+
public Tensors decode_wav(Tensor contents, int desired_channels = -1, int desired_samples = -1, string name = null)
26+
=> tf.Context.ExecuteOp("DecodeWav", name, new ExecuteOpArgs(contents)
27+
.SetAttributes(new { desired_channels, desired_samples }));
28+
}
29+
}

src/TensorFlowNET.Core/Operations/string_ops.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ public Tensor string_format(Tensor[] inputs, string template = "%s", string plac
7373
}
7474
}.SetAttributes(new { template, placeholder, summarize }));
7575

76-
public RaggedTensor string_split_v2(Tensor input, string sep = "", int maxsplit = -1, string name = null)
76+
public RaggedTensor string_split_v2(Tensor input, string sep = " ", int maxsplit = -1, string name = null)
7777
{
7878
return tf_with(ops.name_scope(name, "StringSplit"), scope =>
7979
{
8080
var sep_tensor = ops.convert_to_tensor(sep, dtype: TF_DataType.TF_STRING);
81+
if(input.rank == 0)
82+
{
83+
return string_split_v2(array_ops.stack(new[] { input }),
84+
sep: sep,
85+
maxsplit: maxsplit,
86+
name: name)[0];
87+
}
88+
8189
var result = tf.Context.ExecuteOp("StringSplitV2", name,
8290
new ExecuteOpArgs(input, sep)
8391
{

src/TensorFlowNET.Core/Tensors/Ragged/RaggedTensor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Tensor[] nested_row_splits
134134
=> new[] { _row_splits };
135135

136136
public override string ToString()
137-
=> $"tf.RaggedTensor: shape={shape} [{string.Join(", ", _values.StringData().Take(10))}]";
137+
=> $"tf.RaggedTensor: shape={_values.TensorShape} [{string.Join(", ", _values.StringData().Take(10))}]";
138138

139139
public static implicit operator Tensor(RaggedTensor indexedSlices)
140140
=> indexedSlices._to_variant();

src/TensorFlowNET.Core/Tensors/Tensors.cs

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public static implicit operator Tensor(Tensors tensors)
9292
public static implicit operator Tensor[](Tensors tensors)
9393
=> tensors.items.ToArray();
9494

95+
public void Deconstruct(out Tensor a, out Tensor b)
96+
{
97+
a = items[0];
98+
b = items[1];
99+
}
100+
95101
public override string ToString()
96102
=> items.Count() == 1
97103
? items.First().ToString()

0 commit comments

Comments
 (0)