-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.cpp
101 lines (89 loc) · 2.64 KB
/
backup.cpp
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
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include "Filter.hpp"
#include "Convolution.hpp"
using namespace std;
int w_size, bias; // kernel window size
double** kernel; // image kernel
vector<Filter*> filters;
void push_filter(int idx) {
// make edge detector for color idx
double ***ed = get_tensor(w_size, w_size, 3);
for (int i = 0; i < w_size; ++i) {
for (int j = 0; j < w_size; ++j) {
ed[i][j][idx] = kernel[i][j];
}
}
Filter *f = new Filter(ed, w_size, 3, bias);
f->normalize();
filters.push_back(f);
}
int main(int argc, char *argv[]) {
// input the kernel matrix
cin >> w_size >> bias;
kernel = new double*[w_size];
for (int i = 0; i < w_size; i++) {
kernel[i] = new double[w_size];
for (int j = 0; j < w_size; j++) {
cin >> kernel[i][j];
}
}
push_filter(0); // R
push_filter(1); // G
push_filter(2); // B
if (argc < 3) {
cerr << "Usage <input_data file name> <output file name>" << endl;
return 1;
}
// output file will be written in the same format as input file
ifstream ifile (argv[1]);
ofstream ofile (argv[2]);
if (!ofile.is_open()) {
cerr << "Unable to open " << argv[1] << endl;
return 1;
}
if (!ifile.is_open()) {
cerr << "Unable to open " << argv[0] << endl;
return 1;
}
int width, num_images, height, depth;
int stride = 1, padding = 1;
ifile >> num_images >> width >> height >> depth;
ofile << num_images << " ";
cerr << num_images << " ";
Convolution clayer(width, height, depth, w_size, stride, padding, filters.size());
double ***input;
input = get_tensor(width, height, depth);
for (int id = 0; id < num_images; ++id) {
// read one image
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
for (int k = 0; k < depth; ++k) {
ifile >> input[i][j][k];
if (ifile.peek() == ',') ifile.ignore();
}
}
}
auto output = clayer.conv2d(input, filters);
double ***out_volume = get<3>(output);
int o_width = get<0>(output), o_height = get<1>(output), o_depth = get<2>(output);
if (id == 0) {
// print image dimensions only the first time
ofile << o_width << " " << o_height << " " << o_depth << "\n";
cerr << o_width << " " << o_height << " " << o_depth << "\n";
}
for (int i = 0; i < o_width; ++i) {
for (int j = 0; j < o_height; ++j) {
ofile << out_volume[i][j][0] << "," << out_volume[i][j][1]
<< "," << out_volume[i][j][2] << " ";
}
ofile << "\n";
}
ofile << "\n";
}
ifile.close();
ofile.close();
return 0;
}