|
| 1 | +#include <algorithm> |
| 2 | +#include <filesystem> |
| 3 | +#include <iostream> |
| 4 | +#include <pico_toolshed/format/format_bin.hpp> |
| 5 | +#include <pico_toolshed/format/format_mnist.hpp> |
| 6 | +#include <pico_toolshed/scoped_timer.hpp> |
| 7 | +#include <pico_tree/array_traits.hpp> |
| 8 | +#include <pico_tree/kd_tree.hpp> |
| 9 | +#include <pico_tree/vector_traits.hpp> |
| 10 | +#include <pico_understory/rkd_tree.hpp> |
| 11 | + |
| 12 | +template <typename U, typename T, std::size_t N> |
| 13 | +std::array<U, N> Cast(std::array<T, N> const& i) { |
| 14 | + std::array<U, N> c; |
| 15 | + std::transform(i.begin(), i.end(), c.begin(), [](T a) -> U { |
| 16 | + return static_cast<U>(a); |
| 17 | + }); |
| 18 | + return c; |
| 19 | +} |
| 20 | + |
| 21 | +template <std::size_t N> |
| 22 | +std::vector<std::array<float, N>> Cast( |
| 23 | + std::vector<std::array<std::byte, N>> const& i) { |
| 24 | + std::vector<std::array<float, N>> c; |
| 25 | + std::transform( |
| 26 | + i.begin(), |
| 27 | + i.end(), |
| 28 | + std::back_inserter(c), |
| 29 | + [](std::array<std::byte, N> const& a) -> std::array<float, N> { |
| 30 | + return Cast<float>(a); |
| 31 | + }); |
| 32 | + return c; |
| 33 | +} |
| 34 | + |
| 35 | +int main(int argc, char** argv) { |
| 36 | + using ImageByte = std::array<std::byte, 28 * 28>; |
| 37 | + using ImageFloat = std::array<float, 28 * 28>; |
| 38 | + |
| 39 | + std::string fn_images_train = "train-images.idx3-ubyte"; |
| 40 | + std::string fn_images_test = "t10k-images.idx3-ubyte"; |
| 41 | + std::string fn_mnist_nns_gt = "mnist_nns_gt.bin"; |
| 42 | + |
| 43 | + if (!std::filesystem::exists(fn_images_train)) { |
| 44 | + std::cout << fn_images_train << " doesn't exist." << std::endl; |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + if (!std::filesystem::exists(fn_images_test)) { |
| 49 | + std::cout << fn_images_test << " doesn't exist." << std::endl; |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + std::vector<ImageFloat> images_train; |
| 54 | + { |
| 55 | + std::vector<ImageByte> images_train_u8; |
| 56 | + pico_tree::ReadMnistImages(fn_images_train, images_train_u8); |
| 57 | + images_train = Cast(images_train_u8); |
| 58 | + } |
| 59 | + |
| 60 | + std::vector<ImageFloat> images_test; |
| 61 | + { |
| 62 | + std::vector<ImageByte> images_test_u8; |
| 63 | + pico_tree::ReadMnistImages(fn_images_test, images_test_u8); |
| 64 | + images_test = Cast(images_test_u8); |
| 65 | + } |
| 66 | + |
| 67 | + std::size_t max_leaf_size_ex = 16; |
| 68 | + std::size_t max_leaf_size_rp = 128; |
| 69 | + // With 16 trees we can get a precision of around 85-90%. |
| 70 | + // With 32 trees we can get a precision of around 95-97%. |
| 71 | + std::size_t forest_size = 2; |
| 72 | + std::size_t count = images_test.size(); |
| 73 | + std::vector<pico_tree::Neighbor<int, float>> nns(count); |
| 74 | + |
| 75 | + if (!std::filesystem::exists(fn_images_train)) { |
| 76 | + auto kd_tree = [&images_train, &max_leaf_size_ex]() { |
| 77 | + ScopedTimer t0("kd_tree build"); |
| 78 | + return pico_tree::KdTree<std::reference_wrapper<std::vector<ImageFloat>>>( |
| 79 | + images_train, max_leaf_size_ex); |
| 80 | + }(); |
| 81 | + |
| 82 | + { |
| 83 | + ScopedTimer t1("kd_tree query"); |
| 84 | + for (std::size_t i = 0; i < nns.size(); ++i) { |
| 85 | + kd_tree.SearchNn(images_test[i], nns[i]); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + std::cout << "Writing " << fn_mnist_nns_gt << "." << std::endl; |
| 90 | + pico_tree::WriteBin(fn_mnist_nns_gt, nns); |
| 91 | + } else { |
| 92 | + std::cout << "Reading " << fn_mnist_nns_gt << "." << std::endl; |
| 93 | + pico_tree::ReadBin(fn_mnist_nns_gt, nns); |
| 94 | + } |
| 95 | + |
| 96 | + std::size_t equal = 0; |
| 97 | + |
| 98 | + { |
| 99 | + auto rkd_tree = [&images_train, &max_leaf_size_rp, &forest_size]() { |
| 100 | + ScopedTimer t0("rkd_tree build"); |
| 101 | + return pico_tree::RKdTree< |
| 102 | + std::reference_wrapper<std::vector<ImageFloat>>>( |
| 103 | + images_train, max_leaf_size_rp, forest_size); |
| 104 | + }(); |
| 105 | + |
| 106 | + ScopedTimer t1("rkd_tree query"); |
| 107 | + pico_tree::Neighbor<int, float> nn; |
| 108 | + for (std::size_t i = 0; i < nns.size(); ++i) { |
| 109 | + rkd_tree.SearchNn(images_test[i], nn); |
| 110 | + |
| 111 | + if (nns[i].index == nn.index) { |
| 112 | + ++equal; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + std::cout << "Precision: " |
| 118 | + << (static_cast<float>(equal) / static_cast<float>(count)) |
| 119 | + << std::endl; |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
0 commit comments