60:
env(ORT_LOGGING_LEVEL_WARNING,
"test"),
64 dqm_core::AlgorithmManager::instance().registerAlgorithm(
"AutoencoderExampleAlgorithm",
this);
79 size_t num_input_nodes =
session->GetInputCount();
80 Ort::AllocatorWithDefaultOptions allocator;
81 for (
size_t i = 0; i < num_input_nodes; ++i)
83 std::unique_ptr<char, Ort::detail::AllocatedFree> input_name_ptr =
session->GetInputNameAllocated(i, allocator);
84 char* input_name = input_name_ptr.get();
85 std::cout <<
"Input " << i <<
": name=" << input_name << std::endl;
90 size_t num_output_nodes =
session->GetOutputCount();
91 for (
size_t i = 0; i < num_output_nodes; ++i)
93 std::unique_ptr<char, Ort::detail::AllocatedFree> output_name_ptr =
session->GetOutputNameAllocated(i, allocator);
94 char* output_name = output_name_ptr.get();
95 std::cout <<
"Output " << i <<
": name=" << output_name << std::endl;
118 const TObject&
object,
119 const dqm_core::AlgorithmConfig& )
126 if (
object.
IsA()->InheritsFrom(
"TH2"))
128 histogram =
dynamic_cast<const TH2*
>(&object);
131 throw dqm_core::BadConfig( ERS_HERE, name,
"dimension of histogram < 2");
136 throw dqm_core::BadConfig( ERS_HERE, name,
"does not inherit from TH2" );
143 std::vector<std::vector<float>> output_data;
144 std::vector<int64_t> input_dims = {1, 3};
146 Ort::MemoryInfo memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
147 const char* input_names_cstr[] = {
input_names[0].c_str() };
148 const char* output_names_cstr[] = {
output_names[0].c_str() };
149 for (
auto& input_data : hist_data) {
151 Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, input_data.data(), input_data.size(), input_dims.data(), input_dims.size());
154 auto output_tensors =
session->Run(Ort::RunOptions{
nullptr}, input_names_cstr, &input_tensor, 1, output_names_cstr, 1);
157 if (output_tensors.empty() || output_tensors[0].HasValue())
159 throw dqm_core::BadConfig(ERS_HERE, name,
"Output tensor does not have values or does not exist");
163 const size_t tensor_size = output_tensors[0].GetTensorTypeAndShapeInfo().GetElementCount();
167 throw dqm_core::BadConfig(ERS_HERE, name,
"Output tensor < 3 elements");
171 float* raw_output = output_tensors[0].GetTensorMutableData<
float>();
172 std::vector<float> output_point(raw_output, raw_output + 3);
173 output_data.push_back(output_point);
178 std::vector<float> reconstruction_errors;
179 for (
size_t i = 0; i < hist_data.size(); ++i) {
181 for (
size_t j = 0; j < 3; ++j) {
182 error += std::pow(hist_data[i][j] - output_data[i][j], 2);
184 reconstruction_errors.push_back(std::sqrt(
error));
189 std::nth_element(reconstruction_errors.begin(), reconstruction_errors.begin() + std::roundl(reconstruction_errors.size() * (99. / 100.)), reconstruction_errors.end());
190 float threshold = reconstruction_errors[reconstruction_errors.size() * 99 / 100];
194 std::vector<bool> anomalies;
195 for (
const auto&
error : reconstruction_errors) {
210 resulthisto=
static_cast<TH2*
>(
histogram->Clone());
214 throw dqm_core::BadConfig( ERS_HERE, name,
"does not inherit from TH2" );
216 resulthisto->Reset();
219 std::cout <<
"Anomalies:" << std::endl;
220 for (
size_t i = 0; i < anomalies.size(); ++i) {
224 resulthisto->SetBinContent(hist_data[i][0], hist_data[i][1], hist_data[i][2]);
225 std::cout <<
"Input (" << hist_data[i][0] <<
", " << hist_data[i][1] <<
", " << hist_data[i][2] <<
"): ";
226 std::cout <<
"Output (" << output_data[i][0] <<
", " << output_data[i][1] <<
", " << output_data[i][2] <<
")";
227 std::cout <<
" Reconstruction Error: " << reconstruction_errors[i] << std::endl;
232 dqm_core::Result* result =
new dqm_core::Result();
235 result->tags_[
"NBins"] = anomalies.size();
238 result->object_ = boost::shared_ptr<TObject>(resulthisto);
241 double gthreshold = 5000;
242 double rthreshold = 10000;
245 if (anomalies.size() <= gthreshold)
247 result->status_ = dqm_core::Result::Green;
249 else if (anomalies.size() < rthreshold)
251 result->status_ = dqm_core::Result::Yellow;
255 result->status_ = dqm_core::Result::Red;