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
#pragma once
 
#include "caffe2/core/context.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/operators/filler_op.h"
#include "caffe2/utils/cast.h"
#include "caffe2/utils/math.h"
 
namespace caffe2 {
 
template <class Context>
class GivenTensorByteStringToUInt8FillOp final : public FillerOp<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  explicit GivenTensorByteStringToUInt8FillOp(const OperatorDef& operator_def, Workspace* ws)
      : FillerOp<Context>(operator_def, ws) {
    const ArgumentHelper helper(operator_def);
    if (!helper.HasArgument("dtype")) {
      Extract();
    } else {
      auto dtype = cast::GetCastDataType(helper, "dtype");
      switch (dtype) {
        case TensorProto_DataType_STRING:
          Extract();
          break;
        case TensorProto_DataType_UNDEFINED:
          CAFFE_THROW("Cannot have undefined 'dtype' argument");
        default:
          CAFFE_THROW("Unexpected 'dtype' argument value: ", dtype);
      }
    }
  }
 
  bool Fill(Tensor* output) override {
    DCHECK_EQ(output->numel(), values_.numel())
        << "output size: " << output->numel()
        << " given size: " << values_.numel();
    auto* data = output->template mutable_data<uint8_t>();
    const uint8_t* values_data = values_.template data<uint8_t>();
    if (output->numel()) {
      context_.template CopySameDevice<uint8_t>(
          output->numel(), values_data, data);
    }
    return true;
  }
 
 private:
  void Extract() {
    auto source_values = this->template GetRepeatedArgument<string>("values");
    DCHECK_EQ(source_values.size(), 1)
        << "expected size: 1 "
        << " given size: " << source_values.size();
 
    auto str = source_values[0];
    ReinitializeTensor(&values_, {static_cast<int64_t>(str.size())}, at::dtype<uint8_t>().device(CPU));
    uint8_t* values_data = values_.template mutable_data<uint8_t>();
    for (int i = 0; i < str.size(); i++) {
      values_data[i] = static_cast<uint8_t>(str[i]);
    }
}
 
Tensor values_;
};
} // namespace caffe2