Package ai.djl
Class BaseModel
java.lang.Object
ai.djl.BaseModel
- All Implemented Interfaces:
Model,AutoCloseable
- Direct Known Subclasses:
RpcModel
BaseModel is the basic implementation of Model.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Returns the input descriptor of the model.Returns the output descriptor of the model.protected voidfinalize()getArtifact(String artifactName) Finds an artifact resource with a given name in the model.<T> TgetArtifact(String name, Function<InputStream, T> function) Attempts to load the artifact using the given function and cache it if the specified artifact is not already cached.getArtifactAsStream(String name) Finds an artifact resource with a given name in the model.String[]Returns the artifact names associated with the model.getBlock()Gets the block from the Model.Returns the standard data type used within the model.Returns the directory from where the model is loaded.getName()Gets the model name.Gets theNDManagerfrom the model.Returns the model's properties.getProperty(String key) Returns the property of the model based on property name.voidload(InputStream is, Map<String, ?> options) Loads the model from theInputStreamwith the options provided.protected void<I,O> Predictor<I, O> newPredictor(Translator<I, O> translator, Device device) Creates a new Predictor based on the model.newTrainer(TrainingConfig trainingConfig) Creates a newTrainerinstance for a Model.protected PathparamPathResolver(String prefix, Map<String, ?> options) protected booleanreadParameters(InputStream paramStream, Map<String, ?> options) protected booleanreadParameters(Path paramFile, Map<String, ?> options) voidSaves the model to the specifiedmodelPathwith the name provided.voidSets the block for the Model for training and inference.voidsetDataType(DataType dataType) Sets the standard data type used within the model.protected voidsetModelDir(Path modelDir) voidsetProperty(String key, String value) Sets a property to the model.toString()Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.djl.Model
cast, getProperty, intProperty, load, load, load, load, longProperty, newPredictor, quantize
-
Field Details
-
modelDir
-
block
-
modelName
-
manager
-
dataType
-
wasLoaded
protected boolean wasLoaded -
inputData
-
artifacts
-
properties
-
-
Constructor Details
-
BaseModel
-
-
Method Details
-
getBlock
Gets the block from the Model. -
setBlock
Sets the block for the Model for training and inference. -
getName
Gets the model name. -
getNDManager
Gets theNDManagerfrom the model.- Specified by:
getNDManagerin interfaceModel- Returns:
- the
NDManager
-
newTrainer
Creates a newTrainerinstance for a Model.- Specified by:
newTrainerin interfaceModel- Parameters:
trainingConfig- training configuration settings- Returns:
- the
Trainerinstance
-
newPredictor
Creates a new Predictor based on the model.- Specified by:
newPredictorin interfaceModel- Type Parameters:
I- the input object for pre-processingO- 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
-
setDataType
Sets the standard data type used within the model.- Specified by:
setDataTypein interfaceModel- Parameters:
dataType- the standard data type to use
-
getDataType
Returns the standard data type used within the model.- Specified by:
getDataTypein interfaceModel- Returns:
- the standard data type used within the model
-
load
Loads the model from theInputStreamwith the options provided.- Specified by:
loadin interfaceModel- Parameters:
is- theInputStreamto load the model fromoptions- engine specific load model options, see documentation for each engine- Throws:
IOException- when IO operation fails in loading a resourceMalformedModelException- if model file is corrupted
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceModel
-
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:
describeInputin interfaceModel- Returns:
- a PairList of String and Shape
-
describeOutput
Returns the output descriptor of the model.It contains the output information that can be obtained from the model.
- Specified by:
describeOutputin interfaceModel- Returns:
- a PairList of String and Shape
-
getArtifactNames
Returns the artifact names associated with the model.- Specified by:
getArtifactNamesin interfaceModel- Returns:
- an array of artifact names
-
getArtifact
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:
getArtifactin 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:
IOException- when IO operation fails in loading a resource
-
getArtifact
Finds an artifact resource with a given name in the model.- Specified by:
getArtifactin interfaceModel- Parameters:
artifactName- the name of the desired artifact- Returns:
- a
URLobject ornullif no artifact with this name is found - Throws:
IOException- when IO operation fails in loading a resource
-
getArtifactAsStream
Finds an artifact resource with a given name in the model.- Specified by:
getArtifactAsStreamin interfaceModel- Parameters:
name- the name of the desired artifact- Returns:
- a
InputStreamobject ornullif no resource with this name is found - Throws:
IOException- when IO operation fails in loading a resource
-
setProperty
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:
setPropertyin interfaceModel- Parameters:
key- the name of the propertyvalue- the value of the property
-
getProperty
Returns the property of the model based on property name.- Specified by:
getPropertyin interfaceModel- Parameters:
key- the name of the property- Returns:
- the value of the property
-
getProperties
Returns the model's properties.- Specified by:
getPropertiesin interfaceModel- Returns:
- the model's properties
-
setModelDir
-
loadBlock
protected void loadBlock(String prefix, Map<String, ?> options) throws IOException, MalformedModelException- Throws:
IOExceptionMalformedModelException
-
save
Saves the model to the specifiedmodelPathwith the name provided.- Specified by:
savein interfaceModel- Parameters:
modelPath- the directory or file path of the model locationnewModelName- the new model name to be saved, use null to keep original model name- Throws:
IOException- when IO operation fails in loading a resource
-
getModelPath
Returns the directory from where the model is loaded.- Specified by:
getModelPathin interfaceModel- Returns:
- the directory of the model location
-
toString
-
finalize
-
paramPathResolver
- Throws:
IOException
-
readParameters
protected boolean readParameters(Path paramFile, Map<String, ?> options) throws IOException, MalformedModelException- Throws:
IOExceptionMalformedModelException
-
readParameters
protected boolean readParameters(InputStream paramStream, Map<String, ?> options) throws IOException, MalformedModelException- Throws:
IOExceptionMalformedModelException
-