|
| 1 | +/***************************************************************************** |
| 2 | + Copyright 2020 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 NumSharp; |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.IO; |
| 21 | +using System.Net; |
| 22 | +using System.Text; |
| 23 | + |
| 24 | +namespace Tensorflow.Keras.Datasets |
| 25 | +{ |
| 26 | + public class Mnist |
| 27 | + { |
| 28 | + string origin_folder = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"; |
| 29 | + string file_name = "mnist.npz"; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Loads the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). |
| 33 | + /// </summary> |
| 34 | + /// <returns></returns> |
| 35 | + public DatasetPass load_data() |
| 36 | + { |
| 37 | + var file = Download(); |
| 38 | + var bytes = File.ReadAllBytes(file); |
| 39 | + var datax = LoadX(bytes); |
| 40 | + var datay = LoadY(bytes); |
| 41 | + return new DatasetPass |
| 42 | + { |
| 43 | + Train = (datax.Item1, datay.Item1), |
| 44 | + Test = (datax.Item2, datay.Item2) |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + (NDArray, NDArray) LoadX(byte[] bytes) |
| 49 | + { |
| 50 | + var y = np.Load_Npz<byte[,,]>(bytes); |
| 51 | + return (y["x_train.npy"], y["x_test.npy"]); |
| 52 | + } |
| 53 | + |
| 54 | + (NDArray, NDArray) LoadY(byte[] bytes) |
| 55 | + { |
| 56 | + var y = np.Load_Npz<byte[]>(bytes); |
| 57 | + return (y["y_train.npy"], y["y_test.npy"]); |
| 58 | + } |
| 59 | + |
| 60 | + string Download() |
| 61 | + { |
| 62 | + var fileSaveTo = Path.Combine(Path.GetTempPath(), file_name); |
| 63 | + |
| 64 | + if (File.Exists(fileSaveTo)) |
| 65 | + { |
| 66 | + Console.WriteLine($"The file {fileSaveTo} already exists"); |
| 67 | + return fileSaveTo; |
| 68 | + } |
| 69 | + |
| 70 | + using var wc = new WebClient(); |
| 71 | + wc.DownloadFileTaskAsync(origin_folder + file_name, fileSaveTo).Wait(); |
| 72 | + |
| 73 | + return fileSaveTo; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments