EqualityFilter.cpp
Go to the documentation of this file.
1 #include "EqualityFilter.h"
2 #include <list>
3 
4 #include <IceUtil/Time.h>
7 
8 
10 {
11  bool
13  {
14  auto start = std::chrono::high_resolution_clock::now();
15 
16  int num_instances = 0;
17  std::vector<armarx::aron::data::NDArrayPtr> images_snapshot;
18  std::vector<armarx::aron::data::FloatPtr> floats_snapshot;
19  std::vector<float> distances;
20 
21  e.forEachInstance([&num_instances, &images_snapshot, &floats_snapshot](armem::wm::EntityInstance& i){
23  for(auto key: data->getAllKeys()){
24  aron::data::Descriptor img_desc;
25  try{
26  auto d = data->at(key);
27 ; img_desc = data->at(key)->getDescriptor();
28  } catch (...){
29  ARMARX_INFO << "Problem with accessing image description";
31  }
32  if (img_desc == aron::data::Descriptor::NDARRAY){
33  auto img_nd = aron::data::NDArray::DynamicCastAndCheck(data->at(key));
34  images_snapshot.insert(images_snapshot.end(), img_nd);
35  num_instances++;
36  } else if (img_desc == aron::data::Descriptor::FLOAT){
37  auto fl = aron::data::Float::DynamicCastAndCheck(data->at(key));
38  floats_snapshot.push_back(fl);
39  num_instances++;
40  } else {
41  ARMARX_INFO << "data-type not yet supported. \n"
42  << "Only ndarray and float data types are supported for equality filters yet.";
43  }
44  }
45  });
46 
47  if(images.size() < 2){
48  ARMARX_INFO << "Adding first images, because nothing to compare";
49  images.push_back(images_snapshot);
50  this->stats.accepted += 1;
51  auto end = std::chrono::high_resolution_clock::now();
52  stats.end_time = end;
53  stats.additional_time += (end - start);
54  return true;
55  } else if(images.size() < max_images){
56  ARMARX_INFO << "Not enough elements yet to do full comparison of last " << max_images
57  << " elements";
58  images.push_back(images_snapshot);
59  this->stats.accepted += 1;
60  auto end = std::chrono::high_resolution_clock::now();
61  stats.end_time = end;
62  stats.additional_time += (end - start);
63  return true;
64  }
65 
66 
67  std::vector<armarx::aron::data::NDArrayPtr> lastCommittedImages;
68  int sizeOfCommited = 0;
69  for(int i = 0; i < max_images; i++){
70  std::vector<armarx::aron::data::NDArrayPtr> lastCommitImages = images.at(images.size() - i - 1);
71  sizeOfCommited = lastCommitImages.size();
72  for(int j = 0; j < lastCommitImages.size(); j++){
73  lastCommittedImages.push_back(lastCommitImages.at(j));
74  }
75  // we just concatenate all images (instances)
76  }
77 
78  ARMARX_CHECK(sizeOfCommited == images_snapshot.size()); //make sure we have enough instances
79 
80  for(int i= 0; i < images_snapshot.size(); i++){
81  armarx::aron::data::NDArrayPtr new_image = images_snapshot.at(i);
82  std::vector<armarx::aron::data::NDArrayPtr> commited_images;
83  for(int j = 0; j < max_images; j++){
84  int index = i + 2*j;
85  auto image = lastCommittedImages.at(index);
86  commited_images.emplace_back(image);
87  }
88 
89  float distance = aron::similarity::NDArraySimilarity::calculate_similarity_multi(commited_images, new_image, this->similarity_type);
90 
91  distances.insert(distances.end(), distance);
92  }
93 
94  //check for criterion:
95  float sum_distances = 0;
96  float max_distance = 0;
97  for(auto d: distances){
98  sum_distances += d;
99  if(d > max_distance){
100  max_distance = d;
101  }
102  }
103 
104  bool max = true; //set true if only maximum distance value is important and false if sum of distances is important
105  bool accept = false;
106 
107  if(max){
108  accept = (max_distance > this->threshold);
109  } else {
110  accept = (sum_distances > this->threshold);
111  }
112 
113  if(accept){
114  images.pop_front(); //delete first element
115  images.push_back(images_snapshot);
116  this->stats.accepted += 1;
117  auto end = std::chrono::high_resolution_clock::now();
118  stats.additional_time += (end - start);
119  stats.end_time = end;
120  return true;
121  } else {
122  this->stats.rejected += 1;
123  auto end = std::chrono::high_resolution_clock::now();
124  stats.additional_time += (end - start);
125  stats.end_time = end;
126  return false;
127  }
128  }
129 
130  void SnapshotSimilarityFilter::configure(const nlohmann::json &json)
131  {
132  if (json.find(PARAM_THRESHOLD) != json.end())
133  {
134  threshold = json.at(PARAM_THRESHOLD);
135  ARMARX_INFO << VAROUT(threshold);
136  stats.additional_info += "Threshold-Parameter: ";
137  stats.additional_info += std::to_string(threshold);
138  }
139  if(json.find(PARAM_SIM_MEASURE) != json.end()){
140  std::string type_string = json.at(PARAM_SIM_MEASURE);
141  if(type_string == "MSE"){
142  this->similarity_type = aron::similarity::NDArraySimilarity::Type::MSE;
143  this->float_similarity_type = aron::similarity::FloatSimilarity::Type::MSE;
145  } else if (type_string == "MAE"){
146  this->similarity_type = aron::similarity::NDArraySimilarity::Type::MAE;
147  this->float_similarity_type = aron::similarity::FloatSimilarity::Type::MAE;
149  } else if (type_string == "Chernoff"){
151  this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
153  } else if (type_string == "Cosine"){
155  this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
157  } else {
158  ARMARX_WARNING << "Undefined similarity measure detected in JSON file";
160  this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
161  }
162  ARMARX_INFO << VAROUT(this->similarity_type);
163  }
164  if(json.find(PARAM_MAX_OBJECTS) != json.end()){
165  max_images = json.at(PARAM_MAX_OBJECTS);
166  ARMARX_INFO << VAROUT(max_images);
167  stats.number_of_compared_objects = max_images;
168  }
169 
170  stats.start_time = std::chrono::high_resolution_clock::now();
171  }
172 
174  {
175  return stats;
176  }
177 
179  {
180  return this->NAME;
181  }
182 
183 } // namespace armarx::armem::server::ltm::filter
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::NAME
static const constexpr char * NAME
Definition: EqualityFilter.h:21
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::getFilterStatistics
FilterStatistics getFilterStatistics() override
Definition: EqualityFilter.cpp:173
armarx::aron::data::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::similarity::NDArraySimilarity::CHERNOFF
@ CHERNOFF
Definition: NDArraySimilarity.h:11
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::end_time
std::chrono::high_resolution_clock::time_point end_time
Definition: Filter.h:42
EqualityFilter.h
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::PARAM_THRESHOLD
static const constexpr char * PARAM_THRESHOLD
Definition: EqualityFilter.h:22
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::base::EntitySnapshotBase::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: EntitySnapshotBase.h:178
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::rejected
double rejected
Definition: Filter.h:37
armarx::aron::data::Descriptor::FLOAT
@ FLOAT
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:135
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::aron::similarity::FloatSimilarity::MAE
@ MAE
Definition: FloatSimilarity.h:10
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::number_of_compared_objects
int number_of_compared_objects
Definition: Filter.h:43
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::start_time
std::chrono::high_resolution_clock::time_point start_time
Definition: Filter.h:41
Float.h
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::getName
std::string getName() override
Definition: EqualityFilter.cpp:178
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::similarity::NDArraySimilarity::COSINE
@ COSINE
Definition: NDArraySimilarity.h:12
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::configure
void configure(const nlohmann::json &json) override
Definition: EqualityFilter.cpp:130
armarx::aron::similarity::FloatSimilarity::MSE
@ MSE
Definition: FloatSimilarity.h:9
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_info
std::string additional_info
Definition: Filter.h:39
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::similarity_type
aron::similarity::NDArraySimilarity::Type similarity_type
Definition: Filter.h:40
armarx::aron::similarity::NDArraySimilarity::calculate_similarity_multi
double calculate_similarity_multi(std::vector< armarx::aron::data::NDArrayPtr > images, armarx::aron::data::NDArrayPtr p, Type type)
calculate_similarity_multi compares the image p with all images from the images vector,...
Definition: NDArraySimilarity.cpp:46
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
armarx::armem::server::ltm::processor::filter
Definition: EqualityFilter.cpp:9
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics
Definition: Filter.h:35
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::accept
virtual bool accept(const armem::wm::EntitySnapshot &e) override
Definition: EqualityFilter.cpp:12
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::PARAM_SIM_MEASURE
static const constexpr char * PARAM_SIM_MEASURE
Definition: EqualityFilter.h:23
armarx::armem::server::ltm::processor::filter::SnapshotSimilarityFilter::PARAM_MAX_OBJECTS
static const constexpr char * PARAM_MAX_OBJECTS
Definition: EqualityFilter.h:24
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::accepted
double accepted
Definition: Filter.h:36
NDArray.h
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_time
std::chrono::duration< double > additional_time
Definition: Filter.h:38