|
1 | | -using System; |
| 1 | +using NumSharp; |
| 2 | +using System; |
2 | 3 | using System.Globalization; |
| 4 | +using System.Threading.Tasks; |
3 | 5 | using static Tensorflow.Binding; |
4 | 6 |
|
5 | 7 | namespace Tensorflow.Keras |
6 | 8 | { |
7 | 9 | public partial class Preprocessing |
8 | 10 | { |
9 | | - public Tensor paths_and_labels_to_dataset(string[] image_paths, |
| 11 | + public IDatasetV2 paths_and_labels_to_dataset(string[] image_paths, |
10 | 12 | TensorShape image_size, |
11 | 13 | int num_channels, |
12 | 14 | int[] labels, |
13 | 15 | string label_mode, |
14 | 16 | int num_classes, |
15 | 17 | string interpolation) |
16 | 18 | { |
17 | | - foreach (var image_path in image_paths) |
18 | | - path_to_image(image_path, image_size, num_channels, interpolation); |
| 19 | + Shape shape = (image_paths.Length, image_size.dims[0], image_size.dims[1], num_channels); |
| 20 | + Console.WriteLine($"Allocating memory for shape{shape}, {NPTypeCode.Float}"); |
| 21 | + var data = np.zeros(shape, NPTypeCode.Float); |
19 | 22 |
|
20 | | - throw new NotImplementedException(""); |
| 23 | + for (var i = 0; i < image_paths.Length; i++) |
| 24 | + { |
| 25 | + var image = path_to_image(image_paths[i], image_size, num_channels, interpolation); |
| 26 | + data[i] = image.numpy(); |
| 27 | + if (i % 100 == 0) |
| 28 | + Console.WriteLine($"Filled {i}/{image_paths.Length} data into memory."); |
| 29 | + } |
| 30 | + |
| 31 | + var img_ds = tf.data.Dataset.from_tensor_slices(data); |
| 32 | + |
| 33 | + if (label_mode == "int") |
| 34 | + { |
| 35 | + var label_ds = tf.keras.preprocessing.dataset_utils.labels_to_dataset(labels, label_mode, num_classes); |
| 36 | + img_ds = tf.data.Dataset.zip(img_ds, label_ds); |
| 37 | + } |
| 38 | + else |
| 39 | + throw new NotImplementedException(""); |
| 40 | + |
| 41 | + return img_ds; |
21 | 42 | } |
22 | 43 |
|
23 | 44 | Tensor path_to_image(string path, TensorShape image_size, int num_channels, string interpolation) |
|
0 commit comments