Skip to content
Permalink
Browse files

Added Keras for score testing

  • Loading branch information...
Nirmal-Neel committed Jun 28, 2019
1 parent c88b4d7 commit 757cc87c83bd0550dbdd98097094582d7b93a50a
Showing with 169 additions and 19 deletions.
  1. +1 −0 .travis.yml
  2. +32 −19 nyoka/tests/adapaUtilities.py
  3. BIN nyoka/tests/resizedCat.png
  4. +136 −0 nyoka/tests/testScoreWithAdapaKeras.py
@@ -22,6 +22,7 @@ script:
- python nyoka/tests/testScoreWithAdapaSklearn.py
- python nyoka/tests/testScoreWithAdapaXgboost.py
- python nyoka/tests/testScoreWithAdapaLgbm.py
- python nyoka/tests/testScoreWithAdapaKeras.py
- python nyoka/tests/statsmodels_to_pmml_UnitTest.py
- python nyoka/tests/test2.py
- jupyter nbconvert --to notebook --execute nyoka/tests/Nyoka_String_Export.ipynb
@@ -32,27 +32,40 @@ def delete_model(self, model_name):
res = requests.delete(self.endpoint+"model/"+model_name, auth=HTTPBasicAuth(self.username,self.password))
return res.status_code

def score_in_zserver(self, model_name, test_file):
files = {'file': open(test_file,'r')}
def score_in_zserver(self, model_name, test_file, deep_network=False):
mode = 'r' if test_file.endswith(".csv") else 'rb'
files = {'file': open(test_file,mode)}
res = requests.post(self.endpoint+"apply/"+model_name, auth = HTTPBasicAuth(self.username, self.password),files=files)
all_rows = res.text.split('\r\n')
predictions = []
probabilities = []
if all_rows[0].split(",").__len__() == 1:
for row in all_rows[1:]:
if row.__class__.__name__ == 'str' and row.__len__() > 0:
row = ast.literal_eval(row)
else:
continue
predictions.append(row)
probabilities = None
if deep_network:
if test_file.endswith(".csv"):
info=res.text.split("\n")[1]
info = info.replace('"','')
predictions = info.split(",")[0]
probabilities = {out.split(":")[0]:float(out.split(":")[1]) for out in info.split(",")[2:]}
else:
resp=json.loads(res.text)
outs = resp["outputs"][0]
predictions = outs["predictedValue_predictions"]
probabilities = {out.split(":")[0]:float(out.split(":")[1]) for out in outs["top5_prob"].split(",")}
else:
for row in all_rows[1:]:
cols = row.split(",")
if cols[0] == '':
continue
predictions.append(ast.literal_eval(cols[-1]))
probabilities.append([ast.literal_eval(c) for c in cols[:-1]])
all_rows = res.text.split('\r\n')
predictions = []
probabilities = []
if all_rows[0].split(",").__len__() == 1:
for row in all_rows[1:]:
if row.__class__.__name__ == 'str' and row.__len__() > 0:
row = ast.literal_eval(row)
else:
continue
predictions.append(row)
probabilities = None
else:
for row in all_rows[1:]:
cols = row.split(",")
if cols[0] == '':
continue
predictions.append(ast.literal_eval(cols[-1]))
probabilities.append([ast.literal_eval(c) for c in cols[:-1]])
return predictions, probabilities

def compare_predictions(self, z_pred, m_pred):
Binary file not shown.
@@ -0,0 +1,136 @@
import sys, os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_DIR)

from keras import applications
from keras.layers import *
from keras.models import Model
from keras.models import Sequential

from nyoka import KerasToPmml
from nyoka import PMML44 as pml
from nyoka.Base64 import FloatBase64
import unittest
import requests
import json
from requests.auth import HTTPBasicAuth
from adapaUtilities import AdapaUtility
from dataUtilities import DataUtility

from keras.applications.mobilenet import preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import img_to_array
import numpy as np
import pandas as pd

class TestCases(unittest.TestCase):

@classmethod
def setUpClass(self):
print("******* Unit Test for Keras *******")
self.adapa_utility = AdapaUtility()
self.data_utility = DataUtility()


def test_01_image_classifier_with_image_as_input(self):
model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3))
activType='sigmoid'
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(2, activation=activType)(x)
model_final = Model(inputs =model.input, outputs = predictions,name='predictions')

cnn_pmml = KerasToPmml(model_final,model_name="MobileNetImage",description="Demo",\
copyright="Internal User",dataSet='image',predictedClasses=['dogs','cats'])
cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)

img = image.load_img('nyoka/tests/resizedCat.png')
img = img_to_array(img)
img = preprocess_input(img)
imgtf = np.expand_dims(img, axis=0)
model_pred=model_final.predict(imgtf)
model_preds = {'dogs':model_pred[0][0],'cats':model_pred[0][1]}

model_name = self.adapa_utility.upload_to_zserver('2classMBNet.pmml')

predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, 'nyoka/tests/resizedCat.png',True)

self.assertEqual(abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True)
self.assertEqual(abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)


def test_02_image_classifier_with_base64string_as_input(self):
model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3))
activType='sigmoid'
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(2, activation=activType)(x)
model_final = Model(inputs =model.input, outputs = predictions,name='predictions')

cnn_pmml = KerasToPmml(model_final,model_name="MobileNetBase64",description="Demo",\
copyright="Internal User",dataSet='imageBase64',predictedClasses=['dogs','cats'])
cnn_pmml.export(open('2classMBNetBase64.pmml', "w"), 0)

img = image.load_img('nyoka/tests/resizedCat.png')
img = img_to_array(img)
img = preprocess_input(img)
imgtf = np.expand_dims(img, axis=0)

base64string = "data:float32;base64," + FloatBase64.from_floatArray(img.flatten(),12)
base64string = base64string.replace("\n", "")
csvContent = "imageBase64\n\"" + base64string + "\""
text_file = open("input.csv", "w")
text_file.write(csvContent)
text_file.close()

model_pred=model_final.predict(imgtf)
model_preds = {'dogs':model_pred[0][0],'cats':model_pred[0][1]}

model_name = self.adapa_utility.upload_to_zserver('2classMBNetBase64.pmml')

predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, 'input.csv',True)

self.assertEqual(abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True)
self.assertEqual(abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)


# def test_03_SimpleDNN(self):
# X_train, X_test, y_train, columns, target_name, test_file = self.data_utility.get_data_for_regression()
# model = Sequential()
# model.add(Dense(13, input_dim=3, kernel_initializer='normal', activation='relu'))
# model.add(Dense(23))
# model.add(Dense(1, kernel_initializer='normal'))
# model.compile(loss='mean_squared_error', optimizer='adam')
# model.fit(X_train, y_train, epochs=1000, verbose=0)
# pmmlObj=KerasToPmml(model, model_name='Sequential', description="Demo", copyright="Internal User",\
# dataSet="inputBase64")
# pmmlObj.export(open('sequentialModel.pmml','w'),0)

# base64string = "data:float32;base64," + FloatBase64.from_floatArray(np.array(X_test).flatten(),12)
# base64string = base64string.replace("\n", "")
# csvContent = "inputBase64\n\"" + base64string + "\""
# text_file = open(test_file, "w")
# text_file.write(csvContent)
# text_file.close()

# model_pred = model.predict(X_test)
# model_name = self.adapa_utility.upload_to_zserver('sequentialModel.pmml')

# predictions, probabilities = self.adapa_utility.score_in_zserver(model_name, test_file,True)

# print(model_pred)
# print(predictions)
# print(probabilities)



@classmethod
def tearDownClass(self):
print("\n******* Finished *******\n")

if __name__ == '__main__':
unittest.main(warnings='ignore')


0 comments on commit 757cc87

Please sign in to comment.
You can’t perform that action at this time.