mnist.cpp 5.08 KB
Newer Older
Scott Thornton's avatar
Scott Thornton committed
1
2
3
4
5
#include <cstdio>
#include <string>
#include <fstream>
#include <stdexcept>

6
#include <migraph/onnx.hpp>
Scott Thornton's avatar
Scott Thornton committed
7

8
9
#include <migraph/cpu/cpu_target.hpp>
#include <migraph/generate.hpp>
Scott Thornton's avatar
Scott Thornton committed
10
11
12

std::vector<float> read_mnist_images(std::string full_path, int& number_of_images, int& image_size)
{
13
    auto reverse_int = [](unsigned int i) {
Scott Thornton's avatar
Scott Thornton committed
14
        unsigned char c1, c2, c3, c4;
15
16
17
18
19
20
        c1 = i & 255u;
        c2 = (i >> 8u) & 255u;
        c3 = (i >> 16u) & 255u;
        c4 = (i >> 24u) & 255u;
        return (static_cast<unsigned int>(c1) << 24u) + (static_cast<unsigned int>(c2) << 16u) +
               (static_cast<unsigned int>(c3) << 8u) + c4;
Scott Thornton's avatar
Scott Thornton committed
21
22
    };

23
    using uchar = unsigned char;
Scott Thornton's avatar
Scott Thornton committed
24
25
26
27
28
29
30

    std::ifstream file(full_path, std::ios::binary);

    if(file.is_open())
    {
        int magic_number = 0, n_rows = 0, n_cols = 0;

31
32
        file.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
        magic_number = reverse_int(magic_number);
Scott Thornton's avatar
Scott Thornton committed
33
34
35
36

        if(magic_number != 2051)
            throw std::runtime_error("Invalid MNIST image file!");

37
38
39
40
41
42
        file.read(reinterpret_cast<char*>(&number_of_images), sizeof(number_of_images));
        number_of_images = reverse_int(number_of_images);
        file.read(reinterpret_cast<char*>(&n_rows), sizeof(n_rows)); 
        n_rows = reverse_int(n_rows);
        file.read(reinterpret_cast<char*>(&n_cols), sizeof(n_cols));
        n_cols = reverse_int(n_cols);
Scott Thornton's avatar
Scott Thornton committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

        image_size = n_rows * n_cols;

        // uchar** _dataset = new uchar*[number_of_images];
        // for(int i = 0; i < number_of_images; i++) {
        //     _dataset[i] = new uchar[image_size];
        //     file.read((char *)_dataset[i], image_size);
        // }

        std::vector<float> result(number_of_images * image_size);
        for(int i = 0; i < number_of_images; i++)
        {
            for(int j = 0; j < image_size; j++)
            {
                uchar tmp;
58
                file.read(reinterpret_cast<char*>(&tmp), 1);
Scott Thornton's avatar
Scott Thornton committed
59
60
61
62
63
64
65
66
67
68
69
70
71
                result[i * image_size + j] = tmp / 255.0;
            }
        }
        return result;
    }
    else
    {
        throw std::runtime_error("Cannot open file `" + full_path + "`!");
    }
}

std::vector<int32_t> read_mnist_labels(std::string full_path, int& number_of_labels)
{
72
    auto reverse_int = [](unsigned int i) {
Scott Thornton's avatar
Scott Thornton committed
73
        unsigned char c1, c2, c3, c4;
74
75
76
77
78
79
        c1 = i & 255u;
        c2 = (i >> 8u) & 255u;
        c3 = (i >> 16u) & 255u;
        c4 = (i >> 24u) & 255u;
        return (static_cast<unsigned int>(c1) << 24u) + (static_cast<unsigned int>(c2) << 16u) +
               (static_cast<unsigned int>(c3) << 8u) + c4;
Scott Thornton's avatar
Scott Thornton committed
80
81
    };

82
    using uchar = unsigned char;
Scott Thornton's avatar
Scott Thornton committed
83
84
85
86
87
88

    std::ifstream file(full_path, std::ios::binary);

    if(file.is_open())
    {
        int magic_number = 0;
89
90
        file.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
        magic_number = reverse_int(magic_number);
Scott Thornton's avatar
Scott Thornton committed
91
92
93
94

        if(magic_number != 2049)
            throw std::runtime_error("Invalid MNIST label file!");

95
96
        file.read(reinterpret_cast<char*>(&number_of_labels), sizeof(number_of_labels));
        number_of_labels = reverse_int(number_of_labels);
Scott Thornton's avatar
Scott Thornton committed
97
98
99
100
101

        std::vector<int32_t> result(number_of_labels);
        for(int i = 0; i < number_of_labels; i++)
        {
            uchar tmp;
102
            file.read(reinterpret_cast<char*>(&tmp), 1);
Scott Thornton's avatar
Scott Thornton committed
103
104
105
106
107
108
109
110
111
112
            result[i] = tmp;
        }
        return result;
    }
    else
    {
        throw std::runtime_error("Unable to open file `" + full_path + "`!");
    }
}

Scott Thornton's avatar
Scott Thornton committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
std::vector<float> softmax(std::vector<float> p) {
    size_t n = p.size();
    std::vector<float> result(n);
    float s = 0.0f;
    for (size_t i = 0; i < n; i++) {
        result[i] = std::exp(p[i]);
        s += result[i];
    }
    for (size_t i = 0; i < n; i++) {
        result[i] = result[i]/s;
    }
    return result;
}

Scott Thornton's avatar
Scott Thornton committed
127
128
129
130
131
132
133
134
135
136
137
138
139
int main(int argc, char const* argv[])
{
    if(argc > 1)
    {
        std::string datafile        = argv[2];
        std::string labelfile       = argv[3];
        int nimages                 = -1;
        int image_size              = -1;
        int nlabels                 = -1;
        std::vector<float> input    = read_mnist_images(datafile, nimages, image_size);
        std::vector<int32_t> labels = read_mnist_labels(labelfile, nlabels);

        std::string file = argv[1];
140
141
        auto prog        = migraph::parse_onnx(file);
        prog.compile(migraph::cpu::cpu_target{});
Scott Thornton's avatar
Scott Thornton committed
142
        // auto s = prog.get_parameter_shape("Input3");
143
        auto s = migraph::shape{migraph::shape::float_type, {1, 1, 28, 28}};
Scott Thornton's avatar
Scott Thornton committed
144
        std::cout << s << std::endl;
Scott Thornton's avatar
Scott Thornton committed
145
146
147
        auto ptr = input.data();
        for (int i = 0; i < 20; i++)
        {
148
149
            std::cout << "label: " << labels[i] << "  ---->  ";
            auto input3 = migraph::argument{s, &ptr[784*i]};
Scott Thornton's avatar
Scott Thornton committed
150
151
152
153
            auto result = prog.eval({{"Input3", input3}});
            std::vector<float> logits;
            result.visit([&](auto output) { logits.assign(output.begin(), output.end()); });
            std::vector<float> probs = softmax(logits);
154
155
            for (auto x : probs) std::cout << x << "  ";
            std::cout << std::endl;
Scott Thornton's avatar
Scott Thornton committed
156
        }
157
        std::cout << std::endl;
Scott Thornton's avatar
Scott Thornton committed
158
159
    }
}