151 lines
5.5 KiB
Java
Raw Normal View History

2021-02-01 14:31:20 +09:00
/*
* ******************************************************************************
* *
* *
* * 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.
* *
2021-02-01 17:47:29 +09:00
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
2021-02-01 14:31:20 +09:00
* * 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
* *****************************************************************************
*/
2019-06-06 15:21:15 +03:00
package org.deeplearning4j.common.resources;
import lombok.NonNull;
Refactor packages to fix split package issues (#411) * Refactor nd4j-common: org.nd4j.* -> org.nd4j.common.* Signed-off-by: Alex Black <blacka101@gmail.com> * Fix CUDA (missed nd4j-common package refactoring changes) Signed-off-by: Alex Black <blacka101@gmail.com> * nd4j-kryo: org.nd4j -> org.nd4j.kryo Signed-off-by: Alex Black <blacka101@gmail.com> * Fix nd4j-common for deeplearning4j-cuda Signed-off-by: Alex Black <blacka101@gmail.com> * nd4j-grppc-client: org.nd4j.graph -> org.nd4j.remote.grpc Signed-off-by: Alex Black <blacka101@gmail.com> * deeplearning4j-common: org.deeplearning4.* -> org.deeplearning4j.common.* Signed-off-by: Alex Black <blacka101@gmail.com> * deeplearning4j-core: org.deeplearning4j.* -> org.deeplearning.core.* Signed-off-by: Alex Black <blacka101@gmail.com> * deeplearning4j-cuda: org.deeplearning4j.nn.layers.* -> org.deeplearning4j.cuda.* Signed-off-by: Alex Black <blacka101@gmail.com> * Import fixes Signed-off-by: Alex Black <blacka101@gmail.com> * deeplearning4j-nlp-*: org.deeplearning4.text.* -> org.deeplearning4j.nlp.(language).* Signed-off-by: Alex Black <blacka101@gmail.com> * deeplearning4j-ui-model: org.deeplearning4j.ui -> org.deeplearning4j.ui.model Signed-off-by: Alex Black <blacka101@gmail.com> * datavec-spark-inference-{server/model/client}: org.datavec.spark.transform -> org.datavec.spark.inference.{server/model/client} Signed-off-by: Alex Black <blacka101@gmail.com> * datavec-jdbc: org.datavec.api -> org.datavec.jdbc Signed-off-by: Alex Black <blacka101@gmail.com> * Delete org.deeplearning4j.datasets.iterator.impl.MultiDataSetIteratorAdapter in favor of (essentially identical) org.nd4j.linalg.dataset.adapter.MultiDataSetIteratorAdapter Signed-off-by: Alex Black <blacka101@gmail.com> * ND4S fixes Signed-off-by: Alex Black <blacka101@gmail.com> * Fixes Signed-off-by: Alex Black <blacka101@gmail.com> * nd4j-common-tests: org.nd4j.* -> org.nd4j.common.tests Signed-off-by: Alex Black <blacka101@gmail.com> * Trigger CI Signed-off-by: Alex Black <blacka101@gmail.com> * Fixes Signed-off-by: Alex Black <blacka101@gmail.com> * #8878 Ignore CUDA tests on modules with 'nd4j-native under cuda' issue Signed-off-by: Alex Black <blacka101@gmail.com> * Fix bad imports in tests Signed-off-by: Alex Black <blacka101@gmail.com> * Add ignore on test (already failing) due to #8882 Signed-off-by: Alex Black <blacka101@gmail.com> * Import fixes Signed-off-by: Alex Black <blacka101@gmail.com> * Additional import fixes Signed-off-by: Alex Black <blacka101@gmail.com>
2020-04-29 11:19:26 +10:00
import org.deeplearning4j.common.config.DL4JSystemProperties;
import org.nd4j.common.base.Preconditions;
2019-06-06 15:21:15 +03:00
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class DL4JResources {
/**
* @deprecated Use {@link DL4JSystemProperties#DL4J_RESOURCES_DIR_PROPERTY}
*/
@Deprecated
public static final String DL4J_RESOURCES_DIR_PROPERTY = DL4JSystemProperties.DL4J_RESOURCES_DIR_PROPERTY;
/**
* @deprecated Use {@link DL4JSystemProperties#DL4J_RESOURCES_BASE_URL_PROPERTY}
*/
@Deprecated
public static final String DL4J_BASE_URL_PROPERTY = DL4JSystemProperties.DL4J_RESOURCES_BASE_URL_PROPERTY;
private static final String DL4J_DEFAULT_URL = "https://dl4jdata.blob.core.windows.net/";
2019-06-06 15:21:15 +03:00
private static File baseDirectory;
private static String baseURL;
static {
resetBaseDirectoryLocation();
String property = System.getProperty(DL4JSystemProperties.DL4J_RESOURCES_BASE_URL_PROPERTY);
if(property != null){
baseURL = property;
} else {
baseURL = DL4J_DEFAULT_URL;
}
}
/**
* Set the base download URL for (most) DL4J datasets and models.<br>
* This usually doesn't need to be set manually unless there is some issue with the default location
* @param baseDownloadURL Base download URL to set. For example, https://dl4jdata.blob.core.windows.net/
2019-06-06 15:21:15 +03:00
*/
public static void setBaseDownloadURL(@NonNull String baseDownloadURL){
baseURL = baseDownloadURL;
}
/**
* @return The base URL hosting DL4J datasets and models
*/
public static String getBaseDownloadURL(){
return baseURL;
}
/**
* Get the URL relative to the base URL.<br>
* For example, if baseURL is "https://dl4jdata.blob.core.windows.net/", and relativeToBase is "/datasets/iris.dat"
* this simply returns "https://dl4jdata.blob.core.windows.net/datasets/iris.dat"
2019-06-06 15:21:15 +03:00
*
* @param relativeToBase Relative URL
* @return URL
* @throws MalformedURLException For bad URL
*/
public static URL getURL(String relativeToBase) throws MalformedURLException {
return new URL(getURLString(relativeToBase));
}
/**
* Get the URL relative to the base URL as a String.<br>
* For example, if baseURL is "https://dl4jdata.blob.core.windows.net/", and relativeToBase is "/datasets/iris.dat"
* this simply returns "https://dl4jdata.blob.core.windows.net/datasets/iris.dat"
2019-06-06 15:21:15 +03:00
*
* @param relativeToBase Relative URL
* @return URL
* @throws MalformedURLException For bad URL
*/
public static String getURLString(String relativeToBase){
if(relativeToBase.startsWith("/")){
relativeToBase = relativeToBase.substring(1);
}
return baseURL + relativeToBase;
}
/**
* Reset to the default directory, or the directory set via the {@link DL4JSystemProperties#DL4J_RESOURCES_DIR_PROPERTY} system property,
* org.deeplearning4j.resources.directory
*/
public static void resetBaseDirectoryLocation(){
String property = System.getProperty(DL4JSystemProperties.DL4J_RESOURCES_DIR_PROPERTY);
if(property != null){
baseDirectory = new File(property);
} else {
baseDirectory = new File(System.getProperty("user.home"), ".deeplearning4j");
}
if(!baseDirectory.exists()){
baseDirectory.mkdirs();
}
}
/**
* Set the base directory for local storage of files. Default is: {@code new File(System.getProperty("user.home"), ".deeplearning4j")}
* @param f Base directory to use for resources
*/
public static void setBaseDirectory(@NonNull File f){
Preconditions.checkState(f.exists() && f.isDirectory(), "Specified base directory does not exist and/or is not a directory: %s", f.getAbsolutePath());
baseDirectory = f;
}
/**
* @return The base storage directory for DL4J resources
*/
public static File getBaseDirectory(){
return baseDirectory;
}
/**
* Get the storage location for the specified resource type and resource name
* @param resourceType Type of resource
* @param resourceName Name of the resource
* @return The root directory. Creates the directory and any parent directories, if required
*/
public static File getDirectory(ResourceType resourceType, String resourceName){
File f = new File(baseDirectory, resourceType.resourceName());
f = new File(f, resourceName);
f.mkdirs();
return f;
}
}