Package ai.djl.repository.zoo
Class ZooModel<I,O>
- java.lang.Object
-
- ai.djl.repository.zoo.ZooModel<I,O>
-
-
Constructor Summary
Constructors Constructor Description ZooModel(Model model, Translator<I,O> translator)
Constructs aZooModel
given the model and translator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cast(DataType dataType)
Casts the model to support a different precision level.void
close()
ai.djl.util.PairList<java.lang.String,Shape>
describeInput()
Returns the input descriptor of the model.ai.djl.util.PairList<java.lang.String,Shape>
describeOutput()
Returns the output descriptor of the model.java.net.URL
getArtifact(java.lang.String name)
Finds an artifact resource with a given name in the model.<T> T
getArtifact(java.lang.String name, java.util.function.Function<java.io.InputStream,T> function)
Attempts to load the artifact using the given function and cache it if the specified artifact is not already cached.java.io.InputStream
getArtifactAsStream(java.lang.String name)
Finds an artifact resource with a given name in the model.java.lang.String[]
getArtifactNames()
Returns the artifact names associated with the model.Block
getBlock()
Gets the block from the Model.DataType
getDataType()
Returns the standard data type used within the model.java.nio.file.Path
getModelPath()
Returns the directory from where the model is loaded.java.lang.String
getName()
Gets the model name.NDManager
getNDManager()
Gets theNDManager
from the model.java.lang.String
getProperty(java.lang.String key)
Returns the property of the model based on property name.Translator<I,O>
getTranslator()
Returns the default translator.Model
getWrappedModel()
Returns the wrapped model.void
load(java.io.InputStream modelStream, java.util.Map<java.lang.String,?> options)
Loads the model from theInputStream
with the options provided.void
load(java.nio.file.Path modelPath, java.lang.String prefix, java.util.Map<java.lang.String,?> options)
Loads the model from themodelPath
with the name and options provided.Predictor<I,O>
newPredictor()
Creates a new Predictor based on the model with the default translator.Predictor<I,O>
newPredictor(Device device)
Creates a new Predictor based on the model with the default translator and a specified device.<P,Q>
Predictor<P,Q>newPredictor(Translator<P,Q> translator, Device device)
Creates a new Predictor based on the model.Trainer
newTrainer(TrainingConfig trainingConfig)
Creates a newTrainer
instance for a Model.void
save(java.nio.file.Path modelPath, java.lang.String modelName)
Saves the model to the specifiedmodelPath
with the name provided.void
setBlock(Block block)
Sets the block for the Model for training and inference.void
setDataType(DataType dataType)
Sets the standard data type used within the model.void
setProperty(java.lang.String key, java.lang.String value)
Sets a property to the model.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ai.djl.Model
getProperty, load, load, load, newPredictor, quantize
-
-
-
-
Constructor Detail
-
ZooModel
public ZooModel(Model model, Translator<I,O> translator)
Constructs aZooModel
given the model and translator.- Parameters:
model
- the model to wraptranslator
- the translator
-
-
Method Detail
-
load
public void load(java.nio.file.Path modelPath, java.lang.String prefix, java.util.Map<java.lang.String,?> options)
Loads the model from themodelPath
with the name and options provided.
-
load
public void load(java.io.InputStream modelStream, java.util.Map<java.lang.String,?> options) throws java.io.IOException
Loads the model from theInputStream
with the options provided.
-
getWrappedModel
public Model getWrappedModel()
Returns the wrapped model.- Returns:
- the wrapped model
-
save
public void save(java.nio.file.Path modelPath, java.lang.String modelName) throws java.io.IOException
Saves the model to the specifiedmodelPath
with the name provided.
-
getModelPath
public java.nio.file.Path getModelPath()
Returns the directory from where the model is loaded.- Specified by:
getModelPath
in interfaceModel
- Returns:
- the directory of the model location
-
getBlock
public Block getBlock()
Gets the block from the Model.
-
setBlock
public void setBlock(Block block)
Sets the block for the Model for training and inference.
-
getName
public java.lang.String getName()
Gets the model name.
-
getProperty
public java.lang.String getProperty(java.lang.String key)
Returns the property of the model based on property name.- Specified by:
getProperty
in interfaceModel
- Parameters:
key
- the name of the property- Returns:
- the value of the property
-
setProperty
public void setProperty(java.lang.String key, java.lang.String value)
Sets a property to the model.properties will be saved/loaded with model, user can store some information about the model in here.
- Specified by:
setProperty
in interfaceModel
- Parameters:
key
- the name of the propertyvalue
- the value of the property
-
newTrainer
public Trainer newTrainer(TrainingConfig trainingConfig)
Creates a newTrainer
instance for a Model.- Specified by:
newTrainer
in interfaceModel
- Parameters:
trainingConfig
- training configuration settings- Returns:
- the
Trainer
instance
-
newPredictor
public Predictor<I,O> newPredictor()
Creates a new Predictor based on the model with the default translator.- Returns:
- an instance of
Predictor
-
newPredictor
public Predictor<I,O> newPredictor(Device device)
Creates a new Predictor based on the model with the default translator and a specified device.- Parameters:
device
- the device to use for prediction- Returns:
- an instance of
Predictor
-
newPredictor
public <P,Q> Predictor<P,Q> newPredictor(Translator<P,Q> translator, Device device)
Creates a new Predictor based on the model.- Specified by:
newPredictor
in interfaceModel
- Type Parameters:
P
- the input object for pre-processingQ
- the output object from postprocessing- Parameters:
translator
- the object used for pre-processing and postprocessingdevice
- the device to use for prediction- Returns:
- an instance of
Predictor
-
getTranslator
public Translator<I,O> getTranslator()
Returns the default translator.- Returns:
- the default translator
-
describeInput
public ai.djl.util.PairList<java.lang.String,Shape> describeInput()
Returns the input descriptor of the model.It contains the information that can be extracted from the model, usually name, shape, layout and DataType.
- Specified by:
describeInput
in interfaceModel
- Returns:
- a PairList of String and Shape
-
describeOutput
public ai.djl.util.PairList<java.lang.String,Shape> describeOutput()
Returns the output descriptor of the model.It contains the output information that can be obtained from the model.
- Specified by:
describeOutput
in interfaceModel
- Returns:
- a PairList of String and Shape
-
getArtifactNames
public java.lang.String[] getArtifactNames()
Returns the artifact names associated with the model.- Specified by:
getArtifactNames
in interfaceModel
- Returns:
- an array of artifact names
-
getArtifact
public <T> T getArtifact(java.lang.String name, java.util.function.Function<java.io.InputStream,T> function) throws java.io.IOException
Attempts to load the artifact using the given function and cache it if the specified artifact is not already cached.Model will cache loaded artifact, so the user doesn't need to keep tracking it.
String synset = model.getArtifact("synset.txt", k -> IOUtils.toString(k)));
- Specified by:
getArtifact
in interfaceModel
- Type Parameters:
T
- the type of the returned artifact object- Parameters:
name
- the name of the desired artifactfunction
- the function to load the artifact- Returns:
- the current (existing or computed) artifact associated with the specified name, or null if the computed value is null
- Throws:
java.io.IOException
- when IO operation fails in loading a resource
-
getArtifact
public java.net.URL getArtifact(java.lang.String name) throws java.io.IOException
Finds an artifact resource with a given name in the model.- Specified by:
getArtifact
in interfaceModel
- Parameters:
name
- the name of the desired artifact- Returns:
- a
URL
object ornull
if no artifact with this name is found - Throws:
java.io.IOException
- when IO operation fails in loading a resource
-
getArtifactAsStream
public java.io.InputStream getArtifactAsStream(java.lang.String name) throws java.io.IOException
Finds an artifact resource with a given name in the model.- Specified by:
getArtifactAsStream
in interfaceModel
- Parameters:
name
- the name of the desired artifact- Returns:
- a
InputStream
object ornull
if no resource with this name is found - Throws:
java.io.IOException
- when IO operation fails in loading a resource
-
getNDManager
public NDManager getNDManager()
Gets theNDManager
from the model.- Specified by:
getNDManager
in interfaceModel
- Returns:
- the
NDManager
-
setDataType
public void setDataType(DataType dataType)
Sets the standard data type used within the model.- Specified by:
setDataType
in interfaceModel
- Parameters:
dataType
- the standard data type to use
-
getDataType
public DataType getDataType()
Returns the standard data type used within the model.- Specified by:
getDataType
in interfaceModel
- Returns:
- the standard data type used within the model
-
cast
public void cast(DataType dataType)
Casts the model to support a different precision level.For example, you can cast the precision from Float to Int
-
-