If you want to use the new model zoo, you will need to add it as a dependency. A Maven POM would add the following:

Once you’ve successfully added the zoo dependency to your project, you can start to import and use models. Each model extends the abstract class and uses the InstantiableModel interface. These classes provide methods that help you initialize either an empty, fresh network or a pretrained network.

You can instantly instantiate a model from the zoo using the .init() method. For example, if you want to instantiate a fresh, untrained network of AlexNet you can use the following code:

  1. import org.deeplearning4j.zoo.model.AlexNet
  2. import org.deeplearning4j.zoo.*;
  3. ...
  4. int numberOfClassesInYourData = 1000;
  5. int randomSeed = 123;
  6. ZooModel zooModel = AlexNet.builder()
  7. .numClasses(numberOfClassesInYourData)
  8. .seed(randomSeed)
  9. Model net = zooModel.init();

If you want to tune parameters or change the optimization algorithm, you can obtain a reference to the underlying network configuration:

For example, you can initialize a VGG-16 model with ImageNet weights like so:

  1. import org.deeplearning4j.zoo.model.VGG16;
  2. import org.deeplearning4j.zoo.*;
  3. ...
  4. ZooModel zooModel = VGG16.builder().build();;
  5. Model net = zooModel.initPretrained(PretrainedType.IMAGENET);

And initialize another VGG16 model with weights trained on VGGFace:

If you’re not sure whether a model contains pretrained weights, you can use the method which returns a boolean. Simply pass a PretrainedType enum to this method, which returns true if weights are available.

Note that for convolutional models, input shape information follows the NCHW convention. So if a model’s input shape default is new int[]{3, 224, 224}, this means the model has 3 channels and height/width of 224.

You can find a complete list of models using this .

This includes ImageNet models such as VGG-16, ResNet-50, AlexNet, Inception-ResNet-v1, LeNet, and more.

The zoo comes with a couple additional features if you’re looking to use the models for different use cases.

Aside from passing certain configuration information to the constructor of a zoo model, you can also change its input shape using .setInputShape(). NOTE: this applies to fresh configurations only, and will not affect pretrained models:

  1. int numberOfClassesInYourData = 10;
  2. int randomSeed = 123;
  3. ZooModel zooModel = ResNet50.builder()
  4. .numClasses(numberOfClassesInYourData)
  5. .seed(randomSeed)
  6. .build();
  7. zooModel.setInputShape(new int[][]3);

Initialization methods often have an additional parameter named workspaceMode. For the majority of users you will not need to use this; however, if you have a large machine that has “beefy” specifications, you can pass WorkspaceMode.SINGLE for models such as VGG-19 that have many millions of parameters. To learn more about workspaces, please see this section.