EqualityFilter.cpp
Go to the documentation of this file.
1#include "EqualityFilter.h"
2
3#include <list>
4
5#include <IceUtil/Time.h>
6
9
11{
12 bool
14 {
15 // Thread safety: lock the filter mutex for all mutable state access
16 std::lock_guard<std::mutex> lock(filterMutex_);
17
18 auto start = std::chrono::high_resolution_clock::now();
19
20 int num_instances = 0;
21 std::vector<armarx::aron::data::NDArrayPtr> images_snapshot;
22 std::vector<armarx::aron::data::FloatPtr> floats_snapshot;
23 std::vector<float> distances;
24
26 [&num_instances, &images_snapshot, &floats_snapshot](armem::wm::EntityInstance& i)
27 {
29 for (auto key : data->getAllKeys())
30 {
32 try
33 {
34 auto d = data->at(key);
35 ;
36 img_desc = data->at(key)->getDescriptor();
37 }
38 catch (...)
39 {
40 ARMARX_INFO << "Problem with accessing image description";
42 }
43 if (img_desc == aron::data::Descriptor::NDARRAY)
44 {
45 auto img_nd = aron::data::NDArray::DynamicCastAndCheck(data->at(key));
46 images_snapshot.insert(images_snapshot.end(), img_nd);
47 num_instances++;
48 }
49 else if (img_desc == aron::data::Descriptor::FLOAT)
50 {
52 floats_snapshot.push_back(fl);
53 num_instances++;
54 }
55 else
56 {
57 ARMARX_INFO << "data-type not yet supported. \n"
58 << "Only ndarray and float data types are supported for "
59 "equality filters yet.";
60 }
61 }
62 });
63
64 if (images.size() < 2)
65 {
66 ARMARX_INFO << "Adding first images, because nothing to compare";
67 images.push_back(images_snapshot);
68 this->stats.accepted += 1;
69 auto end = std::chrono::high_resolution_clock::now();
70 stats.end_time = end;
71 stats.additional_time += (end - start);
72 return true;
73 }
74 else if (images.size() < max_images)
75 {
76 ARMARX_INFO << "Not enough elements yet to do full comparison of last " << max_images
77 << " elements";
78 images.push_back(images_snapshot);
79 this->stats.accepted += 1;
80 auto end = std::chrono::high_resolution_clock::now();
81 stats.end_time = end;
82 stats.additional_time += (end - start);
83 return true;
84 }
85
86
87 std::vector<armarx::aron::data::NDArrayPtr> lastCommittedImages;
88 int sizeOfCommited = 0;
89 for (int i = 0; i < max_images; i++)
90 {
91 std::vector<armarx::aron::data::NDArrayPtr> lastCommitImages =
92 images.at(images.size() - i - 1);
93 sizeOfCommited = lastCommitImages.size();
94 for (int j = 0; j < lastCommitImages.size(); j++)
95 {
96 lastCommittedImages.push_back(lastCommitImages.at(j));
97 }
98 // we just concatenate all images (instances)
99 }
100
101 ARMARX_CHECK(sizeOfCommited == images_snapshot.size()); //make sure we have enough instances
102
103 for (int i = 0; i < images_snapshot.size(); i++)
104 {
105 armarx::aron::data::NDArrayPtr new_image = images_snapshot.at(i);
106 std::vector<armarx::aron::data::NDArrayPtr> commited_images;
107 for (int j = 0; j < max_images; j++)
108 {
109 int index = i + 2 * j;
110 auto image = lastCommittedImages.at(index);
111 commited_images.emplace_back(image);
112 }
113
115 commited_images, new_image, this->similarity_type);
116
117 distances.insert(distances.end(), distance);
118 }
119
120 //check for criterion:
121 float sum_distances = 0;
122 float max_distance = 0;
123 for (auto d : distances)
124 {
125 sum_distances += d;
126 if (d > max_distance)
127 {
128 max_distance = d;
129 }
130 }
131
132 bool max =
133 true; //set true if only maximum distance value is important and false if sum of distances is important
134 bool accept = false;
135
136 if (max)
137 {
138 accept = (max_distance > this->threshold);
139 }
140 else
141 {
142 accept = (sum_distances > this->threshold);
143 }
144
145 if (accept)
146 {
147 images.pop_front(); //delete first element
148 images.push_back(images_snapshot);
149 this->stats.accepted += 1;
150 auto end = std::chrono::high_resolution_clock::now();
151 stats.additional_time += (end - start);
152 stats.end_time = end;
153 return true;
154 }
155 else
156 {
157 this->stats.rejected += 1;
158 auto end = std::chrono::high_resolution_clock::now();
159 stats.additional_time += (end - start);
160 stats.end_time = end;
161 return false;
162 }
163 }
164
165 void
166 SnapshotSimilarityFilter::configure(const nlohmann::json& json)
167 {
168 std::lock_guard<std::mutex> lock(filterMutex_);
169 if (json.find(PARAM_THRESHOLD) != json.end())
170 {
171 threshold = json.at(PARAM_THRESHOLD);
172 ARMARX_INFO << VAROUT(threshold);
173 stats.additional_info += "Threshold-Parameter: ";
174 stats.additional_info += std::to_string(threshold);
175 }
176 if (json.find(PARAM_SIM_MEASURE) != json.end())
177 {
178 std::string type_string = json.at(PARAM_SIM_MEASURE);
179 if (type_string == "MSE")
180 {
182 this->float_similarity_type = aron::similarity::FloatSimilarity::Type::MSE;
184 }
185 else if (type_string == "MAE")
186 {
188 this->float_similarity_type = aron::similarity::FloatSimilarity::Type::MAE;
190 }
191 else if (type_string == "Chernoff")
192 {
194 this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
196 }
197 else if (type_string == "Cosine")
198 {
200 this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
202 }
203 else
204 {
205 ARMARX_WARNING << "Undefined similarity measure detected in JSON file";
207 this->float_similarity_type = aron::similarity::FloatSimilarity::Type::NONE;
208 }
209 ARMARX_INFO << VAROUT(this->similarity_type);
210 }
211 if (json.find(PARAM_MAX_OBJECTS) != json.end())
212 {
213 max_images = json.at(PARAM_MAX_OBJECTS);
214 ARMARX_INFO << VAROUT(max_images);
215 stats.number_of_compared_objects = max_images;
216 }
217
218 stats.start_time = std::chrono::high_resolution_clock::now();
219 }
220
223 {
224 std::lock_guard<std::mutex> lock(filterMutex_);
225 return stats;
226 }
227
228 std::string
230 {
231 return this->NAME;
232 }
233
234} // namespace armarx::armem::server::ltm::processor::filter
#define VAROUT(x)
bool forEachInstance(InstanceFunctionT &&func)
std::mutex filterMutex_
Mutex for thread-safe access to filter state (stats and derived class state) Derived classes should l...
Definition Filter.h:61
virtual bool accept(const armem::wm::EntitySnapshot &e, bool simulatedVersion) override
Client-side working entity instance.
Client-side working memory entity snapshot.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::shared_ptr< NDArray > NDArrayPtr
Definition NDArray.h:46
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,...
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
double distance(const Point &a, const Point &b)
Definition point.hpp:95