Browse Source

_CastGrad

tags/TensorFlowOpLayer
Oceania2018 4 years ago
parent
commit
35bcda9fd8
12 changed files with 56 additions and 26 deletions
  1. +4
    -8
      src/TensorFlowNET.Core/Eager/EagerTensor.Creation.cs
  2. +1
    -1
      src/TensorFlowNET.Core/Gradients/image_grad.cs
  3. +14
    -0
      src/TensorFlowNET.Core/Gradients/math_grad.cs
  4. +1
    -1
      src/TensorFlowNET.Core/NumPy/Numpy.Math.cs
  5. +2
    -0
      src/TensorFlowNET.Core/Operations/math_ops.cs
  6. +4
    -4
      src/TensorFlowNET.Core/Tensorflow.Binding.csproj
  7. +1
    -0
      src/TensorFlowNET.Core/Tensors/Tensors.cs
  8. +2
    -1
      src/TensorFlowNET.Core/Tensors/dtypes.cs
  9. +1
    -1
      src/TensorFlowNET.Core/ops.cs
  10. +6
    -1
      src/TensorFlowNET.Keras/Engine/Model.Training.cs
  11. +17
    -6
      src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs
  12. +3
    -3
      src/TensorFlowNET.Keras/Tensorflow.Keras.csproj

+ 4
- 8
src/TensorFlowNET.Core/Eager/EagerTensor.Creation.cs View File

@@ -37,15 +37,8 @@ namespace Tensorflow.Eager
=> NewEagerTensorHandle(_handle);
#endregion

public EagerTensor(object value,string device_name, TF_DataType dtype = TF_DataType.TF_UINT8) : base((float[])value)
{
throw new NotImplementedException("");
}

public EagerTensor(object value, Shape? shape = null, string device_name = null, TF_DataType dtype = TF_DataType.TF_UINT8) : base((float[])value)
{
NewEagerTensorHandle(_handle);
}
=> NewEagerTensorHandle(_handle);

public EagerTensor(Shape shape, TF_DataType dtype) : base(shape, dtype)
=> NewEagerTensorHandle(_handle);
@@ -56,6 +49,9 @@ namespace Tensorflow.Eager
public EagerTensor(byte[] bytes, Shape shape, TF_DataType dtype) : base(bytes, shape, dtype)
=> NewEagerTensorHandle(_handle);

public EagerTensor(IntPtr data_ptr, Shape shape, TF_DataType dtype) : base(data_ptr, shape, dtype)
=> NewEagerTensorHandle(_handle);

void NewEagerTensorHandle(SafeTensorHandle h)
{
_id = ops.uid();


+ 1
- 1
src/TensorFlowNET.Core/Gradients/image_grad.cs View File

@@ -30,7 +30,7 @@ namespace Tensorflow.Gradients
var shape = new Shape(image.shape.dims.Skip(1).Take(2).ToArray());
Tensor image_shape = null;
if (shape.IsFullyDefined)
image_shape = constant_op.constant(image.shape.dims.Skip(1).Take(2).ToArray());
image_shape = constant_op.constant(image.shape.as_int_list().Skip(1).Take(2).ToArray());
else
image_shape = array_ops.shape(image)["1:3"];



+ 14
- 0
src/TensorFlowNET.Core/Gradients/math_grad.cs View File

@@ -694,6 +694,20 @@ namespace Tensorflow.Gradients
});
}

[RegisterGradient("Cast")]
public static Tensor[] _CastGrad(Operation op, Tensor[] grads)
{
var grad = grads[0];
var x = op.inputs[0];

var src_type = x.dtype.as_base_dtype();
var dst_type = grad.dtype.as_base_dtype();
if (src_type.is_value_dtype() && dst_type.is_value_dtype())
return new Tensor[] { math_ops.cast(grad, src_type) };
else
return new Tensor[0];
}

[RegisterGradient("Cos")]
public static Tensor[] _CosGrad(Operation op, Tensor[] grads)
{


+ 1
- 1
src/TensorFlowNET.Core/NumPy/Numpy.Math.cs View File

@@ -13,7 +13,7 @@ namespace Tensorflow.NumPy
public static NDArray exp(NDArray x) => new NDArray(tf.exp(x));

[AutoNumPy]
public static NDArray floor(NDArray x) => new NDArray(tf.floor(x));
public static NDArray floor(NDArray x) => new NDArray(math_ops.floor(x));

[AutoNumPy]
public static NDArray log(NDArray x) => new NDArray(tf.log(x));


+ 2
- 0
src/TensorFlowNET.Core/Operations/math_ops.cs View File

@@ -720,6 +720,8 @@ namespace Tensorflow
return gen_math_ops.range(start1, limit1, delta1, name);
});
}
public static Tensor floor(Tensor x, string name = null)
=> tf.Context.ExecuteOp("Floor", name, new ExecuteOpArgs(x));

public static Tensor floordiv(Tensor x, Tensor y, string name = null)
{


+ 4
- 4
src/TensorFlowNET.Core/Tensorflow.Binding.csproj View File

@@ -5,7 +5,7 @@
<AssemblyName>TensorFlow.NET</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace>
<TargetTensorFlow>2.2.0</TargetTensorFlow>
<Version>0.60.0</Version>
<Version>0.60.1</Version>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
@@ -20,8 +20,8 @@
<Description>Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io</Description>
<AssemblyVersion>0.60.0.0</AssemblyVersion>
<PackageReleaseNotes>tf.net 0.20.x and above are based on tensorflow native 2.x.
<AssemblyVersion>0.60.1.0</AssemblyVersion>
<PackageReleaseNotes>tf.net 0.60.x and above are based on tensorflow native 2.6.0

* Eager Mode is added finally.
* tf.keras is partially working.
@@ -35,7 +35,7 @@ Keras API is a separate package released as TensorFlow.Keras.
tf.net 0.4x.x aligns with TensorFlow v2.4.1 native library.
tf.net 0.5x.x aligns with TensorFlow v2.5.x native library.
tf.net 0.6x.x aligns with TensorFlow v2.6.x native library.</PackageReleaseNotes>
<FileVersion>0.60.0.0</FileVersion>
<FileVersion>0.60.1.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<SignAssembly>true</SignAssembly>


+ 1
- 0
src/TensorFlowNET.Core/Tensors/Tensors.cs View File

@@ -79,6 +79,7 @@ namespace Tensorflow
public static implicit operator Tensors((Tensor, Tensor) tuple)
=> new Tensors(tuple.Item1, tuple.Item2);

[AutoNumPy]
public static implicit operator Tensors(NDArray nd)
=> new Tensors(nd);



+ 2
- 1
src/TensorFlowNET.Core/Tensors/dtypes.cs View File

@@ -138,7 +138,8 @@ namespace Tensorflow
dtype = TF_DataType.TF_BOOL;
break;
default:
throw new NotSupportedException($"Unable to convert {type} to a TensorFlow data type.");
dtype = TF_DataType.DtInvalid;
break;
}

return dtype;


+ 1
- 1
src/TensorFlowNET.Core/ops.cs View File

@@ -165,7 +165,7 @@ namespace Tensorflow
if (dtype == TF_DataType.TF_STRING)
return ret;

if (dtype.as_base_dtype() != ret.dtype.as_base_dtype())
if (dtype != TF_DataType.DtInvalid && dtype.as_base_dtype() != ret.dtype.as_base_dtype())
ret = gen_math_ops.cast(ret, dtype, name: name);

return ret;


+ 6
- 1
src/TensorFlowNET.Keras/Engine/Model.Training.cs View File

@@ -4,6 +4,7 @@ using System.Text;
using HDF.PInvoke;
using HDF5CSharp;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
using Tensorflow.Keras.Saving;

namespace Tensorflow.Keras.Engine
@@ -14,7 +15,11 @@ namespace Tensorflow.Keras.Engine
public void load_weights(string filepath, bool by_name = false, bool skip_mismatch = false, object options = null)
{
long fileId = Hdf5.OpenFile(filepath, true);

if(fileId < 0)
{
tf_output_redirect.WriteLine($"Can't find weights file {filepath}");
return;
}
bool msuccess = Hdf5.GroupExists(fileId, "model_weights");
bool lsuccess = Hdf5.GroupExists(fileId, "layer_names");



+ 17
- 6
src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs View File

@@ -26,12 +26,12 @@ namespace Tensorflow.Keras
images[i] = resized_image.numpy();
tf_output_redirect.WriteLine(image_paths[i]);
};
var img_ds = tf.data.Dataset.from_tensor_slices(images);

// option 2: dynamic load, but has error, need to fix
/* var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
var img_ds = path_ds.map(x => path_to_image(x, image_size, num_channels, interpolation));*/

var img_ds = tf.data.Dataset.from_tensor_slices(images);
// var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
// var img_ds = path_ds.map(x => path_to_image(x, image_size, num_channels, interpolation));
if (label_mode == "int")
{
var label_ds = dataset_utils.labels_to_dataset(labels, label_mode, num_classes);
@@ -43,6 +43,7 @@ namespace Tensorflow.Keras

Tensor path_to_image(Tensor path, Shape image_size, int num_channels, string interpolation)
{
tf.print(path);
var img = tf.io.read_file(path);
img = tf.image.decode_image(
img, channels: num_channels, expand_animations: false);
@@ -57,8 +58,18 @@ namespace Tensorflow.Keras
int num_classes,
int max_length = -1)
{
var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
var string_ds = path_ds.map(x => path_to_string_content(x, max_length));
var text = new string[image_paths.Length];
for (int i = 0; i < text.Length; i++)
{
text[i] = File.ReadAllText(image_paths[i]);
tf_output_redirect.WriteLine(image_paths[i]);
}

var images = np.array(text);
var string_ds = tf.data.Dataset.from_tensor_slices(images);

// var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
// var string_ds = path_ds.map(x => path_to_string_content(x, max_length));

if (label_mode == "int")
{


+ 3
- 3
src/TensorFlowNET.Keras/Tensorflow.Keras.csproj View File

@@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<RootNamespace>Tensorflow.Keras</RootNamespace>
<Platforms>AnyCPU;x64</Platforms>
<Version>0.6.0</Version>
<Version>0.6.1</Version>
<Authors>Haiping Chen</Authors>
<Product>Keras for .NET</Product>
<Copyright>Apache 2.0, Haiping Chen 2021</Copyright>
@@ -37,8 +37,8 @@ Keras is an API designed for human beings, not machines. Keras follows best prac
<RepositoryType>Git</RepositoryType>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Open.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>0.6.0.0</AssemblyVersion>
<FileVersion>0.6.0.0</FileVersion>
<AssemblyVersion>0.6.1.0</AssemblyVersion>
<FileVersion>0.6.1.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>



Loading…
Cancel
Save