| 1 | /********************************************************************** |
| 2 | jlclassify_lib.cc: classify raster image |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <vector> |
| 22 | #include <map> |
| 23 | #include <sstream> |
| 24 | #include <memory> |
| 25 | #include "jlclassify_lib.h" |
| 26 | #include "Jim.h" |
| 27 | #include "VectorOgr.h" |
| 28 | #include "base/Optionjl.h" |
| 29 | #include "apps/AppFactory.h" |
| 30 | #include "jlsml_lib.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace app; |
| 34 | |
| 35 | shared_ptr<Jim> Jim::classify(app::AppFactory& app){ |
| 36 | try{ |
| 37 | shared_ptr<Jim> imgWriter=createImg(); |
| 38 | classify(*imgWriter, app); |
| 39 | return(imgWriter); |
| 40 | } |
| 41 | catch(string helpString){ |
| 42 | cerr << helpString << endl; |
| 43 | throw; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | shared_ptr<VectorOgr> VectorOgr::classify(app::AppFactory& app){ |
| 48 | std::shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 49 | classify(*ogrWriter, app); |
| 50 | return(ogrWriter); |
| 51 | } |
| 52 | |
| 53 | void Jim::classify(Jim& imgWriter, app::AppFactory& app){ |
| 54 | //--------------------------- command line options ------------------------------------ |
| 55 | Optionjl<std::string> method_opt("m", "method", "classification method: 'svm' (support vector machine), 'ann' (artificial neural network), 'sml' (symbolic machine learning)"); |
| 56 | |
| 57 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 58 | try{ |
| 59 | doProcess=method_opt.retrieveOption(app); |
| 60 | if(method_opt.empty()){ |
| 61 | cout << endl; |
| 62 | std::ostringstream helpStream; |
| 63 | helpStream << "Error: no classification method provided" << std::endl; |
| 64 | throw(helpStream.str());//help was invoked, stop processing |
| 65 | } |
| 66 | switch(getClassifier(method_opt[0])){ |
| 67 | case(SVM): |
| 68 | classifySVM(imgWriter,app); |
| 69 | break; |
| 70 | case(ANN): |
| 71 | classifyANN(imgWriter,app); |
| 72 | break; |
| 73 | case(SML): |
| 74 | classifySML(imgWriter,app); |
| 75 | break; |
| 76 | default: |
| 77 | std::ostringstream errorStream; |
| 78 | errorStream << "Error: classification method " << method_opt[0] << "(" << getClassifier(method_opt[0]) << ")" << " is not supported" << std::endl; |
| 79 | throw(errorStream.str());//help was invoked, stop processing |
| 80 | } |
| 81 | } |
| 82 | catch(string predefinedString){ |
| 83 | std::cout << predefinedString << std::endl; |
| 84 | throw; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void VectorOgr::train(app::AppFactory& app){ |
| 90 | //--------------------------- command line options ------------------------------------ |
| 91 | Optionjl<std::string> method_opt("m", "method", "classification method: 'svm' (support vector machine), 'ann' (artificial neural network)"); |
| 92 | Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); |
| 93 | |
| 94 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 95 | try{ |
| 96 | doProcess=method_opt.retrieveOption(app); |
| 97 | model_opt.retrieveOption(app); |
| 98 | |
| 99 | if(method_opt.empty()){ |
| 100 | cout << endl; |
| 101 | std::ostringstream helpStream; |
| 102 | helpStream << "Error: no classification method provided" << std::endl; |
| 103 | throw(helpStream.str());//help was invoked, stop processing |
| 104 | } |
| 105 | if(model_opt.empty()){ |
| 106 | std::string errorString="Error: filename to save model is empty"; |
| 107 | throw(errorString); |
| 108 | } |
| 109 | switch(getClassifier(method_opt[0])){ |
| 110 | case(SVM):{ |
| 111 | std::ofstream outputStream(model_opt[0]); |
| 112 | outputStream << trainSVM(app); |
| 113 | outputStream.close(); |
| 114 | break; |
| 115 | } |
| 116 | case(ANN):{ |
| 117 | std::ofstream outputStream(model_opt[0]); |
| 118 | outputStream << trainANN(app); |
| 119 | outputStream.close(); |
| 120 | break; |
| 121 | } |
| 122 | default: |
| 123 | std::ostringstream errorStream; |
| 124 | errorStream << "Error: classification method " << method_opt[0] << " not supported" << std::endl; |
| 125 | throw(errorStream.str());//help was invoked, stop processing |
| 126 | } |
| 127 | } |
| 128 | catch(string predefinedString){ |
| 129 | std::cerr << predefinedString << std::endl; |
| 130 | throw; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | std::string Jim::trainMem(JimList& referenceReader, app::AppFactory& app){ |
| 135 | //--------------------------- command line options ------------------------------------ |
| 136 | Optionjl<string> method_opt("m", "method", "classification method: 'svm' (support vector machine), 'ann' (artificial neural network)"); |
| 137 | |
| 138 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 139 | try{ |
| 140 | doProcess=method_opt.retrieveOption(app); |
| 141 | if(method_opt.empty()){ |
| 142 | cout << endl; |
| 143 | std::ostringstream helpStream; |
| 144 | helpStream << "Error: no classification method provided" << std::endl; |
| 145 | throw(helpStream.str());//help was invoked, stop processing |
| 146 | } |
| 147 | switch(getClassifier(method_opt[0])){ |
| 148 | // case(SML): |
| 149 | // switch(getDataType()){ |
| 150 | // case(GDT_Byte): |
| 151 | // return(trainSML_t<unsigned char>(referenceReader, app)); |
| 152 | // case(GDT_Int16): |
| 153 | // return(trainSML_t<unsigned short>(referenceReader, app)); |
| 154 | // case(GDT_UInt16): |
| 155 | // return(trainSML_t<short>(referenceReader, app)); |
| 156 | // case(GDT_Int32): |
| 157 | // return(trainSML_t<int>(referenceReader, app)); |
| 158 | // case(GDT_UInt32): |
| 159 | // return(trainSML_t<unsigned int>(referenceReader, app)); |
| 160 | // case(GDT_Float32): |
| 161 | // // return(trainSML<float>(referenceReader, app)); |
| 162 | // case(GDT_Float64): |
| 163 | // // return(trainSML<double>(referenceReader, app)); |
| 164 | // default: |
| 165 | // std::ostringstream errorStream; |
| 166 | // errorStream << "Error: data type " << getDataType() << " not supported" << std::endl; |
| 167 | // throw(errorStream.str());//help was invoked, stop processing |
| 168 | // } |
| 169 | default: |
| 170 | std::ostringstream errorStream; |
| 171 | errorStream << "Error: classification method " << method_opt[0] << " not supported" << std::endl; |
| 172 | throw(errorStream.str());//help was invoked, stop processing |
| 173 | } |
| 174 | return(std::string()); |
| 175 | } |
| 176 | catch(string predefinedString){ |
| 177 | std::cerr << predefinedString << std::endl; |
| 178 | throw; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | std::string VectorOgr::trainMem(app::AppFactory& app){ |
| 183 | //--------------------------- command line options ------------------------------------ |
| 184 | Optionjl<string> method_opt("m", "method", "classification method: 'svm' (support vector machine), 'ann' (artificial neural network)"); |
| 185 | |
| 186 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 187 | try{ |
| 188 | doProcess=method_opt.retrieveOption(app); |
| 189 | if(method_opt.empty()){ |
| 190 | cout << endl; |
| 191 | std::ostringstream helpStream; |
| 192 | helpStream << "Error: no classification method provided" << std::endl; |
| 193 | throw(helpStream.str());//help was invoked, stop processing |
| 194 | } |
| 195 | switch(getClassifier(method_opt[0])){ |
| 196 | case(SVM): |
| 197 | return(trainSVM(app)); |
| 198 | break; |
| 199 | case(ANN): |
| 200 | return(trainANN(app)); |
| 201 | break; |
| 202 | default: |
| 203 | std::ostringstream errorStream; |
| 204 | errorStream << "Error: classification method " << method_opt[0] << " not supported" << std::endl; |
| 205 | throw(errorStream.str());//help was invoked, stop processing |
| 206 | } |
| 207 | return(std::string()); |
| 208 | } |
| 209 | catch(string predefinedString){ |
| 210 | std::cerr << predefinedString << std::endl; |
| 211 | throw; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void VectorOgr::classify(VectorOgr& ogrWriter, app::AppFactory& app){ |
| 216 | //--------------------------- command line options ------------------------------------ |
| 217 | Optionjl<string> method_opt("m", "method", "classification method: 'svm' (support vector machine), 'ann' (artificial neural network)"); |
| 218 | |
| 219 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 220 | try{ |
| 221 | doProcess=method_opt.retrieveOption(app); |
| 222 | if(method_opt.empty()){ |
| 223 | cout << endl; |
| 224 | std::ostringstream helpStream; |
| 225 | helpStream << "Error: no classification method provided" << std::endl; |
| 226 | throw(helpStream.str());//help was invoked, stop processing |
| 227 | } |
| 228 | switch(getClassifier(method_opt[0])){ |
| 229 | case(SVM): |
| 230 | classifySVM(ogrWriter,app); |
| 231 | break; |
| 232 | case(ANN): |
| 233 | classifyANN(ogrWriter,app); |
| 234 | break; |
| 235 | default: |
| 236 | std::ostringstream errorStream; |
| 237 | errorStream << "Error: classification method " << method_opt[0] << " not supported" << std::endl; |
| 238 | throw(errorStream.str());//help was invoked, stop processing |
| 239 | } |
| 240 | } |
| 241 | catch(string predefinedString){ |
| 242 | std::cout << predefinedString << std::endl; |
| 243 | throw; |
| 244 | } |
| 245 | } |