| 1 | /********************************************************************** |
| 2 | jlsml_lib.cc: classify raster image using SML |
| 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 "jlsml_lib.h" |
| 22 | |
| 23 | void Jim::classifySML(Jim& imgWriter, app::AppFactory& app){ |
| 24 | switch(getDataType()){ |
| 25 | case(GDT_Byte): |
| 26 | classifySML_t<unsigned char>(imgWriter,app); |
| 27 | break; |
| 28 | case(GDT_Int16): |
| 29 | classifySML_t<short>(imgWriter,app); |
| 30 | break; |
| 31 | case(GDT_UInt16): |
| 32 | classifySML_t<unsigned short>(imgWriter,app); |
| 33 | break; |
| 34 | case(GDT_Int32): |
| 35 | classifySML_t<int>(imgWriter,app); |
| 36 | break; |
| 37 | case(GDT_UInt32): |
| 38 | classifySML_t<unsigned int>(imgWriter,app); |
| 39 | break; |
| 40 | case(GDT_Float32): |
| 41 | case(GDT_Float64): |
| 42 | default: |
| 43 | std::string errorString="Error: data type not supported"; |
| 44 | throw(errorString); |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void Jim::classifySML(Jim& imgWriter, JimList& referenceReader, app::AppFactory& app){ |
| 50 | switch(getDataType()){ |
| 51 | case(GDT_Byte): |
| 52 | classifySML_t<unsigned char>(imgWriter,referenceReader,app); |
| 53 | break; |
| 54 | case(GDT_Int16): |
| 55 | classifySML_t<short>(imgWriter,referenceReader,app); |
| 56 | break; |
| 57 | case(GDT_UInt16): |
| 58 | classifySML_t<unsigned short>(imgWriter,referenceReader,app); |
| 59 | break; |
| 60 | case(GDT_Int32): |
| 61 | classifySML_t<int>(imgWriter,referenceReader,app); |
| 62 | break; |
| 63 | case(GDT_UInt32): |
| 64 | classifySML_t<unsigned int>(imgWriter,referenceReader,app); |
| 65 | break; |
| 66 | case(GDT_Float32): |
| 67 | case(GDT_Float64): |
| 68 | default: |
| 69 | std::string errorString="Error: data type not supported"; |
| 70 | throw(errorString); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::shared_ptr<Jim> Jim::classifySML(JimList& referenceReader, app::AppFactory& app){ |
| 76 | try{ |
| 77 | std::shared_ptr<Jim> imgWriter=createImg(); |
| 78 | classifySML(*imgWriter, referenceReader, app); |
| 79 | return(imgWriter); |
| 80 | } |
| 81 | catch(std::string helpString){ |
| 82 | std::cerr << helpString << std::endl; |
| 83 | throw; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | std::shared_ptr<Jim> Jim::classifySML(app::AppFactory& app){ |
| 88 | try{ |
| 89 | std::shared_ptr<Jim> imgWriter=createImg(); |
| 90 | classifySML(*imgWriter, app); |
| 91 | return(imgWriter); |
| 92 | } |
| 93 | catch(std::string helpString){ |
| 94 | std::cerr << helpString << std::endl; |
| 95 | throw; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void Jim::trainSML(JimList& referenceReader, app::AppFactory& app){ |
| 100 | //--------------------------- command line options ------------------------------------ |
| 101 | // Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); |
| 102 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 103 | try{ |
| 104 | // doProcess=model_opt.retrieveOption(app); |
| 105 | // if(model_opt.empty()){ |
| 106 | // std::string errorString="Error: filename to save model is empty"; |
| 107 | // throw(errorString); |
| 108 | // } |
| 109 | // std::ofstream outputStream(model_opt[0]); |
| 110 | switch(getDataType()){ |
| 111 | case(GDT_Byte): |
| 112 | trainSML_t<unsigned char>(referenceReader, app); |
| 113 | // outputStream << trainSML_t<unsigned char>(referenceReader, app); |
| 114 | break; |
| 115 | case(GDT_Int16): |
| 116 | trainSML_t<unsigned short>(referenceReader, app); |
| 117 | // outputStream << trainSML_t<unsigned short>(referenceReader, app); |
| 118 | break; |
| 119 | case(GDT_UInt16): |
| 120 | trainSML_t<short>(referenceReader, app); |
| 121 | // outputStream << trainSML_t<short>(referenceReader, app); |
| 122 | break; |
| 123 | case(GDT_Int32): |
| 124 | trainSML_t<int>(referenceReader, app); |
| 125 | // outputStream << trainSML_t<int>(referenceReader, app); |
| 126 | break; |
| 127 | case(GDT_UInt32): |
| 128 | trainSML_t<unsigned int>(referenceReader, app); |
| 129 | // outputStream << trainSML_t<unsigned int>(referenceReader, app); |
| 130 | break; |
| 131 | // case(GDT_Float32): |
| 132 | // trainSML<float>(referenceReader, app); |
| 133 | // outputStream << trainSML<float>(referenceReader, app); |
| 134 | // break; |
| 135 | // case(GDT_Float64): |
| 136 | // trainSML<double>(referenceReader, app); |
| 137 | // outputStream << trainSML<double>(referenceReader, app); |
| 138 | // break; |
| 139 | default: |
| 140 | std::ostringstream errorStream; |
| 141 | errorStream << "Error: data type " << getDataType() << " not supported" << std::endl; |
| 142 | throw(errorStream.str());//help was invoked, stop processing |
| 143 | break; |
| 144 | } |
| 145 | // outputStream.close(); |
| 146 | } |
| 147 | catch(std::string predefinedString){ |
| 148 | std::cerr << predefinedString << std::endl; |
| 149 | throw; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void Jim::trainSML2d(JimList& referenceReader, app::AppFactory& app){ |
| 154 | //--------------------------- command line options ------------------------------------ |
| 155 | Optionjl<std::string> method_opt("m", "method", "classification method: 'sml' (symbolic machine learning)"); |
| 156 | Optionjl<std::string> model_opt("model", "model", "Model filename to save trained classifier."); |
| 157 | |
| 158 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 159 | try{ |
| 160 | doProcess=method_opt.retrieveOption(app); |
| 161 | model_opt.retrieveOption(app); |
| 162 | |
| 163 | if(method_opt.empty()){ |
| 164 | std::cout << std::endl; |
| 165 | std::ostringstream helpStream; |
| 166 | helpStream << "Error: no classification method provided" << std::endl; |
| 167 | throw(helpStream.str());//help was invoked, stop processing |
| 168 | } |
| 169 | if(model_opt.empty()){ |
| 170 | std::string errorString="Error: filename to save model is empty"; |
| 171 | throw(errorString); |
| 172 | } |
| 173 | std::ofstream outputStream(model_opt[0]); |
| 174 | switch(getDataType()){ |
| 175 | case(GDT_Byte): |
| 176 | outputStream << trainSML2d_t<unsigned char>(referenceReader, app); |
| 177 | break; |
| 178 | case(GDT_Int16): |
| 179 | outputStream << trainSML2d_t<unsigned short>(referenceReader, app); |
| 180 | break; |
| 181 | case(GDT_UInt16): |
| 182 | outputStream << trainSML2d_t<short>(referenceReader, app); |
| 183 | break; |
| 184 | case(GDT_Int32): |
| 185 | outputStream << trainSML2d_t<int>(referenceReader, app); |
| 186 | break; |
| 187 | case(GDT_UInt32): |
| 188 | outputStream << trainSML2d_t<unsigned int>(referenceReader, app); |
| 189 | break; |
| 190 | case(GDT_Float32): |
| 191 | // outputStream << trainSML<float>(referenceReader, app); |
| 192 | // break; |
| 193 | case(GDT_Float64): |
| 194 | // outputStream << trainSML<double>(referenceReader, app); |
| 195 | // break; |
| 196 | default: |
| 197 | std::ostringstream errorStream; |
| 198 | errorStream << "Error: data type " << getDataType() << " not supported" << std::endl; |
| 199 | throw(errorStream.str());//help was invoked, stop processing |
| 200 | break; |
| 201 | } |
| 202 | outputStream.close(); |
| 203 | } |
| 204 | catch(std::string predefinedString){ |
| 205 | std::cerr << predefinedString << std::endl; |
| 206 | throw; |
| 207 | } |
| 208 | } |