reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-17 f7c4a3cfd07adede3308f8d9d3d7315427d90a7c
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
 
#ifndef CAFFE2_OPERATORS_PREPEND_DIM_OP_H_
#define CAFFE2_OPERATORS_PREPEND_DIM_OP_H_
 
#include "caffe2/core/common_omp.h"
#include "caffe2/core/context.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
 
namespace caffe2 {
 
template <class Context>
class PrependDimOp : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  template <class... Args>
  explicit PrependDimOp(Args&&... args)
      : Operator<Context>(std::forward<Args>(args)...),
        dim_size_(this->template GetSingleArgument<int64_t>("dim_size", 0)) {
    CAFFE_ENFORCE_GT(
        dim_size_, 0, "Argument dim_size must be greater than zero.");
  }
 
  bool RunOnDevice() override {
    auto& input = Input(0);
    auto* output = Output(0);
 
    CAFFE_ENFORCE(input.dim() > 0, "Input must be at least 1D.");
    CAFFE_ENFORCE(
        input.size(0) % dim_size_ == 0,
        "First dimension must be multiple of prepend_dim. Current first dimension: ",
        input.size(0));
 
    vector<int64_t> actual_new_shape(input.dim() + 1);
    actual_new_shape[0] = dim_size_;
    actual_new_shape[1] = input.size(0) / dim_size_;
    for (int i = 1; i < input.sizes().size(); ++i) {
      actual_new_shape[i + 1] = input.size(i);
    }
    output->Resize(actual_new_shape);
 
    if (output != &input) {
      // If we are not doing in-place computation, a copy is needed.
      context_.CopyItemsSameDevice(
          input.dtype(),
          input.numel(),
          input.raw_data(),
          output->raw_mutable_data(input.dtype()));
    }
    return true;
  }
 
 private:
  int64_t dim_size_;
};
 
template <class Context>
class MergeDimOp : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  template <class... Args>
  explicit MergeDimOp(Args&&... args)
      : Operator<Context>(std::forward<Args>(args)...) {}
 
  bool RunOnDevice() override {
    auto& input = Input(0);
    auto* output = Output(0);
 
    CAFFE_ENFORCE(input.dim() > 1, "Input must be at least 2D.");
 
    vector<int64_t> actual_new_shape(input.dim() - 1);
    actual_new_shape[0] = input.size(0) * input.size(1);
    for (int i = 1; i < input.sizes().size() - 1; ++i) {
      actual_new_shape[i] = input.size(i + 1);
    }
    output->Resize(actual_new_shape);
 
    if (output != &input) {
      // If we are not doing in-place computation, a copy is needed.
      context_.CopyItemsSameDevice(
          input.dtype(),
          input.numel(),
          input.raw_data(),
          output->raw_mutable_data(input.dtype()));
    }
    return true;
  }
 
 private:
  int64_t dim_size_;
};
 
} // namespace caffe2
 
#endif // CAFFE2_OPERATORS_PREPEND_DIM_OP_H_