Browse Source

Unsuccessful experimentation with importing a model from Tensorflow in Java

master
davpapp 4 years ago
parent
commit
6912821b56
37 changed files with 1408 additions and 73 deletions
  1. +18
    -4
      .classpath
  2. +6
    -0
      .project
  3. +1
    -0
      .settings/org.eclipse.jdt.core.prefs
  4. +4
    -0
      .settings/org.eclipse.m2e.core.prefs
  5. BIN
      bin/HelloTF.class
  6. BIN
      bin/ObjectDetector.class
  7. +1209
    -0
      hs_err_pid12348.log
  8. +33
    -0
      hs_err_pid7672.log
  9. +25
    -0
      pom.xml
  10. +57
    -0
      src/HelloTF.java
  11. +55
    -69
      src/ObjectDetector.java
  12. BIN
      target/classes/ColorAnalyzer.class
  13. BIN
      target/classes/Constants.class
  14. BIN
      target/classes/Cursor.class
  15. BIN
      target/classes/CursorDataFileParser.class
  16. BIN
      target/classes/CursorPath.class
  17. BIN
      target/classes/CursorPathTest.class
  18. BIN
      target/classes/CursorPoint.class
  19. BIN
      target/classes/CursorPointTest.class
  20. BIN
      target/classes/CursorTask.class
  21. BIN
      target/classes/CursorTest.class
  22. BIN
      target/classes/HelloTF.class
  23. BIN
      target/classes/ImageCollector$1.class
  24. BIN
      target/classes/ImageCollector.class
  25. BIN
      target/classes/Inventory.class
  26. BIN
      target/classes/InventoryItem.class
  27. BIN
      target/classes/InventoryItems.class
  28. BIN
      target/classes/InventoryItemsTest.class
  29. BIN
      target/classes/InventorySlot.class
  30. BIN
      target/classes/InventoryTest.class
  31. BIN
      target/classes/ObjectDetector.class
  32. BIN
      target/classes/PointTest.class
  33. BIN
      target/classes/Randomizer.class
  34. BIN
      target/classes/RandomizerTest.class
  35. BIN
      target/classes/WillowChopper.class
  36. BIN
      target/classes/cascadeTrainingImageCollector.class
  37. BIN
      target/classes/main.class

+ 18
- 4
.classpath View File

@ -1,9 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-9">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/home/dpapp/lib/Jama-1.0.3.jar" sourcepath="/home/dpapp/lib/Jama">
@ -13,10 +18,19 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/OpenCV-3.4.0">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="/home/dpapp/opencv-3.4.0/build/lib"/>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/TensorFlow">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

+ 6
- 0
.project View File

@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 1
- 0
.settings/org.eclipse.jdt.core.prefs View File

@ -9,4 +9,5 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=9

+ 4
- 0
.settings/org.eclipse.m2e.core.prefs View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

BIN
bin/HelloTF.class View File


BIN
bin/ObjectDetector.class View File


+ 1209
- 0
hs_err_pid12348.log
File diff suppressed because it is too large
View File


+ 33
- 0
hs_err_pid7672.log View File

@ -0,0 +1,33 @@
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f70bef27fb2, pid=7672, tid=29649
#
# JRE version: Java(TM) SE Runtime Environment (9.0+11) (build 9.0.4+11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (9.0.4+11, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C [libjimage.so+0x3fb2] ImageStrings::find(Endian*, char const*, int*, unsigned int)+0x42
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %d %P" (or dumping to /home/dpapp/eclipse-workspace/RunescapeAI/core.7672)
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
--------------- S U M M A R Y ------------
Command Line: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:36189 -Djava.library.path=/home/dpapp/opencv-3.4.0/build/lib -Dfile.encoding=UTF-8 ObjectDetector
Host: Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz, 4 cores, 7G, Ubuntu 16.04.3 LTS
Time: Thu Feb 15 22:10:22 2018 EST elapsed time: 55 seconds (0d 0h 0m 55s)
--------------- T H R E A D ---------------
Current thread (0x00007f7058001000): JavaThread "SIGTERM handler" daemon [_thread_in_vm, id=29649, stack(0x00007f7072acd000,0x00007f7072bce000)]
Stack: [0x00007f7072acd000,0x00007f7072bce000], sp=0x00007f7072bcb160, free space=1016k
Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [libjimage.so+0x3fb2] ImageStrings::find(Endian*, char const*, int*, unsigned int)+0x42
C [libjimage.so+0x4b8c] ImageFileReader::find_location_index(char const*, unsigned long long*) const+0x3c
C [libjimage.so+0x505a] JIMAGE_FindResource+0xba
V [libjvm.so+0x5aae8e]

+ 25
- 0
pom.xml View File

@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RunescapeAI</groupId>
<artifactId>RunescapeAI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>9</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.5.0-rc0</version>
</dependency>
</dependencies>
</project>

+ 57
- 0
src/HelloTF.java View File

@ -0,0 +1,57 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;
public class HelloTF {
public static void test() throws IOException {
//Dataset
float[] x = new float[]{1};
float[] y = x;
//Setting parameters
String modelDir = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/results/checkpoint_23826";
//Reading the graph
byte[] graphDef = Files.readAllBytes(Paths.get(modelDir, "frozen_graph_inference.pb"));
Tensor input = Tensor.create(0.5f);
float results = -99999f;
try (Graph g = new Graph()) {
g.importGraphDef(graphDef);
try (Session s = new Session(g);
Tensor result = s.runner().feed("input", input).fetch("output").run().get(0)) {
results = result.floatValue();
}
}
System.out.println("Expected: " + x + "\tPredicted: " + results);
}
public static void main(String[] args) throws Exception {
System.out.println("Running helloTF");
try (Graph g = new Graph()) {
final String value = "Hello from " + TensorFlow.version();
// Construct the computation graph with a single operation, a constant
// named "MyConst" with a value "value".
try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
}
// Execute the "MyConst" operation in a Session.
try (Session s = new Session(g);
Tensor output = s.runner().fetch("MyConst").run().get(0)) {
System.out.println(new String(output.bytesValue(), "UTF-8"));
}
}
test();
}
}

+ 55
- 69
src/ObjectDetector.java View File

@ -1,22 +1,19 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.List;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.ArrayList;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.DictValue;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Layer;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
@ -29,93 +26,82 @@ public class ObjectDetector {
Net net;
public ObjectDetector() {
public ObjectDetector() throws Exception {
this.inputImagePath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/test_images/ironOre_test_9.jpg";
this.inputModelPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/training/ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb";
//this.inputModelPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/results/checkpoint_23826/frozen_inference_graph.pb";
this.inputModelArgumentsPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/training/ssd_mobilenet_v1_coco_2017_11_17/ssd_mobilenet_v1_coco.pbtxt";
//this.inputModelArgumentsPath = "/home/dpapp/eclipse-workspace/RunescapeAI/TensorFlow/object-detection.pbtxt";
this.inputModelPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/results/checkpoint_23826/frozen_graph_inference.pb";
this.inputModelArgumentsPath = "/home/dpapp/tensorflow-1.5.0/models/raccoon_dataset/generated_graph.pbtxt";
File f = new File(inputImagePath);
if(!f.exists()) throw new Exception("Test image is missing: " + inputImagePath);
File f1 = new File(inputModelPath);
if(!f1.exists()) throw new Exception("Test image is missing: " + inputModelPath);
File f2 = new File(inputModelArgumentsPath);
if(!f2.exists()) throw new Exception("Test image is missing: " + inputModelArgumentsPath);
net = Dnn.readNetFromTensorflow(inputModelPath, inputModelArgumentsPath);
}
public void testGetLayerTypes() {
ArrayList<String> layertypes = new ArrayList();
net.getLayerTypes(layertypes);
assertFalse("No layer types returned!", layertypes.isEmpty());
}
public void testGetLayer() {
ArrayList<String> layernames = (ArrayList<String>) net.getLayerNames();
assertFalse("Test net returned no layers!", layernames.isEmpty());
String testLayerName = layernames.get(0);
DictValue layerId = new DictValue(testLayerName);
assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue());
Layer layer = net.getLayer(layerId);
assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name());
}
public void testImage() throws Exception {
File f = new File(inputImagePath);
if(!f.exists()) throw new Exception("Test image is missing: " + inputImagePath);
Mat rawImage = Imgcodecs.imread(inputImagePath);
Imgproc.cvtColor(rawImage, rawImage, Imgproc.COLOR_BGR2GRAY);
System.out.println(rawImage.width() + "x" +rawImage.height() + ", dim: " + rawImage.dims() + ", channels: " + rawImage.channels());
Mat grayImage = new Mat();
Imgproc.cvtColor(rawImage, grayImage, Imgproc.COLOR_RGB2GRAY);
assertNotNull("Loading image from file failed!", rawImage);
Mat image = new Mat();
Imgproc.resize(rawImage, image, new Size(224,224));
System.out.println(image.width() + "x" + image.height() + ", dim: " + image.dims() + ", channels: " + image.channels());
Imgproc.resize(grayImage, image, new Size(224, 224));
Mat inputBlob = Dnn.blobFromImage(image);
assertNotNull("Converting image to blob failed!", inputBlob);
System.out.println(inputBlob.width() + "x" + inputBlob.height() + ", " + ", dim: " + inputBlob.dims() + ", channels: " + inputBlob.channels());
Mat inputBlobP = new Mat();
Core.subtract(inputBlob, new Scalar(117.0), inputBlobP);
System.out.println(inputBlobP.width() + "x" + inputBlobP.height()+ ", " + ", dim: " + inputBlobP.dims() + ", channels: " + inputBlobP.channels());
net.setInput(inputBlobP);
Mat result = net.forward();
assertNotNull("Net returned no result!", result);
// BufferedImage convertedImage = Mat2BufferedImage(result);
//displayImage(convertedImage);
//Core.MinMaxLocResult minmax = Core.minMaxLoc(result.reshape(1, 1));
//assertTrue("No image recognized!", minmax.maxVal > 0.9);
}
/*
public void displayImage(Image img2)
{
//BufferedImage img=ImageIO.read(new File("/HelloOpenCV/lena.png"));
ImageIcon icon=new ImageIcon(img2);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(img2.getWidth(null)+50, img2.getHeight(null)+50);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public BufferedImage Mat2BufferedImage(Mat m){
// source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
// Fastest code
// The output can be assigned either to a BufferedImage or to an Image
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
*/
public static void main( String[] args ) throws Exception {
System.out.println("Reading model from TensorFlow...");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
//System.out.println("mat = " + mat.dump());
ObjectDetector objectDetector = new ObjectDetector();
objectDetector.testGetLayerTypes();
objectDetector.testGetLayer();
objectDetector.testImage();
System.out.println("Done...");


BIN
target/classes/ColorAnalyzer.class View File


BIN
target/classes/Constants.class View File


BIN
target/classes/Cursor.class View File


BIN
target/classes/CursorDataFileParser.class View File


BIN
target/classes/CursorPath.class View File


BIN
target/classes/CursorPathTest.class View File


BIN
target/classes/CursorPoint.class View File


BIN
target/classes/CursorPointTest.class View File


BIN
target/classes/CursorTask.class View File


BIN
target/classes/CursorTest.class View File


BIN
target/classes/HelloTF.class View File


BIN
target/classes/ImageCollector$1.class View File


BIN
target/classes/ImageCollector.class View File


BIN
target/classes/Inventory.class View File


BIN
target/classes/InventoryItem.class View File


BIN
target/classes/InventoryItems.class View File


BIN
target/classes/InventoryItemsTest.class View File


BIN
target/classes/InventorySlot.class View File


BIN
target/classes/InventoryTest.class View File


BIN
target/classes/ObjectDetector.class View File


BIN
target/classes/PointTest.class View File


BIN
target/classes/Randomizer.class View File


BIN
target/classes/RandomizerTest.class View File


BIN
target/classes/WillowChopper.class View File


BIN
target/classes/cascadeTrainingImageCollector.class View File


BIN
target/classes/main.class View File


Loading…
Cancel
Save