8000 load all images for paths_and_labels_to_dataset · SciSharp/TensorFlow.NET@9a2ed27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a2ed27

Browse files
committed
load all images for paths_and_labels_to_dataset
1 parent 30f8f67 commit 9a2ed27

File tree

4 files changed

+21
-4
lines changed
  • TensorFlowNET.Keras/Preprocessings
  • 4 files changed

    +21
    -4
    lines changed

    src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -42,7 +42,7 @@ public Tensor()
    4242
    public unsafe Tensor(SafeTensorHandle handle, bool clone = false)
    4343
    {
    4444
    _handle = handle;
    45-
    if (clone)
    45+
    if (clone && handle != null)
    4646
    _handle = TF_NewTensor(shape, dtype, data: TensorDataPointer.ToPointer());
    4747

    4848
    isCreatedInGraphMode = !tf.executing_eagerly();

    src/TensorFlowNET.Core/Tensors/dtypes.cs

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -17,6 +17,7 @@ limitations under the License.
    1717
    using Tensorflow.NumPy;
    1818
    using System;
    1919
    using System.Numerics;
    20+
    using System.Diagnostics;
    2021

    2122
    namespace Tensorflow
    2223
    {
    @@ -221,6 +222,7 @@ public static DataType as_base_dtype(this DataType type)
    221222
    return (int)type > 100 ? (DataType)((int)type - 100) : type;
    222223
    }
    223224

    225+
    [DebuggerStepThrough]
    224226
    public static TF_DataType as_tf_dtype(this DataType type)
    225227
    {
    226228
    return (TF_DataType)type;

    src/TensorFlowNET.Core/Variables/BaseResourceVariable.cs

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -222,7 +222,7 @@ public IVariableV1 assign_sub_lazy_load(Tensor delta, string name = null)
    222222
    public override string ToString()
    223223
    {
    224224
    if (tf.Context.executing_eagerly())
    225-
    return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}, numpy={read_value()}";
    225+
    return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}, numpy={read_value().numpy()}";
    226226
    else
    227227
    return $"tf.Variable: '{Name}' shape={string.Join(",", shape)}, dtype={dtype.as_numpy_name()}";
    228228
    }

    src/TensorFlowNET.Keras/Preprocessings/Preprocessing.paths_and_labels_to_dataset.cs

    Lines changed: 17 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,6 +1,7 @@
    11
    using System;
    22
    using System.IO;
    33
    using static Tensorflow.Binding;
    4+
    using Tensorflow.NumPy;
    45

    56
    namespace Tensorflow.Keras
    67
    {
    @@ -14,9 +15,23 @@ public IDatasetV2 paths_and_labels_to_dataset(string[] image_paths,
    1415
    int num_classes,
    1516
    string interpolation)
    1617
    {
    17-
    var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
    18-
    var img_ds = path_ds.map(x => path_to_image(x, image_size, num_channels, interpolation));
    18+
    // option 1: will load all images into memory, not efficient
    19+
    var images = np.zeros((image_paths.Length, image_size[0], image_size[1], num_channels), np.float32);
    20+
    for (int i = 0; i < len(images); i++)
    21+
    {
    22+
    var img = tf.io.read_file(image_paths[i]);
    23+
    img = tf.image.decode_image(
    24+
    img, channels: num_channels, expand_animations: false);
    25+
    var resized_image = tf.image.resize_images_v2(img, image_size, method: interpolation);
    26+
    images[i] = resized_image.numpy();
    27+
    tf_output_redirect.WriteLine(image_paths[i]);
    28+
    };
    29+
    30+
    // option 2: dynamic load, but has error, need to fix
    31+
    /* var path_ds = tf.data.Dataset.from_tensor_slices(image_paths);
    32+
    var img_ds = path_ds.map(x => path_to_image(x, image_size, num_channels, interpolation));*/
    1933

    34+
    var img_ds = tf.data.Dataset.from_tensor_slices(images);
    2035
    if (label_mode == "int")
    2136
    {
    2237
    var label_ds = dataset_utils.labels_to_dataset(labels, label_mode, num_classes);

    0 commit comments

    Comments
     (0)
    0