From 8ea05ac71fac3c5a05e36f5e9e929d725c99b3b6 Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@miranda.cs.illinois.edu>
Date: Tue, 9 Mar 2021 20:06:34 -0600
Subject: [PATCH] Adding comments to AlexNet to serve as a template example

---
 hpvm/projects/keras/src/alexnet.py           | 40 +++++++++++---------
 hpvm/projects/keras/src/alexnet2.py          |  2 +-
 hpvm/projects/keras/src/alexnet_imagenet.py  |  4 +-
 hpvm/projects/keras/src/lenet.py             |  2 +-
 hpvm/projects/keras/src/mobilenet_cifar10.py |  2 +-
 hpvm/projects/keras/src/resnet18_cifar10.py  |  2 +-
 hpvm/projects/keras/src/resnet50_imagenet.py |  4 +-
 hpvm/projects/keras/src/vgg16_cifar10.py     |  2 +-
 hpvm/projects/keras/src/vgg16_cifar100.py    |  2 +-
 hpvm/projects/keras/src/vgg16_imagenet.py    |  4 +-
 10 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/hpvm/projects/keras/src/alexnet.py b/hpvm/projects/keras/src/alexnet.py
index 1d8778727b..0eefe1b3d3 100644
--- a/hpvm/projects/keras/src/alexnet.py
+++ b/hpvm/projects/keras/src/alexnet.py
@@ -21,9 +21,11 @@ from Benchmark import Benchmark
 from Config import MODEL_PARAMS_DIR
 
 
-
+# Inherits from Benchmark class defined in src/Benchmark.py
 class AlexNet_CIFAR10(Benchmark):
 
+    # buildModel overrides the buildModel declared in src/Benchmark.py
+    # Goal: Build a Keras Sequential Model (other model types not supported) and return the (uninitalized/untrained) Model 
     def buildModel(self):
 
         activation_type = 'tanh'
@@ -51,26 +53,23 @@ class AlexNet_CIFAR10(Benchmark):
         model.add(Dropout(0.4))
 
         model.add(Flatten())
-        #model.add(Flatten())
-        #model.add(Dense(256))
         model.add(Dense(self.num_classes))
         model.add(Activation('softmax'))
         
         return model
 
-    
+
+    # This routine is called from the common `run` method in src/Benchmark.py
+    # Goal: Return Training and Testing data after preprocessing/normalization
     def data_preprocess(self):
 
         (X_train, y_train), (X_val, y_val) = cifar10.load_data()
 
         X_train = X_train / 255.0
-        #X_val = X_val / 255.0
-
+ 
         mean = np.mean(X_train)
         std = np.std(X_train)
         X_train = (X_train - mean) / (std + 1e-7)
-        #X_val = (X_val - mean) / (std + 1e-7)
-
 
         X_test = np.fromfile(MODEL_PARAMS_DIR + '/alexnet_cifar10/test_input.bin', dtype=np.float32)
         y_test = np.fromfile(MODEL_PARAMS_DIR + '/alexnet_cifar10/test_labels.bin', dtype=np.uint32)
@@ -86,6 +85,7 @@ class AlexNet_CIFAR10(Benchmark):
         return X_train, y_train, X_test, y_test, X_tuner, y_tuner
 
 
+    # Goal: Given a Keras Sequential Model - setup the training parameters, train, and return the trained Model
     def trainModel(self, model, X_train, y_train, X_test, y_test):
 
         y_train = to_categorical(y_train, self.num_classes)
@@ -133,23 +133,27 @@ class AlexNet_CIFAR10(Benchmark):
 
     
 if __name__ == '__main__':
-  
+
+    # Using GPU ID 0 - Change to use different GPU
     os.environ['CUDA_VISIBLE_DEVICES'] = '0'
-    # Changing to NCHW format
+    # Changing to NCHW format - HPVM currently supports NCHW - NHWC format is not supported
     K.set_image_data_format('channels_first')
 
 
-    ### Parameters specific to each benchmark
+    # *** Below are Parameters specific to each benchmark *****
     reload_dir = MODEL_PARAMS_DIR + '/alexnet_cifar10/'
+    ## Either the HPVM weights are loaded (above) or the Keras Model from the path below 
     keras_model_file = MODEL_PARAMS_DIR + '/alexnet_cifar10/weights.h5'
-    #data_dir = '/alexnet_cifar10/'
-    data_dir = ''
-    src_dir = 'data/alexnet_cifar10_src/'
-    num_classes = 10
-    batch_size = 500
-        
+    data_dir = ''   # if reloading weights, data_dir can be set to empty string (value is ignored)
+ 
+    src_dir = 'data/alexnet_cifar10_src/'  # Directory where HPVM sources are downloaded
+    num_classes = 10  # Specify num out output classes - CIFAR10 has `10` classes
+    batch_size = 500  # Batch Size set to 500 - Adjust this value based on your GPU memory 
+
+    # All Classes inherit from 'Benchmark` class in src/Benchmark.py and have a common Constructor
     model = AlexNet_CIFAR10('AlexNet_CIFAR10', reload_dir, keras_model_file, data_dir, src_dir, num_classes, batch_size)
-    
+
+    # This invokes the common run function in src/Benchmark.py 
     model.run(sys.argv)
 
     
diff --git a/hpvm/projects/keras/src/alexnet2.py b/hpvm/projects/keras/src/alexnet2.py
index 554caf06e4..d2c7d566bb 100644
--- a/hpvm/projects/keras/src/alexnet2.py
+++ b/hpvm/projects/keras/src/alexnet2.py
@@ -137,7 +137,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/alexnet2_cifar10/'
     keras_model_file = MODEL_PARAMS_DIR + '/alexnet2_cifar10/weights.h5'
-    data_dir = '/alexnet2_cifar10/' 
+    data_dir = '' 
     src_dir = 'data/alexnet2_cifar10_src/'
     num_classes = 10
     batch_size = 500
diff --git a/hpvm/projects/keras/src/alexnet_imagenet.py b/hpvm/projects/keras/src/alexnet_imagenet.py
index e3ab937e9b..1cfe7a79c2 100644
--- a/hpvm/projects/keras/src/alexnet_imagenet.py
+++ b/hpvm/projects/keras/src/alexnet_imagenet.py
@@ -94,7 +94,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/alexnet_imagenet/'
     keras_model_file = MODEL_PARAMS_DIR + '/alexnet_imagenet/weights.h5'
-    data_dir = '/alexnet_imagenet/' 
+    data_dir = '' 
     src_dir = 'data/alexnet_imagenet_src/'
     num_classes = 1000
     batch_size = 50
@@ -104,4 +104,4 @@ if __name__ == '__main__':
     model.run(sys.argv)
 
 
-    
\ No newline at end of file
+    
diff --git a/hpvm/projects/keras/src/lenet.py b/hpvm/projects/keras/src/lenet.py
index 01c84719e6..70dd73a66a 100644
--- a/hpvm/projects/keras/src/lenet.py
+++ b/hpvm/projects/keras/src/lenet.py
@@ -103,7 +103,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/lenet_mnist/'
     keras_model_file = MODEL_PARAMS_DIR + '/lenet_mnist/weights.h5'
-    data_dir = '/lenet_mnist/' 
+    data_dir = '' 
     src_dir = 'data/lenet_mnist_src/'
     num_classes = 10
     batch_size = 500
diff --git a/hpvm/projects/keras/src/mobilenet_cifar10.py b/hpvm/projects/keras/src/mobilenet_cifar10.py
index 2406e13435..34335b0f1a 100644
--- a/hpvm/projects/keras/src/mobilenet_cifar10.py
+++ b/hpvm/projects/keras/src/mobilenet_cifar10.py
@@ -183,7 +183,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/mobilenet_cifar10/'
     keras_model_file = MODEL_PARAMS_DIR + '/mobilenet_cifar10/weights.h5'
-    data_dir = '/mobilenet_cifar10/' 
+    data_dir = '' 
     src_dir = 'data/mobilenet_cifar10_src/'
     num_classes = 10
     batch_size = 500
diff --git a/hpvm/projects/keras/src/resnet18_cifar10.py b/hpvm/projects/keras/src/resnet18_cifar10.py
index 6f94a151e8..02753f9eac 100644
--- a/hpvm/projects/keras/src/resnet18_cifar10.py
+++ b/hpvm/projects/keras/src/resnet18_cifar10.py
@@ -555,7 +555,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/resnet18_cifar10/'
     keras_model_file = MODEL_PARAMS_DIR + '/resnet18_cifar10/weights.h5'
-    data_dir = '/resnet18_cifar10/' 
+    data_dir = '' 
     src_dir = 'data/resnet18_cifar10_src/'
     num_classes = 10
     batch_size = 500
diff --git a/hpvm/projects/keras/src/resnet50_imagenet.py b/hpvm/projects/keras/src/resnet50_imagenet.py
index 0c3006213d..de42ae48d8 100644
--- a/hpvm/projects/keras/src/resnet50_imagenet.py
+++ b/hpvm/projects/keras/src/resnet50_imagenet.py
@@ -142,7 +142,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/resnet50_imagenet/'
     keras_model_file = MODEL_PARAMS_DIR + '/resnet50_imagenet/weights.h5'
-    data_dir = '/resnet50_imagenet/' 
+    data_dir = '' 
     src_dir = 'data/resnet50_imagenet_src/'
     num_classes = 1000
     batch_size = 50
@@ -152,4 +152,4 @@ if __name__ == '__main__':
     model.run(sys.argv)
 
 
-    
\ No newline at end of file
+    
diff --git a/hpvm/projects/keras/src/vgg16_cifar10.py b/hpvm/projects/keras/src/vgg16_cifar10.py
index 9677fc2650..9a5071ee94 100644
--- a/hpvm/projects/keras/src/vgg16_cifar10.py
+++ b/hpvm/projects/keras/src/vgg16_cifar10.py
@@ -185,7 +185,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/vgg16_cifar10/'
     keras_model_file = MODEL_PARAMS_DIR + '/vgg16_cifar10/weights.h5'
-    data_dir = '/vgg16_cifar10/' 
+    data_dir = '' 
     src_dir = 'data/vgg16_cifar10_src/'
     num_classes = 10
     batch_size = 500
diff --git a/hpvm/projects/keras/src/vgg16_cifar100.py b/hpvm/projects/keras/src/vgg16_cifar100.py
index 5d68392934..0fd51ebe03 100644
--- a/hpvm/projects/keras/src/vgg16_cifar100.py
+++ b/hpvm/projects/keras/src/vgg16_cifar100.py
@@ -200,7 +200,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/vgg16_cifar100/'
     keras_model_file = MODEL_PARAMS_DIR + '/vgg16_cifar100/weights.h5'
-    data_dir = '/vgg16_cifar100/' 
+    data_dir = '' 
     src_dir = 'data/vgg16_cifar100_src/'
     num_classes = 100
     batch_size = 100
diff --git a/hpvm/projects/keras/src/vgg16_imagenet.py b/hpvm/projects/keras/src/vgg16_imagenet.py
index 35ab92479e..8c27714fcf 100644
--- a/hpvm/projects/keras/src/vgg16_imagenet.py
+++ b/hpvm/projects/keras/src/vgg16_imagenet.py
@@ -127,7 +127,7 @@ if __name__ == '__main__':
     ### Parameters specific to each benchmark
     reload_dir = MODEL_PARAMS_DIR + '/vgg16_imagenet/'
     keras_model_file = MODEL_PARAMS_DIR + '/vgg16_imagenet/weights.h5'
-    data_dir = '/vgg16_imagenet/' 
+    data_dir = '' 
     src_dir = 'data/vgg16_imagenet_src/'
     num_classes = 1000
     batch_size = 50
@@ -137,4 +137,4 @@ if __name__ == '__main__':
     alexnet.run(sys.argv)
 
 
-    
\ No newline at end of file
+    
-- 
GitLab