def test_04_plain_text_script(self): model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) predictions = Dense(2, activation='sigmoid')(x) model_final = Model(inputs=model.input, outputs=predictions, name='predictions') script_content = open("nyoka/tests/preprocess.py", 'r').read() pmml_obj = KerasToPmml(model_final, dataSet='image', predictedClasses=['cat', 'dog'], script_args={ "content": script_content, "def_name": "getBase64EncodedString", "return_type": "string", "encode": False }) pmml_obj.export(open("script_with_keras.pmml", 'w'), 0) self.assertEqual(os.path.isfile("script_with_keras.pmml"), True) reconPmmlObj = pml.parse("script_with_keras.pmml", True) content = reconPmmlObj.TransformationDictionary.DefineFunction[ 0].Apply.Extension[0].anytypeobjs_ content[0] = content[0].replace("\t", "") content = "\n".join(content) self.assertEqual(script_content, content) self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
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', 'DN') self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)
def test_keras_03(self): input_tensor = x = Input(shape=(2, 4, 1)) x = Conv2D(activation="relu", filters=32, kernel_size=(2, 3), padding='same', strides=(1, 1))(x) x = BatchNormalization(center=True, scale=False)(x) x = ZeroPadding2D(padding=((3, 4), (1, 2)))(x) x = Conv2D(activation="relu", filters=64, kernel_size=(2, 2), padding='valid', strides=(1, 1))(x) x = MaxPooling2D(padding="same", strides=(2, 2), pool_size=(2, 2))(x) x = Dropout(rate=0.25)(x) x = BatchNormalization(center=False, scale=True)(x) x = Conv2D(activation="relu", filters=32, kernel_size=(2, 2), padding='valid', strides=(1, 1))(x) x = MaxPooling2D(padding="same", strides=(2, 2), pool_size=(2, 2))(x) x = Dropout(rate=0.25)(x) x = BatchNormalization(center=False, scale=False)(x) x = Flatten()(x) x = Dense(units=2, activation="softmax")(x) model = Model(input=input_tensor, output=x) pmml_obj = KerasToPmml(keras_model=model, predictedClasses=["yes", "no"]) pmml_obj.export(open("custom_model.pmml", 'w'), 0) self.assertEqual(os.path.isfile("custom_model.pmml"), True)
def test_keras_02(self): boston = load_boston() data = pd.DataFrame(boston.data) features = list(boston.feature_names) target = 'PRICE' data.columns = features data['PRICE'] = boston.target x_train, x_test, y_train, y_test = train_test_split(data[features], data[target], test_size=0.20, random_state=42) model = Sequential() model.add( Dense(13, input_dim=13, 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) pmmlObj.export(open('sequentialModel.pmml', 'w'), 0) reconPmmlObj = ny.parse('sequentialModel.pmml', True) self.assertEqual(os.path.isfile("sequentialModel.pmml"), True) self.assertEqual(len(model.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer) - 1)
def test_validate_keras_densenet(self): input_tensor = Input(shape=(224, 224, 3)) model = DenseNet121(weights="imagenet", input_tensor=input_tensor) file_name = "keras" + model.name + ".pmml" pmml_obj = KerasToPmml(model, dataSet="image", predictedClasses=[str(i) for i in range(1000)]) pmml_obj.export(open(file_name, 'w'), 0) self.assertEqual(self.schema.is_valid(file_name), True)
def test_keras_01(self): cnn_pmml = KerasToPmml(self.model_final,model_name="MobileNet",description="Demo",\ copyright="Internal User",dataSet='image',predictedClasses=['cats','dogs']) cnn_pmml.export(open('2classMBNet.pmml', "w"), 0) reconPmmlObj = ny.parse('2classMBNet.pmml', True) self.assertEqual(os.path.isfile("2classMBNet.pmml"), True) self.assertEqual(len(self.model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
def test_keras_01(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="MobileNet",description="Demo",\ copyright="Internal User",dataSet='image',predictedClasses=['cats','dogs']) cnn_pmml.export(open('2classMBNet.pmml', "w"), 0) reconPmmlObj=ny.parse('2classMBNet.pmml',True) self.assertEqual(os.path.isfile("2classMBNet.pmml"),True) self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
def test_keras_01(self): model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3)) #last layer not included 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,predictedClasses=['cats','dogs']) cnn_pmml.export(open('2classMBNet.pmml', "w"), 0) self.assertEqual(os.path.isfile("2classMBNet.pmml"),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', 'DN') 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_encoded_script(self): script_content = open("nyoka/tests/preprocess.py", 'r').read() pmml_obj = KerasToPmml(self.model_final, dataSet='image', predictedClasses=['cat', 'dog'], script_args={ "content": script_content, "def_name": "getBase64EncodedString", "return_type": "string", "encode": True }) pmml_obj.export(open("script_with_keras.pmml", 'w'), 0) self.assertEqual(os.path.isfile("script_with_keras.pmml"), True) reconPmmlObj = pml.parse("script_with_keras.pmml", True) content = reconPmmlObj.TransformationDictionary.DefineFunction[ 0].Apply.Extension[0].anytypeobjs_[0] content = base64.b64decode(content).decode() self.assertEqual(script_content, content) self.assertEqual(len(self.model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
def test_01_image_classifier_with_image_as_input(self): cnn_pmml = KerasToPmml(self.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 = self.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', 'DN') self.assertEqual( abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True) self.assertEqual( abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)