reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-11 bdf3ad71583fb4ef100d3819ecdae8fd9f70083e
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
#pragma once
 
#include <torch/data/datasets/base.h>
#include <torch/data/example.h>
#include <torch/types.h>
 
#include <cstddef>
#include <vector>
 
namespace torch {
namespace data {
namespace datasets {
 
/// A dataset of tensors.
/// Stores a single tensor internally, which is then indexed inside `get()`.
struct TensorDataset : public Dataset<TensorDataset, TensorExample> {
  /// Creates a `TensorDataset` from a vector of tensors.
  explicit TensorDataset(const std::vector<Tensor>& tensors)
      : TensorDataset(torch::stack(tensors)) {}
 
  explicit TensorDataset(torch::Tensor tensor) : tensor(std::move(tensor)) {}
 
  /// Returns a single `TensorExample`.
  TensorExample get(size_t index) override {
    return tensor[index];
  }
 
  /// Returns the number of tensors in the dataset.
  optional<size_t> size() const override {
    return tensor.size(0);
  }
 
  Tensor tensor;
};
 
} // namespace datasets
} // namespace data
} // namespace torch