The SAS Deep Learning action set is a powerful tool for creating and deploying deep learning models. It works seamlessly when your deep learning models have been created by using SAS. Sometimes, however, you must work with a model that was created with some other popular package, like PyTorch. You could recreate the PyTorch model inside of SAS by using SAS Deep Learning, but that adds extra work and time to your project. Fortunately, it is not necessary because SAS created an action set called dlModelZoo that supports importing PyTorch models.

The dlModelZoo action set can import PyTorch models and use those models alongside the other powerful modeling capabilities of dlModelZoo. This handy feature lets you skip the extra step of recreating the model in SAS Deep Learning. It enables you to leverage the PyTorch model along with many other dlModelZoo capabilities. This includes training, scoring, and even tuning hyperparameters. In this post, we will demonstrate how to import PyTorch models into dlModelZoo and introduce you to some of its modeling capabilities.

PyTorch model

First, an artificial neural network model in PyTorch is created to split images into distinct objects. We won’t be labeling the objects, just creating image masks for different objects in the scene. The specifics of the model aren’t important. What is important is that the artificial neural network, created in PyTorch, can be exported into SAS Deep Learning by using TorchScript. Later it can be trained and scored using dlModelZoo and SAS Deep Learning.

To get started with the example, we’ll use a subset of data from kaggle. The data contain simulated images from the viewpoint of a driving car. Figure 1 is an example image from the data set.

dlModelZoo action set - Figure 1: Example image from kaggle data set
Figure 1: Example image from kaggle data set

To separate the different objects in the scene, we need to train the weights of an existing PyTorch model that was designed for a segmentation problem. Many deep learning models written in PyTorch are meant to handle this kind of problem. The specific model that we use here is called Deeplab. The output of our model will be a set of masks that distinguish different objects in the scene. Based on an example in Figure 1, the output of the model is depicted in Figure 2. Each different shade represents a different object in the scene.

dlModelZoo action set - Figure 2: Example of the ground truth masks that the model will attempt to recreate
Figure 2: Example of the ground truth masks that the model will attempt to recreate

To import these images to SAS, a table needs to be created containing two columns. The first column shows the path to the original image. The second column shows the path to the desired masked image. An example of a representative table is shown in Table 1.

Image Target
image_path/image001.png mask_path/mask001.png
image_path/image002.png mask_path/mask002.png

Table 1: Example of a representative image table

Loading the PyTorch model

Now that we have processed the images, we can get to the important part! Let’s import a PyTorch model into SAS. You need to export your model from PyTorch in a way that can be used by the dlModelZoo action set.  The tool we will be using is called TorchScript.  The process is simple. You just need the following two lines of Python code:

model_scripted = torch.jit.script(model) # Export to TorchScript‘') # Save


For this example, we export the model into a file named “” by using the two lines above. The PyTorch model has been exported in a way that SAS can understand, but we still need to provide more details about the model. To describe the model to dlModelZoo, we need to create a yaml string. Most of this string will be the same for all models. Inline comments shown in the following code describe what is needed to be modified for your specific model.

documents = """ #key is case-sensitive, value is not case-sensitive
  label: "my_torchscript" #referenced in action calls
  type: "Segmentation"
  preProcessing: #a section to place any preprocessing of the input, in our case I am just resizing
    - modelInput: 
        label: input_tensor1
              type: TO_FIX_DIM
              size: 256 256 
              targetSize: 256 256
    type: "TORCHSCRIPT" # TORCHSCRIPT if importing a .pt file with model and weights
                                           # TORCHNATIVE if you want to use a predefined model with a .pt file that contains only weights
    majorVersion: 1
    minorVersion: 2
    caslib: "modellib" # just the caslib where the model exists
    path: "deeplab-model/" # the location of the TorchScript model
    classNumber: 16    # number of classes to predict (in our case, number of objects to mask)
      - label: input_tensor1
        - 0
      - label: output_tensor1
        - 0

Once the yaml string has been created, dlModelZoo knows how to import the model and use it. All that remains to do is to call the training and scoring actions from dlModelZoo.

Training and scoring (and tuning)

We have imported the model into SAS, and now we’d like to fine-tune our model with our specific data in SAS. Assuming that the CAS action session object “s” is loaded, we will use the dlmzTrain Action to train the model as shown in the following code:

res = s.dlmztrain(
    #The location of the data
    #Where to put the results
        #The training algorithm to use
    #Here is where that yaml string from before is used
    extraOptions=dict(yaml=documents, label="my_torchscript"),

If we wanted to tune the learning rate hyperparameter in that way, we would replace the single value with a range as demonstrated here:

learningRate=dict(lb = 1e-6, ub=5e-1, logScale="LN"),

Lb and ub represent the lower and upper bounds for the hyperparameter of learningRate. Logscale=”LN” indicates that you want to scale the learning rate by the natural log function and tune that. As soon as a range is provided for at least one hyperparameter, dlmzTrain will tune and train the model.

Scoring is just as easy. This code shows an example by using the dlmzScore Action:

res = s.dlmzscore(
    model="trained_model", #this was the output of dlmzTrain
    tableOut=dict(name="output", replace=True),
    #and here I use the label from the scoring section of the yaml string
    extraOptions=dict(yaml=documents, label="my_torchscript")

Predefined models

The example in the code uses TorchScript to import a model from PyTorch into SAS Deep Learning. In most cases, this is not necessary because SAS has a variety of predefined models that can be used right out of the box. All that needs to be done is to replace “TORCHSCRIPT” with “TORCHNATIVE” in the model->type section of the yaml description. Also, make sure that if you have pretrained model weights for a pretrained model the path to your model file containing those weights is correct. Once this change to the yaml string is completed, there will be access to many different neural network models that cover many different deep learning tasks. These include ENet, Dilated RNN, LeNet, MobileNet, ResNet, ShuffleNet, UNet, VGG, and YOLOV5.


This example shows how to import PyTorch models into SAS Deep Learning. We have described how to load a TorchScript exported PyTorch model into SAS, train that model, tune hyperparameters, and score against a validation data set by using the dlModelZoo action set. The dlModelZoo action set has also a variety of predefined models available. The predfined models can be used without being imported. They will likely serve well for most deep learning modeling needs. Hopefully, this post will encourage you to try out the dlModelZoo action set. Have fun!


About Author

Scott Pope

Machine Learning Optimization and Linear Algebra Team Leader, SAS

Scott Pope is currently the Machine Learning Optimization and Linear Algebra Team Leader at SAS (a business title for which there is no good abbreviation). Scott has been working at SAS since 2008 and holds several patents relating to parallel computing. Among his interests are learning about new technologies and making machine learning more accessible for everyone. He holds a Ph.D. from North Carolina State University in Applied Mathematics.

Related Posts

Comments are closed.

Back to Top