You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2018/08/14 15:49:40 UTC
[tika] branch branch_1x updated (2cdf627 -> f44e109)
This is an automated email from the ASF dual-hosted git repository.
tallison pushed a change to branch branch_1x
in repository https://gitbox.apache.org/repos/asf/tika.git.
from 2cdf627 TIKA-2695 -- upgrade Lucene to 7.4.0
new b717ca6 TIKA-2667 upgrade jmatio
new 719826a fix doubled junit dependency in tika-nlp
new f44e109 TIKA-2672 -- upgrade deeplearning4j to 1.0.0-beta2 via Thejan Wijesinghe. Thank you, Thejan!!!
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
tika-dl/pom.xml | 223 ++++++++++++++++-----
.../tika/dl/imagerec/DL4JInceptionV3Net.java | 121 ++++++-----
.../org/apache/tika/dl/imagerec/DL4JVGG16Net.java | 52 ++---
.../imagerec/imagenet_incpetionv3_class_index.json | 1 -
.../apache/tika/dl/imagerec/inceptionv3-model.json | 1 -
.../tika/dl/imagerec/DL4JInceptionV3NetTest.java | 5 +-
.../apache/tika/dl/imagerec/DL4JVGG16NetTest.java | 8 +-
.../tika/dl/imagerec/dl4j-inception3-config.xml | 3 +-
tika-nlp/pom.xml | 16 +-
tika-parsers/pom.xml | 2 +-
10 files changed, 279 insertions(+), 153 deletions(-)
delete mode 100644 tika-dl/src/main/resources/org/apache/tika/dl/imagerec/imagenet_incpetionv3_class_index.json
delete mode 100644 tika-dl/src/main/resources/org/apache/tika/dl/imagerec/inceptionv3-model.json
[tika] 03/03: TIKA-2672 -- upgrade deeplearning4j to 1.0.0-beta2
via Thejan Wijesinghe. Thank you, Thejan!!!
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tallison pushed a commit to branch branch_1x
in repository https://gitbox.apache.org/repos/asf/tika.git
commit f44e109d423ac99e74cc55fc78c7daf4fb81a8fd
Author: TALLISON <ta...@apache.org>
AuthorDate: Tue Aug 14 11:49:13 2018 -0400
TIKA-2672 -- upgrade deeplearning4j to 1.0.0-beta2 via Thejan Wijesinghe. Thank you, Thejan!!!
---
tika-dl/pom.xml | 223 ++++++++++++++++-----
.../tika/dl/imagerec/DL4JInceptionV3Net.java | 121 ++++++-----
.../org/apache/tika/dl/imagerec/DL4JVGG16Net.java | 52 ++---
.../imagerec/imagenet_incpetionv3_class_index.json | 1 -
.../apache/tika/dl/imagerec/inceptionv3-model.json | 1 -
.../tika/dl/imagerec/DL4JInceptionV3NetTest.java | 5 +-
.../apache/tika/dl/imagerec/DL4JVGG16NetTest.java | 8 +-
.../tika/dl/imagerec/dl4j-inception3-config.xml | 3 +-
8 files changed, 272 insertions(+), 142 deletions(-)
diff --git a/tika-dl/pom.xml b/tika-dl/pom.xml
index fbed520..278852e 100644
--- a/tika-dl/pom.xml
+++ b/tika-dl/pom.xml
@@ -36,8 +36,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <dl4j.version>0.8.0</dl4j.version>
- <dl4j.model.version>0.8.0-2</dl4j.model.version>
+ <dl4j.version>1.0.0-beta2</dl4j.version>
</properties>
<dependencies>
@@ -51,6 +50,18 @@
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-compress</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
@@ -60,35 +71,24 @@
</dependency>
<dependency>
<groupId>org.deeplearning4j</groupId>
- <artifactId>deeplearning4j-keras</artifactId>
- <version>${dl4j.model.version}</version>
- <!-- exclude this because of non-ASF friendly "do no evil" license.
- Because this relies on tika-parsers, that should have ted-dunning's drop-in
- -->
+ <artifactId>deeplearning4j-zoo</artifactId>
+ <version>${dl4j.version}</version>
<exclusions>
<exclusion>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- </exclusion>
- <exclusion>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </exclusion>
- <exclusion>
<groupId>org.deeplearning4j</groupId>
- <artifactId>deeplearning4j-modelimport</artifactId>
+ <artifactId>deeplearning4j-nn</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-compress</artifactId>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-math3</artifactId>
+ <groupId>org.nd4j</groupId>
+ <artifactId>jackson</artifactId>
</exclusion>
<exclusion>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
@@ -100,12 +100,20 @@
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-modelimport</artifactId>
- <version>${dl4j.model.version}</version>
+ <version>${dl4j.version}</version>
<exclusions>
- <exclusion>
- <groupId>org.deeplearning4j</groupId>
- <artifactId>deeplearning4j-keras</artifactId>
- </exclusion>
+ <exclusion>
+ <groupId>org.deeplearning4j</groupId>
+ <artifactId>deeplearning4j-keras</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>jackson</artifactId>
+ </exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
@@ -117,11 +125,54 @@
</exclusions>
</dependency>
<dependency>
+ <groupId>org.deeplearning4j</groupId>
+ <artifactId>deeplearning4j-nn</artifactId>
+ <version>${dl4j.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>jackson</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-compress</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-math3</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.datavec</groupId>
+ <artifactId>datavec-data-image</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-data-image</artifactId>
<version>${dl4j.version}</version>
<exclusions>
<exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ </exclusion>
+ <exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
@@ -144,25 +195,118 @@
</exclusions>
</dependency>
<dependency>
+ <groupId>org.datavec</groupId>
+ <artifactId>datavec-api</artifactId>
+ <version>${dl4j.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-compress</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>jackson</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.objenesis</groupId>
+ <artifactId>objenesis</artifactId>
+ <version>2.6</version>
+ </dependency>
+ <dependency>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ <version>${dl4j.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.objenesis</groupId>
+ <artifactId>objenesis</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-math3</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>${dl4j.version}</version>
<exclusions>
<exclusion>
+ <groupId>org.nd4j</groupId>
+ <artifactId>nd4j-api</artifactId>
+ </exclusion>
+ <exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
- <groupId>org.bytedeco</groupId>
- <artifactId>javacpp</artifactId>
- <version>1.3.2</version>
+ <groupId>org.nd4j</groupId>
+ <artifactId>jackson</artifactId>
+ <version>${dl4j.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-compress</artifactId>
+ <version>${commons.compress.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.16.22</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>20.0</version>
+ </dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.9.2</version>
</dependency>
<dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-compress</artifactId>
- <version>${commons.compress.version}</version>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.6</version>
</dependency>
</dependencies>
@@ -197,22 +341,9 @@
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<configuration>
- <excludes>
- <exclude>src/main/resources/org/apache/tika/dl/imagerec/*.json</exclude>
- </excludes>
</configuration>
</plugin>
</plugins>
</build>
- <repositories>
- <repository>
- <id>oss-sonatype</id>
- <name>oss-sonatype</name>
- <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
</project>
diff --git a/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java b/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java
index 608e7eb..5930765 100644
--- a/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java
+++ b/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java
@@ -16,22 +16,6 @@
*/
package org.apache.tika.dl.imagerec;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
import org.apache.commons.io.FileUtils;
import org.apache.tika.config.Field;
import org.apache.tika.config.InitializableProblemHandler;
@@ -46,9 +30,10 @@ import org.apache.tika.parser.recognition.ObjectRecogniser;
import org.apache.tika.parser.recognition.RecognisedObject;
import org.datavec.image.loader.NativeImageLoader;
import org.deeplearning4j.nn.graph.ComputationGraph;
-import org.deeplearning4j.nn.modelimport.keras.InvalidKerasConfigurationException;
-import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
-import org.deeplearning4j.nn.modelimport.keras.UnsupportedKerasConfigurationException;
+import org.deeplearning4j.nn.modelimport.keras.KerasModel;
+import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException;
+import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException;
+import org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@@ -59,6 +44,22 @@ import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
/**
* {@link DL4JInceptionV3Net} is an implementation of {@link ObjectRecogniser}.
* This object recogniser is powered by <a href="http://deeplearning4j.org">Deeplearning4j</a>.
@@ -69,7 +70,6 @@ import org.xml.sax.SAXException;
* for advances users who are interested in tweaking the settings, the following fields are configurable:
* <ul>
* <li>{@link #modelWeightsPath}</li>
- * <li>{@link #modelJsonPath}</li>
* <li>{@link #labelFile}</li>
* <li>{@link #labelLang}</li>
* <li>{@link #cacheDir}</li>
@@ -91,16 +91,18 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
private static final Set<MediaType> MEDIA_TYPES
= Collections.singleton(MediaType.image("jpeg"));
private static final Logger LOG = LoggerFactory.getLogger(DL4JInceptionV3Net.class);
- private static final String DEF_WEIGHTS_URL = "https://raw.githubusercontent.com/USCDataScience/dl4j-kerasimport-examples/98ec48b56a5b8fb7d54a2994ce9cb23bfefac821/dl4j-import-example/data/inception-model-weights.h5";
- public static final String DEF_MODEL_JSON = "org/apache/tika/dl/imagerec/inceptionv3-model.json";
- public static final String DEF_LABEL_MAPPING = "org/apache/tika/dl/imagerec/imagenet_incpetionv3_class_index.json";
+ private static final String DEF_WEIGHTS_URL = "https://github.com/USCDataScience/tika-dockers/releases/download/v0.2/inception_v3_keras_2.h5";
+ private static final String DEF_LABEL_MAPPING_URL = "https://github.com/USCDataScience/tika-dockers/releases/download/v0.2/imagenet_class_index.json";
+ private static final String BASE_DIR = System.getProperty("user.home") + File.separator + ".tika-dl" +
+ File.separator + "models" + File.separator + "keras";
+ private static final String MODEL_DIR = BASE_DIR + File.separator + "inception-v3";
/**
* Cache dir to be used for downloading the weights file.
* This is used to download the model.
*/
@Field
- private File cacheDir = new File(".tmp-inception");
+ private File cacheDir = new File(MODEL_DIR);
/**
* Path to a HDF5 file that contains weights of the Keras network
@@ -112,21 +114,13 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
@Field
private String modelWeightsPath = DEF_WEIGHTS_URL;
- /**
- * Path to a JSON file that contains network (graph) structure exported from Keras.
- * <p>
- * <br/>
- * Default is retrieved from {@value DEF_MODEL_JSON}
- */
- @Field
- private String modelJsonPath = DEF_MODEL_JSON;
/***
* Path to file that tells how to map node index to human readable label names
* <br/>
- * The default is retrieved from {@value DEF_LABEL_MAPPING}
+ * The default is retrieved from {@value DEF_LABEL_MAPPING_URL}
*/
@Field
- private String labelFile = DEF_LABEL_MAPPING;
+ private String labelFile = DEF_LABEL_MAPPING_URL;
/**
* Language name of the labels.
@@ -138,8 +132,10 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
@Field
private int imgHeight = 299;
+
@Field
private int imgWidth = 299;
+
@Field
private int imgChannels = 3;
/***
@@ -169,10 +165,10 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
" Asking the classloader", path);
URL url = getClass().getClassLoader().getResource(path);
if (url == null) {
- LOG.debug("Classloader does not knows the file {}", path);
+ LOG.debug("Classloader does not know the file {}", path);
file = null;
} else {
- LOG.debug("Class loader knows the file {}", path);
+ LOG.debug("Classloader knows the file {}", path);
try {
file = cachedDownload(cacheDir, url.toURI());
} catch (URISyntaxException | IOException e) {
@@ -192,7 +188,7 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
return getClass().getClassLoader().getResourceAsStream(path);
}
- public static synchronized File cachedDownload(File cacheDir, URI uri)
+ private static synchronized File cachedDownload(File cacheDir, URI uri)
throws IOException {
if ("file".equals(uri.getScheme()) || uri.getScheme() == null) {
@@ -222,49 +218,52 @@ public class DL4JInceptionV3Net implements ObjectRecogniser {
return cacheFile;
}
- @Override
- public void initialize(Map<String, Param> params)
- throws TikaConfigException {
- //STEP 1: resolve weights file, download if necessary
- if (modelWeightsPath.startsWith("http://") || modelWeightsPath.startsWith("https://")) {
- LOG.debug("Config instructed to download the weights file, doing so.");
+ private String mayBeDownloadFile(String path) throws TikaConfigException {
+ String resolvedFilePath;
+ if (path.startsWith("http://") || path.startsWith("https://")) {
+ LOG.debug("Config instructed to download the file, doing so.");
try {
- modelWeightsPath = cachedDownload(cacheDir, URI.create(modelWeightsPath)).getAbsolutePath();
+ resolvedFilePath = cachedDownload(cacheDir, URI.create(path)).getAbsolutePath();
} catch (IOException e) {
throw new TikaConfigException(e.getMessage(), e);
}
} else {
- File modelFile = retrieveFile(modelWeightsPath);
- if (!modelFile.exists()) {
- LOG.error("modelWeights does not exist at :: {}", modelWeightsPath);
- return;
+ File file = retrieveFile(path);
+ if (!file.exists()) {
+ LOG.error("File does not exist at :: {}", path);
}
- modelWeightsPath = modelFile.getAbsolutePath();
+ resolvedFilePath = file.getAbsolutePath();
}
+ return resolvedFilePath;
+ }
- //STEP 2: resolve model JSON
- File modelJsonFile = retrieveFile(modelJsonPath);
- if (modelJsonFile == null || !modelJsonFile.exists()) {
- LOG.error("Could not locate file {}", modelJsonPath);
- return;
- }
- modelJsonPath = modelJsonFile.getAbsolutePath();
+ @Override
+ public void initialize(Map<String, Param> params)
+ throws TikaConfigException {
+
+ //STEP 1: resolve weights file, download if necessary
+ modelWeightsPath = mayBeDownloadFile(modelWeightsPath);
- //STEP 3: Load labels map
- try (InputStream stream = retrieveResource(labelFile)) {
+ //STEP 2: Load labels map
+ try (InputStream stream = retrieveResource(mayBeDownloadFile(labelFile))) {
this.labelMap = loadClassIndex(stream);
} catch (IOException | ParseException e) {
LOG.error("Could not load labels map", e);
return;
}
- //STEP 4: initialize the graph
+ //STEP 3: initialize the graph
try {
this.imageLoader = new NativeImageLoader(imgHeight, imgWidth, imgChannels);
LOG.info("Going to load Inception network...");
long st = System.currentTimeMillis();
- this.graph = KerasModelImport.importKerasModelAndWeights(modelJsonPath,
- modelWeightsPath, false);
+
+ KerasModelBuilder builder = new KerasModel().modelBuilder().modelHdf5Filename(modelWeightsPath)
+ .enforceTrainingConfig(false);
+ builder.inputShape(new int[]{299, 299, 3});
+ KerasModel model = builder.buildModel();
+ this.graph = model.getComputationGraph();
+
long time = System.currentTimeMillis() - st;
LOG.info("Loaded the Inception model. Time taken={}ms", time);
} catch (IOException | InvalidKerasConfigurationException
diff --git a/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JVGG16Net.java b/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JVGG16Net.java
index 778d00f..e142d8d 100644
--- a/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JVGG16Net.java
+++ b/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JVGG16Net.java
@@ -18,8 +18,8 @@
package org.apache.tika.dl.imagerec;
import org.apache.tika.config.Field;
-import org.apache.tika.config.Param;
import org.apache.tika.config.InitializableProblemHandler;
+import org.apache.tika.config.Param;
import org.apache.tika.exception.TikaConfigException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
@@ -29,9 +29,11 @@ import org.apache.tika.parser.recognition.ObjectRecogniser;
import org.apache.tika.parser.recognition.RecognisedObject;
import org.datavec.image.loader.NativeImageLoader;
import org.deeplearning4j.nn.graph.ComputationGraph;
-import org.deeplearning4j.nn.modelimport.keras.trainedmodels.TrainedModelHelper;
-import org.deeplearning4j.nn.modelimport.keras.trainedmodels.TrainedModels;
import org.deeplearning4j.util.ModelSerializer;
+import org.deeplearning4j.zoo.PretrainedType;
+import org.deeplearning4j.zoo.ZooModel;
+import org.deeplearning4j.zoo.model.VGG16;
+import org.deeplearning4j.zoo.util.imagenet.ImageNetLabels;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
import org.nd4j.linalg.dataset.api.preprocessor.VGG16ImagePreProcessor;
@@ -40,7 +42,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
-import org.deeplearning4j.nn.modelimport.keras.trainedmodels.Utils.ImageNetLabels;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -54,23 +56,19 @@ public class DL4JVGG16Net implements ObjectRecogniser {
private static final Logger LOG = LoggerFactory.getLogger(DL4JVGG16Net.class);
public static final Set<MediaType> SUPPORTED_MIMES = Collections.singleton(MediaType.image("jpeg"));
- private static final String HOME_DIR = System.getProperty("user.home");
- private static final String BASE_DIR = ".dl4j" + File.separator + "trainedmodels";
- private static String MODEL_DIR = HOME_DIR + File.separator + BASE_DIR;
- private static String MODEL_DIR_PREPROCESSED = MODEL_DIR + File.separator + "tikaPreprocessed" + File.separator;
- private static TrainedModelHelper MODEL_HELPER = new TrainedModelHelper(TrainedModels.VGG16);
+ private static final String BASE_DIR = System.getProperty("user.home") + File.separator + ".tika-dl" +
+ File.separator + "models" + File.separator + "dl4j";
+ private static final String MODEL_DIR = BASE_DIR + File.separator + "vgg-16";
@Field
- private File modelFile = new File(MODEL_DIR_PREPROCESSED + File.separator + "vgg16.zip");
-
- @Field
- private File locationToSave = new File(MODEL_DIR + File.separator
- + "tikaPreprocessed" + File.separator + "vgg16.zip");
+ private File cacheDir = new File(MODEL_DIR + File.separator + "vgg16.zip");
@Field
private boolean serialize = true;
+
@Field
private int topN;
+
private NativeImageLoader imageLoader = new NativeImageLoader(224, 224, 3);
private DataNormalization preProcessor = new VGG16ImagePreProcessor();
private boolean available = false;
@@ -78,6 +76,7 @@ public class DL4JVGG16Net implements ObjectRecogniser {
public Set<MediaType> getSupportedMimes() {
return SUPPORTED_MIMES;
}
+ private ImageNetLabels imageNetLabels;
@Override
public boolean isAvailable() {
@@ -86,27 +85,30 @@ public class DL4JVGG16Net implements ObjectRecogniser {
@Override
public void checkInitialization(InitializableProblemHandler problemHandler) throws TikaConfigException {
- //TODO: what do we want to check here?
+ //TODO: what do we want to check here?
}
@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
try {
if (serialize) {
- if (locationToSave.exists()) {
- model = ModelSerializer.restoreComputationGraph(locationToSave);
- LOG.info("Preprocessed Model Loaded from {}", locationToSave);
+ if (cacheDir.exists()) {
+ model = ModelSerializer.restoreComputationGraph(cacheDir);
+ LOG.info("Preprocessed Model Loaded from {}", cacheDir);
} else {
- LOG.warn("Preprocessed Model doesn't exist at {}", locationToSave);
- locationToSave.getParentFile().mkdirs();
- model = MODEL_HELPER.loadModel();
+ LOG.warn("Preprocessed Model doesn't exist at {}", cacheDir);
+ cacheDir.getParentFile().mkdirs();
+ ZooModel zooModel = VGG16.builder().build();
+ model = (ComputationGraph)zooModel.initPretrained(PretrainedType.IMAGENET);
LOG.info("Saving the Loaded model for future use. Saved models are more optimised to consume less resources.");
- ModelSerializer.writeModel(model, locationToSave, true);
+ ModelSerializer.writeModel(model, cacheDir, true);
}
} else {
LOG.info("Weight graph model loaded via dl4j Helper functions");
- model = MODEL_HELPER.loadModel();
+ ZooModel zooModel = VGG16.builder().build();
+ model = (ComputationGraph)zooModel.initPretrained(PretrainedType.IMAGENET);
}
+ imageNetLabels = new ImageNetLabels();
available = true;
} catch (Exception e) {
available = false;
@@ -126,8 +128,6 @@ public class DL4JVGG16Net implements ObjectRecogniser {
}
private List<RecognisedObject> predict(INDArray predictions)
{
- ArrayList<String> labels;
- labels=ImageNetLabels.getLabels();
List<RecognisedObject> objects = new ArrayList<>();
int[] topNPredictions = new int[topN];
float[] topNProb = new float[topN];
@@ -140,7 +140,7 @@ public class DL4JVGG16Net implements ObjectRecogniser {
topNPredictions[i] = Nd4j.argMax(currentBatch, 1).getInt(0, 0);
topNProb[i] = currentBatch.getFloat(batch, topNPredictions[i]);
currentBatch.putScalar(0, topNPredictions[i], 0);
- outLabels[i]= labels.get(topNPredictions[i]);
+ outLabels[i]= imageNetLabels.getLabel(topNPredictions[i]);
objects.add(new RecognisedObject(outLabels[i], "eng", outLabels[i], topNProb[i]));
i++;
}
diff --git a/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/imagenet_incpetionv3_class_index.json b/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/imagenet_incpetionv3_class_index.json
deleted file mode 100644
index c286b58..0000000
--- a/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/imagenet_incpetionv3_class_index.json
+++ /dev/null
@@ -1 +0,0 @@
-{"0":["n01440764","tench"],"1":["n01443537","goldfish"],"2":["n01484850","great_white_shark"],"3":["n01491361","tiger_shark"],"4":["n01494475","hammerhead"],"5":["n01496331","electric_ray"],"6":["n01498041","stingray"],"7":["n01514668","cock"],"8":["n01514859","hen"],"9":["n01518878","ostrich"],"10":["n01530575","brambling"],"11":["n01531178","goldfinch"],"12":["n01532829","house_finch"],"13":["n01534433","junco"],"14":["n01537544","indigo_bunting"],"15":["n01558993","robin"],"16":["n015 [...]
diff --git a/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/inceptionv3-model.json b/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/inceptionv3-model.json
deleted file mode 100644
index 6f62c23..0000000
--- a/tika-dl/src/main/resources/org/apache/tika/dl/imagerec/inceptionv3-model.json
+++ /dev/null
@@ -1 +0,0 @@
-{"class_name":"Model","keras_version":"1.1.0","config":{"layers":[{"class_name":"InputLayer","config":{"batch_input_shape":[null,299,299,3],"input_dtype":"float32","sparse":false,"name":"input_1"},"inbound_nodes":[],"name":"input_1"},{"class_name":"Convolution2D","config":{"W_constraint":null,"b_constraint":null,"name":"convolution2d_1","activity_regularizer":null,"trainable":true,"dim_ordering":"tf","nb_col":3,"subsample":[2,2],"init":"glorot_uniform","bias":true,"nb_filter":32,"border_ [...]
diff --git a/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JInceptionV3NetTest.java b/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JInceptionV3NetTest.java
index 341ba23..7e41cb8 100644
--- a/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JInceptionV3NetTest.java
+++ b/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JInceptionV3NetTest.java
@@ -16,14 +16,13 @@
*/
package org.apache.tika.dl.imagerec;
-import static org.junit.Assert.assertTrue;
-
import org.apache.tika.Tika;
import org.apache.tika.config.TikaConfig;
-import org.apache.tika.exception.TikaConfigException;
import org.apache.tika.metadata.Metadata;
import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+
public class DL4JInceptionV3NetTest {
@Test
diff --git a/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JVGG16NetTest.java b/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JVGG16NetTest.java
index cfc3e7b..714a20f 100644
--- a/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JVGG16NetTest.java
+++ b/tika-dl/src/test/java/org/apache/tika/dl/imagerec/DL4JVGG16NetTest.java
@@ -16,19 +16,21 @@
*/
package org.apache.tika.dl.imagerec;
-import static org.junit.Assert.assertTrue;
-
import org.apache.tika.Tika;
import org.apache.tika.config.TikaConfig;
-import org.apache.tika.exception.TikaConfigException;
import org.apache.tika.metadata.Metadata;
import org.junit.Test;
+import java.io.InputStream;
+
+import static org.junit.Assert.assertTrue;
+
public class DL4JVGG16NetTest {
@Test
public void recognise() throws Exception {
TikaConfig config = null;
+ InputStream is = getClass().getResourceAsStream("dl4j-vgg16-config.xml");
try {
config = new TikaConfig(getClass().getResourceAsStream("dl4j-vgg16-config.xml"));
} catch (Exception e) {
diff --git a/tika-dl/src/test/resources/org/apache/tika/dl/imagerec/dl4j-inception3-config.xml b/tika-dl/src/test/resources/org/apache/tika/dl/imagerec/dl4j-inception3-config.xml
index 7cc297a..2728063 100644
--- a/tika-dl/src/test/resources/org/apache/tika/dl/imagerec/dl4j-inception3-config.xml
+++ b/tika-dl/src/test/resources/org/apache/tika/dl/imagerec/dl4j-inception3-config.xml
@@ -24,7 +24,8 @@
<parser class="org.apache.tika.parser.recognition.ObjectRecognitionParser">
<mime>image/jpeg</mime>
<params>
- <param name="modelWeightsPath" type="string">https://raw.githubusercontent.com/USCDataScience/dl4j-kerasimport-examples/98ec48b56a5b8fb7d54a2994ce9cb23bfefac821/dl4j-import-example/data/inception-model-weights.h5</param>
+ <param name="modelWeightsPath" type="string">https://github.com/USCDataScience/tika-dockers/releases/download/v0.2/inception_v3_keras_2.h5</param>
+ <param name="labelFile" type="string">https://github.com/USCDataScience/tika-dockers/releases/download/v0.2/imagenet_class_index.json</param>
<param name="topN" type="int">10</param>
<param name="minConfidence" type="double">0.015</param>
<param name="class" type="string">org.apache.tika.dl.imagerec.DL4JInceptionV3Net</param>
[tika] 02/03: fix doubled junit dependency in tika-nlp
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tallison pushed a commit to branch branch_1x
in repository https://gitbox.apache.org/repos/asf/tika.git
commit 719826a984f29c7a0847efd3436647c203c5be3d
Author: TALLISON <ta...@apache.org>
AuthorDate: Tue Aug 14 08:25:32 2018 -0400
fix doubled junit dependency in tika-nlp
---
tika-nlp/pom.xml | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/tika-nlp/pom.xml b/tika-nlp/pom.xml
index c279875..61ec0ab 100644
--- a/tika-nlp/pom.xml
+++ b/tika-nlp/pom.xml
@@ -45,11 +45,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>${project.groupId}</groupId>
@@ -210,10 +205,6 @@
</dependency>
<!-- Test dependencies -->
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
@@ -224,7 +215,12 @@
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
-
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
<build>
[tika] 01/03: TIKA-2667 upgrade jmatio
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tallison pushed a commit to branch branch_1x
in repository https://gitbox.apache.org/repos/asf/tika.git
commit b717ca62ca8bbe3fc0a74a778c5bb6cbbba28953
Author: TALLISON <ta...@apache.org>
AuthorDate: Tue Aug 14 08:22:43 2018 -0400
TIKA-2667 upgrade jmatio
---
tika-parsers/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tika-parsers/pom.xml b/tika-parsers/pom.xml
index e9e5b83..cdb1529 100644
--- a/tika-parsers/pom.xml
+++ b/tika-parsers/pom.xml
@@ -153,7 +153,7 @@
<dependency>
<groupId>org.tallison</groupId>
<artifactId>jmatio</artifactId>
- <version>1.3</version>
+ <version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>