|
| 1 | +#include "tests/accuracy/datasets/cifar10.h" |
| 2 | + |
| 3 | +#include "torch/torch.h" |
| 4 | +#include "torch/data/example.h" |
| 5 | +#include "torch/types.h" |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <cstddef> |
| 9 | +#include <fstream> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | +#include <utility> |
| 13 | +#include <sstream> |
| 14 | +#include <memory> |
| 15 | + |
| 16 | +namespace datasets { |
| 17 | +namespace { |
| 18 | +constexpr const char* kTrainFilenamePrefix = "data_batch_"; |
| 19 | +constexpr const uint32_t kNumTrainFiles = 5; |
| 20 | +constexpr const char* kTestFilename = "test_batch.bin"; |
| 21 | +constexpr const size_t kLabelSize = 1; // B |
| 22 | +constexpr const size_t kImageSize = 3072; // B |
| 23 | +constexpr const size_t kImageDim = 32; |
| 24 | +constexpr const size_t kImageChannels = 3; |
| 25 | +constexpr const size_t kBatchSize = 10000; |
| 26 | + |
| 27 | +std::pair<torch::Tensor, torch::Tensor> read_batch(const std::string& path) { |
| 28 | + std::ifstream batch; |
| 29 | + batch.open(path, std::ios::in|std::ios::binary|std::ios::ate); |
| 30 | + |
| 31 | + auto file_size = batch.tellg(); |
| 32 | + std::unique_ptr<char[]> buf(new char[file_size]); |
| 33 | + |
| 34 | + batch.seekg(0, std::ios::beg); |
| 35 | + batch.read(buf.get(), file_size); |
| 36 | + batch.close(); |
| 37 | + |
| 38 | + std::vector<uint8_t> labels; |
| 39 | + std::vector<torch::Tensor> images; |
| 40 | + labels.reserve(kBatchSize); |
| 41 | + images.reserve(kBatchSize); |
| 42 | + |
| 43 | + for (size_t i = 0; i < kBatchSize; i++) { |
| 44 | + uint8_t label = buf[i * (kImageSize + kLabelSize)]; |
| 45 | + std::vector<uint8_t> image; |
| 46 | + image.reserve(kImageSize); |
| 47 | + std::copy(&buf[i * (kImageSize + kLabelSize) + 1], &buf[i * (kImageSize + kLabelSize) + kImageSize], std::back_inserter(image)); |
| 48 | + labels.push_back(label); |
| 49 | + auto image_tensor = torch::from_blob(image.data(), |
| 50 | + {kImageChannels, kImageDim, kImageDim}, |
| 51 | + torch::TensorOptions().dtype(torch::kU8)).to(torch::kF32); |
| 52 | + images.push_back(image_tensor); |
| 53 | + } |
| 54 | + |
| 55 | + auto labels_tensor = torch::from_blob(labels.data(), |
| 56 | + {kBatchSize}, |
| 57 | + torch::TensorOptions().dtype(torch::kU8)).to(torch::kF32); |
| 58 | + assert(labels_tensor.size(0) == kBatchSize); |
| 59 | + |
| 60 | + auto images_tensor = torch::stack(images); |
| 61 | + assert(images_tensor.size(0) == kBatchSize); |
| 62 | + |
| 63 | + return std::make_pair(images_tensor, labels_tensor); |
| 64 | +} |
| 65 | + |
| 66 | +std::pair<torch::Tensor, torch::Tensor> read_train_data(const std::string& root) { |
| 67 | + std::vector<torch::Tensor> images, targets; |
| 68 | + for(uint32_t i = 1; i <= 5; i++) { |
| 69 | + std::stringstream ss; |
| 70 | + ss << root << '/' << kTrainFilenamePrefix << i << ".bin"; |
| 71 | + auto batch = read_batch(ss.str()); |
| 72 | + images.push_back(batch.first); |
| 73 | + targets.push_back(batch.second); |
| 74 | + } |
| 75 | + |
| 76 | + torch::Tensor image_tensor = std::accumulate(++images.begin(), images.end(), *images.begin(), [&](torch::Tensor a, torch::Tensor b) {return torch::cat({a, b}, 0);}); |
| 77 | + torch::Tensor target_tensor = std::accumulate(++targets.begin(), targets.end(), *targets.begin(), [&](torch::Tensor a, torch::Tensor b) {return torch::cat({a, b}, 0);}); |
| 78 | + |
| 79 | + return std::make_pair(image_tensor, target_tensor); |
| 80 | +} |
| 81 | + |
| 82 | +std::pair<torch::Tensor, torch::Tensor> read_test_data(const std::string& root) { |
| 83 | + std::stringstream ss; |
| 84 | + ss << root << '/' << kTestFilename; |
| 85 | + return read_batch(ss.str()); |
| 86 | +} |
| 87 | +} |
| 88 | + |
| 89 | +CIFAR10::CIFAR10(const std::string& root, Mode mode) |
| 90 | + : mode_(mode) { |
| 91 | + |
| 92 | + std::pair<torch::Tensor, torch::Tensor> data; |
| 93 | + if (mode_ == Mode::kTrain) { |
| 94 | + data = read_train_data(root); |
| 95 | + } else { |
| 96 | + data = read_test_data(root); |
| 97 | + } |
| 98 | + |
| 99 | + images_ = std::move(data.first); |
| 100 | + targets_ = std::move(data.second); |
| 101 | + assert(images_.sizes()[0] == images_.sizes()[0]); |
| 102 | +} |
| 103 | + |
| 104 | +torch::data::Example<> CIFAR10::get(size_t index) { |
| 105 | + return {images_[index], targets_[index]}; |
| 106 | +} |
| 107 | + |
| 108 | +c10::optional<size_t> CIFAR10::size() const { |
| 109 | + return images_.size(0); |
| 110 | +} |
| 111 | + |
| 112 | +bool CIFAR10::is_train() const noexcept { |
| 113 | + return mode_ == Mode::kTrain; |
| 114 | +} |
| 115 | + |
| 116 | +const torch::Tensor& CIFAR10::images() const { |
| 117 | + return images_; |
| 118 | +} |
| 119 | + |
| 120 | +const torch::Tensor& CIFAR10::targets() const { |
| 121 | + return targets_; |
| 122 | +} |
| 123 | + |
| 124 | +CIFAR10&& CIFAR10::use_subset(int64_t new_size) { |
| 125 | + assert(new_size <= images_.sizes()[0]); |
| 126 | + images_ = images_.slice(0, 0, new_size); |
| 127 | + targets_ = targets_.slice(0, 0, new_size); |
| 128 | + return std::move(*this); |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace datasets |
| 132 | + |
0 commit comments