├── .github └── workflows │ ├── build.yml │ ├── snapshot.yml │ └── staging.yml ├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── LICENSE.md ├── README.md ├── logs ├── deepnetts-error.log └── deepnetts-training.log ├── mvnw ├── mvnw.cmd ├── pom.xml ├── settings.xml └── src ├── main ├── java │ └── javax │ │ └── visrec │ │ └── ri │ │ ├── BufferedImageFactory.java │ │ ├── ml │ │ ├── classification │ │ │ ├── AbstractImageClassifier.java │ │ │ ├── FeedForwardNetBinaryClassifier.java │ │ │ ├── ImageClassifierNetwork.java │ │ │ ├── MultiClassClassifierNetwork.java │ │ │ └── ZeroRuleClassifier.java │ │ ├── detection │ │ │ ├── AbstractObjectDetector.java │ │ │ └── SimpleObjectDetector.java │ │ └── regression │ │ │ ├── LogisticRegressionNetwork.java │ │ │ └── SimpleLinearRegressionNetwork.java │ │ ├── spi │ │ ├── BufferedImageClassifierFactory.java │ │ ├── DeepNettsImplementationService.java │ │ ├── DefaultImageFactoryService.java │ │ ├── DefaultServiceProvider.java │ │ └── FloatArrayBinaryClassifierFactory.java │ │ └── util │ │ └── DataSets.java └── resources │ └── META-INF │ └── services │ ├── javax.visrec.spi.BinaryClassifierFactory │ ├── javax.visrec.spi.ImageClassifierFactory │ └── javax.visrec.spi.ServiceProvider └── test └── java └── visrec └── ri ├── spi └── DefaultImageFactoryServiceTest.java └── util └── BuilderConfigurationTest.java /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 1.8 16 | uses: actions/setup-java@v1 17 | with: 18 | java-version: 1.8 19 | #settings-path: ${{ github.workspace }} # location for the settings.xml 20 | #server-id: github # Value of the distributionManagement/repository/id field of the pom.xml 21 | - name: Build with Maven 22 | run: ./mvnw package 23 | -------------------------------------------------------------------------------- /.github/workflows/snapshot.yml: -------------------------------------------------------------------------------- 1 | name: snapshot 2 | 3 | # To trigger this workflow manually, you can use the following curl command: 4 | # curl -XPOST -u "USERNAME:PERSONAL_TOKEN" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/JavaVisRec/visrec-ri/dispatches --data '{"event_type": "snapshot-pub"}' 5 | 6 | on: 7 | repository_dispatch: 8 | types: [snapshot-pub] 9 | pull_request: 10 | branches: 11 | - master 12 | 13 | jobs: 14 | build: 15 | 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Set up JDK 1.8 21 | uses: actions/setup-java@v1 22 | with: 23 | java-version: 1.8 24 | #settings-path: ${{ github.workspace }} # location for the settings.xml 25 | #server-id: github # Value of the distributionManagement/repository/id field of the pom.xml 26 | - name: Build with Maven 27 | run: ./mvnw package 28 | 29 | - name: Add private key to keyring 30 | run: | 31 | echo "${PRIVATE_KEY}" > private.key 32 | gpg --import --batch private.key 33 | env: 34 | PRIVATE_KEY: ${{ secrets.GPG_SECRET_KEY }} 35 | 36 | - name: Publish to repository 37 | run: ./mvnw deploy -s settings.xml 38 | env: 39 | OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} 40 | OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} 41 | GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} 42 | -------------------------------------------------------------------------------- /.github/workflows/staging.yml: -------------------------------------------------------------------------------- 1 | name: staging 2 | 3 | on: 4 | # To trigger this workflow manually, you can use the following curl command: 5 | # curl -XPOST -u "USERNAME:PERSONAL_TOKEN" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/JavaVisRec/visrec-api/dispatches --data '{"event_type": "staging-pub"}' 6 | 7 | # Make sure you create your personal token with repo access. Follow steps in 8 | # https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line 9 | # to create your personal token. 10 | 11 | # Special thanks to AWS Labs & AWS DJL project for this approach 12 | repository_dispatch: 13 | types: [staging-pub] 14 | 15 | 16 | jobs: 17 | build: 18 | 19 | runs-on: ubuntu-latest 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Set up JDK 1.8 24 | uses: actions/setup-java@v1 25 | with: 26 | java-version: 1.8 27 | #settings-path: ${{ github.workspace }} # location for the settings.xml 28 | #server-id: github # Value of the distributionManagement/repository/id field of the pom.xml 29 | - name: Build with Maven 30 | run: ./mvnw package 31 | 32 | - name: Add private key to keyring 33 | run: | 34 | echo "${PRIVATE_KEY}" > private.key 35 | gpg --import --batch private.key 36 | env: 37 | PRIVATE_KEY: ${{ secrets.GPG_SECRET_KEY }} 38 | 39 | - name: Publish to repository 40 | run: | 41 | sed -i "s/-SNAPSHOT//g" pom.xml 42 | ./mvnw deploy -s settings.xml 43 | env: 44 | OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} 45 | OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} 46 | GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | .gradle/ 4 | gradle/ 5 | build/** 6 | target/** 7 | *.asc 8 | /target/ 9 | nb*.xml 10 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import java.net.*; 17 | import java.io.*; 18 | import java.nio.channels.*; 19 | import java.util.Properties; 20 | 21 | public class MavenWrapperDownloader { 22 | 23 | private static final String WRAPPER_VERSION = "0.5.6"; 24 | /** 25 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 26 | */ 27 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 28 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 29 | 30 | /** 31 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 32 | * use instead of the default one. 33 | */ 34 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 35 | ".mvn/wrapper/maven-wrapper.properties"; 36 | 37 | /** 38 | * Path where the maven-wrapper.jar will be saved to. 39 | */ 40 | private static final String MAVEN_WRAPPER_JAR_PATH = 41 | ".mvn/wrapper/maven-wrapper.jar"; 42 | 43 | /** 44 | * Name of the property which should be used to override the default download url for the wrapper. 45 | */ 46 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 47 | 48 | public static void main(String args[]) { 49 | System.out.println("- Downloader started"); 50 | File baseDirectory = new File(args[0]); 51 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 52 | 53 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 54 | // wrapperUrl parameter. 55 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 56 | String url = DEFAULT_DOWNLOAD_URL; 57 | if(mavenWrapperPropertyFile.exists()) { 58 | FileInputStream mavenWrapperPropertyFileInputStream = null; 59 | try { 60 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 61 | Properties mavenWrapperProperties = new Properties(); 62 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 63 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 64 | } catch (IOException e) { 65 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 66 | } finally { 67 | try { 68 | if(mavenWrapperPropertyFileInputStream != null) { 69 | mavenWrapperPropertyFileInputStream.close(); 70 | } 71 | } catch (IOException e) { 72 | // Ignore ... 73 | } 74 | } 75 | } 76 | System.out.println("- Downloading from: " + url); 77 | 78 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 79 | if(!outputFile.getParentFile().exists()) { 80 | if(!outputFile.getParentFile().mkdirs()) { 81 | System.out.println( 82 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 83 | } 84 | } 85 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 86 | try { 87 | downloadFileFromURL(url, outputFile); 88 | System.out.println("Done"); 89 | System.exit(0); 90 | } catch (Throwable e) { 91 | System.out.println("- Error downloading"); 92 | e.printStackTrace(); 93 | System.exit(1); 94 | } 95 | } 96 | 97 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 98 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 99 | String username = System.getenv("MVNW_USERNAME"); 100 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 101 | Authenticator.setDefault(new Authenticator() { 102 | @Override 103 | protected PasswordAuthentication getPasswordAuthentication() { 104 | return new PasswordAuthentication(username, password); 105 | } 106 | }); 107 | } 108 | URL website = new URL(urlString); 109 | ReadableByteChannel rbc; 110 | rbc = Channels.newChannel(website.openStream()); 111 | FileOutputStream fos = new FileOutputStream(destination); 112 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 113 | fos.close(); 114 | rbc.close(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaVisRec/visrec-ri/c4f296c937ba0549dbb10d4f964e29ffec3c384c/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | 342 | CLASSPATH" EXCEPTION TO THE GPL 343 | 344 | Linking this library statically or dynamically with other modules is making 345 | a combined work based on this library. Thus, the terms and conditions of 346 | the GNU General Public License cover the whole combination. 347 | 348 | As a special exception, the copyright holders of this library give you 349 | permission to link this library with independent modules to produce an 350 | executable, regardless of the license terms of these independent modules, 351 | and to copy and distribute the resulting executable under terms of your 352 | choice, provided that you also meet, for each linked independent module, 353 | the terms and conditions of the license of that module. An independent 354 | module is a module which is not derived from or based on this library. If 355 | you modify this library, you may extend this exception to your version of 356 | the library, but you are not obligated to do so. If you do not wish to do 357 | so, delete this exception statement from your version. 358 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VisRec API JSR381 Reference implementation 2 | ![build](https://github.com/JavaVisRec/visrec-ri/workflows/build/badge.svg) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/javax.visrec/visrec-ri/badge.svg)](https://maven-badges.herokuapp.com/maven-central/javax.visrec/visrec-ri) 3 | 4 | 5 | The repository contains the source code of the reference implementation of JSR 381, 6 | a standardization in Java for Visual Recognition. 7 | 8 | The Visual Recognition API JSR #381 is a software development standard recognized by the Java Community Process (JCP) that simplifies and standardizes a set of APIs familiar to Java developers for classifying and recognizing objects in images using machine learning. Beside classes specific for visual recognition tasks, it provides general abstractions for machine learning tasks like classification, regression and data set, and reusable design which can be applied for machine learning systems in other domains. At the current stage it provides basic hello world examples for supported machine learning tasks (classification and regression) and image classification. 9 | 10 | Reference implementation is based on community edition of Deep Netts Deep Learning Engine available at 11 | 12 | https://github.com/deepnetts/deepnetts-communityedition 13 | 14 | Specification for VisRec API is available at 15 | 16 | https://github.com/JavaVisRec/visrec-api 17 | 18 | ## Getting Started Guide 19 | For step by step guide and additional info see getting started guide at 20 | 21 | https://github.com/JavaVisRec/visrec-api/wiki/Getting-Started-Guide 22 | 23 | ## Quick Start with Examples 24 | 25 | Introductory examples are available at 26 | 27 | https://github.com/JavaVisRec/jsr381-examples 28 | 29 | Quick start with commands: 30 | 31 | git clone https://github.com/JavaVisRec/jsr381-examples.git 32 | cd jsr381-examples 33 | mvn clean install 34 | mvn exec:java -Dexec.mainClass=jsr381.example.ImplementationExample 35 | 36 | -------------------------------------------------------------------------------- /logs/deepnetts-error.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaVisRec/visrec-ri/c4f296c937ba0549dbb10d4f964e29ffec3c384c/logs/deepnetts-error.log -------------------------------------------------------------------------------- /logs/deepnetts-training.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaVisRec/visrec-ri/c4f296c937ba0549dbb10d4f964e29ffec3c384c/logs/deepnetts-training.log -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | fi 118 | 119 | if [ -z "$JAVA_HOME" ]; then 120 | javaExecutable="`which javac`" 121 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 122 | # readlink(1) is not available as standard on Solaris 10. 123 | readLink=`which readlink` 124 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 125 | if $darwin ; then 126 | javaHome="`dirname \"$javaExecutable\"`" 127 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 128 | else 129 | javaExecutable="`readlink -f \"$javaExecutable\"`" 130 | fi 131 | javaHome="`dirname \"$javaExecutable\"`" 132 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 133 | JAVA_HOME="$javaHome" 134 | export JAVA_HOME 135 | fi 136 | fi 137 | fi 138 | 139 | if [ -z "$JAVACMD" ] ; then 140 | if [ -n "$JAVA_HOME" ] ; then 141 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 142 | # IBM's JDK on AIX uses strange locations for the executables 143 | JAVACMD="$JAVA_HOME/jre/sh/java" 144 | else 145 | JAVACMD="$JAVA_HOME/bin/java" 146 | fi 147 | else 148 | JAVACMD="`which java`" 149 | fi 150 | fi 151 | 152 | if [ ! -x "$JAVACMD" ] ; then 153 | echo "Error: JAVA_HOME is not defined correctly." >&2 154 | echo " We cannot execute $JAVACMD" >&2 155 | exit 1 156 | fi 157 | 158 | if [ -z "$JAVA_HOME" ] ; then 159 | echo "Warning: JAVA_HOME environment variable is not set." 160 | fi 161 | 162 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 163 | 164 | # traverses directory structure from process work directory to filesystem root 165 | # first directory with .mvn subdirectory is considered project base directory 166 | find_maven_basedir() { 167 | 168 | if [ -z "$1" ] 169 | then 170 | echo "Path not specified to find_maven_basedir" 171 | return 1 172 | fi 173 | 174 | basedir="$1" 175 | wdir="$1" 176 | while [ "$wdir" != '/' ] ; do 177 | if [ -d "$wdir"/.mvn ] ; then 178 | basedir=$wdir 179 | break 180 | fi 181 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 182 | if [ -d "${wdir}" ]; then 183 | wdir=`cd "$wdir/.."; pwd` 184 | fi 185 | # end of workaround 186 | done 187 | echo "${basedir}" 188 | } 189 | 190 | # concatenates all lines of a file 191 | concat_lines() { 192 | if [ -f "$1" ]; then 193 | echo "$(tr -s '\n' ' ' < "$1")" 194 | fi 195 | } 196 | 197 | BASE_DIR=`find_maven_basedir "$(pwd)"` 198 | if [ -z "$BASE_DIR" ]; then 199 | exit 1; 200 | fi 201 | 202 | ########################################################################################## 203 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 204 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 205 | ########################################################################################## 206 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 207 | if [ "$MVNW_VERBOSE" = true ]; then 208 | echo "Found .mvn/wrapper/maven-wrapper.jar" 209 | fi 210 | else 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 213 | fi 214 | if [ -n "$MVNW_REPOURL" ]; then 215 | jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 216 | else 217 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 218 | fi 219 | while IFS="=" read key value; do 220 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 221 | esac 222 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 223 | if [ "$MVNW_VERBOSE" = true ]; then 224 | echo "Downloading from: $jarUrl" 225 | fi 226 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 227 | if $cygwin; then 228 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 229 | fi 230 | 231 | if command -v wget > /dev/null; then 232 | if [ "$MVNW_VERBOSE" = true ]; then 233 | echo "Found wget ... using wget" 234 | fi 235 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 236 | wget "$jarUrl" -O "$wrapperJarPath" 237 | else 238 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" 239 | fi 240 | elif command -v curl > /dev/null; then 241 | if [ "$MVNW_VERBOSE" = true ]; then 242 | echo "Found curl ... using curl" 243 | fi 244 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 245 | curl -o "$wrapperJarPath" "$jarUrl" -f 246 | else 247 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 248 | fi 249 | 250 | else 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo "Falling back to using Java to download" 253 | fi 254 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 255 | # For Cygwin, switch paths to Windows format before running javac 256 | if $cygwin; then 257 | javaClass=`cygpath --path --windows "$javaClass"` 258 | fi 259 | if [ -e "$javaClass" ]; then 260 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 261 | if [ "$MVNW_VERBOSE" = true ]; then 262 | echo " - Compiling MavenWrapperDownloader.java ..." 263 | fi 264 | # Compiling the Java class 265 | ("$JAVA_HOME/bin/javac" "$javaClass") 266 | fi 267 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 268 | # Running the downloader 269 | if [ "$MVNW_VERBOSE" = true ]; then 270 | echo " - Running MavenWrapperDownloader.java ..." 271 | fi 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 282 | if [ "$MVNW_VERBOSE" = true ]; then 283 | echo $MAVEN_PROJECTBASEDIR 284 | fi 285 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 286 | 287 | # For Cygwin, switch paths to Windows format before running java 288 | if $cygwin; then 289 | [ -n "$M2_HOME" ] && 290 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 291 | [ -n "$JAVA_HOME" ] && 292 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 293 | [ -n "$CLASSPATH" ] && 294 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 295 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 296 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 297 | fi 298 | 299 | # Provide a "standardized" way to retrieve the CLI args that will 300 | # work with both Windows and non-Windows executions. 301 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 302 | export MAVEN_CMD_LINE_ARGS 303 | 304 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 305 | 306 | exec "$JAVACMD" \ 307 | $MAVEN_OPTS \ 308 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 309 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 310 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 311 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 124 | 125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 162 | if ERRORLEVEL 1 goto error 163 | goto end 164 | 165 | :error 166 | set ERROR_CODE=1 167 | 168 | :end 169 | @endlocal & set ERROR_CODE=%ERROR_CODE% 170 | 171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 175 | :skipRcPost 176 | 177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 179 | 180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 181 | 182 | exit /B %ERROR_CODE% 183 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | javax.visrec 8 | visrec-ri 9 | 1.0.3 10 | 11 | 12 | UTF-8 13 | 1.8 14 | 1.8 15 | 16 | 17 | 18 | 19 | javax.visrec 20 | visrec-api 21 | 1.0.5 22 | provided 23 | 24 | 25 | com.deepnetts 26 | deepnetts-core 27 | 1.13.2 28 | 29 | 30 | javax.visrec 31 | visrec-api 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.junit.platform 39 | junit-platform-launcher 40 | 1.2.0 41 | test 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | 5.1.0 47 | test 48 | 49 | 50 | org.junit.vintage 51 | junit-vintage-engine 52 | 5.1.0 53 | test 54 | 55 | 56 | 57 | 58 | 59 | 60 | snapshots 61 | https://oss.sonatype.org/content/groups/public/ 62 | 63 | true 64 | 65 | 66 | false 67 | 68 | 69 | 70 | 71 | javax.visrec:visrec-ri 72 | Reference implementation of JSR 381 Visual Recognition 73 | https://github.com/JavaVisRec/visrec-ri 74 | 75 | 76 | 77 | GNU General Public License, version 2, with Classpath Exception 78 | https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 79 | 80 | 81 | 82 | 83 | 84 | Zoran Sevarac 85 | sevarac@gmail.com 86 | 87 | 88 | Jyothiprasad Buddha 89 | jyothiprasadb@gmail.com 90 | 91 | 92 | Kevin Berendsen 93 | berendsen.kevin@gmail.com 94 | 95 | 96 | 97 | 98 | scm:git:git://github.com/JavaVisRec/visrec-ri.git 99 | scm:git:ssh://github.com:JavaVisRec/visrec-ri.git 100 | http://github.com/JavaVisRec/visrec-ri/tree/master 101 | 102 | 103 | 104 | 105 | ossrh 106 | https://oss.sonatype.org/content/repositories/snapshots 107 | 108 | 109 | ossrh 110 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 111 | 112 | 113 | 114 | 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-gpg-plugin 119 | 1.5 120 | 121 | 122 | sign-artifacts 123 | deploy 124 | 125 | sign 126 | 127 | 128 | 129 | 130 | --pinentry-mode 131 | loopback 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | org.sonatype.plugins 140 | nexus-staging-maven-plugin 141 | 1.6.7 142 | true 143 | 144 | ossrh 145 | https://oss.sonatype.org/ 146 | false 147 | 148 | 149 | 150 | 151 | true 152 | org.apache.maven.plugins 153 | maven-javadoc-plugin 154 | 3.1.0 155 | 156 | true 157 | none 158 | 8 159 | false 160 | 161 | 162 | 163 | attach-javadoc 164 | 165 | jar 166 | 167 | 168 | 169 | 170 | 171 | 172 | org.apache.maven.plugins 173 | maven-source-plugin 174 | 2.2.1 175 | 176 | 177 | attach-sources 178 | 179 | jar-no-fork 180 | 181 | 182 | 183 | 184 | 185 | org.apache.maven.plugins 186 | maven-compiler-plugin 187 | 188 | 1.8 189 | 1.8 190 | 191 | 192 | 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | ossrh 9 | ${env.OSSRH_USERNAME} 10 | ${env.OSSRH_PASSWORD} 11 | 12 | 13 | 14 | 15 | 16 | ossrh 17 | 18 | true 19 | 20 | 21 | gpg 22 | ${env.GPG_PASSPHRASE} 23 | 24 | 25 | 26 | 27 | ossrh 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/BufferedImageFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri; 21 | 22 | import javax.imageio.ImageIO; 23 | import javax.visrec.ImageFactory; 24 | import java.awt.image.BufferedImage; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.net.URL; 28 | import java.nio.file.Path; 29 | 30 | /** 31 | * {@link ImageFactory} to provide {@link BufferedImage} as return object. 32 | * 33 | */ 34 | public class BufferedImageFactory implements ImageFactory { 35 | 36 | /** 37 | * {@inheritDoc} 38 | */ 39 | @Override 40 | public BufferedImage getImage(Path path) throws IOException { 41 | BufferedImage img = ImageIO.read(path.toFile()); 42 | if (img == null) { 43 | throw new IOException("Failed to transform Path into BufferedImage due to unknown image encoding"); 44 | } 45 | return img; 46 | } 47 | 48 | /** 49 | * {@inheritDoc} 50 | */ 51 | @Override 52 | public BufferedImage getImage(URL file) throws IOException { 53 | BufferedImage img = ImageIO.read(file); 54 | if (img == null) { 55 | throw new IOException("Failed to transform URL into BufferedImage due to unknown image encoding"); 56 | } 57 | return img; 58 | } 59 | 60 | /** 61 | * {@inheritDoc} 62 | */ 63 | @Override 64 | public BufferedImage getImage(InputStream file) throws IOException { 65 | BufferedImage img = ImageIO.read(file); 66 | if (img == null) { 67 | throw new IOException("Failed to transform InputStream into BufferedImage due to unknown image encoding"); 68 | } 69 | return img; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/classification/AbstractImageClassifier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.classification; 21 | 22 | import javax.visrec.ImageFactory; 23 | import javax.visrec.ml.classification.ClassificationException; 24 | import javax.visrec.ml.classification.ImageClassifier; 25 | import javax.visrec.ml.model.ModelProvider; 26 | import javax.visrec.spi.ServiceProvider; 27 | import java.awt.image.BufferedImage; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.nio.file.Path; 31 | import java.util.Map; 32 | import java.util.Objects; 33 | import java.util.Optional; 34 | 35 | /** 36 | * Skeleton abstract class to make it easier to implement image classifier. 37 | * It provides implementation of Classifier interface for images, along with 38 | * image factory for specific type of images. 39 | * This class solves the problem of using various implementation of images and machine learning models in Java, 40 | * and provides standard Classifier API for clients. 41 | *

42 | * By default the type of key in the Map the {@link ImageClassifier} is {@code String} 43 | * 44 | * @param class to classify 45 | * @param class of machine learning model 46 | * @since 1.0 47 | */ 48 | public abstract class AbstractImageClassifier implements ImageClassifier, ModelProvider { 49 | 50 | private final ImageFactory imageFactory; 51 | private MODEL_CLASS model; 52 | 53 | // TODO: this should ba a part of every classifier 54 | private float threshold = 0.0f; 55 | 56 | protected AbstractImageClassifier(final Class imgCls, final MODEL_CLASS model) { 57 | final Optional> optionalImageFactory = ServiceProvider.current() 58 | .getImageFactoryService() 59 | .getByImageType(imgCls); 60 | if (!optionalImageFactory.isPresent()) { 61 | throw new IllegalArgumentException(String.format("Could not find ImageFactory by '%s'", BufferedImage.class.getName())); 62 | } 63 | imageFactory = optionalImageFactory.get(); 64 | setModel(model); 65 | } 66 | 67 | public ImageFactory getImageFactory() { 68 | return imageFactory; 69 | } 70 | 71 | @Override 72 | public Map classify(Path path) throws ClassificationException { 73 | IMAGE_CLASS image; 74 | try { 75 | image = imageFactory.getImage(path); 76 | return classify(image); 77 | } catch (IOException e) { 78 | throw new ClassificationException("Failed to transform input into a BufferedImage", e); 79 | } 80 | } 81 | 82 | @Override 83 | public Map classify(InputStream inputStream) throws ClassificationException { 84 | IMAGE_CLASS image; 85 | try { 86 | image = imageFactory.getImage(inputStream); 87 | return classify(image); 88 | } catch (IOException e) { 89 | throw new ClassificationException("Failed to transform input into a BufferedImage", e); 90 | } 91 | } 92 | 93 | // todo: provide get top 1, 3, 5 results; sort and get 94 | 95 | @Override 96 | public MODEL_CLASS getModel() { 97 | return model; 98 | } 99 | 100 | protected final void setModel(MODEL_CLASS model) { 101 | this.model = Objects.requireNonNull(model, "Model cannot bu null!"); 102 | } 103 | 104 | public float getThreshold() { 105 | return threshold; 106 | } 107 | 108 | public void setThreshold(float threshold) { 109 | this.threshold = threshold; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/classification/FeedForwardNetBinaryClassifier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.classification; 21 | 22 | import deepnetts.net.FeedForwardNetwork; 23 | 24 | import javax.visrec.ml.classification.NeuralNetBinaryClassifier; 25 | import javax.visrec.ml.model.ModelProvider; 26 | 27 | /** 28 | * Implementation of a classifier using Feed Forward neural network in background for binary classification tasks. 29 | */ 30 | public class FeedForwardNetBinaryClassifier implements ModelProvider, NeuralNetBinaryClassifier { 31 | 32 | private final FeedForwardNetwork model; 33 | private float threshold; 34 | 35 | public FeedForwardNetBinaryClassifier(FeedForwardNetwork model) { 36 | this.model = model; 37 | } 38 | 39 | @Override 40 | public FeedForwardNetwork getModel() { 41 | return model; 42 | } 43 | 44 | public static NeuralNetBinaryClassifier.Builder builder() { 45 | return NeuralNetBinaryClassifier.builder(); 46 | } 47 | 48 | @Override 49 | public Float classify(float[] inputs) { 50 | model.setInput(inputs); 51 | return model.getOutput()[0]; 52 | } 53 | 54 | public float getThreshold() { 55 | return threshold; 56 | } 57 | 58 | public void setThreshold(float threshold) { 59 | this.threshold = threshold; 60 | } 61 | 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/classification/ImageClassifierNetwork.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.classification; 21 | 22 | import deepnetts.data.ExampleImage; 23 | import deepnetts.net.ConvolutionalNetwork; 24 | 25 | import java.awt.image.BufferedImage; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | /** 30 | * Implementation of abstract image classifier for BufferedImage-s using 31 | * Convolutional network form Deep Netts. 32 | */ 33 | public class ImageClassifierNetwork extends AbstractImageClassifier { 34 | 35 | // it seems that these are not used at the end, onlz in builder. Do we need them exposed here__ 36 | private int inputWidth, inputHeight; 37 | 38 | public ImageClassifierNetwork(ConvolutionalNetwork network) { 39 | super(BufferedImage.class, network); 40 | } 41 | 42 | @Override 43 | public Map classify(BufferedImage inputImage) { 44 | // create input for neural network from image 45 | ExampleImage exImage = new ExampleImage(inputImage); 46 | 47 | // get underlying ML model, in this case convolutional network 48 | ConvolutionalNetwork neuralNet = getModel(); 49 | // set neural network input and get outputs 50 | neuralNet.setInput(exImage.getInput()); 51 | float[] outputs = neuralNet.getOutput(); 52 | 53 | // get all class labels with corresponding output larger then classification threshold 54 | Map results = new HashMap<>(); 55 | for (int i = 0; i < outputs.length; i++) { 56 | if (outputs[i] >= getThreshold()) 57 | results.put(neuralNet.getOutputLabel(i), outputs[i]); 58 | } 59 | 60 | return results; 61 | } 62 | 63 | public int getInputWidth() { 64 | return inputWidth; 65 | } 66 | 67 | public int getInputHeight() { 68 | return inputHeight; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/classification/MultiClassClassifierNetwork.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.classification; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import deepnetts.net.FeedForwardNetwork; 24 | import deepnetts.net.layers.activation.ActivationType; 25 | import deepnetts.net.loss.LossType; 26 | import deepnetts.net.train.BackpropagationTrainer; 27 | import deepnetts.util.Tensor; 28 | 29 | import javax.visrec.ml.classification.AbstractMultiClassClassifier; 30 | import java.util.HashMap; 31 | import java.util.Map; 32 | import javax.visrec.ml.data.DataSet; 33 | 34 | public class MultiClassClassifierNetwork extends AbstractMultiClassClassifier { 35 | 36 | @Override 37 | public Map classify(float[] input) { 38 | FeedForwardNetwork model = getModel(); 39 | model.setInput(Tensor.create(1, input.length, input)); //TODO: put array to input tensor placeholder 40 | float[] outputs = model.getOutput(); 41 | String[] labels = model.getOutputLabels(); 42 | Map result = new HashMap<>(); 43 | for(int i=0; i { 54 | private MultiClassClassifierNetwork building = new MultiClassClassifierNetwork(); 55 | 56 | private float learningRate = 0.01f; 57 | private float maxError = 0.03f; 58 | private long maxEpochs = Long.MAX_VALUE; 59 | private int inputsNum; 60 | private int outputsNum; 61 | private int[] hiddenLayers; 62 | 63 | private DataSet trainingSet; 64 | 65 | @Override 66 | public MultiClassClassifierNetwork build() { 67 | // Network architecture as Map/properties, json? 68 | FeedForwardNetwork.Builder builder = FeedForwardNetwork.builder() 69 | .addInputLayer(inputsNum); 70 | for(int h : hiddenLayers) { 71 | builder.addFullyConnectedLayer(h, ActivationType.TANH); 72 | } 73 | 74 | builder.addOutputLayer(outputsNum, ActivationType.SOFTMAX) 75 | .lossFunction(LossType.CROSS_ENTROPY) 76 | .hiddenActivationFunction(ActivationType.TANH); 77 | 78 | FeedForwardNetwork model = builder.build(); 79 | 80 | // aslo can be replaced with model.getTrainer() 81 | BackpropagationTrainer trainer = new BackpropagationTrainer(model); // model as param in constructor 82 | trainer.setLearningRate(learningRate) 83 | .setMaxError(maxError) 84 | .setMaxEpochs(maxEpochs); 85 | 86 | if (trainingSet!=null) 87 | trainer.train(trainingSet); // move model to constructor 88 | 89 | building.setModel(model); 90 | 91 | return building; 92 | } 93 | 94 | public Builder learningRate(float learningRate) { 95 | this.learningRate = learningRate; 96 | return this; 97 | } 98 | 99 | public Builder maxError(float maxError) { 100 | this.maxError = maxError; 101 | return this; 102 | } 103 | 104 | public Builder maxEpochs(int maxEpochs) { 105 | this.maxEpochs = maxEpochs; 106 | return this; 107 | } 108 | 109 | public Builder inputsNum(int inputsNum) { 110 | this.inputsNum = inputsNum; 111 | return this; 112 | } 113 | 114 | public Builder outputsNum(int outputsNum) { 115 | this.outputsNum = outputsNum; 116 | return this; 117 | } 118 | 119 | public Builder hiddenLayers(int... hiddenLayers) { 120 | this.hiddenLayers = hiddenLayers; 121 | return this; 122 | } 123 | 124 | public Builder trainingSet(DataSet trainingSet) { 125 | this.trainingSet = trainingSet; 126 | return this; 127 | } 128 | 129 | 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/classification/ZeroRuleClassifier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.classification; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | import javax.visrec.ml.classification.Classifier; 26 | import javax.visrec.ml.data.BasicDataSet; 27 | import javax.visrec.ml.data.DataSet; 28 | 29 | 30 | /** 31 | * Zero rule classifier always returns as a result the most frequent class from the data set. 32 | * It is used for benchmarking purposes: if a model performs worse than that, then it is useless. 33 | */ 34 | public class ZeroRuleClassifier implements Classifier> { 35 | 36 | R mostFrequentClass; 37 | 38 | @Override 39 | public Map classify(T input) { 40 | Map map = new HashMap<>(); 41 | map.put(mostFrequentClass, 1.0f); 42 | return map; 43 | } 44 | 45 | public static ZeroRuleClassifierBuilder builder() { 46 | return new ZeroRuleClassifierBuilder(); 47 | } 48 | 49 | 50 | public static class ZeroRuleClassifierBuilder { 51 | ZeroRuleClassifier buildingBlock; 52 | 53 | 54 | public ZeroRuleClassifierBuilder() { 55 | buildingBlock = new ZeroRuleClassifier(); 56 | } 57 | 58 | public ZeroRuleClassifierBuilder trainingSet(DataSet dataSet) { 59 | int[] targetClassCount = new int[dataSet.get(0).getTargetOutput().size()]; // dataSet.getTargetNames().length 60 | // iterate entire data set 61 | for(MLDataItem ml : dataSet.getItems()) { 62 | float[] cols = ml.getTargetOutput().getValues(); // get output/target columns 63 | for(int i=0; i max) { 82 | max = classCount[i]; 83 | maxIdx = i; 84 | } 85 | } 86 | return maxIdx; 87 | } 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/detection/AbstractObjectDetector.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.detection; 21 | 22 | import javax.visrec.ri.ml.classification.AbstractImageClassifier; 23 | import javax.visrec.ml.classification.ClassificationException; 24 | import javax.visrec.ml.detection.BoundingBox; 25 | import javax.visrec.ml.detection.ObjectDetector; 26 | import java.awt.image.BufferedImage; 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | import java.nio.file.Path; 30 | import java.util.List; 31 | import java.util.Map; 32 | import java.util.Objects; 33 | 34 | /** 35 | * Abstract object detector which implements {@link ObjectDetector} to return the positions 36 | * of an object within the given image. 37 | */ 38 | public abstract class AbstractObjectDetector implements ObjectDetector { 39 | 40 | private AbstractImageClassifier imageClassifier; 41 | 42 | /** 43 | * Creates an instance of the object detector 44 | * 45 | * @param imageClassifier A {@link AbstractImageClassifier} which may not be null 46 | */ 47 | public AbstractObjectDetector(AbstractImageClassifier imageClassifier) { 48 | Objects.requireNonNull(imageClassifier, "A classifier is required for the object detector."); 49 | this.imageClassifier = imageClassifier; 50 | } 51 | 52 | /** 53 | * Scan entire image and return positions where object is detected 54 | * 55 | * @param image {@code IMAGE_CLASS} image 56 | * @return {@code Map} of {@link BoundingBox} of where the object 57 | * has been detected. 58 | */ 59 | @Override 60 | public abstract Map> detectObject(BufferedImage image) throws ClassificationException; 61 | 62 | /** 63 | * Detect the object based on the given {@code File}. 64 | * 65 | * @param path Image file. 66 | * @return {@code Map} of {@link BoundingBox} of where the object 67 | * has been detected. 68 | * @throws IOException if the image couldn't be retrieved from storage. 69 | * @throws ClassificationException when the detector was unable to classify and detect the input 70 | */ 71 | public Map> detect(Path path) throws IOException, ClassificationException { 72 | BufferedImage image = imageClassifier.getImageFactory().getImage(path); 73 | return detectObject(image); 74 | } 75 | 76 | /** 77 | * Detect the object based on the given {@code InputStream}. 78 | * 79 | * @param inStream {@code InputStream} of the image 80 | * @return {@code Map} of {@link BoundingBox} of where the object 81 | * has been detected. 82 | * @throws IOException if the image couldn't be retrieved from storage. 83 | * @throws ClassificationException when the detector was unable to classify and detect the input 84 | */ 85 | public Map> detect(InputStream inStream) throws IOException, ClassificationException { 86 | BufferedImage image = imageClassifier.getImageFactory().getImage(inStream); 87 | return detectObject(image); 88 | } 89 | 90 | /** 91 | * Subclasses should use this method to use the underlying image classifier 92 | * 93 | * @return configured {@link AbstractImageClassifier} of the {@link AbstractObjectDetector} 94 | */ 95 | public AbstractImageClassifier getImageClassifier() { 96 | return imageClassifier; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/detection/SimpleObjectDetector.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.detection; 21 | 22 | import javax.visrec.ri.ml.classification.AbstractImageClassifier; 23 | import javax.visrec.ml.classification.ClassificationException; 24 | import javax.visrec.ml.detection.BoundingBox; 25 | import java.awt.image.BufferedImage; 26 | import java.util.HashMap; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * A simple object detector. 32 | * 33 | */ 34 | public class SimpleObjectDetector extends AbstractObjectDetector { 35 | 36 | private double threshold = 0.5; 37 | 38 | /** 39 | * Creates an instance 40 | * 41 | * @param classifier A {@link AbstractImageClassifier} which may not be null 42 | */ 43 | public SimpleObjectDetector(AbstractImageClassifier classifier) { 44 | super(classifier); 45 | } 46 | 47 | /** 48 | * Scan image using brute force sliding window and return positions where 49 | * classifier returns score greater then threshold. 50 | *

51 | * get width and height of the image and scan image with classifier - apply 52 | * classifier to each position This is trivial implementation and should be 53 | * replaced with something better 54 | * 55 | * @param image {@code BufferedImage} to scan 56 | * @return A {@code Map} of {@link BoundingBox} which contain 57 | * the positions of the detected object. 58 | */ 59 | @Override 60 | public Map> detectObject(BufferedImage image) throws ClassificationException { 61 | Map> results = new HashMap<>(); 62 | 63 | int boxWidth = 64, boxHeight = 64; 64 | 65 | for (int y = 0; y < image.getHeight() - boxHeight; y++) { 66 | for (int x = 0; x < image.getWidth() - boxWidth; x++) { 67 | 68 | Map results2 = getImageClassifier().classify(image.getSubimage(x, y, boxWidth, boxHeight)); 69 | for (Map.Entry keyValPair : results2.entrySet()) { 70 | if (keyValPair.getValue() > threshold) { 71 | BoundingBox bbox = new BoundingBox(keyValPair.getKey(), keyValPair.getValue(), x, y, boxWidth, boxHeight); 72 | //results.put(keyValPair.getKey(), bboxes); add these to list 73 | } 74 | } 75 | } 76 | } 77 | 78 | return results; 79 | } 80 | 81 | /** 82 | * Get the threshold 83 | * 84 | * @return theshold as {@code double} 85 | */ 86 | public double getThreshold() { 87 | return threshold; 88 | } 89 | 90 | /** 91 | * Set the threshold 92 | * 93 | * @param threshold as {@code double} 94 | */ 95 | public void setThreshold(double threshold) { 96 | this.threshold = threshold; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/regression/LogisticRegressionNetwork.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.regression; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import deepnetts.net.FeedForwardNetwork; 24 | import deepnetts.net.layers.activation.ActivationType; 25 | import deepnetts.net.loss.LossType; 26 | import deepnetts.net.train.BackpropagationTrainer; 27 | import deepnetts.util.Tensor; 28 | 29 | import javax.visrec.ml.classification.LogisticRegression; 30 | import javax.visrec.ml.data.DataSet; 31 | 32 | 33 | /** 34 | * Logistic regresion algorithm implemented by neural network. 35 | */ 36 | public class LogisticRegressionNetwork extends LogisticRegression { 37 | 38 | @Override 39 | public Float classify(float[] input) { 40 | FeedForwardNetwork model = getModel(); 41 | model.setInput(Tensor.create(1, input.length, input)); //TODO: put array to input tensor placeholder 42 | return model.getOutput()[0]; 43 | } 44 | 45 | public static Builder builder() { 46 | return new Builder(); 47 | } 48 | 49 | 50 | 51 | public static class Builder implements javax.visrec.ml.model.ModelBuilder { 52 | 53 | private float learningRate = 0.01f; 54 | private float maxError = 0.03f; 55 | private int maxEpochs = 1000; 56 | private int inputsNum; 57 | 58 | private DataSet trainingSet; // replace with DataSet from visrec 59 | 60 | public Builder inputsNum(int inputsNum) { 61 | this.inputsNum = inputsNum; 62 | return this; 63 | } 64 | 65 | public Builder learningRate(float learningRate) { 66 | this.learningRate = learningRate; 67 | return this; 68 | } 69 | 70 | public Builder maxError(float maxError) { 71 | this.maxError = maxError; 72 | return this; 73 | } 74 | 75 | public Builder maxEpochs(int maxEpochs) { 76 | this.maxEpochs = maxEpochs; 77 | return this; 78 | } 79 | 80 | public Builder trainingSet(DataSet trainingSet) { 81 | this.trainingSet = trainingSet; 82 | return this; 83 | } 84 | 85 | // test set 86 | // target accuracy 87 | @Override 88 | public LogisticRegressionNetwork build() { 89 | FeedForwardNetwork model = FeedForwardNetwork.builder() 90 | .addInputLayer(inputsNum) 91 | .addOutputLayer(1, ActivationType.SIGMOID) 92 | .lossFunction(LossType.CROSS_ENTROPY) 93 | .build(); 94 | 95 | BackpropagationTrainer trainer = new BackpropagationTrainer(model); 96 | trainer.setLearningRate(learningRate) 97 | .setMaxEpochs(maxEpochs) 98 | .setMaxError(maxError); 99 | 100 | if (trainingSet != null) { 101 | trainer.train(trainingSet); 102 | } 103 | 104 | LogisticRegressionNetwork product = new LogisticRegressionNetwork(); 105 | product.setModel(model); 106 | return product; 107 | } 108 | 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/ml/regression/SimpleLinearRegressionNetwork.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.ml.regression; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import deepnetts.net.FeedForwardNetwork; 24 | import deepnetts.net.layers.activation.ActivationType; 25 | import deepnetts.net.loss.LossType; 26 | import deepnetts.net.train.BackpropagationTrainer; 27 | import deepnetts.util.Tensor; 28 | 29 | import javax.visrec.ml.regression.SimpleLinearRegression; 30 | import javax.visrec.ml.data.DataSet; 31 | 32 | /** 33 | * Simple linear regression implemented Feed Forward Neural Network as a back-end. 34 | * 35 | * @see SimpleLinearRegression 36 | */ 37 | public class SimpleLinearRegressionNetwork extends SimpleLinearRegression { 38 | 39 | private final float[] input = new float[1]; 40 | private final Tensor inputTensor = Tensor.create(1, 1, input); 41 | 42 | private float slope; 43 | private float intercept; 44 | 45 | @Override 46 | public Float predict(Float inputs) { 47 | input[0] = inputs; 48 | FeedForwardNetwork ffn = getModel(); 49 | ffn.setInput(inputTensor); 50 | float[] output = ffn.getOutput(); 51 | return output[0]; 52 | } 53 | 54 | public static Builder builder() { 55 | return new Builder(); 56 | } 57 | 58 | @Override 59 | public float getSlope() { 60 | return slope; 61 | } 62 | 63 | @Override 64 | public float getIntercept() { 65 | return intercept; 66 | } 67 | 68 | 69 | public static class Builder implements javax.visrec.ml.model.ModelBuilder { 70 | private SimpleLinearRegressionNetwork buildingBlock = new SimpleLinearRegressionNetwork(); 71 | 72 | private float learningRate = 0.01f; 73 | private float maxError = 0.03f; 74 | private int maxEpochs = 1000; 75 | 76 | private DataSet trainingSet; // replace with DataSet from visrec 77 | 78 | 79 | public Builder learningRate(float learningRate) { 80 | this.learningRate = learningRate; 81 | return this; 82 | } 83 | 84 | public Builder maxError(float maxError) { 85 | this.maxError = maxError; 86 | return this; 87 | } 88 | 89 | public Builder maxEpochs(int maxEpochs) { 90 | this.maxEpochs = maxEpochs; 91 | return this; 92 | } 93 | 94 | public Builder trainingSet(DataSet trainingSet) { 95 | this.trainingSet = trainingSet; 96 | return this; 97 | } 98 | 99 | // test set 100 | // target accuracy 101 | 102 | @Override 103 | public SimpleLinearRegressionNetwork build() { 104 | FeedForwardNetwork model= FeedForwardNetwork.builder() 105 | .addInputLayer(1) 106 | .addOutputLayer(1, ActivationType.LINEAR) 107 | .lossFunction(LossType.MEAN_SQUARED_ERROR) 108 | .build(); 109 | 110 | BackpropagationTrainer trainer = new BackpropagationTrainer(model); 111 | trainer.setLearningRate(learningRate) 112 | .setMaxError(maxError) 113 | .setMaxEpochs(maxEpochs); 114 | trainer.train(trainingSet); 115 | 116 | buildingBlock.intercept = model.getOutputLayer().getBiases()[0]; 117 | buildingBlock.slope = model.getOutputLayer().getWeights().get(0); 118 | 119 | buildingBlock.setModel(model); 120 | return buildingBlock; 121 | } 122 | 123 | 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/spi/BufferedImageClassifierFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.spi; 21 | 22 | import deepnetts.data.ImageSet; 23 | import deepnetts.net.ConvolutionalNetwork; 24 | import deepnetts.net.train.BackpropagationTrainer; 25 | import deepnetts.net.train.opt.OptimizerType; 26 | import deepnetts.util.DeepNettsException; 27 | import deepnetts.util.FileIO; 28 | 29 | import javax.visrec.ml.classification.ImageClassifier; 30 | import javax.visrec.ml.classification.NeuralNetImageClassifier; 31 | import javax.visrec.ml.model.ModelCreationException; 32 | import javax.visrec.ri.ml.classification.ImageClassifierNetwork; 33 | import javax.visrec.spi.ImageClassifierFactory; 34 | import java.awt.image.BufferedImage; 35 | import java.io.FileInputStream; 36 | import java.io.FileNotFoundException; 37 | import java.io.IOException; 38 | import java.io.ObjectInputStream; 39 | import java.util.logging.Logger; 40 | 41 | public class BufferedImageClassifierFactory implements ImageClassifierFactory { 42 | 43 | private static final Logger LOGGER = Logger.getLogger(BufferedImageClassifierFactory.class.getName()); 44 | 45 | @Override 46 | public Class getImageClass() { 47 | return BufferedImage.class; 48 | } 49 | 50 | @Override 51 | public ImageClassifier create(NeuralNetImageClassifier.BuildingBlock block) throws ModelCreationException { 52 | if (block.getImportPath() != null) { 53 | return onImport(block); 54 | } 55 | return onCreate(block); 56 | } 57 | 58 | private ImageClassifier onImport(NeuralNetImageClassifier.BuildingBlock block) throws ModelCreationException { 59 | try { 60 | ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(block.getImportPath().toFile())); 61 | ConvolutionalNetwork model = (ConvolutionalNetwork) inputStream.readObject(); 62 | return new ImageClassifierNetwork(model); 63 | } catch (IOException | ClassNotFoundException e) { 64 | throw new ModelCreationException("Failed to import existing model", e); 65 | } 66 | } 67 | 68 | private ImageClassifier onCreate(NeuralNetImageClassifier.BuildingBlock block) throws ModelCreationException { 69 | ImageSet imageSet = new ImageSet(block.getImageWidth(), block.getImageHeight()); 70 | LOGGER.info("Loading images..."); 71 | 72 | imageSet.loadLabels(block.getLabelsPath().toFile()); 73 | try { 74 | imageSet.loadImages(block.getTrainingPath().toFile()); 75 | imageSet.shuffle(); 76 | } catch (DeepNettsException | FileNotFoundException ex) { 77 | throw new ModelCreationException("Failed to load images from dataset", ex); 78 | } 79 | 80 | LOGGER.info("Done!"); 81 | LOGGER.info("Creating neural network..."); 82 | 83 | ConvolutionalNetwork neuralNet = null; 84 | try { 85 | neuralNet = (ConvolutionalNetwork) FileIO.createFromJson(block.getNetworkArchitecture().toFile()); 86 | neuralNet.setOutputLabels(imageSet.getTargetColumnsNames()); 87 | } catch (IOException ex) { 88 | throw new ModelCreationException("Failed to create convolutional network from JSON file", ex); 89 | } 90 | 91 | LOGGER.info("Done!"); 92 | LOGGER.info("Training neural network"); 93 | 94 | // create a set of convolutional networks and do training, crossvalidation and performance evaluation 95 | BackpropagationTrainer trainer = new BackpropagationTrainer(neuralNet) 96 | .setLearningRate(block.getLearningRate()) 97 | .setMomentum(0.7f) 98 | .setMaxError(block.getMaxError()) 99 | .setMaxEpochs(block.getMaxEpochs()) 100 | .setBatchMode(false) 101 | .setOptimizer(OptimizerType.SGD); 102 | trainer.train(imageSet); 103 | 104 | ImageClassifierNetwork imageClassifier = new ImageClassifierNetwork(neuralNet); 105 | try { 106 | FileIO.writeToFile(neuralNet, block.getExportPath().toFile().getAbsolutePath()); 107 | } catch (IOException ex) { 108 | throw new ModelCreationException("Failed to write trained model to file", ex); 109 | } 110 | 111 | imageClassifier.setThreshold(block.getThreshold()); 112 | 113 | return imageClassifier; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/spi/DeepNettsImplementationService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.spi; 21 | 22 | import javax.visrec.spi.ImplementationService; 23 | 24 | /** 25 | * DeepNetts' {@link ImplementationService} 26 | */ 27 | public class DeepNettsImplementationService extends ImplementationService { 28 | 29 | /** {@inheritDoc} */ 30 | @Override 31 | public String getName() { 32 | return "DeepNetts"; 33 | } 34 | 35 | /** {@inheritDoc} */ 36 | @Override 37 | public String getVersion() { 38 | return "1.1"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/spi/DefaultImageFactoryService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.spi; 21 | 22 | import javax.visrec.ri.BufferedImageFactory; 23 | 24 | import javax.visrec.ImageFactory; 25 | import javax.visrec.spi.ImageFactoryService; 26 | import java.awt.image.BufferedImage; 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | import java.util.Objects; 30 | import java.util.Optional; 31 | 32 | /** 33 | * Default implementation of {@link ImageFactoryService} which serves the implementations of {@link ImageFactory}. 34 | * 35 | */ 36 | public final class DefaultImageFactoryService implements ImageFactoryService { 37 | 38 | private static final Map, ImageFactory> imageFactories; 39 | static { 40 | imageFactories = new HashMap<>(); 41 | imageFactories.put(BufferedImage.class, new BufferedImageFactory()); 42 | } 43 | 44 | /** 45 | * Get the {@link ImageFactory} by image type. 46 | * @param imageCls image type in {@link Class} object which is able to 47 | * be processed by the image factory implementation. 48 | * @param image type. 49 | * @return {@link ImageFactory} wrapped in {@link Optional}. If the {@link ImageFactory} could not be 50 | * found then the {@link Optional} would contain null. 51 | */ 52 | @Override 53 | public Optional> getByImageType(Class imageCls) { 54 | Objects.requireNonNull(imageCls, "imageCls == null"); 55 | ImageFactory imageFactory = imageFactories.get(imageCls); 56 | return Optional.ofNullable((ImageFactory) imageFactory); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/spi/DefaultServiceProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.spi; 21 | 22 | import javax.visrec.spi.ImageFactoryService; 23 | import javax.visrec.spi.ImplementationService; 24 | import javax.visrec.spi.ServiceProvider; 25 | 26 | /** 27 | * Default {@link ServiceProvider} of the implementation of the visual recognition API 28 | * 29 | */ 30 | public final class DefaultServiceProvider extends ServiceProvider { 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | @Override 36 | public ImageFactoryService getImageFactoryService() { 37 | return new DefaultImageFactoryService(); 38 | } 39 | 40 | /** 41 | * {@inheritDoc} 42 | */ 43 | @Override 44 | public ImplementationService getImplementationService() { 45 | return new DeepNettsImplementationService(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/spi/FloatArrayBinaryClassifierFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.spi; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import deepnetts.data.TabularDataSet; 24 | import deepnetts.net.FeedForwardNetwork; 25 | import deepnetts.net.layers.activation.ActivationType; 26 | import deepnetts.net.loss.LossType; 27 | 28 | import javax.visrec.ml.classification.BinaryClassifier; 29 | import javax.visrec.ml.model.ModelCreationException; 30 | import javax.visrec.ml.classification.NeuralNetBinaryClassifier; 31 | import javax.visrec.ri.ml.classification.FeedForwardNetBinaryClassifier; 32 | import javax.visrec.ri.util.DataSets; 33 | import javax.visrec.spi.BinaryClassifierFactory; 34 | import java.io.IOException; 35 | 36 | public class FloatArrayBinaryClassifierFactory implements BinaryClassifierFactory { 37 | 38 | @Override 39 | public Class getTargetClass() { 40 | return float[].class; 41 | } 42 | 43 | @Override 44 | public BinaryClassifier create(NeuralNetBinaryClassifier.BuildingBlock block) throws ModelCreationException { 45 | FeedForwardNetwork.Builder ffnBuilder = FeedForwardNetwork.builder(); 46 | ffnBuilder.addInputLayer(block.getInputsNum()); 47 | 48 | for (int h : block.getHiddenLayers()) { 49 | ffnBuilder.addFullyConnectedLayer(h); 50 | } 51 | 52 | ffnBuilder.addOutputLayer(1, ActivationType.SIGMOID) 53 | .lossFunction(LossType.CROSS_ENTROPY); 54 | 55 | FeedForwardNetwork ffn = ffnBuilder.build(); 56 | ffn.getTrainer() 57 | .setMaxEpochs(block.getMaxEpochs()) 58 | .setMaxError(block.getMaxError()) 59 | .setLearningRate(block.getLearningRate()); 60 | 61 | TabularDataSet trainingSet = null; 62 | try { 63 | trainingSet = DataSets.readCsv(block.getTrainingPath().toFile(), block.getInputsNum(), 1, true, ","); 64 | //deepnetts.data.DataSets.normalizeMax(trainingSet); 65 | } catch (IOException e) { 66 | throw new ModelCreationException("Failed to create training set based on training file", e); 67 | } 68 | ffn.train(trainingSet); 69 | FeedForwardNetBinaryClassifier ffnbc = new FeedForwardNetBinaryClassifier(ffn); 70 | ffnbc.setThreshold(block.getThreshold()); 71 | 72 | return ffnbc; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/javax/visrec/ri/util/DataSets.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package javax.visrec.ri.util; 21 | 22 | import deepnetts.data.MLDataItem; 23 | import deepnetts.data.TabularDataSet; 24 | import deepnetts.util.DeepNettsException; 25 | 26 | import java.io.*; 27 | 28 | public class DataSets { 29 | 30 | private DataSets() { 31 | // Prevent instantiation 32 | } 33 | 34 | /** 35 | * Creates and returns data set from specified CSV file. Empty lines are 36 | * skipped 37 | * 38 | * @param csvFile CSV file 39 | * @param numInputs number of input values in a row 40 | * @param numOutputs number of output values in a row 41 | * @param hasColumnNames true if first row contains column names 42 | * @param delimiter delimiter used to separate values 43 | * @return instance of data set with values loaded from file 44 | * 45 | * @throws FileNotFoundException if file was not found 46 | * @throws IOException if there was an error reading file 47 | * 48 | * TODO: Detect if there are labels in the first line, if there are no 49 | * labels, set class1, class2, class3 in classifier evaluation! and detect 50 | * type of attributes Move this method to some factory class or something? 51 | * or as a default method in data set? 52 | * 53 | * TODO: should I wrap IO with DeepNetts Exception? 54 | * Autodetetect delimiter; header and column type 55 | * 56 | */ 57 | public static TabularDataSet readCsv(File csvFile, int numInputs, int numOutputs, boolean hasColumnNames, String delimiter) throws FileNotFoundException, IOException { 58 | TabularDataSet dataSet = new TabularDataSet<>(numInputs, numOutputs); 59 | BufferedReader br = new BufferedReader(new FileReader(csvFile)); 60 | String line=null; 61 | // auto detect column names - ako sadrzi slova onda ima imena. Sta ako su atributi nominalni? U ovoj fazi se pretpostavlja d anisu... 62 | // i ako u redovima ispod takodje ima stringova u istoj koloni - detect header 63 | if (hasColumnNames) { // get col names from the first line 64 | line = br.readLine().trim(); 65 | String[] colNames = line.split(delimiter); 66 | // todo checsk number of col names 67 | dataSet.setColumnNames(colNames); 68 | } else { 69 | String[] colNames = new String[numInputs+numOutputs]; 70 | for(int i=0; i> imageFactory = ServiceProvider.current().getImageFactoryService().getByImageType(BufferedImage.class); 45 | assertTrue(imageFactory.isPresent()); 46 | // If the casting fails, the implementation is incorrect and it will fail the test. 47 | BufferedImageFactory.class.cast(imageFactory.get()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/visrec/ri/util/BuilderConfigurationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Recognition API for Java, JSR381 3 | * Copyright (C) 2020 Zoran Sevarac, Frank Greco 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | 20 | package visrec.ri.util; 21 | 22 | import org.junit.jupiter.api.Test; 23 | 24 | import javax.visrec.ml.model.ModelCreationException; 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals; 29 | import static org.junit.jupiter.api.Assertions.fail; 30 | import javax.visrec.ml.model.ModelBuilder; 31 | 32 | /** 33 | * @author Kevin Berendsen 34 | */ 35 | public class BuilderConfigurationTest { 36 | 37 | /** 38 | * Successfully build the output of the builder. 39 | */ 40 | @Test 41 | public void testReflectionInvocationBuild() throws ModelCreationException { 42 | Map trainingSet = new HashMap<>(); 43 | trainingSet.put("hello", "world"); 44 | trainingSet.put("lorem", "ipsum"); 45 | Map configuration = new HashMap<>(); 46 | configuration.put("trainingSet", trainingSet); 47 | 48 | BuilderImpl builder = new BuilderImpl(); 49 | String output = builder.build(configuration); 50 | assertEquals("{lorem=ipsum, hello=world}", output); 51 | } 52 | 53 | /** 54 | * The trainingSet method is invoked without the valid parameter and should 55 | * throw an exception. 56 | */ 57 | @Test 58 | public void testInvalidParameterForMethod() { 59 | String trainingSet = "invalid"; 60 | Map configuration = new HashMap<>(); 61 | configuration.put("trainingSet", trainingSet); 62 | 63 | BuilderImpl builder = new BuilderImpl(); 64 | try { 65 | builder.build(configuration); 66 | fail("The configuration is invalid and should throw the InvalidBuilderConfigurationException"); 67 | } catch (ModelCreationException e) { 68 | /* Expected */ 69 | } 70 | } 71 | 72 | 73 | 74 | public static class BuilderImpl implements ModelBuilder { 75 | 76 | private Map trainingSet; 77 | 78 | public void trainingSet(Map trainingSet) { 79 | this.trainingSet = trainingSet; 80 | } 81 | 82 | @Override 83 | public String build() { 84 | return trainingSet.toString(); 85 | } 86 | } 87 | 88 | } 89 | --------------------------------------------------------------------------------