chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:47:05 +08:00
commit 4f3b7da785
7394 changed files with 2005594 additions and 0 deletions
+266
View File
@@ -0,0 +1,266 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>deeplearning4j</artifactId>
<groupId>org.eclipse.deeplearning4j</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>omnihub</artifactId>
<name>omnihub</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commonsio.version>2.7</commonsio.version>
<commons.lang.version>3.12.0</commons.lang.version>
<commons.dbutils.version>1.7</commons.dbutils.version>
<junit.version>5.8.0-M1</junit.version>
<junit.platform.launcher.version>1.8.0-M1</junit.platform.launcher.version>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<java.version>11</java.version>
<maven-shade-plugin.version>3.5.1</maven-shade-plugin.version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<module.name>omnihub</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<!-- Redirect jackson to slf4j. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.28</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commonsio.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.deeplearning4j</groupId>
<artifactId>nd4j-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.deeplearning4j</groupId>
<artifactId>deeplearning4j-modelimport</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.deeplearning4j</groupId>
<artifactId>deeplearning4j-nn</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.deeplearning4j</groupId>
<artifactId>samediff-import-onnx</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.deeplearning4j</groupId>
<artifactId>samediff-import-tensorflow</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.5.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>src/main/stubs</source>
</sources>
</configuration>
</execution>
<execution>
<id>get-cpu-count</id>
<goals>
<goal>cpu-count</goal>
</goals>
<configuration>
<cpuCount>cpu.core.count</cpuCount>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>org/datanucleus/**</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- https://kotlinlang.org/docs/reference/using-maven.html -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgs>
<arg>--add-exports</arg>
<arg>java.base/java.nio=ALL-UNNAMED</arg>
<arg>--add-opens</arg>
<arg>java.base/java.nio=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,137 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub;
import lombok.SneakyThrows;
import org.apache.commons.io.FilenameUtils;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException;
import org.deeplearning4j.omnihub.OmnihubConfig;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.samediff.frameworkimport.onnx.importer.OnnxFrameworkImporter;
import org.nd4j.samediff.frameworkimport.tensorflow.importer.TensorflowFrameworkImporter;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
/**
* Main class for taking the local downloads of an omnihub download directory
* and converting them to output formats.
*
* @author Adam Gibson
*/
public class BootstrapFromLocal {
public static void main(String...args) {
File localOmnihubHome = OmnihubConfig.getOmnihubHome();
File[] frameworks = localOmnihubHome.listFiles();
OnnxFrameworkImporter onnxFrameworkImporter = new OnnxFrameworkImporter();
TensorflowFrameworkImporter tensorflowFrameworkImporter = new TensorflowFrameworkImporter();
for(File frameworkFile : frameworks) {
Framework framework = Framework.valueOf(frameworkFile.getName().toUpperCase());
if(Framework.isInput(framework)) {
File[] inputFiles = frameworkFile.listFiles();
for(File inputFile : inputFiles) {
try {
extracted(localOmnihubHome, onnxFrameworkImporter, tensorflowFrameworkImporter, framework, inputFile);
} catch (Exception e) {
System.err.println("Failed to import model at path " + inputFile.getAbsolutePath());
e.printStackTrace();
}
}
}
}
}
private static void extracted(File localOmnihubHome, OnnxFrameworkImporter onnxFrameworkImporter,
TensorflowFrameworkImporter tensorflowFrameworkImporter,
Framework framework,
File inputFile) throws Exception {
String inputFileNameMinusFormat = FilenameUtils.getBaseName(inputFile.getName());
String format = FilenameUtils.getExtension(inputFile.getName());
Framework outputFramework = Framework.outputFrameworkFor(framework);
File saveModelDir = new File(localOmnihubHome, outputFramework.name().toLowerCase());
if(!saveModelDir.exists()) {
saveModelDir.mkdirs();
}
switch(outputFramework) {
case SAMEDIFF:
importTfOnnxSameDiff(onnxFrameworkImporter, tensorflowFrameworkImporter, framework, inputFile, inputFileNameMinusFormat, format, saveModelDir);
break;
case DL4J:
File saveModel2 = new File(saveModelDir,inputFileNameMinusFormat + ".zip");
//filter out invalid file formats
if(format.equals("h5")) {
importKerasDl4j(inputFile, saveModel2);
}
break;
}
}
private static void importTfOnnxSameDiff(OnnxFrameworkImporter onnxFrameworkImporter, TensorflowFrameworkImporter tensorflowFrameworkImporter, Framework framework, File inputFile, String inputFileNameMinusFormat, String format, File saveModelDir) throws IOException {
SameDiff sameDiff = null;
switch(framework) {
case ONNX:
case PYTORCH:
//filter out invalid files
if(format.equals("onnx"))
sameDiff = onnxFrameworkImporter.runImport(inputFile.getAbsolutePath(), Collections.emptyMap(),true, false);
break;
case TENSORFLOW:
if(format.equals("pb"))
sameDiff = tensorflowFrameworkImporter.runImport(inputFile.getAbsolutePath(), Collections.emptyMap(),true, false);
break;
}
//reuse the same model name but with the samediff format
File saveModel = new File(saveModelDir, inputFileNameMinusFormat + ".fb");
if(sameDiff != null)
sameDiff.asFlatFile(saveModel,true);
else {
System.err.println("Skipping model " + inputFile.getAbsolutePath());
}
}
@SneakyThrows
private static void importKerasDl4j(File inputFile, File saveModel2) {
try {
ComputationGraph computationGraph = KerasModelImport.importKerasModelAndWeights(inputFile.getAbsolutePath(),true);
computationGraph.save(saveModel2,true);
}catch(Exception e) {
if(e instanceof InvalidKerasConfigurationException) {
e.printStackTrace();
} else {
MultiLayerNetwork multiLayerNetwork = KerasModelImport.importKerasSequentialModelAndWeights(inputFile.getAbsolutePath(), true);
multiLayerNetwork.save(saveModel2,true);
}
}
}
}
@@ -0,0 +1,89 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub;
/**
* Represents a framework for model conversion and manipulation
*
* @author Adam Gibson
*/
public enum Framework {
SAMEDIFF(0),
PYTORCH(1),
TENSORFLOW(2),
KERAS(3),
DL4J(4),
ONNX(5),
HUGGINGFACE(6);
private final int frameworkIndex;
Framework(int index) { this.frameworkIndex = index; }
public int frameworkIndex() { return frameworkIndex; }
/**
* Returns true if the framework is an input framework (pytorch, keras, tensorflow,onnx)
* @param framework the input framework
* @return
*/
public static boolean isInput(Framework framework) {
switch(framework) {
case TENSORFLOW:
case KERAS:
case PYTORCH:
case ONNX:
return true;
default:
return false;
}
}
/**
* Returns true if the framework is an output framework (dl4j or samediff)
* @param framework the input framework
* @return
*/
public static boolean isOutput(Framework framework) {
return !isInput(framework);
}
/**
* Return the output framework for a given framework.
* Most of the time it will be samediff, but keras's h5 format will use dl4j.
* Note an {@link IllegalArgumentException} will be thrown for either {@link #SAMEDIFF}
* or {@link #DL4J}
* @param framework the input framework
* @return the appropriate output framework for the given input framework.
*/
public static Framework outputFrameworkFor(Framework framework) {
if(!isInput(framework)) {
throw new IllegalArgumentException("Input framework " + framework.name() + " is not an input framework");
}
switch(framework) {
case ONNX:
case PYTORCH:
case TENSORFLOW:
return SAMEDIFF;
default:
return DL4J;
}
}
}
@@ -0,0 +1,148 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub;
import com.squareup.javapoet.*;
import org.apache.commons.io.FileUtils;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.eclipse.deeplearning4j.omnihub.api.Model;
import org.eclipse.deeplearning4j.omnihub.api.NamespaceModels;
import org.nd4j.autodiff.samediff.SameDiff;
import javax.lang.model.element.Modifier;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
public class ModelNamespaceGenerator {
private static String copyright =
"/*\n" +
" * ******************************************************************************\n" +
" * *\n" +
" * *\n" +
" * * This program and the accompanying materials are made available under the\n" +
" * * terms of the Apache License, Version 2.0 which is available at\n" +
" * * https://www.apache.org/licenses/LICENSE-2.0.\n" +
" * *\n" +
" * * See the NOTICE file distributed with this work for additional\n" +
" * * information regarding copyright ownership.\n" +
" * * Unless required by applicable law or agreed to in writing, software\n" +
" * * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n" +
" * * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n" +
" * * License for the specific language governing permissions and limitations\n" +
" * * under the License.\n" +
" * *\n" +
" * * SPDX-License-Identifier: Apache-2.0\n" +
" * *****************************************************************************\n" +
" */\n";
private static String codeGenWarning =
"\n//================== GENERATED CODE - DO NOT MODIFY THIS FILE ==================\n\n";
public static void generateModels(NamespaceModels models, File outputDirectory, String className, String basePackage, String parentClass) throws IOException {
TypeSpec.Builder builder = TypeSpec.classBuilder(className)
.addModifiers(Modifier.PUBLIC);
models.getModels().stream()
.sorted(Comparator.comparing(Model::modelName))
.forEachOrdered(model -> builder.addMethod(createSignature(model)));
addDefaultConstructor(builder);
TypeSpec spec = builder.build();
final String modelsPackage = "org.eclipse.deeplearning4j.omnihub.models";
JavaFile jf = JavaFile.builder(modelsPackage,spec).build();
StringBuilder sb = new StringBuilder();
sb.append(copyright);
sb.append(codeGenWarning);
jf.writeTo(sb);
File outFile = new File(outputDirectory,packageToDirectory(basePackage) + File.separator + className + ".java");
FileUtils.writeStringToFile(outFile,sb.toString(), StandardCharsets.UTF_8);
System.out.println("Wrote class to " + outFile.getAbsolutePath());
}
private static void addDefaultConstructor(TypeSpec.Builder builder) {
//Add private no-arg constructor
MethodSpec noArg = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.build();
builder.addMethod(noArg);
}
private static String packageToDirectory(String packageName){
return packageName.replace(".", File.separator);
}
public static MethodSpec createSignature(Model model) {
MethodSpec.Builder c = MethodSpec.methodBuilder(model.modelName())
.addModifiers(Modifier.PUBLIC)
.addException(Exception.class)
.addJavadoc(model.documentation());
c.addParameter(ParameterSpec.builder(TypeName.BOOLEAN,"forceDownload").build());
String[] segmented = model.modelUrl().split("/");
String modelFileName = segmented[segmented.length - 1];
switch(model.framework()) {
case DL4J:
switch(model.modelType()) {
case COMP_GRAPH:
c.returns(ComputationGraph.class);
c.addStatement(CodeBlock.builder()
.add(String.format("return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadCompGraph(\"%s\",forceDownload)",modelFileName))
.build());
break;
case SEQUENTIAL:
c.returns(MultiLayerNetwork.class);
c.addStatement(CodeBlock.builder()
.add(String.format("return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadNetwork(\"%s\",forceDownload)",modelFileName))
.build());
break;
}
break;
case SAMEDIFF:
c.addStatement(CodeBlock.builder()
.add(String.format("return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadSameDiffModel(\"%s\",forceDownload)",modelFileName))
.build());
c.returns(SameDiff.class);
break;
}
return c.build();
}
public static void main(String... args) throws Exception {
}
}
@@ -0,0 +1,172 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.omnihub.OmnihubConfig;
import org.nd4j.autodiff.samediff.SameDiff;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
/**
* Various utils for dealing with downloading files
* and handling the omnihub cache.
*
* @author Adam Gibson
*/
public class OmniHubUtils {
/**
* Loads a {@link MultiLayerNetwork} from the local cache
* or downloads the file from the remote model zoo.
* Note this is the same as calling {@link #loadNetwork(String, boolean)}
* with a value of false
* @param name the name of the file to load
* @return the loaded {@link MultiLayerNetwork}
* @throws IOException
*/
public static MultiLayerNetwork loadNetwork(String name) throws IOException {
return loadNetwork(name,false);
}
/**
* Loads a {@link MultiLayerNetwork} from the local cache
* or downloads the file from the remote model zoo.
* @param name the name of the file to load
* @param forceDownload whether to force a new download if the file exists or not
* @return the loaded {@link MultiLayerNetwork}
* @throws IOException
*/
public static MultiLayerNetwork loadNetwork(String name,boolean forceDownload) throws IOException {
return MultiLayerNetwork.load(downloadAndLoadFromZoo("dl4j",name,forceDownload),true);
}
/**
* Loads a {@link ComputationGraph} from the local cache
* or downloads the file from the remote model zoo.
* Note this is the same as calling {@link #loadCompGraph(String, boolean)}
* with a value of false.
* @param name the name of the file to load
* @return the loaded {@link ComputationGraph}
* @throws IOException
*/
public static ComputationGraph loadCompGraph(String name) throws IOException {
return loadCompGraph(name,false);
}
/**
* Loads a {@link ComputationGraph} from the local cache
* or downloads the file from the remote model zoo.
* @param name the name of the file to load
* @param forceDownload whether to force a new download if the file exists or not
* @return the loaded {@link ComputationGraph}
* @throws IOException
*/
public static ComputationGraph loadCompGraph(String name,boolean forceDownload) throws IOException {
return ComputationGraph.load(downloadAndLoadFromZoo("dl4j",name,forceDownload),true);
}
/**
* Loads a samediff model from either the local cache
* or downloads it to the model zoo
* @param name the name of the file to download
* @return
*/
public static SameDiff loadSameDiffModel(String name) {
return loadSameDiffModel(name,false);
}
/**
* Loads a samediff model from either the local cache
* or downloads it to the model zoo
* @param name the name of the file to download
* @param forceDownload whether to force the download of the file
* @return the loaded samediff model
*/
public static SameDiff loadSameDiffModel(String name,boolean forceDownload) {
return SameDiff.load(downloadAndLoadFromZoo("samediff",name,forceDownload),true);
}
/**
* Download and load a model from the model zoo using the given file name
* for the given framework
* @param framework the framework to load from
* @param name the name of the file to load
* @param forceDownload whether to force the download
* @return the
*/
@SneakyThrows
public static File downloadAndLoadFromZoo(String framework,String name,boolean forceDownload) {
File destination = new File(OmnihubConfig.getOmnihubHome(),framework);
File destFile = new File(destination,name);
if(forceDownload && destFile.exists()) {
destFile.delete();
}
if(!destFile.exists()) {
String url = new StringBuilder()
.append(OmnihubConfig.getOmnihubUrl()).append("/").append(framework).append("/").
append(name).toString();
URL remoteUrl = URI.create(url).toURL();
long size = getFileSize(remoteUrl);
try(InputStream is = new ProgressInputStream(new BufferedInputStream(URI.create(url).toURL().openStream()),size)) {
FileUtils.copyInputStreamToFile(is,destFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return destFile;
}
private static int getFileSize(URL url) {
URLConnection conn = null;
try {
conn = url.openConnection();
if(conn instanceof HttpURLConnection) {
((HttpURLConnection)conn).setRequestMethod("HEAD");
}
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(conn instanceof HttpURLConnection) {
((HttpURLConnection)conn).disconnect();
}
}
}
}
@@ -0,0 +1,53 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub;
import me.tongfei.progressbar.ProgressBar;
import org.apache.commons.io.input.CountingInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
public class ProgressInputStream extends CountingInputStream {
private long max;
private ProgressBar pb = new ProgressBar("Download model:", 100); // name, initial max
public ProgressInputStream(InputStream in,long max) {
super(in);
this.max = max;
pb.start();
}
@Override
protected synchronized void afterRead(int n) {
super.afterRead(n);
double progress = (getByteCount() / (double) max) * 100.0;
pb.stepTo((long) progress);
}
@Override
public void close() throws IOException {
super.close();
pb.stop();
}
}
@@ -0,0 +1,51 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.api;
import org.eclipse.deeplearning4j.omnihub.Framework;
import java.util.Locale;
public enum FrameworkNamespace {
DL4J,SAMEDIFF;
public static FrameworkNamespace fromString(String namespace) {
switch(namespace.toLowerCase()) {
case "dl4j":
return DL4J;
case "samediff":
return SAMEDIFF;
default:
return null;
}
}
public String javaClassName() {
switch(this) {
case DL4J:
return "DL4J";
case SAMEDIFF:
return "SAMEDIFF";
}
throw new IllegalStateException("Unable to determine java class type. Invalid namespace type " + this.name());
}
}
@@ -0,0 +1,37 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.api;
public enum ModelType {
COMP_GRAPH,SEQUENTIAL;
public static ModelType fromString(String namespace) {
switch(namespace.toLowerCase()) {
case "comp_graph":
return COMP_GRAPH;
case "sequential":
return SEQUENTIAL;
default:
return null;
}
}
}
@@ -0,0 +1,37 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
//================== GENERATED CODE - DO NOT MODIFY THIS FILE ==================
package org.eclipse.deeplearning4j.omnihub.models;
import java.lang.Exception;
import org.deeplearning4j.nn.graph.ComputationGraph;
public class Dl4jModels {
public Dl4jModels() {
}
/**
* This model is converted from keras applications.Keras applications vgg19 weights can be created with: */
public ComputationGraph vgg19noTop(boolean forceDownload) throws Exception {
return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadCompGraph("vgg19_weights_tf_dim_ordering_tf_kernels_notop.zip",forceDownload);
}
}
@@ -0,0 +1,41 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.models;
/**
* Top level class for accessing pretrained mdoels from Omnihub
* separated by dl4j and samediff.
*
* @author Adam Gibson
*/
public class Pretrained {
private static Dl4jModels dl4j = new Dl4jModels();
private static SameDiffModels samediff = new SameDiffModels();
public static SameDiffModels samediff() {
return samediff;
}
public static Dl4jModels dl4j() {
return dl4j;
}
}
@@ -0,0 +1,43 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
//================== GENERATED CODE - DO NOT MODIFY THIS FILE ==================
package org.eclipse.deeplearning4j.omnihub.models;
import java.lang.Exception;
import org.nd4j.autodiff.samediff.SameDiff;
public class SameDiffModels {
public SameDiffModels() {
}
/**
* This model is converted from the onnx model age_googlenet.onnx at the onnx model zoo at: https://github.com/onnx/models */
public SameDiff ageGooglenet(boolean forceDownload) throws Exception {
return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadSameDiffModel("age_googlenet.fb",forceDownload);
}
/**
* This model is converted from pytorch's torchvision resnet18 model via onnx. For more information see: https://pytorch.org/vision/stable/models.html */
public SameDiff resnet18(boolean forceDownload) throws Exception {
return org.eclipse.deeplearning4j.omnihub.OmniHubUtils.loadSameDiffModel("resnet18.fb",forceDownload);
}
}
@@ -0,0 +1,45 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.api
interface ModelLike {
fun modelUrl(): String
fun modelName(): String
fun pretrained(): Boolean
fun documentation(): String
fun framework(): FrameworkNamespace
fun modelType(): ModelType
}
data class Model(val modelUrl: String,
val modelName: String,
val pretrained: Boolean,
val documentation: String,
val framework: FrameworkNamespace,
val modelType: ModelType = ModelType.COMP_GRAPH): ModelLike {
override fun modelUrl() = modelUrl
override fun modelName() = modelName
override fun pretrained() = pretrained
override fun documentation() = documentation
override fun framework() = framework
override fun modelType() = modelType
}
@@ -0,0 +1,7 @@
package org.eclipse.deeplearning4j.omnihub.api
data class NamespaceModels @JvmOverloads constructor(
val name: String,
val models: MutableList<Model> = mutableListOf()) {
}
@@ -0,0 +1,72 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.dsl
import org.eclipse.deeplearning4j.omnihub.api.FrameworkNamespace
import org.eclipse.deeplearning4j.omnihub.api.Model
import org.eclipse.deeplearning4j.omnihub.api.ModelType
import org.eclipse.deeplearning4j.omnihub.api.NamespaceModels
fun FrameworkNamespace(name: String, block: NamespaceModels.() -> Unit): NamespaceModels {
val ret = NamespaceModels(name)
ret.block()
return ret
}
fun NamespaceModels.DL4JModel(name: String,
documentation: String,
url: String,
pretrained:Boolean = true,
modelType: ModelType,
block: (Model.() -> Unit)? = null): Model {
return this.Model(name,documentation,url,pretrained,FrameworkNamespace.DL4J,modelType,block)
}
fun NamespaceModels.SameDiffModel(name: String,
documentation: String,
url: String,
pretrained:Boolean = true,
block: (Model.() -> Unit)? = null): Model {
return this.Model(name,documentation,url,pretrained,FrameworkNamespace.SAMEDIFF,ModelType.COMP_GRAPH,block)
}
fun NamespaceModels.Model(name: String,
documentation: String,
url: String,
pretrained:Boolean = true,
framework: FrameworkNamespace,
modelType: ModelType = ModelType.COMP_GRAPH,
block: (Model.() -> Unit)? = null): Model {
val model = org.eclipse.deeplearning4j.omnihub.api.Model(url,name,pretrained,documentation,framework,modelType)
if(block != null)
model.block()
this.models.add(model)
return model
}
@@ -0,0 +1,32 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.frameworks
import org.deeplearning4j.omnihub.OmnihubConfig
import org.eclipse.deeplearning4j.omnihub.api.ModelType
import org.eclipse.deeplearning4j.omnihub.dsl.DL4JModel
import org.eclipse.deeplearning4j.omnihub.dsl.FrameworkNamespace
fun Dl4jModels() = FrameworkNamespace("DL4JModels") {
DL4JModel("vgg19noTop","This model is converted from keras applications." +
"Keras applications vgg19 weights can be created with:","${OmnihubConfig.DEFAULT_OMNIHUB_URL}/dl4j/vgg19_weights_tf_dim_ordering_tf_kernels_notop.zip",
true,
ModelType.COMP_GRAPH)
}
@@ -0,0 +1,30 @@
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.eclipse.deeplearning4j.omnihub.frameworks
import org.deeplearning4j.omnihub.OmnihubConfig
import org.eclipse.deeplearning4j.omnihub.dsl.FrameworkNamespace
import org.eclipse.deeplearning4j.omnihub.dsl.SameDiffModel
fun SameDiffModels() = FrameworkNamespace("SameDiffModels") {
SameDiffModel("ageGooglenet","This model is converted from the onnx model age_googlenet.onnx at the onnx model zoo at: https://github.com/onnx/models","${OmnihubConfig.DEFAULT_OMNIHUB_URL}/samediff/age_googlenet.fb",true)
SameDiffModel("resnet18","This model is converted from pytorch's torchvision resnet18 model via onnx. For more information see: https://pytorch.org/vision/stable/models.html","${OmnihubConfig.DEFAULT_OMNIHUB_URL}/samediff/resnet18.fb",true)
}