0
# Core Model Interface
1
2
Foundation interface and abstract class defining how all zoo models are instantiated, configured, and used. Includes pre-trained model support with automatic downloading and caching.
3
4
## Capabilities
5
6
### InstantiableModel Interface
7
8
Core interface that all zoo models must implement, defining the contract for model instantiation and metadata access.
9
10
```java { .api }
11
/**
12
* Interface for defining a model that can be instantiated and return information about itself.
13
*/
14
interface InstantiableModel {
15
/**
16
* Sets the input shape for the model
17
* @param inputShape 2D array representing input dimensions
18
*/
19
void setInputShape(int[][] inputShape);
20
21
/**
22
* Initializes and returns the model instance
23
* @return Model instance ready for training or inference
24
*/
25
Model init();
26
27
/**
28
* Returns metadata about the model including input shapes and outputs
29
* @return ModelMetaData containing model information
30
*/
31
ModelMetaData metaData();
32
33
/**
34
* Returns the zoo type classification for this model
35
* @return ZooType enum value
36
*/
37
ZooType zooType();
38
39
/**
40
* Returns the DeepLearning4j model class type
41
* @return Class extending Model (MultiLayerNetwork or ComputationGraph)
42
*/
43
Class<? extends Model> modelType();
44
45
/**
46
* Returns URL for downloading pre-trained weights
47
* @param pretrainedType Type of pre-trained weights to download
48
* @return URL string or null if not available
49
*/
50
String pretrainedUrl(PretrainedType pretrainedType);
51
52
/**
53
* Returns checksum for verifying downloaded pre-trained weights
54
* @param pretrainedType Type of pre-trained weights
55
* @return Checksum value for verification
56
*/
57
long pretrainedChecksum(PretrainedType pretrainedType);
58
}
59
```
60
61
**Usage Example:**
62
63
```java
64
AlexNet model = new AlexNet(1000, 42, 1);
65
ModelMetaData metadata = model.metaData();
66
int[][] inputShape = metadata.getInputShape(); // [[3, 224, 224]]
67
ZooType type = model.zooType(); // ZooType.ALEXNET
68
Class<? extends Model> modelClass = model.modelType(); // MultiLayerNetwork.class
69
```
70
71
### ZooModel Abstract Class
72
73
Base implementation providing common functionality for pre-trained model downloading, caching, and initialization.
74
75
```java { .api }
76
/**
77
* A zoo model is instantiable, returns information about itself, and can download
78
* pretrained models if available.
79
*/
80
abstract class ZooModel<T> implements InstantiableModel {
81
/**
82
* Root directory for caching downloaded models (defaults to ~/.deeplearning4j/)
83
*/
84
static File ROOT_CACHE_DIR;
85
86
/**
87
* Checks if pre-trained weights are available for the specified type
88
* @param pretrainedType Type of pre-trained weights to check
89
* @return true if pre-trained weights are available
90
*/
91
boolean pretrainedAvailable(PretrainedType pretrainedType);
92
93
/**
94
* Initializes model with ImageNet pre-trained weights (default)
95
* @return Model instance with pre-trained weights loaded
96
* @throws IOException if download or loading fails
97
*/
98
Model initPretrained() throws IOException;
99
100
/**
101
* Initializes model with specified pre-trained weights
102
* @param pretrainedType Type of pre-trained weights to load
103
* @return Model instance with pre-trained weights loaded
104
* @throws IOException if download or loading fails
105
* @throws UnsupportedOperationException if pre-trained weights not available
106
*/
107
Model initPretrained(PretrainedType pretrainedType) throws IOException;
108
}
109
```
110
111
**Usage Examples:**
112
113
```java
114
// Check if pre-trained weights are available
115
VGG16 vgg16 = new VGG16(1000, 42, 1);
116
boolean hasImageNet = vgg16.pretrainedAvailable(PretrainedType.IMAGENET); // true
117
boolean hasMNIST = vgg16.pretrainedAvailable(PretrainedType.MNIST); // false
118
119
// Load pre-trained model (downloads and caches automatically)
120
Model pretrainedModel = vgg16.initPretrained(PretrainedType.IMAGENET);
121
122
// Load default pre-trained weights (ImageNet)
123
Model defaultPretrained = vgg16.initPretrained();
124
125
// Custom cache directory
126
ZooModel.ROOT_CACHE_DIR = new File("/custom/cache/path");
127
```
128
129
### ModelMetaData Class
130
131
Contains metadata describing a model including input shapes, outputs, and configuration information.
132
133
```java { .api }
134
/**
135
* Metadata describing a model, including input shapes. This is helpful for instantiating
136
* the model programmatically and ensuring appropriate inputs are used.
137
*/
138
class ModelMetaData {
139
/**
140
* Creates model metadata
141
* @param inputShape Array of input shape dimensions
142
* @param numOutputs Number of output classes/labels
143
* @param zooType Zoo type classification
144
*/
145
ModelMetaData(int[][] inputShape, int numOutputs, ZooType zooType);
146
147
/**
148
* Gets the input shape dimensions
149
* @return 2D array representing input shapes
150
*/
151
int[][] getInputShape();
152
153
/**
154
* Gets the number of output classes
155
* @return Number of outputs
156
*/
157
int getNumOutputs();
158
159
/**
160
* Gets the zoo type classification
161
* @return ZooType enum value
162
*/
163
ZooType getZooType();
164
165
/**
166
* Indicates if the model should use MultiDataSet (multiple inputs)
167
* @return true if multiple inputs are expected
168
*/
169
boolean useMDS();
170
}
171
```
172
173
**Usage Example:**
174
175
```java
176
AlexNet alexNet = new AlexNet(10, 42, 1);
177
ModelMetaData metadata = alexNet.metaData();
178
int[][] inputShape = metadata.getInputShape(); // [[3, 224, 224]]
179
int numOutputs = metadata.getNumOutputs(); // 1
180
ZooType zooType = metadata.getZooType(); // ZooType.CNN
181
boolean multipleInputs = metadata.useMDS(); // false
182
```