SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SegmentationConfig.cpp
Go to the documentation of this file.
1
22
23#include <iostream>
24#include <fstream>
25
26#include <boost/regex.hpp>
27#include <boost/algorithm/string.hpp>
28
29#include "ElementsKernel/Exception.h"
30#include "Configuration/ConfigManager.h"
35
36using boost::regex;
37using boost::regex_match;
38using boost::smatch;
39
40using namespace Euclid::Configuration;
41namespace po = boost::program_options;
42
43namespace SourceXtractor {
44
46
47static const std::string SEGMENTATION_ALGORITHM {"segmentation-algorithm" };
48static const std::string SEGMENTATION_USE_FILTERING {"segmentation-use-filtering" };
49static const std::string SEGMENTATION_FILTER {"segmentation-filter" };
50static const std::string SEGMENTATION_LUTZ_WINDOW_SIZE {"segmentation-lutz-window-size" };
51static const std::string SEGMENTATION_BFS_MAX_DELTA {"segmentation-bfs-max-delta" };
52static const std::string SEGMENTATION_ML_MODEL {"segmentation-ml-model" };
53static const std::string SEGMENTATION_ML_THRESHOLD {"segmentation-ml-threshold" };
54
59
61 return { {"Detection image", {
62 {SEGMENTATION_ALGORITHM.c_str(), po::value<std::string>()->default_value("LUTZ"),
63 "Segmentation algorithm to be used (LUTZ, TILES or ML (a ONNX-format model must be provided))"},
64 {SEGMENTATION_USE_FILTERING.c_str(), po::value<bool>()->default_value(true),
65 "Is filtering used"},
66 {SEGMENTATION_FILTER.c_str(), po::value<std::string>()->default_value(""),
67 "Loads a filter"},
68 {SEGMENTATION_LUTZ_WINDOW_SIZE.c_str(), po::value<int>()->default_value(0),
69 "Lutz sliding window size (0=disable)"},
70 {SEGMENTATION_BFS_MAX_DELTA.c_str(), po::value<int>()->default_value(1000),
71 "BFS algorithm max source x/y size (default=1000)"},
72 {SEGMENTATION_ML_MODEL.c_str(), po::value<std::string>()->default_value(""),
73 "ONNX model to use with machine learning segmentation"},
74 {SEGMENTATION_ML_THRESHOLD.c_str(), po::value<double>()->default_value(0.9),
75 "Probability threshold for ML detection"},
76 }}};
77}
78
80 auto algorithm_name = boost::to_upper_copy(args.at(SEGMENTATION_ALGORITHM).as<std::string>());
81 if (algorithm_name == "LUTZ") {
83 } else if (algorithm_name == "BFS") {
85 } else if (algorithm_name == "ASSOC") {
87 } else if (algorithm_name == "ML") {
88#ifdef WITH_ML_SEGMENTATION
90#else
91 throw Elements::Exception() << "SourceXtractor++ has not been compiled with ONNX support";
92#endif
93 } else {
94 throw Elements::Exception() << "Unknown segmentation algorithm : " << algorithm_name;
95 }
96
97 if (args.at(SEGMENTATION_USE_FILTERING).as<bool>()) {
98 auto filter_filename = args.at(SEGMENTATION_FILTER).as<std::string>();
99 if (filter_filename != "") {
100 m_filter = loadFilter(filter_filename);
101 if (m_filter == nullptr)
102 throw Elements::Exception() << "Can not load filter: " << filter_filename;
103 } else {
105 }
106 } else {
107 m_filter = nullptr;
108 }
109
113 m_ml_threshold = args.at(SEGMENTATION_ML_THRESHOLD).as<double>();
114
116 throw Elements::Exception() << "Machine learning segmentation requested but no ONNX model was provided";
117 }
118}
119
121 segConfigLogger.info() << "Using the default segmentation (3x3) filter.";
122 auto convolution_kernel = VectorImage<SeFloat>::create(3, 3);
123 convolution_kernel->setValue(0,0, 1);
124 convolution_kernel->setValue(0,1, 2);
125 convolution_kernel->setValue(0,2, 1);
126
127 convolution_kernel->setValue(1,0, 2);
128 convolution_kernel->setValue(1,1, 4);
129 convolution_kernel->setValue(1,2, 2);
130
131 convolution_kernel->setValue(2,0, 1);
132 convolution_kernel->setValue(2,1, 2);
133 convolution_kernel->setValue(2,2, 1);
134
135 return std::make_shared<BackgroundConvolution>(convolution_kernel, true);
136}
137
139 // check for the extension ".fits"
140 std::string fits_ending(".fits");
141 if (filename.length() >= fits_ending.length()
142 && filename.compare (filename.length() - fits_ending.length(), fits_ending.length(), fits_ending)==0) {
143 // load a FITS filter
144 return loadFITSFilter(filename);
145 }
146 else{
147 // load an ASCII filter
148 return loadASCIIFilter(filename);
149 }
150}
151
153
154 // read in the FITS file
155 auto convolution_kernel = FitsReader<SeFloat>::readFile(filename);
156
157 // give some feedback on the filter
158 segConfigLogger.info() << "Loaded segmentation filter: " << filename << " height: " << convolution_kernel->getHeight() << " width: " << convolution_kernel->getWidth();
159
160 // return the correct object
161 return std::make_shared<BackgroundConvolution>(convolution_kernel, true);
162}
163
164static bool getNormalization(std::istream& line_stream) {
165 std::string conv, norm_type;
166 line_stream >> conv >> norm_type;
167 if (conv != "CONV") {
168 throw Elements::Exception() << "Unexpected start for ASCII filter: " << conv;
169 }
170 if (norm_type == "NORM") {
171 return true;
172 }
173 else if (norm_type == "NONORM") {
174 return false;
175 }
176
177 throw Elements::Exception() << "Unexpected normalization type: " << norm_type;
178}
179
180template <typename T>
181static void extractValues(std::istream& line_stream, std::vector<T>& data) {
182 T value;
183 while (line_stream.good()) {
184 line_stream >> value;
185 data.push_back(value);
186 }
187}
188
190 std::ifstream file;
191
192 // open the file and check
193 file.open(filename);
194 if (!file.good() || !file.is_open()){
195 throw Elements::Exception() << "Can not load filter: " << filename;
196 }
197
198 enum class LoadState {
199 STATE_START,
200 STATE_FIRST_LINE,
201 STATE_OTHER_LINES
202 };
203
204 LoadState state = LoadState::STATE_START;
205 bool normalize = false;
206 std::vector<SeFloat> kernel_data;
207 size_t kernel_width = 0;
208
209 while (file.good()) {
210 std::string line;
211 std::getline(file, line);
212 line = regex_replace(line, regex("\\s*#.*"), std::string(""));
213 line = regex_replace(line, regex("\\s*$"), std::string(""));
214 if (line.size() == 0) {
215 continue;
216 }
217
218 std::stringstream line_stream(line);
219
220 switch (state) {
221 case LoadState::STATE_START:
222 normalize = getNormalization(line_stream);
223 state = LoadState::STATE_FIRST_LINE;
224 break;
225 case LoadState::STATE_FIRST_LINE:
226 extractValues(line_stream, kernel_data);
227 kernel_width = kernel_data.size();
228 state = LoadState::STATE_OTHER_LINES;
229 break;
230 case LoadState::STATE_OTHER_LINES:
231 extractValues(line_stream, kernel_data);
232 break;
233 }
234 }
235
236 // compute the dimensions and create the kernel
237 if (kernel_width == 0) {
238 throw Elements::Exception() << "Malformed segmentation filter: width is 0";
239 }
240 auto kernel_height = kernel_data.size() / kernel_width;
241 auto convolution_kernel = VectorImage<SeFloat>::create(kernel_width, kernel_height, kernel_data);
242
243 // give some feedback on the filter
244 segConfigLogger.info() << "Loaded segmentation filter: " << filename << " width: " << convolution_kernel->getWidth() << " height: " << convolution_kernel->getHeight();
245
246 // return the correct object
247 return std::make_shared<BackgroundConvolution>(convolution_kernel, normalize);
248}
249
250} // SourceXtractor namespace
T at(T... args)
static Logging getLogger(const std::string &name="")
std::map< std::string, boost::program_options::variable_value > UserValues
static std::shared_ptr< Image< T > > readFile(const std::string &filename)
Definition FitsReader.h:46
std::shared_ptr< DetectionImageFrame::ImageFilter > m_filter
SegmentationConfig(long manager_id)
Constructs a new SegmentationConfig object.
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
void preInitialize(const UserValues &args) override
std::shared_ptr< DetectionImageFrame::ImageFilter > getDefaultFilter() const
std::shared_ptr< DetectionImageFrame::ImageFilter > loadFITSFilter(const std::string &filename) const
std::shared_ptr< DetectionImageFrame::ImageFilter > loadASCIIFilter(const std::string &filename) const
std::shared_ptr< DetectionImageFrame::ImageFilter > loadFilter(const std::string &filename) const
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
T getline(T... args)
T good(T... args)
T is_open(T... args)
T make_shared(T... args)
static void extractValues(std::istream &line_stream, std::vector< T > &data)
static const std::string SEGMENTATION_ML_THRESHOLD
static const std::string SEGMENTATION_USE_FILTERING
static const std::string SEGMENTATION_ALGORITHM
static const std::string SEGMENTATION_FILTER
static bool getNormalization(std::istream &line_stream)
static const std::string SEGMENTATION_LUTZ_WINDOW_SIZE
static const std::string SEGMENTATION_ML_MODEL
static Elements::Logging segConfigLogger
static const std::string SEGMENTATION_BFS_MAX_DELTA
T open(T... args)
T push_back(T... args)
T regex_replace(T... args)
T length(T... args)