|
| 1 | +/*M/////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | +// |
| 5 | +// By downloading, copying, installing or using the software you agree to this license. |
| 6 | +// If you do not agree to this license, do not download, install, |
| 7 | +// copy or use the software. |
| 8 | +// |
| 9 | +// |
| 10 | +// License Agreement |
| 11 | +// For Open Source Computer Vision Library |
| 12 | +// |
| 13 | +// Copyright (C) 2014, Itseez Inc, all rights reserved. |
| 14 | +// Third party copyrights are property of their respective owners. |
| 15 | +// |
| 16 | +// Redistribution and use in source and binary forms, with or without modification, |
| 17 | +// are permitted provided that the following conditions are met: |
| 18 | +// |
| 19 | +// * Redistribution's of source code must retain the above copyright notice, |
| 20 | +// this list of conditions and the following disclaimer. |
| 21 | +// |
| 22 | +// * Redistribution's in binary form must reproduce the above copyright notice, |
| 23 | +// this list of conditions and the following disclaimer in the documentation |
| 24 | +// and/or other materials provided with the distribution. |
| 25 | +// |
| 26 | +// * The name of the copyright holders may not be used to endorse or promote products |
| 27 | +// derived from this software without specific prior written permission. |
| 28 | +// |
| 29 | +// This software is provided by the copyright holders and contributors "as is" and |
| 30 | +// any express or implied warranties, including, but not limited to, the implied |
| 31 | +// warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 32 | +// In no event shall the Itseez Inc or contributors be liable for any direct, |
| 33 | +// indirect, incidental, special, exemplary, or consequential damages |
| 34 | +// (including, but not limited to, procurement of substitute goods or services; |
| 35 | +// loss of use, data, or profits; or business interruption) however caused |
| 36 | +// and on any theory of liability, whether in contract, strict liability, |
| 37 | +// or tort (including negligence or otherwise) arising in any way out of |
| 38 | +// the use of this software, even if advised of the possibility of such damage. |
| 39 | +// |
| 40 | +//M*/ |
| 41 | + |
| 42 | +#include "opencv2/datasetstools/or_mnist.hpp" |
| 43 | +#include "precomp.hpp" |
| 44 | + |
| 45 | +namespace cv |
| 46 | +{ |
| 47 | +namespace datasetstools |
| 48 | +{ |
| 49 | + |
| 50 | +using namespace std; |
| 51 | + |
| 52 | +class CV_EXPORTS OR_mnistImp : public OR_mnist |
| 53 | +{ |
| 54 | +public: |
| 55 | + OR_mnistImp() {} |
| 56 | + //OR_mnistImp(const string &path); |
| 57 | + virtual ~OR_mnistImp() {} |
| 58 | + |
| 59 | + virtual void load(const string &path); |
| 60 | + |
| 61 | +private: |
| 62 | + void loadDataset(const string &path); |
| 63 | + |
| 64 | + void loadDatasetPart(const string &imagesFile, const string &labelsFile, unsigned int num, vector< Ptr<Object> > &dataset_); |
| 65 | +}; |
| 66 | + |
| 67 | +/*OR_mnistImp::OR_mnistImp(const string &path) |
| 68 | +{ |
| 69 | + loadDataset(path); |
| 70 | +}*/ |
| 71 | + |
| 72 | +void OR_mnistImp::load(const string &path) |
| 73 | +{ |
| 74 | + loadDataset(path); |
| 75 | +} |
| 76 | + |
| 77 | +void OR_mnistImp::loadDatasetPart(const string &imagesFile, const string &labelsFile, unsigned int num, vector< Ptr<Object> > &dataset_) |
| 78 | +{ |
| 79 | + FILE *f = fopen(imagesFile.c_str(), "rb"); |
| 80 | + fseek(f, 16, SEEK_CUR); |
| 81 | + unsigned int imageSize = 28*28; |
| 82 | + char *images = new char[num*imageSize]; |
| 83 | + size_t res = fread(images, 1, num*imageSize, f); |
| 84 | + fclose(f); |
| 85 | + if (num*imageSize != res) |
| 86 | + { |
| 87 | + return; |
| 88 | + } |
| 89 | + f = fopen(labelsFile.c_str(), "rb"); |
| 90 | + fseek(f, 8, SEEK_CUR); |
| 91 | + char *labels = new char[num]; |
| 92 | + res = fread(labels, 1, num, f); |
| 93 | + fclose(f); |
| 94 | + if (num != res) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + for (unsigned int i=0; i<num; ++i) |
| 100 | + { |
| 101 | + Ptr<OR_mnistObj> curr(new OR_mnistObj); |
| 102 | + curr->label = labels[i]; |
| 103 | + |
| 104 | + curr->image = Mat(28, 28, CV_8U); |
| 105 | + unsigned int imageIdx = i*imageSize; |
| 106 | + for (int j=0; j<curr->image.rows; ++j) |
| 107 | + { |
| 108 | + char *im = curr->image.ptr<char>(j); |
| 109 | + for (int k=0; k<curr->image.cols; ++k) |
| 110 | + { |
| 111 | + im[k] = images[imageIdx + j*28 + k]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + dataset_.push_back(curr); |
| 116 | + } |
| 117 | + delete[] images; |
| 118 | + delete[] labels; |
| 119 | +} |
| 120 | + |
| 121 | +void OR_mnistImp::loadDataset(const string &path) |
| 122 | +{ |
| 123 | + train.push_back(vector< Ptr<Object> >()); |
| 124 | + test.push_back(vector< Ptr<Object> >()); |
| 125 | + validation.push_back(vector< Ptr<Object> >()); |
| 126 | + |
| 127 | + string trainImagesFile(path + "train-images.idx3-ubyte"); |
| 128 | + string trainLabelsFile(path + "train-labels.idx1-ubyte"); |
| 129 | + loadDatasetPart(trainImagesFile, trainLabelsFile, 60000, train.back()); |
| 130 | + |
| 131 | + string testImagesFile(path + "t10k-images.idx3-ubyte"); |
| 132 | + string testLabelsFile(path + "t10k-labels.idx1-ubyte"); |
| 133 | + loadDatasetPart(testImagesFile, testLabelsFile, 10000, test.back()); |
| 134 | +} |
| 135 | + |
| 136 | +Ptr<OR_mnist> OR_mnist::create() |
| 137 | +{ |
| 138 | + return Ptr<OR_mnistImp>(new OR_mnistImp); |
| 139 | +} |
| 140 | + |
| 141 | +} |
| 142 | +} |
0 commit comments