Class ModelSerializer


  • public class ModelSerializer
    extends Object
    • 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 write
        file - the file to write to
        saveUpdater - 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 write
        file - the file to write to
        saveUpdater - whether to save the updater or not
        dataNormalization - 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 write
        path - the path to write to
        saveUpdater - 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 save
        stream - the output stream to write to
        saveUpdater - 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 save
        stream - the output stream to write to
        saveUpdater - whether to save the updater for the model or not
        dataNormalization - 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 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 from
        loadUpdater - 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 from
        loadUpdater - 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 from
        loadUpdater - 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 from
        loadUpdater - 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
      • taskByModel

        public static Task taskByModel​(Model model)
        Parameters:
        model -
        Returns:
      • 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 using getObjectFromFile(File, String)
        Parameters:
        f - File to add the object to
        key - Key to store the object under
        o - 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 using addObjectToFile(File, String, Object)
        Type Parameters:
        T - Type of the object
        Parameters:
        f - model file to add the object to
        key - Key for the object
        Returns:
        The serialized object
        See Also:
        listObjectsInFile(File)
      • 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