-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.cpp
More file actions
153 lines (125 loc) · 4.75 KB
/
Dataset.cpp
File metadata and controls
153 lines (125 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by Hà Tường Nguyên on 5/29/24.
//
#include "Dataset.h"
namespace AI {
Dataset::Dataset(std::string csv_path) :
feature(new std::vector<std::vector<int>*>), target(new std::vector<int>), errorIdx(new std::vector<int>) {
path = std::move(csv_path);
std::ifstream mnist(path);
if (!mnist.is_open()) throw std::invalid_argument("Failed to open file \"" + path + "\"");
std::string str;
int current_index = 0;
while (std::getline(mnist, str, '\n')) {
std::stringstream ss(str);
std::string temp;
std::getline(ss, temp, ',');
try {
target->push_back(std::stoi(temp));
} catch (std::exception& e) {
errorIdx->push_back(current_index++);
continue;
}
try {
auto* row = new std::vector<int>;
while (std::getline(ss, temp, ',')) {
row->push_back(std::stoi(temp));
}
feature->push_back(row);
} catch (std::exception& e) {
target->pop_back();
errorIdx->push_back(current_index++);
continue;
}
if (feature->back()->size() != 784) {
target->pop_back();
feature->pop_back();
errorIdx->push_back(current_index);
}
++current_index;
}
mnist.close();
}
Dataset::~Dataset() {
for (auto row_ptr: *feature) {
delete row_ptr;
}
delete feature;
delete target;
delete errorIdx;
}
std::vector<int>& Dataset::operator[](int idx) const {
return *(*feature)[idx];
}
std::vector<int>& Dataset::get_error() const {
return *errorIdx;
}
void Dataset::shape(int& num_of_data, int& num_of_feature) const {
num_of_feature = (int) feature->size();
num_of_data = (int) target->size();
}
int Dataset::num_error() const {
return (int) errorIdx->size();
}
void Dataset::print() const {
int i = 0;
std::cout << target->size() << " - " << feature->size() << std::endl;
for (const auto& row_ptr: *feature) {
std::cout << std::setw(3) << (*target)[i++] << " ";
for (int num: *row_ptr) {
std::cout << std::setw(3) << num << " ";
}
std::cout << std::endl;
}
}
void test_mnist_animation(std::string csv) {
Dataset mnist(std::move(csv));
int num_data, num_feature;
mnist.shape(num_data, num_feature);
if (mnist.num_error() != 0) {
std::cout << color::RED << "Number of error line in dataset: " << color::RESET
<< mnist.num_error()
<< std::endl;
std::cout << color::RED << "\t->Error at index: " << color::RESET;
for (int i = 0; i < mnist.num_error(); ++i) {
std::cout << mnist.get_error()[i] << ' ';
}
std::cout << std::endl;
}
graphic::Screen scr(28, 28);
scr.setUp()->setMapFunc(graphic::MapFunc::two4_bit);
scr.setUp()->setMapChar(graphic::mapchar::none_char);
for (int i = 0; i < num_data; i++) {
scr.fill(mnist[i]);
std::cout << scr;
std::cout << cursor::preline(28);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
std::cout << cursor::nextline(28);
}
void test_mnist_bitmap(std::string csv) {
Dataset mnist(std::move(csv));
int num_data, num_feature;
mnist.shape(num_data, num_feature);
std::cout << color::GREEN << "Number of data: " << num_data << color::RESET << std::endl;
std::cout << color::GREEN << "Number of feature: " << num_feature << color::RESET << std::endl;
if (mnist.num_error() != 0) {
std::cout << color::RED << "Number of error line in dataset: " << color::RESET
<< mnist.num_error()
<< std::endl;
std::cout << color::RED << "\t->Error at index: " << color::RESET;
for (int i = 0; i < mnist.num_error(); ++i) {
std::cout << mnist.get_error()[i] << ' ';
}
std::cout << std::endl;
}
std::filesystem::create_directory("bmp_mnist");
std::filesystem::current_path(std::filesystem::current_path() / "bmp_mnist");
graphic::Screen scr(28, 28);
for (int i = 0; i < num_data; i++) {
scr.fill(mnist[i]);
graphic::Bitmap(std::to_string(i)) << scr;
}
std::filesystem::current_path(std::filesystem::current_path().parent_path());
}
}