Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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))
Ejemplo n.º 7
0
    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)