betterthanmnist.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef BETTERTHANMNIST_H
  2. #define BETTERTHANMNIST_H
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. #include <iostream>
  7. using namespace std;
  8. const string c_dataFileName = "learningData.txt";
  9. class BetterThanMnist{
  10. public:
  11. BetterThanMnist(){
  12. inputFile.open(c_dataFileName);
  13. cout<<"[+] File "<<c_dataFileName<<" opened."<<endl;
  14. }
  15. unsigned NumImages () const { return imageCount; }
  16. bool LoadNextPicture(){
  17. pixels.clear();
  18. string check;
  19. inputFile >> check;
  20. pixels.resize(784);
  21. for (unsigned i = 0; i < 784;++i){
  22. float a; inputFile >> a;
  23. pixels.at(i) = a;
  24. }
  25. inputFile >> check;
  26. if (check == "out:")
  27. inputFile >> label;
  28. return true;
  29. }
  30. vector<float> GetTestImage(const string filename){
  31. fstream fin;
  32. fin.open(filename);
  33. string check;
  34. fin >> check;
  35. pixels.resize(784);
  36. for (unsigned i = 0; i < 784;++i){
  37. float a; fin >> a;
  38. pixels.at(i) = a;
  39. }
  40. fin.close();
  41. return pixels;
  42. }
  43. void reopen(){
  44. inputFile.close();
  45. inputFile.open(c_dataFileName);
  46. cout<<"[+] File "<<c_dataFileName<<" reopened."<<endl;
  47. }
  48. vector<float> GetImage (unsigned& label_out, bool newPic = true)
  49. {
  50. if (newPic)
  51. this->LoadNextPicture();
  52. label_out = label;
  53. ++imageCount;
  54. return pixels;
  55. }
  56. private:
  57. unsigned imageCount = 0;
  58. unsigned label = 0;
  59. vector<float> pixels;
  60. fstream inputFile;
  61. };
  62. #endif // BETTERTHANMNIST_H