Repository: systemml
Updated Branches:
  refs/heads/master cf9396b5c -> a99cf844a


[SYSTEMML-540] Add tests to validate SystemML's deep learning APIs.

- This commit adds python unit tests that compares the results of keras
  with that returned by SystemML.
- Even though runtime operators are tested in the junit tests, these tests
  will help validate end-to-end deep learning pipeline. For example: are
  the python APIs (Keras2DML and Caffe2DML) working as expected, are the
  results returned by nn layers consistent with that of Keras, etc.


Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/a99cf844
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/a99cf844
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/a99cf844

Branch: refs/heads/master
Commit: a99cf844a8a50a70a65347dad0d656763bc8a408
Parents: cf9396b
Author: Niketan Pansare <[email protected]>
Authored: Tue Dec 5 10:53:32 2017 -0800
Committer: Niketan Pansare <[email protected]>
Committed: Tue Dec 5 10:53:31 2017 -0800

----------------------------------------------------------------------
 src/main/python/tests/test_nn_numpy.py | 86 +++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/a99cf844/src/main/python/tests/test_nn_numpy.py
----------------------------------------------------------------------
diff --git a/src/main/python/tests/test_nn_numpy.py 
b/src/main/python/tests/test_nn_numpy.py
new file mode 100644
index 0000000..d3dc663
--- /dev/null
+++ b/src/main/python/tests/test_nn_numpy.py
@@ -0,0 +1,86 @@
+#!/usr/bin/python
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+# Assumption: pip install keras
+# 
+# This test validates SystemML's deep learning APIs (Keras2DML, Caffe2DML and 
nn layer) by comparing the results with that of keras.
+#
+# To run:
+#   - Python 2: `PYSPARK_PYTHON=python2 spark-submit --master local[*] 
--driver-memory 10g --driver-class-path SystemML.jar,systemml-*-extra.jar 
test_nn_numpy.py`
+#   - Python 3: `PYSPARK_PYTHON=python3 spark-submit --master local[*] 
--driver-memory 10g --driver-class-path SystemML.jar,systemml-*-extra.jar 
test_nn_numpy.py`
+
+# Make the `systemml` package importable
+import os
+import sys
+path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")
+sys.path.insert(0, path)
+
+import unittest
+
+import numpy as np
+from keras.models import Sequential
+from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout,Flatten
+from keras import backend as K
+from keras.models import Model
+from systemml.mllearn import Keras2DML
+from pyspark.sql import SparkSession
+
+batch_size = 32
+input_shape = (3,64,64)
+K.set_image_data_format("channels_first")
+# K.set_image_dim_ordering("th")
+keras_tensor = np.random.rand(batch_size,input_shape[0], input_shape[1], 
input_shape[2])
+sysml_matrix = keras_tensor.reshape((batch_size, -1))
+tmp_dir = 'tmp_dir'
+
+spark = SparkSession.builder.getOrCreate()
+
+def are_predictions_all_close(keras_model):
+    sysml_model = Keras2DML(spark, keras_model, input_shape=input_shape, 
weights=tmp_dir)
+    keras_preds = keras_model.predict(keras_tensor).flatten()
+    sysml_preds = sysml_model.predict_proba(sysml_matrix).flatten()
+    #print(str(keras_preds))
+    #print(str(sysml_preds))
+    return np.allclose(keras_preds, sysml_preds)
+
+class TestNNLibrary(unittest.TestCase):
+    def test_1layer_cnn_predictions(self):
+        keras_model = Sequential()
+        keras_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', 
input_shape=input_shape, padding='valid'))
+        keras_model.add(Flatten())
+        keras_model.add(Dense(10, activation='softmax'))
+        self.failUnless(are_predictions_all_close(keras_model))
+
+    def test_multilayer_cnn_predictions(self):
+        keras_model = Sequential()
+        keras_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', 
input_shape=input_shape, padding='valid'))
+        keras_model.add(MaxPooling2D(pool_size=(2, 2)))
+        keras_model.add(Conv2D(64, (3, 3), activation='relu'))
+        keras_model.add(MaxPooling2D(pool_size=(2, 2)))
+        keras_model.add(Flatten())
+        keras_model.add(Dense(256, activation='softmax'))
+        keras_model.add(Dropout(0.25))
+        keras_model.add(Dense(10, activation='softmax'))
+        self.failUnless(are_predictions_all_close(keras_model))    
+
+if __name__ == '__main__':
+    unittest.main()

Reply via email to