-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.cpp
More file actions
182 lines (156 loc) · 5.07 KB
/
common.cpp
File metadata and controls
182 lines (156 loc) · 5.07 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "common.h"
#include <iostream>
#include <fstream>
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
using std::string;
using std::vector;
namespace {
int reverse_int (int i)
{
unsigned char c1, c2, c3, c4;
c1 = i & 255;
c2 = (i >> 8) & 255;
c3 = (i >> 16) & 255;
c4 = (i >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
// Convert label info to one-hot format
vector<vector<float>> to_one_hot(vector<float> labels)
{
vector<vector<float>> result;
for (float label : labels) {
vector<float> val(10);
val[(int)label] = 1.0f;
result.push_back(val);
}
return result;
}
// Create a Tensor(N, 784) from N image data
tensorflow::Tensor MakeTensor(const std::vector<vector<float>>& batch) {
tensorflow::Tensor t(tensorflow::DT_FLOAT,
tensorflow::TensorShape({(int)batch.size(), 784}));
auto dst = t.flat<float>().data();
for (auto img : batch) {
std::copy_n(img.begin(), 784, dst);
dst += 784;
}
return t;
}
// Create a Tensor(N, 10) from N label data
tensorflow::Tensor MakeTargetTensor(const std::vector<vector<float>>& batch) {
tensorflow::Tensor t(tensorflow::DT_FLOAT,
tensorflow::TensorShape({(int)batch.size(), 10}));
auto dst = t.flat<float>().data();
for (auto target : batch) {
std::copy_n(target.begin(), 10, dst);
dst += 10;
}
return t;
}
}
vector<vector<float>> read_training_file(string filename)
{
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
int magic_number = 0;
int number_of_images = 0;
int rows = 0;
int cols = 0;
// Read header
ifs.read((char*)&magic_number, sizeof(magic_number));
magic_number = reverse_int(magic_number);
if (magic_number != 0x803) {
std::cout << "Invalid magic number: " << magic_number << std::endl;
std::cout << "Expected value is: " << 0x803 << std::endl;
exit(1);
}
ifs.read((char*)&number_of_images, sizeof(number_of_images));
number_of_images = reverse_int(number_of_images);
ifs.read((char*)&rows, sizeof(rows));
rows = reverse_int(rows);
ifs.read((char*)&cols, sizeof(cols));
cols = reverse_int(cols);
vector<vector<float>> images(number_of_images);
// Read image data
for (int i = 0; i < number_of_images; i++) {
images[i].resize(rows * cols);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
unsigned char temp = 0;
ifs.read((char*)&temp, sizeof(temp));
images[i][rows*row+col] = (float)temp / 255.0f;
}
}
}
return images;
}
vector<float> read_label_file(string filename)
{
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
int magic_number = 0;
int number_of_images = 0;
// Read header
ifs.read((char*)&magic_number, sizeof(magic_number));
magic_number = reverse_int(magic_number);
if (magic_number != 0x801) {
std::cout << "Invalid magic number: " << magic_number << std::endl;
std::cout << "Expected value is: " << 0x803 << std::endl;
exit(1);
}
ifs.read((char*)&number_of_images, sizeof(number_of_images));
number_of_images = reverse_int(number_of_images);
vector<float> label(number_of_images);
// Read image data
for(int i = 0; i < number_of_images; i++){
unsigned char temp = 0;
ifs.read((char*)&temp, sizeof(temp));
label[i] = (float)temp;
}
return label;
}
void predict(const std::unique_ptr<tensorflow::Session>& session, const vector<vector<float>>& batch, const vector<float>& labels) {
// Create an input data
tensorflow::Tensor lp(tensorflow::DT_BOOL, tensorflow::TensorShape({}));
lp.flat<bool>().setZero();
vector<std::pair<string, tensorflow::Tensor>> inputs = {
{"input", MakeTensor(batch)},
{"batch_normalization_1/keras_learning_phase", lp}
};
std::vector<tensorflow::Tensor> out_tensors;
// Predict
TF_CHECK_OK(session->Run(inputs, {"output/Softmax"}, {}, &out_tensors));
// Calculate its accuracy
int hits = 0;
for (auto tensor : out_tensors) {
auto items = tensor.shaped<float, 2>({static_cast<int>(batch.size()), 10});
for (int i = 0; i < batch.size(); i++) {
int arg_max = 0;
float val_max = items(i, 0);
for (int j = 0; j < 10; j++) {
if (items(i, j) > val_max) {
arg_max = j;
val_max = items(i, j);
}
}
if (arg_max == labels[i]) {
hits++;
}
}
}
std::cout << "Accuracy: " << hits / (float)batch.size() << std::endl;
}
// Train
void run_train_step(const std::unique_ptr<tensorflow::Session>& session,
const std::vector<vector<float>>& train_x,
const std::vector<float>& train_y) {
auto train_y_ = to_one_hot(train_y);
vector<std::pair<string, tensorflow::Tensor>> inputs = {
{"image", MakeTensor(train_x)},
{"target", MakeTargetTensor(train_y_)}
};
TF_CHECK_OK(session->Run(inputs, {}, {"train"}, nullptr));
}