<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Juletip #4: Going generative with Christmas trees in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-4-Going-generative-with-Christmas-trees/m-p/905939#M438</link>
    <description>&lt;DIV&gt;
&lt;DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;The other day in November I was about to build&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;an image classification model with SAS tools.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;I already started googling for examples, but because&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;I'm lazy, I gave up and decided to give ChatGPT a chance to shine.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So what I asked first was:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Write me some SAS code for training image classification model&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;This is what I got as answer:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Certainly! To train an image classification model in SAS, you can use the 
deep learning capabilities provided by SAS Viya, which includes a high-level 
deep learning API. Below is an example of SAS code for training an image classification model 
using a deep learning neural network.

Before you use this code, make sure you have SAS Viya installed and configured on your system. 
You also need your dataset of labeled images.

/* Load necessary packages */
%let targetServer = YOUR_TARGET_SERVER;
%let targetPort = YOUR_TARGET_PORT;
%let targetUser = YOUR_USERNAME;
%let targetPassword = YOUR_PASSWORD;

options dlCreateCnnConfig = YES;

/* Connect to the CAS server */
cas mySession sessopts= (caslib='CASUSER',host="&amp;amp;targetServer",port=&amp;amp;targetPort, 
                          user="&amp;amp;targetUser",password="&amp;amp;targetPassword");

/* Load the image data into CAS */
caslib _caslib_ sessopts=(caslib=CASUSER);

data _caslib_.your_image_data;
   /* Use the IMAGEFILE function to load image files into a CAS table */
   /* Specify your image file directory */
   image_bytes = imagefile('/path/to/your/images', '*.jpg', 'Label', 'jpg');
run;

/* Define the deep learning model */
data _caslib_.cnn_model;
   retain caslib 'CASUSER';
   inputTable = '_caslib_.your_image_data';
   modelType = 'CNN';
   target = 'Label';
   target = { "Label" };

   /* Define the convolutional layers */
   modelOpt = modelweights('/path/to/pretrained_weights.weights');
   numConv = 2;
   convLayer = { { 6, 5 }, { 12, 5 } };
   convAct = 'RELU';
   poolLayer = { { 2 }, { 2 } };
   poolAct = 'RELU';

   /* Define the fully connected layers */
   numFC = 2;
   fcLayer = { 100, 50 };
   fcAct = 'RELU';

   /* Define the output layer */
   outputAct = 'SOFTMAX';
   n=3;

   /* Specify the optimization parameters */
   casout = casOut( append=T );
run;

/* Train the deep learning model */
proc deepLearn trainSeed=12345 data=_caslib_.cnn_model;
   targetModel=_caslib_.your_trained_model;
   savestate state=_caslib_.your_model_state;
   casout= { caslib = _caslib_ name = 'cnn_model_training' replace=TRUE };
run;

/* Assess the model's performance */
proc deepLearn assess seed=12345 data=_caslib_.your_image_data;
   targetModel=_caslib_.your_trained_model;
   casout= { caslib = _caslib_ name = 'cnn_model_assessment' replace=TRUE };
run;

/* Terminate the CAS session */
cas mySession terminate;

/* View the assessment results */
proc print data=_caslib_.cnn_model_assessment;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Great! Nice one with first attempt. &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;But this was too simple network in my opinion: CNN with only two layers. &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;I knew that in CAS Deep Learning actions, there are more complex built-in CNN &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;models for image classification purposes.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So, my next request was:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I know there is built-in Resnet50 in SAS. Rewrite the previous code&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Oh yes, ChatGPT knew it too! :&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SAS provides a built-in ResNet50 model that you can use for image classification. 
You can train a ResNet50 model with your own dataset using the deep learning capabilities in SAS Viya. 
Here's how you can modify the previous code to use the ResNet50 model..:&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;To making the story a bit shorter, I won't include the modified code here.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;The given code did most obviously what I needed.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;But I wanted to demonstrate the Viya Deep Learning capabilities to my open-source friend.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Could I do the same using Python? I had heard that there is something called 'dlpy' that&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;enables using Viya deep learning actions in Python.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So, I blindly asked this:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Can you do the same with dlpy?&lt;/CODE&gt;&lt;/PRE&gt;
&lt;SPAN&gt;Answer:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Certainly! You can use SAS DLPy, a Python library for deep learning in SAS, 
to train an image classification model using the ResNet50 architecture. 
Here is an example of how to do it:

# Import necessary libraries
import dlpy
from dlpy.images import ImageTable
from dlpy.applications import ResNet50
from dlpy.model import Model
from dlpy.layers import Input, Output
from dlpy.utils import display_table
from dlpy import gpuinfo
from dlpy.metrics import accuracy_score
from dlpy.model import Gpu
from swat import CAS

# Initialize a CAS session
s = CAS(hostname='YOUR_TARGET_SERVER', port=YOUR_TARGET_PORT, username='YOUR_USERNAME', password='YOUR_PASSWORD')

# Load the image data into CAS
image_table = ImageTable.load_files(s, path='/path/to/your/images', casout=dict(name='your_image_data', replace=True))

# Define the ResNet50 model
model = ResNet50(s, model_table='ResNet50',
                width=224, height=224, random_flip='HV', random_crop='RESIZETHENCROP',
                offsets=(103.939, 116.779, 123.68), scale=1, random_mutation='none',
                pre_trained_weights=True, pre_trained_weights_file='/path/to/pretrained_weights.weights')

# Specify the training settings
model.print_summary()
model.fit(data=image_table, n_threads=4, max_epochs=100, lr=0.001, gpu=Gpu(devices=[1]), target='Label')

# Evaluate the model on the validation data
model.evaluate(validation_data)

# Display the evaluation results
print(model.valid_res_tbl)

# Disconnect from CAS
s.close()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;How cool!&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;But now it started to interest me if this code really works.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;And - it appeared to be &lt;EM&gt;&lt;STRONG&gt;almost&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;good for running. The most essential fix was the&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;name of the Resnet constructor (Resnet50_Caffe instead of Resnet50).&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Then, there was some minor fixes and additions like removing unnecessary import-clauses, removing everything that refers into GPU since I didn't have one in my environment, dividing dataset into train-test -partitions, testing and evaluating the model.. I might have got them too if I had played with ChatGPT little bit longer.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;BTW, what was my model about?&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;As Christmas is approaching, I wanted to train a model that classifies pictures of trees: Christmas trees into first category and non-Christmas trees into second.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;I used ~600 Christmas tree pictures and ~1000 non-Christmas tree pictures as training material from &lt;A href="https://storage.googleapis.com/openimages/web/index.html" target="_self"&gt;Open Images Dataset&lt;/A&gt;,&amp;nbsp; a rich source of labeled images of all sorts.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Resnet50 performed quite well in predicting these image classes!&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Testing with 319 images I got following accuracy figures:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="conf mat and acc score.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90170i285C70C68D5CCF01/image-size/medium?v=v2&amp;amp;px=400" role="button" title="conf mat and acc score.PNG" alt="conf mat and acc score.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;SPAN&gt;So, only 26 of 319 were misclassified.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;To finalize my story, I include a couple of examples of correctly classified and misclassified cases:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;Correct classification:&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="correct no xmastree.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90171i2F13160AE756031A/image-size/large?v=v2&amp;amp;px=999" role="button" title="correct no xmastree.PNG" alt="correct no xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="correct xmastree.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90172i5C58428ED644DA6D/image-size/large?v=v2&amp;amp;px=999" role="button" title="correct xmastree.PNG" alt="correct xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Not correct classification:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="not correct no xmastree.PNG" style="width: 875px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90175iD23B06DC614861D6/image-size/large?v=v2&amp;amp;px=999" role="button" title="not correct no xmastree.PNG" alt="not correct no xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="not correct xmastree.PNG" style="width: 862px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90174i551E148AD31D1325/image-size/large?v=v2&amp;amp;px=999" role="button" title="not correct xmastree.PNG" alt="not correct xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV&gt;I still wonder why the last picture was labeled as Christmas tree in the first place.. Resnet knew it better &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Thu, 04 Jan 2024 13:02:38 GMT</pubDate>
    <dc:creator>askojuvonen1</dc:creator>
    <dc:date>2024-01-04T13:02:38Z</dc:date>
    <item>
      <title>Juletip #4: Going generative with Christmas trees</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-4-Going-generative-with-Christmas-trees/m-p/905939#M438</link>
      <description>&lt;DIV&gt;
&lt;DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;The other day in November I was about to build&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;an image classification model with SAS tools.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;I already started googling for examples, but because&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;I'm lazy, I gave up and decided to give ChatGPT a chance to shine.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So what I asked first was:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Write me some SAS code for training image classification model&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;This is what I got as answer:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Certainly! To train an image classification model in SAS, you can use the 
deep learning capabilities provided by SAS Viya, which includes a high-level 
deep learning API. Below is an example of SAS code for training an image classification model 
using a deep learning neural network.

Before you use this code, make sure you have SAS Viya installed and configured on your system. 
You also need your dataset of labeled images.

/* Load necessary packages */
%let targetServer = YOUR_TARGET_SERVER;
%let targetPort = YOUR_TARGET_PORT;
%let targetUser = YOUR_USERNAME;
%let targetPassword = YOUR_PASSWORD;

options dlCreateCnnConfig = YES;

/* Connect to the CAS server */
cas mySession sessopts= (caslib='CASUSER',host="&amp;amp;targetServer",port=&amp;amp;targetPort, 
                          user="&amp;amp;targetUser",password="&amp;amp;targetPassword");

/* Load the image data into CAS */
caslib _caslib_ sessopts=(caslib=CASUSER);

data _caslib_.your_image_data;
   /* Use the IMAGEFILE function to load image files into a CAS table */
   /* Specify your image file directory */
   image_bytes = imagefile('/path/to/your/images', '*.jpg', 'Label', 'jpg');
run;

/* Define the deep learning model */
data _caslib_.cnn_model;
   retain caslib 'CASUSER';
   inputTable = '_caslib_.your_image_data';
   modelType = 'CNN';
   target = 'Label';
   target = { "Label" };

   /* Define the convolutional layers */
   modelOpt = modelweights('/path/to/pretrained_weights.weights');
   numConv = 2;
   convLayer = { { 6, 5 }, { 12, 5 } };
   convAct = 'RELU';
   poolLayer = { { 2 }, { 2 } };
   poolAct = 'RELU';

   /* Define the fully connected layers */
   numFC = 2;
   fcLayer = { 100, 50 };
   fcAct = 'RELU';

   /* Define the output layer */
   outputAct = 'SOFTMAX';
   n=3;

   /* Specify the optimization parameters */
   casout = casOut( append=T );
run;

/* Train the deep learning model */
proc deepLearn trainSeed=12345 data=_caslib_.cnn_model;
   targetModel=_caslib_.your_trained_model;
   savestate state=_caslib_.your_model_state;
   casout= { caslib = _caslib_ name = 'cnn_model_training' replace=TRUE };
run;

/* Assess the model's performance */
proc deepLearn assess seed=12345 data=_caslib_.your_image_data;
   targetModel=_caslib_.your_trained_model;
   casout= { caslib = _caslib_ name = 'cnn_model_assessment' replace=TRUE };
run;

/* Terminate the CAS session */
cas mySession terminate;

/* View the assessment results */
proc print data=_caslib_.cnn_model_assessment;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Great! Nice one with first attempt. &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;But this was too simple network in my opinion: CNN with only two layers. &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;I knew that in CAS Deep Learning actions, there are more complex built-in CNN &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;models for image classification purposes.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So, my next request was:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I know there is built-in Resnet50 in SAS. Rewrite the previous code&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Oh yes, ChatGPT knew it too! :&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SAS provides a built-in ResNet50 model that you can use for image classification. 
You can train a ResNet50 model with your own dataset using the deep learning capabilities in SAS Viya. 
Here's how you can modify the previous code to use the ResNet50 model..:&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;To making the story a bit shorter, I won't include the modified code here.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;The given code did most obviously what I needed.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;But I wanted to demonstrate the Viya Deep Learning capabilities to my open-source friend.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Could I do the same using Python? I had heard that there is something called 'dlpy' that&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;enables using Viya deep learning actions in Python.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;BR /&gt;
&lt;DIV&gt;&lt;SPAN&gt;So, I blindly asked this:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Can you do the same with dlpy?&lt;/CODE&gt;&lt;/PRE&gt;
&lt;SPAN&gt;Answer:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Certainly! You can use SAS DLPy, a Python library for deep learning in SAS, 
to train an image classification model using the ResNet50 architecture. 
Here is an example of how to do it:

# Import necessary libraries
import dlpy
from dlpy.images import ImageTable
from dlpy.applications import ResNet50
from dlpy.model import Model
from dlpy.layers import Input, Output
from dlpy.utils import display_table
from dlpy import gpuinfo
from dlpy.metrics import accuracy_score
from dlpy.model import Gpu
from swat import CAS

# Initialize a CAS session
s = CAS(hostname='YOUR_TARGET_SERVER', port=YOUR_TARGET_PORT, username='YOUR_USERNAME', password='YOUR_PASSWORD')

# Load the image data into CAS
image_table = ImageTable.load_files(s, path='/path/to/your/images', casout=dict(name='your_image_data', replace=True))

# Define the ResNet50 model
model = ResNet50(s, model_table='ResNet50',
                width=224, height=224, random_flip='HV', random_crop='RESIZETHENCROP',
                offsets=(103.939, 116.779, 123.68), scale=1, random_mutation='none',
                pre_trained_weights=True, pre_trained_weights_file='/path/to/pretrained_weights.weights')

# Specify the training settings
model.print_summary()
model.fit(data=image_table, n_threads=4, max_epochs=100, lr=0.001, gpu=Gpu(devices=[1]), target='Label')

# Evaluate the model on the validation data
model.evaluate(validation_data)

# Display the evaluation results
print(model.valid_res_tbl)

# Disconnect from CAS
s.close()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;How cool!&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;But now it started to interest me if this code really works.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;And - it appeared to be &lt;EM&gt;&lt;STRONG&gt;almost&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;good for running. The most essential fix was the&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;name of the Resnet constructor (Resnet50_Caffe instead of Resnet50).&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Then, there was some minor fixes and additions like removing unnecessary import-clauses, removing everything that refers into GPU since I didn't have one in my environment, dividing dataset into train-test -partitions, testing and evaluating the model.. I might have got them too if I had played with ChatGPT little bit longer.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;BTW, what was my model about?&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;As Christmas is approaching, I wanted to train a model that classifies pictures of trees: Christmas trees into first category and non-Christmas trees into second.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;I used ~600 Christmas tree pictures and ~1000 non-Christmas tree pictures as training material from &lt;A href="https://storage.googleapis.com/openimages/web/index.html" target="_self"&gt;Open Images Dataset&lt;/A&gt;,&amp;nbsp; a rich source of labeled images of all sorts.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Resnet50 performed quite well in predicting these image classes!&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;Testing with 319 images I got following accuracy figures:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="conf mat and acc score.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90170i285C70C68D5CCF01/image-size/medium?v=v2&amp;amp;px=400" role="button" title="conf mat and acc score.PNG" alt="conf mat and acc score.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;SPAN&gt;So, only 26 of 319 were misclassified.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;To finalize my story, I include a couple of examples of correctly classified and misclassified cases:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;STRONG&gt;Correct classification:&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="correct no xmastree.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90171i2F13160AE756031A/image-size/large?v=v2&amp;amp;px=999" role="button" title="correct no xmastree.PNG" alt="correct no xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="correct xmastree.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90172i5C58428ED644DA6D/image-size/large?v=v2&amp;amp;px=999" role="button" title="correct xmastree.PNG" alt="correct xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Not correct classification:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="not correct no xmastree.PNG" style="width: 875px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90175iD23B06DC614861D6/image-size/large?v=v2&amp;amp;px=999" role="button" title="not correct no xmastree.PNG" alt="not correct no xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="not correct xmastree.PNG" style="width: 862px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90174i551E148AD31D1325/image-size/large?v=v2&amp;amp;px=999" role="button" title="not correct xmastree.PNG" alt="not correct xmastree.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV&gt;I still wonder why the last picture was labeled as Christmas tree in the first place.. Resnet knew it better &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 Jan 2024 13:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-4-Going-generative-with-Christmas-trees/m-p/905939#M438</guid>
      <dc:creator>askojuvonen1</dc:creator>
      <dc:date>2024-01-04T13:02:38Z</dc:date>
    </item>
  </channel>
</rss>

