LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
SemanticDecoder Class Reference
Inheritance diagram for SemanticDecoder:
DecoderToolBase

Public Member Functions

 SemanticDecoder (const fhicl::ParameterSet &pset)
 Constructor. More...
 
virtual ~SemanticDecoder () noexcept=default
 Virtual Destructor. More...
 
void declareProducts (art::ProducesCollector &collector) override
 declareProducts function More...
 
void writeEmptyToEvent (art::Event &e, const vector< vector< size_t >> &idsmap) override
 writeEmptyToEvent function More...
 
void writeToEvent (art::Event &e, const vector< vector< size_t >> &idsmap, const vector< NuGraphOutput > &infer_output) override
 Decoder function. More...
 
void printVector (const std::vector< float > &vec)
 
template<typename T , size_t N>
void softmax (std::array< T, N > &arr)
 
void setDebugAndPlanes (bool d, vector< std::string > &p)
 

Protected Attributes

bool debug
 
vector< std::string > planes
 
std::string instancename
 
std::string outputname
 

Private Attributes

std::vector< std::string > categories
 
art::InputTag hitInput
 

Detailed Description

Definition at line 10 of file SemanticDecoder_tool.cc.

Constructor & Destructor Documentation

SemanticDecoder::SemanticDecoder ( const fhicl::ParameterSet pset)

Constructor.

Parameters
pset

Definition at line 57 of file SemanticDecoder_tool.cc.

References fhicl::ParameterSet::get(), and hitInput.

58  : DecoderToolBase(p)
59  , categories{p.get<std::vector<std::string>>("categories")}
60  , hitInput{p.get<art::InputTag>("hitInput")}
61 {}
art::InputTag hitInput
DecoderToolBase(fhicl::ParameterSet const &p)
Construcutor.
std::vector< std::string > categories
virtual SemanticDecoder::~SemanticDecoder ( )
virtualdefaultnoexcept

Virtual Destructor.

Member Function Documentation

void SemanticDecoder::declareProducts ( art::ProducesCollector collector)
inlineoverridevirtual

declareProducts function

Parameters
art::ProducesCollector

Implements DecoderToolBase.

Definition at line 30 of file SemanticDecoder_tool.cc.

References e, DecoderToolBase::instancename, art::ProducesCollector::produces(), lar::dump::vector(), writeEmptyToEvent(), and writeToEvent().

31  {
32  collector.produces<vector<FeatureVector<5>>>(instancename);
34  }
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
std::string instancename
void DecoderToolBase::printVector ( const std::vector< float > &  vec)
inlineinherited

Definition at line 69 of file DecoderToolBase.h.

Referenced by FilterDecoder::writeToEvent(), and writeToEvent().

70  {
71  for (size_t i = 0; i < vec.size(); ++i) {
72  std::cout << vec[i];
73  // Print space unless it's the last element
74  if (i != vec.size() - 1) { std::cout << " "; }
75  }
76  std::cout << std::endl;
77  std::cout << std::endl;
78  }
void DecoderToolBase::setDebugAndPlanes ( bool  d,
vector< std::string > &  p 
)
inlineinherited

Definition at line 98 of file DecoderToolBase.h.

References d, and tca::debug.

99  {
100  debug = d;
101  planes = p;
102  }
vector< std::string > planes
Float_t d
Definition: plot.C:235
template<typename T , size_t N>
void DecoderToolBase::softmax ( std::array< T, N > &  arr)
inlineinherited

Definition at line 81 of file DecoderToolBase.h.

References sum.

Referenced by writeToEvent().

82  {
83  T m = -std::numeric_limits<T>::max();
84  for (size_t i = 0; i < arr.size(); i++) {
85  if (arr[i] > m) { m = arr[i]; }
86  }
87  T sum = 0.0;
88  for (size_t i = 0; i < arr.size(); i++) {
89  sum += expf(arr[i] - m);
90  }
91  T offset = m + logf(sum);
92  for (size_t i = 0; i < arr.size(); i++) {
93  arr[i] = expf(arr[i] - offset);
94  }
95  return;
96  }
Double_t sum
Definition: plot.C:31
void SemanticDecoder::writeEmptyToEvent ( art::Event e,
const vector< vector< size_t >> &  idsmap 
)
overridevirtual

writeEmptyToEvent function

Parameters
art::Eventevent record

Implements DecoderToolBase.

Definition at line 63 of file SemanticDecoder_tool.cc.

References categories, trkf::fill(), hitInput, DecoderToolBase::instancename, art::InputTag::label(), art::Event::put(), and util::size().

Referenced by declareProducts().

64 {
65  //
66  auto semtdes = std::make_unique<MVADescription<5>>(hitInput.label(), instancename, categories);
67  e.put(std::move(semtdes), instancename);
68  //
69  size_t size = 0;
70  for (auto& v : idsmap)
71  size += v.size();
72  std::array<float, 5> arr;
73  std::fill(arr.begin(), arr.end(), -1.);
74  auto semtcol = std::make_unique<vector<FeatureVector<5>>>(size, FeatureVector<5>(arr));
75  e.put(std::move(semtcol), instancename);
76  //
77 }
art::InputTag hitInput
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
std::string const & label() const noexcept
Definition: InputTag.cc:79
std::vector< std::string > categories
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
std::string instancename
void SemanticDecoder::writeToEvent ( art::Event e,
const vector< vector< size_t >> &  idsmap,
const vector< NuGraphOutput > &  infer_output 
)
overridevirtual

Decoder function.

Parameters
art::Eventevent record for decoder

Implements DecoderToolBase.

Definition at line 79 of file SemanticDecoder_tool.cc.

References categories, DecoderToolBase::debug, DEFINE_ART_CLASS_TOOL, trkf::fill(), hitInput, DecoderToolBase::instancename, art::InputTag::label(), DecoderToolBase::outputname, DecoderToolBase::planes, DecoderToolBase::printVector(), art::Event::put(), util::size(), and DecoderToolBase::softmax().

Referenced by declareProducts().

82 {
83  //
84  auto semtdes = std::make_unique<MVADescription<5>>(hitInput.label(), instancename, categories);
85  e.put(std::move(semtdes), instancename);
86  //
87  size_t size = 0;
88  for (auto& v : idsmap)
89  size += v.size();
90  std::array<float, 5> arr;
91  std::fill(arr.begin(), arr.end(), -1.);
92  auto semtcol = std::make_unique<vector<FeatureVector<5>>>(size, FeatureVector<5>(arr));
93 
94  size_t n_cols = categories.size();
95  for (size_t p = 0; p < planes.size(); p++) {
96  //
97  const std::vector<float>* x_semantic_data = 0;
98  for (auto& io : infer_output) {
99  if (io.output_name == outputname + planes[p]) x_semantic_data = &io.output_vec;
100  }
101  if (debug) {
102  std::cout << outputname + planes[p] << std::endl;
103  printVector(*x_semantic_data);
104  }
105 
106  torch::TensorOptions options = torch::TensorOptions().dtype(torch::kFloat32);
107  size_t n_rows = x_semantic_data->size() / n_cols;
108  const torch::Tensor s =
109  torch::from_blob(const_cast<float*>(x_semantic_data->data()),
110  {static_cast<int64_t>(n_rows), static_cast<int64_t>(n_cols)},
111  options);
112 
113  for (int i = 0; i < s.sizes()[0]; ++i) {
114  size_t idx = idsmap[p][i];
115  std::array<float, 5> input;
116  for (size_t j = 0; j < n_cols; ++j)
117  input[j] = s[i][j].item<float>();
118  softmax(input);
119  FeatureVector<5> semt = FeatureVector<5>(input);
120  (*semtcol)[idx] = semt;
121  }
122  }
123  e.put(std::move(semtcol), instancename);
124 }
vector< std::string > planes
art::InputTag hitInput
std::string outputname
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
std::string const & label() const noexcept
Definition: InputTag.cc:79
std::vector< std::string > categories
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
void printVector(const std::vector< float > &vec)
std::string instancename
void softmax(std::array< T, N > &arr)

Member Data Documentation

std::vector<std::string> SemanticDecoder::categories
private

Definition at line 53 of file SemanticDecoder_tool.cc.

Referenced by writeEmptyToEvent(), and writeToEvent().

bool DecoderToolBase::debug
protectedinherited
art::InputTag SemanticDecoder::hitInput
private

Definition at line 54 of file SemanticDecoder_tool.cc.

Referenced by SemanticDecoder(), writeEmptyToEvent(), and writeToEvent().

std::string DecoderToolBase::outputname
protectedinherited

Definition at line 108 of file DecoderToolBase.h.

Referenced by FilterDecoder::writeToEvent(), and writeToEvent().

vector<std::string> DecoderToolBase::planes
protectedinherited

Definition at line 106 of file DecoderToolBase.h.

Referenced by FilterDecoder::writeToEvent(), and writeToEvent().


The documentation for this class was generated from the following file: