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
#ifndef CAFFE2_OPERATORS_CLIP_TENSOR_OP_H_
#define CAFFE2_OPERATORS_CLIP_TENSOR_OP_H_
 
#include <vector>
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/tensor.h"
#include "caffe2/utils/math.h"
 
namespace caffe2 {
 
template <typename Context>
class ClipTensorByScalingOp final : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
 
  ClipTensorByScalingOp(const OperatorDef& operator_def, Workspace* ws)
      : Operator<Context>(operator_def, ws) {
    threshold_ = this->template GetSingleArgument<float>("threshold", 0.0);
    CAFFE_ENFORCE_GT(threshold_, 0, "Threshold must be greater than 0");
  }
 
  bool RunOnDevice() override {
    const auto& input_tensor = Input(0);
    CAFFE_ENFORCE_GT(input_tensor.numel(), 0);
    const auto& val = Input(1);
    CAFFE_ENFORCE_EQ(val.numel(), 1);
 
    const auto* input_tensor_data = input_tensor.template data<float>();
    const auto* val_data = val.template data<float>();
 
    auto* clipped = Output(0, input_tensor.sizes(), at::dtype<float>());
    float* clipped_tensor_data = clipped->template mutable_data<float>();
 
    if (InputSize() > 2) {
      const auto& additional_threshold = Input(2);
      CAFFE_ENFORCE_EQ(additional_threshold.numel(), 1);
 
      threshold_ *= *(additional_threshold.template data<float>());
    }
 
    if (*val_data > threshold_) {
      float ratio = threshold_ / *val_data;
 
      math::Scale<float, float, Context>(
          clipped->numel(),
          ratio,
          input_tensor_data,
          clipped_tensor_data,
          &context_);
    } else {
      if (input_tensor_data != clipped_tensor_data) {
        clipped->CopyFrom(input_tensor, /*async*/ true);
      }
    }
 
    return true;
  }
 
 private:
  float threshold_;
};
 
} // namespace caffe2
 
#endif // CAFFE2_OPERATORS_CLIP_TENSOR_OP_H_