Package org.deeplearning4j.util
Class ModelSerializer
- java.lang.Object
-
- org.deeplearning4j.util.ModelSerializer
-
public class ModelSerializer extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static String
COEFFICIENTS_BIN
static String
CONFIGURATION_JSON
static String
NO_PARAMS_MARKER
static String
NORMALIZER_BIN
static String
PREPROCESSOR_BIN
static String
UPDATER_BIN
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addNormalizerToModel(File f, Normalizer<?> normalizer)
This method appends normalizer to a given persisted model.static void
addObjectToFile(@NonNull File f, @NonNull String key, @NonNull Object o)
Add an object to the (already existing) model file using Java Object Serialization.static <T> T
getObjectFromFile(@NonNull File f, @NonNull String key)
Get an object with the specified key from the model file, that was previously added to the file usingaddObjectToFile(File, String, Object)
static List<String>
listObjectsInFile(@NonNull File f)
List the keys of all objects added using the methodaddObjectToFile(File, String, Object)
static ComputationGraph
restoreComputationGraph(@NonNull File file)
Load a computation graph from a filestatic ComputationGraph
restoreComputationGraph(@NonNull File file, boolean loadUpdater)
Load a computation graph from a filestatic ComputationGraph
restoreComputationGraph(@NonNull InputStream is)
Load a computation graph from a InputStreamstatic ComputationGraph
restoreComputationGraph(@NonNull InputStream is, boolean loadUpdater)
Load a computation graph from a InputStreamstatic ComputationGraph
restoreComputationGraph(@NonNull String path)
Load a computation graph from a filestatic ComputationGraph
restoreComputationGraph(@NonNull String path, boolean loadUpdater)
Load a computation graph from a filestatic Pair<ComputationGraph,Normalizer>
restoreComputationGraphAndNormalizer(@NonNull File file, boolean loadUpdater)
Restore a ComputationGraph and Normalizer (if present - null if not) from a Filestatic Pair<ComputationGraph,Normalizer>
restoreComputationGraphAndNormalizer(@NonNull InputStream is, boolean loadUpdater)
Restore a ComputationGraph and Normalizer (if present - null if not) from the InputStream.static MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull File file)
Load a multi layer network from a filestatic MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull File file, boolean loadUpdater)
Load a multi layer network from a filestatic MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull InputStream is)
Restore a multi layer network from an input stream
* Note: the input stream is read fully and closed by this method.static MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull InputStream is, boolean loadUpdater)
Load a MultiLayerNetwork from InputStream from an input stream
Note: the input stream is read fully and closed by this method.static MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull String path)
Load a MultilayerNetwork model from a filestatic MultiLayerNetwork
restoreMultiLayerNetwork(@NonNull String path, boolean loadUpdater)
Load a MultilayerNetwork model from a filestatic Pair<MultiLayerNetwork,Normalizer>
restoreMultiLayerNetworkAndNormalizer(@NonNull File file, boolean loadUpdater)
Restore a MultiLayerNetwork and Normalizer (if present - null if not) from a Filestatic Pair<MultiLayerNetwork,Normalizer>
restoreMultiLayerNetworkAndNormalizer(@NonNull InputStream is, boolean loadUpdater)
Restore a MultiLayerNetwork and Normalizer (if present - null if not) from the InputStream.static <T extends Normalizer>
TrestoreNormalizerFromFile(File file)
This method restores normalizer from a given persisted model file PLEASE NOTE: File should be model file saved earlier with ModelSerializer with addNormalizerToModel being calledstatic <T extends Normalizer>
TrestoreNormalizerFromInputStream(InputStream is)
This method restores the normalizer form a persisted model file.static Task
taskByModel(Model model)
static void
writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater)
Write a model to a filestatic void
writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater, DataNormalization dataNormalization)
Write a model to a filestatic void
writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater)
Write a model to an output streamstatic void
writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater, DataNormalization dataNormalization)
Write a model to an output streamstatic void
writeModel(@NonNull Model model, @NonNull String path, boolean saveUpdater)
Write a model to a file path
-
-
-
Field Detail
-
UPDATER_BIN
public static final String UPDATER_BIN
- See Also:
- Constant Field Values
-
NORMALIZER_BIN
public static final String NORMALIZER_BIN
- See Also:
- Constant Field Values
-
CONFIGURATION_JSON
public static final String CONFIGURATION_JSON
- See Also:
- Constant Field Values
-
COEFFICIENTS_BIN
public static final String COEFFICIENTS_BIN
- See Also:
- Constant Field Values
-
NO_PARAMS_MARKER
public static final String NO_PARAMS_MARKER
- See Also:
- Constant Field Values
-
PREPROCESSOR_BIN
public static final String PREPROCESSOR_BIN
- See Also:
- Constant Field Values
-
-
Method Detail
-
writeModel
public static void writeModel(@NonNull @NonNull Model model, @NonNull @NonNull File file, boolean saveUpdater) throws IOException
Write a model to a file- Parameters:
model
- the model to writefile
- the file to write tosaveUpdater
- whether to save the updater or not- Throws:
IOException
-
writeModel
public static void writeModel(@NonNull @NonNull Model model, @NonNull @NonNull File file, boolean saveUpdater, DataNormalization dataNormalization) throws IOException
Write a model to a file- Parameters:
model
- the model to writefile
- the file to write tosaveUpdater
- whether to save the updater or notdataNormalization
- the normalizer to save (optional)- Throws:
IOException
-
writeModel
public static void writeModel(@NonNull @NonNull Model model, @NonNull @NonNull String path, boolean saveUpdater) throws IOException
Write a model to a file path- Parameters:
model
- the model to writepath
- the path to write tosaveUpdater
- whether to save the updater or not- Throws:
IOException
-
writeModel
public static void writeModel(@NonNull @NonNull Model model, @NonNull @NonNull OutputStream stream, boolean saveUpdater) throws IOException
Write a model to an output stream- Parameters:
model
- the model to savestream
- the output stream to write tosaveUpdater
- whether to save the updater for the model or not- Throws:
IOException
-
writeModel
public static void writeModel(@NonNull @NonNull Model model, @NonNull @NonNull OutputStream stream, boolean saveUpdater, DataNormalization dataNormalization) throws IOException
Write a model to an output stream- Parameters:
model
- the model to savestream
- the output stream to write tosaveUpdater
- whether to save the updater for the model or notdataNormalization
- the normalizer ot save (may be null)- Throws:
IOException
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull File file) throws IOException
Load a multi layer network from a file- Parameters:
file
- the file to load from- Returns:
- the loaded multi layer network
- Throws:
IOException
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull File file, boolean loadUpdater) throws IOException
Load a multi layer network from a file- Parameters:
file
- the file to load from- Returns:
- the loaded multi layer network
- Throws:
IOException
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull InputStream is, boolean loadUpdater) throws IOException
Load a MultiLayerNetwork from InputStream from an input stream
Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.- Parameters:
is
- the inputstream to load from- Returns:
- the loaded multi layer network
- Throws:
IOException
- See Also:
restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull InputStream is) throws IOException
Restore a multi layer network from an input stream
* Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.- Parameters:
is
- the input stream to restore from- Returns:
- the loaded multi layer network
- Throws:
IOException
- See Also:
restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull String path) throws IOException
Load a MultilayerNetwork model from a file- Parameters:
path
- path to the model file, to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreMultiLayerNetwork
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull @NonNull String path, boolean loadUpdater) throws IOException
Load a MultilayerNetwork model from a file- Parameters:
path
- path to the model file, to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreMultiLayerNetworkAndNormalizer
public static Pair<MultiLayerNetwork,Normalizer> restoreMultiLayerNetworkAndNormalizer(@NonNull @NonNull InputStream is, boolean loadUpdater) throws IOException
Restore a MultiLayerNetwork and Normalizer (if present - null if not) from the InputStream. Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.- Parameters:
is
- Input stream to read fromloadUpdater
- Whether to load the updater from the model or not- Returns:
- Model and normalizer, if present
- Throws:
IOException
- If an error occurs when reading from the stream
-
restoreMultiLayerNetworkAndNormalizer
public static Pair<MultiLayerNetwork,Normalizer> restoreMultiLayerNetworkAndNormalizer(@NonNull @NonNull File file, boolean loadUpdater) throws IOException
Restore a MultiLayerNetwork and Normalizer (if present - null if not) from a File- Parameters:
file
- File to read the model and normalizer fromloadUpdater
- Whether to load the updater from the model or not- Returns:
- Model and normalizer, if present
- Throws:
IOException
- If an error occurs when reading from the File
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull String path) throws IOException
Load a computation graph from a file- Parameters:
path
- path to the model file, to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull String path, boolean loadUpdater) throws IOException
Load a computation graph from a file- Parameters:
path
- path to the model file, to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull InputStream is, boolean loadUpdater) throws IOException
Load a computation graph from a InputStream- Parameters:
is
- the inputstream to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull InputStream is) throws IOException
Load a computation graph from a InputStream- Parameters:
is
- the inputstream to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull File file) throws IOException
Load a computation graph from a file- Parameters:
file
- the file to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
restoreComputationGraphAndNormalizer
public static Pair<ComputationGraph,Normalizer> restoreComputationGraphAndNormalizer(@NonNull @NonNull InputStream is, boolean loadUpdater) throws IOException
Restore a ComputationGraph and Normalizer (if present - null if not) from the InputStream. Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.- Parameters:
is
- Input stream to read fromloadUpdater
- Whether to load the updater from the model or not- Returns:
- Model and normalizer, if present
- Throws:
IOException
- If an error occurs when reading from the stream
-
restoreComputationGraphAndNormalizer
public static Pair<ComputationGraph,Normalizer> restoreComputationGraphAndNormalizer(@NonNull @NonNull File file, boolean loadUpdater) throws IOException
Restore a ComputationGraph and Normalizer (if present - null if not) from a File- Parameters:
file
- File to read the model and normalizer fromloadUpdater
- Whether to load the updater from the model or not- Returns:
- Model and normalizer, if present
- Throws:
IOException
- If an error occurs when reading from the File
-
restoreComputationGraph
public static ComputationGraph restoreComputationGraph(@NonNull @NonNull File file, boolean loadUpdater) throws IOException
Load a computation graph from a file- Parameters:
file
- the file to get the computation graph from- Returns:
- the loaded computation graph
- Throws:
IOException
-
addNormalizerToModel
public static void addNormalizerToModel(File f, Normalizer<?> normalizer)
This method appends normalizer to a given persisted model. PLEASE NOTE: File should be model file saved earlier with ModelSerializer- Parameters:
f
-normalizer
-
-
addObjectToFile
public static void addObjectToFile(@NonNull @NonNull File f, @NonNull @NonNull String key, @NonNull @NonNull Object o)
Add an object to the (already existing) model file using Java Object Serialization. Objects can be restored usinggetObjectFromFile(File, String)
- Parameters:
f
- File to add the object tokey
- Key to store the object undero
- Object to store using Java object serialization
-
getObjectFromFile
public static <T> T getObjectFromFile(@NonNull @NonNull File f, @NonNull @NonNull String key)
Get an object with the specified key from the model file, that was previously added to the file usingaddObjectToFile(File, String, Object)
- Type Parameters:
T
- Type of the object- Parameters:
f
- model file to add the object tokey
- Key for the object- Returns:
- The serialized object
- See Also:
listObjectsInFile(File)
-
listObjectsInFile
public static List<String> listObjectsInFile(@NonNull @NonNull File f)
List the keys of all objects added using the methodaddObjectToFile(File, String, Object)
- Parameters:
f
- File previously created with ModelSerializer- Returns:
- List of keys that can be used with
getObjectFromFile(File, String)
-
restoreNormalizerFromFile
public static <T extends Normalizer> T restoreNormalizerFromFile(File file) throws IOException
This method restores normalizer from a given persisted model file PLEASE NOTE: File should be model file saved earlier with ModelSerializer with addNormalizerToModel being called- Parameters:
file
-- Returns:
- Throws:
IOException
-
restoreNormalizerFromInputStream
public static <T extends Normalizer> T restoreNormalizerFromInputStream(InputStream is) throws IOException
This method restores the normalizer form a persisted model file.- Parameters:
is
- A stream to load data from.- Returns:
- the loaded normalizer
- Throws:
IOException
-
-