OpenGM  2.3.x
Discrete Graphical Model Library
unary_loss_function.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_UNARY_LOSS_FUNCTION
3 #define OPENGM_UNARY_LOSS_FUNCTION
4 
6 
7 namespace opengm {
8 
9 
10 
11 
12 
13 
14 
15 
16 
18 
22 template<class T,class I, class L>
24 : public FunctionBase<UnaryLossFunction<T,I,L>, T,I,L>
25 {
26 public:
27 
28  typedef T ValueType;
29  typedef T value_type;
30  typedef I IndexType;
31  typedef L LabelType;
32 
33 
34  enum LossType{
39  L1Loss = 4,
40  L2Loss = 5
41  };
42 
45  };
46 
47 
48 
49 
51  const LabelType numberOfLabels,
52  const LabelType gtLabel,
53  const LossType lossType,
54  const ValueType multiplier,
55  const SharedMultiplers & sharedMultiplers,
56  const bool owner
57  );
58  template<class Iterator> ValueType operator()(Iterator begin) const;
59  IndexType shape(const IndexType) const;
60  IndexType dimension() const;
61  IndexType size() const;
62 
63 private:
64  LabelType numberOfLabels_;
65  LabelType gtLabel_;
66  LossType lossType_;
67  ValueType multiplier_;
68  const SharedMultiplers * sharedMultiplers_;
69  bool owner_;
70 };
71 
72 template<class T,class I, class L>
73 inline
75  const LabelType numberOfLabels,
76  const LabelType gtLabel,
77  const LossType lossType,
78  const ValueType multiplier,
79  const SharedMultiplers & sharedMultiplers,
80  const bool owner
81 )
82 : numberOfLabels_(numberOfLabels),
83  gtLabel_(gtLabel),
84  lossType_(lossType),
85  multiplier_(multiplier),
86  sharedMultiplers_(&sharedMultiplers),
87  owner_(owner)
88 {
89 
90 }
91 
92 template<class T,class I, class L>
93 template<class Iterator>
96 (
97  Iterator begin
98 ) const {
99 
100  const LabelType l = *begin;
101  const ValueType isDifferent = (l != gtLabel_ ? 1.0 : 0.0);
102 
103  switch(lossType_){
104  case HammingLoss:{
105  return static_cast<ValueType>(-1.0) * multiplier_ * isDifferent;
106  }
107  case LabelVectorConf:{
108  return multiplier_ * isDifferent * sharedMultiplers_->labelMult_(l);
109  }
110  case LabelVectorGt:{
111  return multiplier_ * isDifferent * sharedMultiplers_->labelMult_(gtLabel_);
112  }
113  case LabelMatrix:{
114  return multiplier_ * isDifferent * sharedMultiplers_->labelMult_(l, gtLabel_);
115  }
116  case L1Loss:{
117  return multiplier_ * static_cast<ValueType>(std::abs(int(l)-int(gtLabel_)));
118  }
119  case L2Loss:{
120  return multiplier_ * std::pow(int(l)-int(gtLabel_),2);
121  }
122  default :{
123  throw RuntimeError("wrong loss type");
124  }
125  }
126 }
127 
128 template<class T,class I, class L>
131 (
132  const typename UnaryLossFunction<T,I,L>::IndexType index
133 ) const{
134  return numberOfLabels_;
135 }
136 
137 template<class T,class I, class L>
140  return 1;
141 }
142 
143 template<class T,class I, class L>
146  return numberOfLabels_;
147 }
148 
149 } // namespace opengm
150 
151 #endif // #ifndef OPENGM_UNARY_LOSS_FUNCTION
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
UnaryLossFunction(const LabelType numberOfLabels, const LabelType gtLabel, const LossType lossType, const ValueType multiplier, const SharedMultiplers &sharedMultiplers, const bool owner)
UnaryLossFunction convert semi-ring in a lazy fashion.
ValueType operator()(Iterator begin) const
IndexType shape(const IndexType) const
OpenGM runtime error.
Definition: opengm.hxx:100
T abs(const T &x)
Definition: opengm.hxx:111