STYLEGAN Procedure
Getting Started: STYLEGAN Procedure
Note: This example assumes that the CAS engine libref mylib and the caslib that is associated with mylib have already been created. For more information, see the section Using CAS Sessions and CAS Engine Librefs and Chapter 1, Shared Concepts (SAS Viya: Computer Vision Procedures).
This example shows how to use the STYLEGAN procedure to train a Style GAN model and generate new images by using a data set that contains 60,000 gray-scale images of fashion items at a resolution of pixels. The data set includes 10 classes that represent different types of clothing and accessories: T-shirts, pants, pullovers, dresses, coats, sandals, shirts, sneakers, bags, and ankle boots. The Fashion MNIST data set is available at Deep Learning Example Data. Note that the shape of the input image for PROC STYLEGAN must be square and selected from {4, 8, 16, 32, 64, 128, 256, 512, 1024}. Figure 1 shows 100 images that are randomly selected from the Fashion MNIST data set.
Figure 1: 100 Images Randomly Selected from the Fashion MNIST Data Set

First, you use the LOADIMAGES procedure (see Chapter 10, LOADIMAGES Procedure (SAS Viya: Computer Vision Procedures)) as follows to load the images from the specified path into the table mylib.fashionImages in your CAS session. This assumes that your CAS engine libref is named mylib, as in the section Using CAS Sessions and CAS Engine Librefs, but you can substitute any appropriately defined CAS engine libref. The specified path must be relative to the caslib that is associated with the mylib libref:
proc loadimages libref=mylib path='path-to-images-folder/';
output out=mylib.fashionImages;
run;
Then, you use the PROCESSIMAGES procedure (see Chapter 12, PROCESSIMAGES Procedure (SAS Viya: Computer Vision Procedures)) as follows to resize the -pixel images that are loaded in the table
mylib.fashionImages to -pixel images and to save the resized images in the
mylib.resizedImages table:
proc processimages data=mylib.fashionImages;
resize type=keep_aspect_ratio width=32;
output out=mylib.resizedImages;
run;
Next, you use the following PROC STYLEGAN statements to train a Style GAN model and save the trained generator network in an analytic store:
proc stylegan data=mylib.resizedImages imagesize=32
numsamples=100 useGPU seed=123;
optimization numiters=5;
savestate rstore=mylib.astore;
output out=mylib.outtable;
run;
The DATA= option specifies the input images data set. The IMAGESIZE= option specifies the input image size, which is the image width of the square images in pixels. You specify the number of images that you want to generate in the NUMSAMPLES= option. The USEGPU option specifies that a GPU device is used to train the Style GAN model. The SEED= option specifies the random seed to use in the training process. You specify the number of training iterations to use for the GAN model in the NUMITERS= option in the OPTIMIZATION statement. One iteration processes one minibatch of your training data. Note that for the Fashion MNIST data, you can get a converged model by running 30,000 iterations. In this example, small option values are used for brevity. The SAVESTATE statement specifies the table to use for saving the trained model, and the OUTPUT statement specifies the table to use for outputting result images.
PROC STYLEGAN generates four ODS tables.
Figure 2 shows the iteration history, including the generator and discriminator loss values for each epoch of training.
Figure 2: Iteration History
| Iteration History | ||||
|---|---|---|---|---|
| Iteration Number | Generator Loss | Discriminator Loss | Path Length Regularization Loss | R1 Regularization Loss |
| 1 | 3.738638 | 1.524220 | 1.755333 | 0.009592 |
| 2 | 2.746564 | 0.952844 | 1.755333 | 0.009592 |
| 3 | 13.112035 | 1.174145 | 1.755333 | 0.009592 |
| 4 | 1.670602 | 1.189783 | 1.755333 | 0.009592 |
| 5 | 13.850622 | 0.723998 | 0.849263 | 0.009592 |
Figure 3 shows the number of observations that are read and number of observations that are actually used.
Figure 3: Number of Observations
| Number of Observations | |
|---|---|
| Number of Observations Read | 60000 |
| Number of Observations Used | 60000 |
Figure 4 shows the values of the parameters that the Style GAN model uses.
Figure 4: Model Information
| Model Information | |
|---|---|
| Exponential Decay Rate for the First-Moment Estimates | 0 |
| Exponential Decay Rate for the Second-Moment Estimates | 0.99 |
| Width of Square Images in Pixels | 32 |
| Use Large Network | True |
| Learning Rate for the Optimizer | 0.002 |
| Number of Images in One Minibatch | 32 |
| Number of Iterations | 5 |
| Seed for Random Initialization | 123 |
| Discriminator Regularization Frequency | 16 |
| Generator Regularization Frequency | 4 |
| Weight for Path Length Regularization | 2 |
| Weight for R1 Regularization | 10 |
Figure 5 illustrates the timing of the various steps that PROC STYLEGAN performs.
Figure 5: Timing
| Timing | |
|---|---|
| Task | Time in Seconds |
| Reserving GPU Device | 2.043964 |
| Loading Training Data | 4.929908 |
| Setting Up Model | 2.118650 |
| Training Model | 15.908062 |
| Generating Images | 0.331942 |
| Saving Generator to Astore | 1.513873 |
In this example, PROC STYLEGAN generates the score output table mylib.outtable in the active library. This table contains 100 generated images from the trained model. Figure 6 shows 100 images that were generated by using PROC STYLEGAN.
Figure 6: Images Generated Using PROC STYLEGAN

The following statements run the SAVEIMAGES procedure (see Chapter 16, SAVEIMAGES Procedure (SAS Viya: Computer Vision Procedures)) to write the generated images from the table mylib.outtable in bitmap format onto a disk:
proc saveimages libref=mylib path="path-to-write-images"
data=mylib.outtable encoding=BMP;
run;
You can generate additional images by using the trained Style GAN model that is saved in the mylib.astore table and PROC ASTORE (see Chapter 4, ASTORE Procedure.)
The following statements use PROC ASTORE to generate 10 images by using the trained Style GAN model that is stored in mylib.astore and then save the results in the mylib.astoreout table:
proc astore;
setoption GENOBS 10;
setoption SEED 42;
score rstore=mylib.astore
out=mylib.astoreout;
run;
The GENOBS option in the SETOPTION statement specifies the number of images to generate. To generate different images, change the SEED option value in the SETOPTION statement.
Note: For Style GAN analytic stores that are trained on the 2026.02 release or later releases, you can generate a specific number of images without input data by specifying the GENOBS option when you call PROC ASTORE. However, for Style GAN analytic stores that are trained on releases earlier than 2026.02, you must provide an input data set that contains the number of rows that corresponds to the number of images that you want to generate. See the corresponding document version for details.