├── .gitignore ├── LICENSE ├── LabelImage.java ├── README.md ├── TensorFlowExample.java ├── download.sh ├── images └── example-400x288.jpg ├── jni.sh ├── label.sh ├── lib └── README.md ├── models ├── imagenet_comp_graph_label_strings.txt ├── inception5h.zip └── tensorflow_inception_graph.pb ├── process.sh └── test.sh /.gitignore: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | .DS_Store 3 | LabelImage$GraphBuilder.class LabelImage.class TensorFlowExample.class 4 | jni/LICENSE jni/libtensorflow_jni.dylib 5 | ======= 6 | *.class 7 | 8 | # Mobile Tools for Java (J2ME) 9 | .mtj.tmp/ 10 | 11 | # Package Files # 12 | *.jar 13 | *.war 14 | *.ear 15 | 16 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 17 | hs_err_pid* 18 | >>>>>>> ae7fd4603e8557d5d53f81c40b8e51a0ad8f06da 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Loreto Parisi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LabelImage.java: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | ==============================================================================*/ 15 | 16 | import java.io.IOException; 17 | import java.io.PrintStream; 18 | import java.nio.charset.Charset; 19 | import java.nio.file.Files; 20 | import java.nio.file.Path; 21 | import java.nio.file.Paths; 22 | import java.util.Arrays; 23 | import java.util.List; 24 | import org.tensorflow.DataType; 25 | import org.tensorflow.Graph; 26 | import org.tensorflow.Output; 27 | import org.tensorflow.Session; 28 | import org.tensorflow.Tensor; 29 | import org.tensorflow.TensorFlow; 30 | import org.tensorflow.types.UInt8; 31 | 32 | /** Sample use of the TensorFlow Java API to label images using a pre-trained model. */ 33 | public class LabelImage { 34 | private static void printUsage(PrintStream s) { 35 | final String url = 36 | "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip"; 37 | s.println( 38 | "Java program that uses a pre-trained Inception model (http://arxiv.org/abs/1512.00567)"); 39 | s.println("to label JPEG images."); 40 | s.println("TensorFlow version: " + TensorFlow.version()); 41 | s.println(); 42 | s.println("Usage: label_image "); 43 | s.println(); 44 | s.println("Where:"); 45 | s.println(" is a directory containing the unzipped contents of the inception model"); 46 | s.println(" (from " + url + ")"); 47 | s.println(" is the path to a JPEG image file"); 48 | } 49 | 50 | public static void main(String[] args) { 51 | if (args.length != 2) { 52 | printUsage(System.err); 53 | System.exit(1); 54 | } 55 | String modelDir = args[0]; 56 | String imageFile = args[1]; 57 | 58 | byte[] graphDef = readAllBytesOrExit(Paths.get(modelDir, "tensorflow_inception_graph.pb")); 59 | List labels = 60 | readAllLinesOrExit(Paths.get(modelDir, "imagenet_comp_graph_label_strings.txt")); 61 | byte[] imageBytes = readAllBytesOrExit(Paths.get(imageFile)); 62 | 63 | try (Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes)) { 64 | float[] labelProbabilities = executeInceptionGraph(graphDef, image); 65 | int bestLabelIdx = maxIndex(labelProbabilities); 66 | System.out.println( 67 | String.format("BEST MATCH: %s (%.2f%% likely)", 68 | labels.get(bestLabelIdx), 69 | labelProbabilities[bestLabelIdx] * 100f)); 70 | } 71 | } 72 | 73 | private static Tensor constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) { 74 | try (Graph g = new Graph()) { 75 | GraphBuilder b = new GraphBuilder(g); 76 | // Some constants specific to the pre-trained model at: 77 | // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip 78 | // 79 | // - The model was trained with images scaled to 224x224 pixels. 80 | // - The colors, represented as R, G, B in 1-byte each were converted to 81 | // float using (value - Mean)/Scale. 82 | final int H = 224; 83 | final int W = 224; 84 | final float mean = 117f; 85 | final float scale = 1f; 86 | 87 | // Since the graph is being constructed once per execution here, we can use a constant for the 88 | // input image. If the graph were to be re-used for multiple input images, a placeholder would 89 | // have been more appropriate. 90 | final Output input = b.constant("input", imageBytes); 91 | final Output output = 92 | b.div( 93 | b.sub( 94 | b.resizeBilinear( 95 | b.expandDims( 96 | b.cast(b.decodeJpeg(input, 3), Float.class), 97 | b.constant("make_batch", 0)), 98 | b.constant("size", new int[] {H, W})), 99 | b.constant("mean", mean)), 100 | b.constant("scale", scale)); 101 | try (Session s = new Session(g)) { 102 | return s.runner().fetch(output.op().name()).run().get(0).expect(Float.class); 103 | } 104 | } 105 | } 106 | 107 | private static float[] executeInceptionGraph(byte[] graphDef, Tensor image) { 108 | try (Graph g = new Graph()) { 109 | g.importGraphDef(graphDef); 110 | try (Session s = new Session(g); 111 | Tensor result = 112 | s.runner().feed("input", image).fetch("output").run().get(0).expect(Float.class)) { 113 | final long[] rshape = result.shape(); 114 | if (result.numDimensions() != 2 || rshape[0] != 1) { 115 | throw new RuntimeException( 116 | String.format( 117 | "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s", 118 | Arrays.toString(rshape))); 119 | } 120 | int nlabels = (int) rshape[1]; 121 | return result.copyTo(new float[1][nlabels])[0]; 122 | } 123 | } 124 | } 125 | 126 | private static int maxIndex(float[] probabilities) { 127 | int best = 0; 128 | for (int i = 1; i < probabilities.length; ++i) { 129 | if (probabilities[i] > probabilities[best]) { 130 | best = i; 131 | } 132 | } 133 | return best; 134 | } 135 | 136 | private static byte[] readAllBytesOrExit(Path path) { 137 | try { 138 | return Files.readAllBytes(path); 139 | } catch (IOException e) { 140 | System.err.println("Failed to read [" + path + "]: " + e.getMessage()); 141 | System.exit(1); 142 | } 143 | return null; 144 | } 145 | 146 | private static List readAllLinesOrExit(Path path) { 147 | try { 148 | return Files.readAllLines(path, Charset.forName("UTF-8")); 149 | } catch (IOException e) { 150 | System.err.println("Failed to read [" + path + "]: " + e.getMessage()); 151 | System.exit(0); 152 | } 153 | return null; 154 | } 155 | 156 | // In the fullness of time, equivalents of the methods of this class should be auto-generated from 157 | // the OpDefs linked into libtensorflow_jni.so. That would match what is done in other languages 158 | // like Python, C++ and Go. 159 | static class GraphBuilder { 160 | GraphBuilder(Graph g) { 161 | this.g = g; 162 | } 163 | 164 | Output div(Output x, Output y) { 165 | return binaryOp("Div", x, y); 166 | } 167 | 168 | Output sub(Output x, Output y) { 169 | return binaryOp("Sub", x, y); 170 | } 171 | 172 | Output resizeBilinear(Output images, Output size) { 173 | return binaryOp3("ResizeBilinear", images, size); 174 | } 175 | 176 | Output expandDims(Output input, Output dim) { 177 | return binaryOp3("ExpandDims", input, dim); 178 | } 179 | 180 | Output cast(Output value, Class type) { 181 | DataType dtype = DataType.fromClass(type); 182 | return g.opBuilder("Cast", "Cast") 183 | .addInput(value) 184 | .setAttr("DstT", dtype) 185 | .build() 186 | .output(0); 187 | } 188 | 189 | Output decodeJpeg(Output contents, long channels) { 190 | return g.opBuilder("DecodeJpeg", "DecodeJpeg") 191 | .addInput(contents) 192 | .setAttr("channels", channels) 193 | .build() 194 | .output(0); 195 | } 196 | 197 | Output constant(String name, Object value, Class type) { 198 | try (Tensor t = Tensor.create(value, type)) { 199 | return g.opBuilder("Const", name) 200 | .setAttr("dtype", DataType.fromClass(type)) 201 | .setAttr("value", t) 202 | .build() 203 | .output(0); 204 | } 205 | } 206 | Output constant(String name, byte[] value) { 207 | return this.constant(name, value, String.class); 208 | } 209 | 210 | Output constant(String name, int value) { 211 | return this.constant(name, value, Integer.class); 212 | } 213 | 214 | Output constant(String name, int[] value) { 215 | return this.constant(name, value, Integer.class); 216 | } 217 | 218 | Output constant(String name, float value) { 219 | return this.constant(name, value, Float.class); 220 | } 221 | 222 | private Output binaryOp(String type, Output in1, Output in2) { 223 | return g.opBuilder(type, type).addInput(in1).addInput(in2).build().output(0); 224 | } 225 | 226 | private Output binaryOp3(String type, Output in1, Output in2) { 227 | return g.opBuilder(type, type).addInput(in1).addInput(in2).build().output(0); 228 | } 229 | private Graph g; 230 | } 231 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-java 2 | Tensorflow Java pipeline and examples. This simple Java pipeline for TensorFlow Java API supports *Tensorflow >= 1.4* and it has been tested with Tensorflow from *TF 1.4.0* to *TF 1.13.1*. :new: 3 | 4 | ## How To Install 5 | You need to run the `jni.sh` to install the right Java bindings for your platform and the `download.sh` script that will download the `inception5` model in order to be ready to run a simple examples: 6 | 7 | ```bash 8 | git clone https://github.com/loretoparisi/tensorflow-java.git \ 9 | cd tensorflow-java \ 10 | sh jni.sh \ 11 | sh download.sh \ 12 | ``` 13 | 14 | ## A simple Hello World example 15 | Create a simple Java class with a main to be executable and import `org.tensorflow.TensorFlow` 16 | 17 | ```java 18 | import org.tensorflow.TensorFlow; 19 | 20 | public class TensorFlowExample { 21 | public static void main(String[] args) { 22 | System.out.println("TensorFlowExample using TensorFlow version: " + TensorFlow.version()); 23 | } 24 | } 25 | ``` 26 | 27 | Save it and then from command line compile and run 28 | 29 | ```bash 30 | cd tensorflow-java 31 | javac -cp lib/libtensorflow-1.13.1.jar TensorFlowExample.java 32 | java -cp lib/libtensorflow-1.13.1.jar:. -Djava.library.path=./jni TensorFlowExample 33 | ``` 34 | 35 | If you get the TensorFlow version as output it worked! 36 | 37 | ```bash 38 | TensorFlowExample using TensorFlow version: 1.13.1 39 | ``` 40 | 41 | ## Real world (Inception) example 42 | We use the `LabelImage` official Tensorflow example to label an example image with the inception graph model. 43 | 44 | ``` 45 | $ javac -cp lib/libtensorflow-1.13.1.jar LabelImage.java 46 | $ java -cp lib/libtensorflow-1.13.1.jar:. -Djava.library.path=./jni LabelImage models/ images/example-400x288.jpg 47 | BEST MATCH: lakeside (19,00% likely) 48 | ``` 49 | 50 | ## Disclaimer 51 | This example is provided as it is and it is based on the official Tensorflow Java JNI wrapper and example available [here](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java). 52 | -------------------------------------------------------------------------------- /TensorFlowExample.java: -------------------------------------------------------------------------------- 1 | import org.tensorflow.TensorFlow; 2 | 3 | public class TensorFlowExample { 4 | public static void main(String[] args) { 5 | System.out.println("TensorFlowExample using TensorFlow version: " + TensorFlow.version()); 6 | } 7 | } -------------------------------------------------------------------------------- /download.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Downloading inception5 model..." 4 | curl https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip -o models/inception5h.zip -------------------------------------------------------------------------------- /images/example-400x288.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loretoparisi/tensorflow-java/fc7e4cd6fd58363e2b8661f9ad6dba30fc44909c/images/example-400x288.jpg -------------------------------------------------------------------------------- /jni.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Tensorflow Java 4 | # @author Loreto Parisi (loretoparisi at gmail dot com) 5 | # v1.0.0 6 | # @2017-2019 Loreto Parisi (loretoparisi at gmail dot com) 7 | # 8 | 9 | TF_TYPE="cpu" # Default processor is CPU. If you want GPU, set to "gpu" 10 | OS=$(uname -s | tr '[:upper:]' '[:lower:]') 11 | 12 | # get latest from https://mvnrepository.com/artifact/org.tensorflow/libtensorflow_jni 13 | TF_VERSION=1.13.1 14 | 15 | mkdir -p ./jni 16 | curl -L \ 17 | "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-${TF_TYPE}-${OS}-x86_64-${TF_VERSION}.tar.gz" | 18 | tar -xz -C ./jni 19 | 20 | curl -L \ 21 | -o lib/libtensorflow-${TF_VERSION}.jar "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_VERSION}.jar" 22 | -------------------------------------------------------------------------------- /label.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Tensorflow Java 4 | # @author Loreto Parisi (loretoparisi at gmail dot com) 5 | # v1.0.0 6 | # @2017-2019 Loreto Parisi (loretoparisi at gmail dot com) 7 | # 8 | 9 | 10 | TF_VERSION=1.13.1 11 | 12 | javac -cp lib/libtensorflow-${TF_VERSION}.jar LabelImage.java 13 | java -cp lib/libtensorflow-${TF_VERSION}.jar:. -Djava.library.path=./jni LabelImage models/ images/example-400x288.jpg 14 | 15 | -------------------------------------------------------------------------------- /lib/README.md: -------------------------------------------------------------------------------- 1 | Tensorflow Java library 2 | -------------------------------------------------------------------------------- /models/imagenet_comp_graph_label_strings.txt: -------------------------------------------------------------------------------- 1 | dummy 2 | kit fox 3 | English setter 4 | Siberian husky 5 | Australian terrier 6 | English springer 7 | grey whale 8 | lesser panda 9 | Egyptian cat 10 | ibex 11 | Persian cat 12 | cougar 13 | gazelle 14 | porcupine 15 | sea lion 16 | malamute 17 | badger 18 | Great Dane 19 | Walker hound 20 | Welsh springer spaniel 21 | whippet 22 | Scottish deerhound 23 | killer whale 24 | mink 25 | African elephant 26 | Weimaraner 27 | soft-coated wheaten terrier 28 | Dandie Dinmont 29 | red wolf 30 | Old English sheepdog 31 | jaguar 32 | otterhound 33 | bloodhound 34 | Airedale 35 | hyena 36 | meerkat 37 | giant schnauzer 38 | titi 39 | three-toed sloth 40 | sorrel 41 | black-footed ferret 42 | dalmatian 43 | black-and-tan coonhound 44 | papillon 45 | skunk 46 | Staffordshire bullterrier 47 | Mexican hairless 48 | Bouvier des Flandres 49 | weasel 50 | miniature poodle 51 | Cardigan 52 | malinois 53 | bighorn 54 | fox squirrel 55 | colobus 56 | tiger cat 57 | Lhasa 58 | impala 59 | coyote 60 | Yorkshire terrier 61 | Newfoundland 62 | brown bear 63 | red fox 64 | Norwegian elkhound 65 | Rottweiler 66 | hartebeest 67 | Saluki 68 | grey fox 69 | schipperke 70 | Pekinese 71 | Brabancon griffon 72 | West Highland white terrier 73 | Sealyham terrier 74 | guenon 75 | mongoose 76 | indri 77 | tiger 78 | Irish wolfhound 79 | wild boar 80 | EntleBucher 81 | zebra 82 | ram 83 | French bulldog 84 | orangutan 85 | basenji 86 | leopard 87 | Bernese mountain dog 88 | Maltese dog 89 | Norfolk terrier 90 | toy terrier 91 | vizsla 92 | cairn 93 | squirrel monkey 94 | groenendael 95 | clumber 96 | Siamese cat 97 | chimpanzee 98 | komondor 99 | Afghan hound 100 | Japanese spaniel 101 | proboscis monkey 102 | guinea pig 103 | white wolf 104 | ice bear 105 | gorilla 106 | borzoi 107 | toy poodle 108 | Kerry blue terrier 109 | ox 110 | Scotch terrier 111 | Tibetan mastiff 112 | spider monkey 113 | Doberman 114 | Boston bull 115 | Greater Swiss Mountain dog 116 | Appenzeller 117 | Shih-Tzu 118 | Irish water spaniel 119 | Pomeranian 120 | Bedlington terrier 121 | warthog 122 | Arabian camel 123 | siamang 124 | miniature schnauzer 125 | collie 126 | golden retriever 127 | Irish terrier 128 | affenpinscher 129 | Border collie 130 | hare 131 | boxer 132 | silky terrier 133 | beagle 134 | Leonberg 135 | German short-haired pointer 136 | patas 137 | dhole 138 | baboon 139 | macaque 140 | Chesapeake Bay retriever 141 | bull mastiff 142 | kuvasz 143 | capuchin 144 | pug 145 | curly-coated retriever 146 | Norwich terrier 147 | flat-coated retriever 148 | hog 149 | keeshond 150 | Eskimo dog 151 | Brittany spaniel 152 | standard poodle 153 | Lakeland terrier 154 | snow leopard 155 | Gordon setter 156 | dingo 157 | standard schnauzer 158 | hamster 159 | Tibetan terrier 160 | Arctic fox 161 | wire-haired fox terrier 162 | basset 163 | water buffalo 164 | American black bear 165 | Angora 166 | bison 167 | howler monkey 168 | hippopotamus 169 | chow 170 | giant panda 171 | American Staffordshire terrier 172 | Shetland sheepdog 173 | Great Pyrenees 174 | Chihuahua 175 | tabby 176 | marmoset 177 | Labrador retriever 178 | Saint Bernard 179 | armadillo 180 | Samoyed 181 | bluetick 182 | redbone 183 | polecat 184 | marmot 185 | kelpie 186 | gibbon 187 | llama 188 | miniature pinscher 189 | wood rabbit 190 | Italian greyhound 191 | lion 192 | cocker spaniel 193 | Irish setter 194 | dugong 195 | Indian elephant 196 | beaver 197 | Sussex spaniel 198 | Pembroke 199 | Blenheim spaniel 200 | Madagascar cat 201 | Rhodesian ridgeback 202 | lynx 203 | African hunting dog 204 | langur 205 | Ibizan hound 206 | timber wolf 207 | cheetah 208 | English foxhound 209 | briard 210 | sloth bear 211 | Border terrier 212 | German shepherd 213 | otter 214 | koala 215 | tusker 216 | echidna 217 | wallaby 218 | platypus 219 | wombat 220 | revolver 221 | umbrella 222 | schooner 223 | soccer ball 224 | accordion 225 | ant 226 | starfish 227 | chambered nautilus 228 | grand piano 229 | laptop 230 | strawberry 231 | airliner 232 | warplane 233 | airship 234 | balloon 235 | space shuttle 236 | fireboat 237 | gondola 238 | speedboat 239 | lifeboat 240 | canoe 241 | yawl 242 | catamaran 243 | trimaran 244 | container ship 245 | liner 246 | pirate 247 | aircraft carrier 248 | submarine 249 | wreck 250 | half track 251 | tank 252 | missile 253 | bobsled 254 | dogsled 255 | bicycle-built-for-two 256 | mountain bike 257 | freight car 258 | passenger car 259 | barrow 260 | shopping cart 261 | motor scooter 262 | forklift 263 | electric locomotive 264 | steam locomotive 265 | amphibian 266 | ambulance 267 | beach wagon 268 | cab 269 | convertible 270 | jeep 271 | limousine 272 | minivan 273 | Model T 274 | racer 275 | sports car 276 | go-kart 277 | golfcart 278 | moped 279 | snowplow 280 | fire engine 281 | garbage truck 282 | pickup 283 | tow truck 284 | trailer truck 285 | moving van 286 | police van 287 | recreational vehicle 288 | streetcar 289 | snowmobile 290 | tractor 291 | mobile home 292 | tricycle 293 | unicycle 294 | horse cart 295 | jinrikisha 296 | oxcart 297 | bassinet 298 | cradle 299 | crib 300 | four-poster 301 | bookcase 302 | china cabinet 303 | medicine chest 304 | chiffonier 305 | table lamp 306 | file 307 | park bench 308 | barber chair 309 | throne 310 | folding chair 311 | rocking chair 312 | studio couch 313 | toilet seat 314 | desk 315 | pool table 316 | dining table 317 | entertainment center 318 | wardrobe 319 | Granny Smith 320 | orange 321 | lemon 322 | fig 323 | pineapple 324 | banana 325 | jackfruit 326 | custard apple 327 | pomegranate 328 | acorn 329 | hip 330 | ear 331 | rapeseed 332 | corn 333 | buckeye 334 | organ 335 | upright 336 | chime 337 | drum 338 | gong 339 | maraca 340 | marimba 341 | steel drum 342 | banjo 343 | cello 344 | violin 345 | harp 346 | acoustic guitar 347 | electric guitar 348 | cornet 349 | French horn 350 | trombone 351 | harmonica 352 | ocarina 353 | panpipe 354 | bassoon 355 | oboe 356 | sax 357 | flute 358 | daisy 359 | yellow lady's slipper 360 | cliff 361 | valley 362 | alp 363 | volcano 364 | promontory 365 | sandbar 366 | coral reef 367 | lakeside 368 | seashore 369 | geyser 370 | hatchet 371 | cleaver 372 | letter opener 373 | plane 374 | power drill 375 | lawn mower 376 | hammer 377 | corkscrew 378 | can opener 379 | plunger 380 | screwdriver 381 | shovel 382 | plow 383 | chain saw 384 | cock 385 | hen 386 | ostrich 387 | brambling 388 | goldfinch 389 | house finch 390 | junco 391 | indigo bunting 392 | robin 393 | bulbul 394 | jay 395 | magpie 396 | chickadee 397 | water ouzel 398 | kite 399 | bald eagle 400 | vulture 401 | great grey owl 402 | black grouse 403 | ptarmigan 404 | ruffed grouse 405 | prairie chicken 406 | peacock 407 | quail 408 | partridge 409 | African grey 410 | macaw 411 | sulphur-crested cockatoo 412 | lorikeet 413 | coucal 414 | bee eater 415 | hornbill 416 | hummingbird 417 | jacamar 418 | toucan 419 | drake 420 | red-breasted merganser 421 | goose 422 | black swan 423 | white stork 424 | black stork 425 | spoonbill 426 | flamingo 427 | American egret 428 | little blue heron 429 | bittern 430 | crane 431 | limpkin 432 | American coot 433 | bustard 434 | ruddy turnstone 435 | red-backed sandpiper 436 | redshank 437 | dowitcher 438 | oystercatcher 439 | European gallinule 440 | pelican 441 | king penguin 442 | albatross 443 | great white shark 444 | tiger shark 445 | hammerhead 446 | electric ray 447 | stingray 448 | barracouta 449 | coho 450 | tench 451 | goldfish 452 | eel 453 | rock beauty 454 | anemone fish 455 | lionfish 456 | puffer 457 | sturgeon 458 | gar 459 | loggerhead 460 | leatherback turtle 461 | mud turtle 462 | terrapin 463 | box turtle 464 | banded gecko 465 | common iguana 466 | American chameleon 467 | whiptail 468 | agama 469 | frilled lizard 470 | alligator lizard 471 | Gila monster 472 | green lizard 473 | African chameleon 474 | Komodo dragon 475 | triceratops 476 | African crocodile 477 | American alligator 478 | thunder snake 479 | ringneck snake 480 | hognose snake 481 | green snake 482 | king snake 483 | garter snake 484 | water snake 485 | vine snake 486 | night snake 487 | boa constrictor 488 | rock python 489 | Indian cobra 490 | green mamba 491 | sea snake 492 | horned viper 493 | diamondback 494 | sidewinder 495 | European fire salamander 496 | common newt 497 | eft 498 | spotted salamander 499 | axolotl 500 | bullfrog 501 | tree frog 502 | tailed frog 503 | whistle 504 | wing 505 | paintbrush 506 | hand blower 507 | oxygen mask 508 | snorkel 509 | loudspeaker 510 | microphone 511 | screen 512 | mouse 513 | electric fan 514 | oil filter 515 | strainer 516 | space heater 517 | stove 518 | guillotine 519 | barometer 520 | rule 521 | odometer 522 | scale 523 | analog clock 524 | digital clock 525 | wall clock 526 | hourglass 527 | sundial 528 | parking meter 529 | stopwatch 530 | digital watch 531 | stethoscope 532 | syringe 533 | magnetic compass 534 | binoculars 535 | projector 536 | sunglasses 537 | loupe 538 | radio telescope 539 | bow 540 | cannon [ground] 541 | assault rifle 542 | rifle 543 | projectile 544 | computer keyboard 545 | typewriter keyboard 546 | crane 547 | lighter 548 | abacus 549 | cash machine 550 | slide rule 551 | desktop computer 552 | hand-held computer 553 | notebook 554 | web site 555 | harvester 556 | thresher 557 | printer 558 | slot 559 | vending machine 560 | sewing machine 561 | joystick 562 | switch 563 | hook 564 | car wheel 565 | paddlewheel 566 | pinwheel 567 | potter's wheel 568 | gas pump 569 | carousel 570 | swing 571 | reel 572 | radiator 573 | puck 574 | hard disc 575 | sunglass 576 | pick 577 | car mirror 578 | solar dish 579 | remote control 580 | disk brake 581 | buckle 582 | hair slide 583 | knot 584 | combination lock 585 | padlock 586 | nail 587 | safety pin 588 | screw 589 | muzzle 590 | seat belt 591 | ski 592 | candle 593 | jack-o'-lantern 594 | spotlight 595 | torch 596 | neck brace 597 | pier 598 | tripod 599 | maypole 600 | mousetrap 601 | spider web 602 | trilobite 603 | harvestman 604 | scorpion 605 | black and gold garden spider 606 | barn spider 607 | garden spider 608 | black widow 609 | tarantula 610 | wolf spider 611 | tick 612 | centipede 613 | isopod 614 | Dungeness crab 615 | rock crab 616 | fiddler crab 617 | king crab 618 | American lobster 619 | spiny lobster 620 | crayfish 621 | hermit crab 622 | tiger beetle 623 | ladybug 624 | ground beetle 625 | long-horned beetle 626 | leaf beetle 627 | dung beetle 628 | rhinoceros beetle 629 | weevil 630 | fly 631 | bee 632 | grasshopper 633 | cricket 634 | walking stick 635 | cockroach 636 | mantis 637 | cicada 638 | leafhopper 639 | lacewing 640 | dragonfly 641 | damselfly 642 | admiral 643 | ringlet 644 | monarch 645 | cabbage butterfly 646 | sulphur butterfly 647 | lycaenid 648 | jellyfish 649 | sea anemone 650 | brain coral 651 | flatworm 652 | nematode 653 | conch 654 | snail 655 | slug 656 | sea slug 657 | chiton 658 | sea urchin 659 | sea cucumber 660 | iron 661 | espresso maker 662 | microwave 663 | Dutch oven 664 | rotisserie 665 | toaster 666 | waffle iron 667 | vacuum 668 | dishwasher 669 | refrigerator 670 | washer 671 | Crock Pot 672 | frying pan 673 | wok 674 | caldron 675 | coffeepot 676 | teapot 677 | spatula 678 | altar 679 | triumphal arch 680 | patio 681 | steel arch bridge 682 | suspension bridge 683 | viaduct 684 | barn 685 | greenhouse 686 | palace 687 | monastery 688 | library 689 | apiary 690 | boathouse 691 | church 692 | mosque 693 | stupa 694 | planetarium 695 | restaurant 696 | cinema 697 | home theater 698 | lumbermill 699 | coil 700 | obelisk 701 | totem pole 702 | castle 703 | prison 704 | grocery store 705 | bakery 706 | barbershop 707 | bookshop 708 | butcher shop 709 | confectionery 710 | shoe shop 711 | tobacco shop 712 | toyshop 713 | fountain 714 | cliff dwelling 715 | yurt 716 | dock 717 | brass 718 | megalith 719 | bannister 720 | breakwater 721 | dam 722 | chainlink fence 723 | picket fence 724 | worm fence 725 | stone wall 726 | grille 727 | sliding door 728 | turnstile 729 | mountain tent 730 | scoreboard 731 | honeycomb 732 | plate rack 733 | pedestal 734 | beacon 735 | mashed potato 736 | bell pepper 737 | head cabbage 738 | broccoli 739 | cauliflower 740 | zucchini 741 | spaghetti squash 742 | acorn squash 743 | butternut squash 744 | cucumber 745 | artichoke 746 | cardoon 747 | mushroom 748 | shower curtain 749 | jean 750 | carton 751 | handkerchief 752 | sandal 753 | ashcan 754 | safe 755 | plate 756 | necklace 757 | croquet ball 758 | fur coat 759 | thimble 760 | pajama 761 | running shoe 762 | cocktail shaker 763 | chest 764 | manhole cover 765 | modem 766 | tub 767 | tray 768 | balance beam 769 | bagel 770 | prayer rug 771 | kimono 772 | hot pot 773 | whiskey jug 774 | knee pad 775 | book jacket 776 | spindle 777 | ski mask 778 | beer bottle 779 | crash helmet 780 | bottlecap 781 | tile roof 782 | mask 783 | maillot 784 | Petri dish 785 | football helmet 786 | bathing cap 787 | teddy bear 788 | holster 789 | pop bottle 790 | photocopier 791 | vestment 792 | crossword puzzle 793 | golf ball 794 | trifle 795 | suit 796 | water tower 797 | feather boa 798 | cloak 799 | red wine 800 | drumstick 801 | shield 802 | Christmas stocking 803 | hoopskirt 804 | menu 805 | stage 806 | bonnet 807 | meat loaf 808 | baseball 809 | face powder 810 | scabbard 811 | sunscreen 812 | beer glass 813 | hen-of-the-woods 814 | guacamole 815 | lampshade 816 | wool 817 | hay 818 | bow tie 819 | mailbag 820 | water jug 821 | bucket 822 | dishrag 823 | soup bowl 824 | eggnog 825 | mortar 826 | trench coat 827 | paddle 828 | chain 829 | swab 830 | mixing bowl 831 | potpie 832 | wine bottle 833 | shoji 834 | bulletproof vest 835 | drilling platform 836 | binder 837 | cardigan 838 | sweatshirt 839 | pot 840 | birdhouse 841 | hamper 842 | ping-pong ball 843 | pencil box 844 | pay-phone 845 | consomme 846 | apron 847 | punching bag 848 | backpack 849 | groom 850 | bearskin 851 | pencil sharpener 852 | broom 853 | mosquito net 854 | abaya 855 | mortarboard 856 | poncho 857 | crutch 858 | Polaroid camera 859 | space bar 860 | cup 861 | racket 862 | traffic light 863 | quill 864 | radio 865 | dough 866 | cuirass 867 | military uniform 868 | lipstick 869 | shower cap 870 | monitor 871 | oscilloscope 872 | mitten 873 | brassiere 874 | French loaf 875 | vase 876 | milk can 877 | rugby ball 878 | paper towel 879 | earthstar 880 | envelope 881 | miniskirt 882 | cowboy hat 883 | trolleybus 884 | perfume 885 | bathtub 886 | hotdog 887 | coral fungus 888 | bullet train 889 | pillow 890 | toilet tissue 891 | cassette 892 | carpenter's kit 893 | ladle 894 | stinkhorn 895 | lotion 896 | hair spray 897 | academic gown 898 | dome 899 | crate 900 | wig 901 | burrito 902 | pill bottle 903 | chain mail 904 | theater curtain 905 | window shade 906 | barrel 907 | washbasin 908 | ballpoint 909 | basketball 910 | bath towel 911 | cowboy boot 912 | gown 913 | window screen 914 | agaric 915 | cellular telephone 916 | nipple 917 | barbell 918 | mailbox 919 | lab coat 920 | fire screen 921 | minibus 922 | packet 923 | maze 924 | pole 925 | horizontal bar 926 | sombrero 927 | pickelhaube 928 | rain barrel 929 | wallet 930 | cassette player 931 | comic book 932 | piggy bank 933 | street sign 934 | bell cote 935 | fountain pen 936 | Windsor tie 937 | volleyball 938 | overskirt 939 | sarong 940 | purse 941 | bolo tie 942 | bib 943 | parachute 944 | sleeping bag 945 | television 946 | swimming trunks 947 | measuring cup 948 | espresso 949 | pizza 950 | breastplate 951 | shopping basket 952 | wooden spoon 953 | saltshaker 954 | chocolate sauce 955 | ballplayer 956 | goblet 957 | gyromitra 958 | stretcher 959 | water bottle 960 | dial telephone 961 | soap dispenser 962 | jersey 963 | school bus 964 | jigsaw puzzle 965 | plastic bag 966 | reflex camera 967 | diaper 968 | Band Aid 969 | ice lolly 970 | velvet 971 | tennis ball 972 | gasmask 973 | doormat 974 | Loafer 975 | ice cream 976 | pretzel 977 | quilt 978 | maillot 979 | tape player 980 | clog 981 | iPod 982 | bolete 983 | scuba diver 984 | pitcher 985 | matchstick 986 | bikini 987 | sock 988 | CD player 989 | lens cap 990 | thatch 991 | vault 992 | beaker 993 | bubble 994 | cheeseburger 995 | parallel bars 996 | flagpole 997 | coffee mug 998 | rubber eraser 999 | stole 1000 | carbonara 1001 | dumbbell -------------------------------------------------------------------------------- /models/inception5h.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loretoparisi/tensorflow-java/fc7e4cd6fd58363e2b8661f9ad6dba30fc44909c/models/inception5h.zip -------------------------------------------------------------------------------- /models/tensorflow_inception_graph.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loretoparisi/tensorflow-java/fc7e4cd6fd58363e2b8661f9ad6dba30fc44909c/models/tensorflow_inception_graph.pb -------------------------------------------------------------------------------- /process.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Tensorflow Java 4 | # @author Loreto Parisi (loretoparisi at gmail dot com) 5 | # v1.0.0 6 | # @2017-2019 Loreto Parisi (loretoparisi at gmail dot com) 7 | # 8 | 9 | DIR=$1 10 | IMG=$2 11 | 12 | TF_VERSION=1.13.1 13 | RED='\033[0;31m' 14 | NC='\033[0m' # No Color 15 | 16 | for filename in $DIR/*.jpg; do 17 | OUT=`java -cp lib/libtensorflow-${TF_VERSION}.jar:. -Djava.library.path=./jni LabelImage models/ $filename 2>/dev/null` 18 | 19 | fname=$(basename "$filename") 20 | extension="${fname##*.}" 21 | fname="${fname%.*}" 22 | 23 | if [ ! -d "$IMG/$OUT" ]; then 24 | # Control will enter here if $DIRECTORY doesn't exist. 25 | #mkdir $IMG/$OUT 26 | echo $IMG/$OUT 27 | fi 28 | echo $filename $IMG/OUT/ 29 | #cp $filename $IMG/$OUT/ 30 | 31 | echo -e "${NC}FRAME:$fname - ${RED}$OUT${NC}" 32 | done 33 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Tensorflow Java 4 | # @author Loreto Parisi (loretoparisi at gmail dot com) 5 | # v1.0.0 6 | # @2017-2019 Loreto Parisi (loretoparisi at gmail dot com) 7 | # 8 | 9 | 10 | TF_VERSION=1.13.1 11 | 12 | javac -cp lib/libtensorflow-${TF_VERSION}.jar TensorFlowExample.java 13 | java -cp lib/libtensorflow-${TF_VERSION}.jar:. -Djava.library.path=./jni TensorFlowExample 14 | 15 | --------------------------------------------------------------------------------