|
1 | 1 | using static Tensorflow.KerasApi; |
| 2 | +using static Tensorflow.Binding; |
| 3 | +using NumSharp; |
2 | 4 |
|
3 | 5 | namespace Tensorflow.Keras |
4 | 6 | { |
@@ -41,7 +43,7 @@ public IDatasetV2 image_dataset_from_directory(string directory, |
41 | 43 | int num_channels = 0; |
42 | 44 | if (color_mode == "rgb") |
43 | 45 | num_channels = 3; |
44 | | - |
| 46 | + |
45 | 47 | var (image_paths, label_list, class_name_list) = keras.preprocessing.dataset_utils.index_directory(directory, |
46 | 48 | labels, |
47 | 49 | formats: WHITELIST_FORMATS, |
@@ -90,5 +92,50 @@ public IDatasetV2 text_dataset_from_directory(string directory, |
90 | 92 | dataset.class_names = class_name_list; |
91 | 93 | return dataset; |
92 | 94 | } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Creates a dataset of sliding windows over a timeseries provided as array. |
| 98 | + /// </summary> |
| 99 | + /// <param name="data"></param> |
| 100 | + /// <param name="sequence_length"></param> |
| 101 | + /// <param name="sequence_stride"></param> |
| 102 | + /// <param name="sampling_rate"></param> |
| 103 | + /// <param name="batch_size"></param> |
| 104 | + /// <param name="shuffle"></param> |
| 105 | + /// <returns></returns> |
| 106 | + public IDatasetV2 timeseries_dataset_from_array(Tensor data, int sequence_length, |
| 107 | + int sequence_stride = 1, int sampling_rate = 1, int batch_size = 128, |
| 108 | + bool shuffle = false, int seed = (int)1e6, int start_index = 0, int? end_index = null) |
| 109 | + { |
| 110 | + if (!end_index.HasValue) |
| 111 | + end_index = len(data); |
| 112 | + |
| 113 | + var num_seqs = end_index.Value - start_index - (sequence_length * sampling_rate) + 1; |
| 114 | + var index_dtype = num_seqs < 2147483647 ? tf.int32 : tf.int64; |
| 115 | + var start_positions = np.arange(0, num_seqs, sequence_stride); |
| 116 | + if (shuffle) |
| 117 | + { |
| 118 | + var rng = np.random.RandomState(seed); |
| 119 | + rng.shuffle(start_positions); |
| 120 | + } |
| 121 | + |
| 122 | + var sequence_length_tensor = math_ops.cast(sequence_length, dtype: index_dtype); |
| 123 | + var sampling_rate_tensor = math_ops.cast(sampling_rate, dtype: index_dtype); |
| 124 | + |
| 125 | + var start_positions_tensor = tf.constant(start_positions); |
| 126 | + var positions_ds = tf.data.Dataset.from_tensor(start_positions_tensor).repeat(); |
| 127 | + var z = tf.data.Dataset.zip(tf.data.Dataset.range(len(start_positions)), positions_ds); |
| 128 | + var indices = z.map(m => |
| 129 | + { |
| 130 | + var (i, positions) = (m[0], m[1]); |
| 131 | + return tf.range(positions[i], positions[i] + sequence_length_tensor * sampling_rate_tensor, sampling_rate_tensor); |
| 132 | + }, num_parallel_calls: -1); |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + IDatasetV2 sequences_from_indices(Tensor array, Tensor indices_ds, Tensor start_index, Tensor end_index) |
| 137 | + { |
| 138 | + return null; |
| 139 | + } |
93 | 140 | } |
94 | 141 | } |
0 commit comments