| 13 | namespace dlib |
| 14 | { |
| 15 | void load_mnist_dataset ( |
| 16 | const std::string& folder_name, |
| 17 | std::vector<matrix<unsigned char> >& training_images, |
| 18 | std::vector<unsigned long>& training_labels, |
| 19 | std::vector<matrix<unsigned char> >& testing_images, |
| 20 | std::vector<unsigned long>& testing_labels |
| 21 | ) |
| 22 | { |
| 23 | using namespace std; |
| 24 | ifstream fin1((folder_name+"/train-images-idx3-ubyte").c_str(), ios::binary); |
| 25 | if (!fin1) |
| 26 | { |
| 27 | fin1.open((folder_name + "/train-images.idx3-ubyte").c_str(), ios::binary); |
| 28 | } |
| 29 | |
| 30 | ifstream fin2((folder_name+"/train-labels-idx1-ubyte").c_str(), ios::binary); |
| 31 | if (!fin2) |
| 32 | { |
| 33 | fin2.open((folder_name + "/train-labels.idx1-ubyte").c_str(), ios::binary); |
| 34 | } |
| 35 | |
| 36 | ifstream fin3((folder_name+"/t10k-images-idx3-ubyte").c_str(), ios::binary); |
| 37 | if (!fin3) |
| 38 | { |
| 39 | fin3.open((folder_name + "/t10k-images.idx3-ubyte").c_str(), ios::binary); |
| 40 | } |
| 41 | |
| 42 | ifstream fin4((folder_name+"/t10k-labels-idx1-ubyte").c_str(), ios::binary); |
| 43 | if (!fin4) |
| 44 | { |
| 45 | fin4.open((folder_name + "/t10k-labels.idx1-ubyte").c_str(), ios::binary); |
| 46 | } |
| 47 | |
| 48 | if (!fin1) throw error("Unable to open file train-images-idx3-ubyte or train-images.idx3-ubyte"); |
| 49 | if (!fin2) throw error("Unable to open file train-labels-idx1-ubyte or train-labels.idx1-ubyte"); |
| 50 | if (!fin3) throw error("Unable to open file t10k-images-idx3-ubyte or t10k-images.idx3-ubyte"); |
| 51 | if (!fin4) throw error("Unable to open file t10k-labels-idx1-ubyte or t10k-labels.idx1-ubyte"); |
| 52 | |
| 53 | byte_orderer bo; |
| 54 | |
| 55 | // make sure the files have the contents we expect. |
| 56 | uint32 magic, num, nr, nc, num2, num3, num4; |
| 57 | fin1.read((char*)&magic, sizeof(magic)); bo.big_to_host(magic); |
| 58 | fin1.read((char*)&num, sizeof(num)); bo.big_to_host(num); |
| 59 | fin1.read((char*)&nr, sizeof(nr)); bo.big_to_host(nr); |
| 60 | fin1.read((char*)&nc, sizeof(nc)); bo.big_to_host(nc); |
| 61 | if (magic != 2051 || num != 60000 || nr != 28 || nc != 28) |
| 62 | throw error("mnist dat files are corrupted."); |
| 63 | |
| 64 | fin2.read((char*)&magic, sizeof(magic)); bo.big_to_host(magic); |
| 65 | fin2.read((char*)&num2, sizeof(num2)); bo.big_to_host(num2); |
| 66 | if (magic != 2049 || num2 != 60000) |
| 67 | throw error("mnist dat files are corrupted."); |
| 68 | |
| 69 | fin3.read((char*)&magic, sizeof(magic)); bo.big_to_host(magic); |
| 70 | fin3.read((char*)&num3, sizeof(num3)); bo.big_to_host(num3); |
| 71 | fin3.read((char*)&nr, sizeof(nr)); bo.big_to_host(nr); |
| 72 | fin3.read((char*)&nc, sizeof(nc)); bo.big_to_host(nc); |