Esempio n. 1
0
 def selectArchitecture(self,checkTemplateID):
     if checkTemplateID=='mobilenetArch':
         pmmlObj = ny.parse(open(settingFilePath+'MobilenetArch.pmml','r'), silence=True)
         templateArch=self.pmmlToJson(settingFilePath+'MobilenetArch.pmml')
     elif checkTemplateID=='vgg16Arch':
         pmmlObj = ny.parse(open(settingFilePath+'vGG16Arch.pmml','r'), silence=True)
         templateArch=self.pmmlToJson(settingFilePath+'vGG16Arch.pmml')
     elif checkTemplateID=='vgg19Arch':
         pmmlObj = ny.parse(open(settingFilePath+'vGG19Arch.pmml','r'), silence=True)
         templateArch=self.pmmlToJson(settingFilePath+'vGG19Arch.pmml')
     return templateArch,pmmlObj
Esempio n. 2
0
 def nyObjOfModel(self, pmmlObj, singMod):
     import nyoka.PMML43Ext as ny
     if singMod['pmmlModelObject'].__dict__[
             'original_tagname_'] == 'MiningModel':
         nyokaObj = ny.PMML(MiningBuildTask=pmmlObj.MiningBuildTask,
                            DataDictionary=pmmlObj.DataDictionary,
                            MiningModel=[singMod['pmmlModelObject']])
     elif singMod['pmmlModelObject'].__dict__[
             'original_tagname_'] == 'DeepNetwork':
         nyokaObj = ny.PMML(DataDictionary=pmmlObj.DataDictionary,
                            DeepNetwork=[singMod['pmmlModelObject']])
     else:
         nyokaObj = None
     return nyokaObj
Esempio n. 3
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)
Esempio n. 4
0
    def test_binarizer(self):

        trfm_obj = Binarizer()
        trfm_obj, feature_names, target_name = auto_dataset_for_regression(
            trfm_obj)

        self.assertEqual(pp.get_class_name(trfm_obj),
                         trfm_obj.__class__.__name__)

        self.assertEqual(
            pp.get_derived_colnames('binarizer', ['displacement']),
            ['binarizer(displacement)'])

        self.assertEqual(
            pp.binarizer(trfm_obj,
                         feature_names)['der_fld'][0].__class__.__name__,
            pml.DerivedField().__class__.__name__)

        self.assertEqual(
            pp.binarizer(trfm_obj, feature_names)['der_fld'][0].get_optype(),
            "continuous")

        self.assertEqual(
            pp.binarizer(trfm_obj, feature_names)['der_fld'][0].get_dataType(),
            "double")

        self.assertEqual(
            pp.binarizer(trfm_obj, feature_names)['der_fld']
            [0].get_Apply().get_Constant()[0].get_valueOf_(),
            trfm_obj.threshold)
Esempio n. 5
0
    def test_max_abs_scaler(self):

        trfm_obj = MaxAbsScaler()
        trfm_obj, feature_names, target_name = auto_dataset_for_regression(
            trfm_obj)

        self.assertEqual(pp.get_class_name(trfm_obj),
                         trfm_obj.__class__.__name__)

        self.assertEqual(
            pp.get_derived_colnames('max_abs__scaler', ['displacement']),
            ['max_abs__scaler(displacement)'])

        self.assertEqual(
            pp.max_abs_scaler(trfm_obj,
                              feature_names)['der_fld'][0].__class__.__name__,
            pml.DerivedField().__class__.__name__)

        self.assertEqual(
            pp.max_abs_scaler(trfm_obj,
                              feature_names)['der_fld'][0].get_optype(),
            "continuous")

        self.assertEqual(
            pp.max_abs_scaler(trfm_obj,
                              feature_names)['der_fld'][0].get_dataType(),
            "double")

        self.assertEqual(
            pp.max_abs_scaler(trfm_obj, feature_names)['der_fld']
            [0].get_Apply().get_Constant()[0].get_valueOf_(),
            "{:.25f}".format(trfm_obj.max_abs_[0]))
Esempio n. 6
0
 def __init__(self, pmml):
     self.nyoka_pmml = ny.parse(pmml, True)
     self.image_input = None
     self.layer_input = None
     self.model = None
     self.layers_outputs = {}
     self.model = self._build_model()
Esempio n. 7
0
    def test_XGBoost(self):
        model=XGBClassifier()
        sk_model=iris_dataset(model)
        derived_col_names=['sepal_length','petal_length']
        col_names=['sepal_length','petal_length']
        target_name='species'
        mining_imp_value=()
        categoric_values=()

        self.assertEqual(
            len(xgb.get_segments_for_xgbc(sk_model,derived_col_names,col_names,target_name,mining_imp_value,categoric_values)),
            4)
        self.assertEqual(
            xgb.get_segments_for_xgbc(sk_model,derived_col_names,col_names,target_name,mining_imp_value,categoric_values)[
                3].get_id(),4)
        self.assertEqual(
            xgb.get_segments_for_xgbc(sk_model, derived_col_names, col_names, target_name, mining_imp_value,categoric_values)[
                3].get_RegressionModel().get_RegressionTable()[0].get_intercept(), '0.0')



        self.assertEqual(
            xgb.mining_Field_For_First_Segment(col_names).__class__.__name__,
            pml.MiningSchema().__class__.__name__)
        self.assertEqual(
            xgb.mining_Field_For_First_Segment(col_names).get_MiningField()[0].get_name(),
            'sepal_length')
        self.assertEqual(len(xgb.mining_Field_For_First_Segment(['a','b','d','e']).get_MiningField())
                         ,4)


        self.assertEqual(
            type(xgb.generate_Segments_Equal_To_Estimators([],derived_col_names,col_names)),
            type([]))


        self.assertEqual(
            xgb.add_segmentation(sk_model,[],[],pml.Output,1).__class__.__name__,
            pml.Segment().__class__.__name__)
        self.assertEqual(
            xgb.add_segmentation(sk_model,[],[],pml.Output,1).get_MiningModel().__class__.__name__,
            pml.MiningModel().__class__.__name__)
        self.assertEqual(xgb.add_segmentation(sk_model, [], [], pml.Output, 1).get_id(),
                         2)

        self.assertEqual(type(xgb.get_regrs_tabl(sk_model,col_names,'species',categoric_values)),type([]))
Esempio n. 8
0
    def test_LightGradientBoostingClassifier(self):
        model = LGBMClassifier()
        sk_model, feature_names, target_name = iris_dataset(model)
        derived_col_names = feature_names
        mining_imp_value = ()
        categoric_values = ()

        self.assertEqual(
            lgbToPmml.get_segments_for_lgbc(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value, categoric_values)[0].__class__.__name__,
            pml.Segment().__class__.__name__)

        self.assertEqual(
            lgbToPmml.get_segments_for_lgbc(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value,
                categoric_values)[-1].get_RegressionModel().__class__.__name__,
            pml.RegressionModel().__class__.__name__)

        self.assertEqual(
            len(
                lgbToPmml.get_segments_for_lgbc(sk_model, derived_col_names,
                                                feature_names, target_name,
                                                mining_imp_value,
                                                categoric_values)),
            sk_model.n_classes_ + 1)

        self.assertEqual(
            lgbToPmml.get_ensemble_models(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value, categoric_values)[0].get_functionName(),
            'classification')

        self.assertEqual(
            len(
                lgbToPmml.get_ensemble_models(
                    sk_model, derived_col_names, feature_names, target_name,
                    mining_imp_value,
                    categoric_values)[0].get_MiningSchema().get_MiningField()),
            model.n_features_ + 1)
Esempio n. 9
0
    def test_LightGradientBoostingRegressor(self):
        model = LGBMRegressor()
        sk_model, feature_names, target_name = auto_dataset_for_regression(
            model)
        derived_col_names = feature_names
        mining_imp_value = ()
        categoric_values = ()

        self.assertEqual(
            lgbToPmml.get_segments_for_lgbr(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value, categoric_values).__class__.__name__,
            pml.Segmentation().__class__.__name__)

        self.assertEqual(
            len(
                lgbToPmml.get_segments_for_lgbr(
                    sk_model, derived_col_names, feature_names, target_name,
                    mining_imp_value, categoric_values).get_Segment()),
            model.n_estimators)

        self.assertEqual(
            lgbToPmml.get_ensemble_models(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value, categoric_values)[0].__class__.__name__,
            pml.MiningModel().__class__.__name__)

        self.assertEqual(
            lgbToPmml.get_ensemble_models(
                sk_model, derived_col_names, feature_names, target_name,
                mining_imp_value, categoric_values)[0].get_functionName(),
            'regression')

        self.assertEqual(
            len(
                lgbToPmml.get_ensemble_models(
                    sk_model, derived_col_names, feature_names, target_name,
                    mining_imp_value,
                    categoric_values)[0].get_MiningSchema().get_MiningField()),
            model.n_features_ + 1)
Esempio n. 10
0
    def test_SVM(self):
        model = SVR(kernel='sigmoid')
        sk_model, feature_names, target_name = auto_dataset_for_regression(
            model)
        categoric_values = ()

        self.assertEqual(
            sklToPmml.get_kernel_type(sk_model)
            ['SigmoidKernelType'].__class__.__name__,
            pml.SigmoidKernelType().__class__.__name__)

        self.assertEqual(
            sklToPmml.get_kernel_type(sk_model)
            ['SigmoidKernelType'].get_gamma(), 0.14285714285714285)

        self.assertEqual(sklToPmml.get_classificationMethod(sk_model),
                         'OneAgainstAll')

        self.assertEqual(
            sklToPmml.get_vectorDictionary(
                sk_model, feature_names, categoric_values).__class__.__name__,
            pml.VectorDictionary().__class__.__name__)

        self.assertEqual(
            sklToPmml.get_vectorDictionary(
                sk_model, feature_names,
                categoric_values).get_VectorFields().get_FieldRef()
            [0].get_field(), 'cylinders')

        self.assertEqual(
            len(
                sklToPmml.get_vectorDictionary(
                    sk_model, feature_names,
                    categoric_values).get_VectorFields().get_FieldRef()),
            len(feature_names))

        self.assertEqual(
            sklToPmml.get_supportVectorMachine(sk_model)
            [0].get_Coefficients().get_absoluteValue(), 23.0)
 def test_construction_vgg(self):
     model = applications.VGG16(weights="imagenet",
                                include_top=False,
                                input_shape=(224, 224, 3))
     x = model.output
     x = layers.Flatten()(x)
     x = layers.Dense(1024, activation="relu")(x)
     x = layers.Dropout(0.5)(x)
     x = layers.Dense(1024, activation="relu")(x)
     predictions = layers.Dense(2, activation="softmax")(x)
     model_final = models.Model(input=model.input, output=predictions)
     model_final.compile(loss="binary_crossentropy",
                         optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
                         metrics=["accuracy"])
     pmmlObj = KerasToPmml(model_final, dataSet='image')
     pmmlObj.export(open('vgg.pmml', 'w'), 0)
     reconPmmlObj = ny.parse('vgg.pmml', True)
     self.assertEqual(os.path.isfile("vgg.pmml"), True)
     self.assertEqual(len(model_final.layers),
                      len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
Esempio n. 12
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,
                               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))
Esempio n. 13
0
    def test_lbl_encoder(self):

        trfm_obj = LabelEncoder()
        trfm_obj, feature_names, target_name = auto_dataset_for_regression(
            trfm_obj)
        self.assertEqual(pp.get_class_name(trfm_obj),
                         trfm_obj.__class__.__name__)

        self.assertEqual(pp.get_derived_colnames('labelEncoder', ['origin']),
                         ['labelEncoder(origin)'])

        self.assertEqual(
            pp.lbl_encoder(trfm_obj,
                           feature_names)['der_fld'][0].__class__.__name__,
            pml.DerivedField().__class__.__name__)

        self.assertEqual(
            pp.lbl_encoder(trfm_obj, feature_names)['der_fld'][0].get_optype(),
            "continuous")

        self.assertEqual(
            pp.lbl_encoder(trfm_obj,
                           feature_names)['der_fld'][0].get_dataType(),
            "double")
        self.assertEqual(
            pp.lbl_encoder(trfm_obj, feature_names)['der_col_names'][0],
            "labelEncoder(origin)")
        self.assertEqual(
            pp.lbl_encoder(trfm_obj, feature_names)['pp_feat_class_lbl'][0],
            trfm_obj.classes_[0])

        self.assertEqual(
            pp.lbl_encoder(trfm_obj, feature_names)['der_fld']
            [0].get_MapValues().get_outputColumn(), "output")

        self.assertEqual(
            pp.lbl_encoder(trfm_obj, feature_names)['pp_feat_name'], "origin")
Esempio n. 14
0
    def test_lbl_binarizer(self):

        trfm_obj = LabelBinarizer()
        trfm_obj, feature_names, target_name = auto_dataset_for_regression(
            trfm_obj)
        self.assertEqual(pp.get_class_name(trfm_obj),
                         trfm_obj.__class__.__name__)

        self.assertEqual(
            pp.lbl_binarizer(trfm_obj,
                             feature_names)['der_fld'][0].__class__.__name__,
            pml.DerivedField().__class__.__name__)

        self.assertEqual(
            pp.lbl_binarizer(trfm_obj,
                             feature_names)['der_fld'][0].get_optype(),
            "categorical")

        self.assertEqual(
            pp.lbl_binarizer(trfm_obj,
                             feature_names)['der_fld'][0].get_dataType(),
            "double")

        self.assertEqual(
            pp.lbl_binarizer(trfm_obj, feature_names)['pp_feat_class_lbl'][0],
            trfm_obj.classes_[0])

        self.assertEqual(
            pp.lbl_binarizer(
                trfm_obj,
                feature_names)['der_fld'][0].get_NormDiscrete().get_field(),
            "origin")

        self.assertEqual(
            pp.lbl_binarizer(trfm_obj, feature_names)['pp_feat_name'],
            "origin")
Esempio n. 15
0
    def pmmlToJson(self, filePath):
        pmmlObj = ny.parse(filePath, silence=True)
        pmmlDictObj = pmmlObj.__dict__

        # print ('0'*100,pmmlObj.get_type())

        if pmmlObj.get_type() == 'multi':
            print('came to Workflow')
            # print('*'*100)

            # print(PMMLMODELSTORAGE)
            # print('*'*100)

            import pathlib
            from trainModel.mergeTrainingV2 import TrainingViewModels
            pmmlFileObj = pathlib.Path(filePath)
            pmmlFileForKey = pmmlFileObj.name.replace(pmmlFileObj.suffix, '')
            from trainModel.mergeTrainingV2 import NewModelOperations
            NewModelOperations().loadExecutionModel(filePath)
            modelInformation = PMMLMODELSTORAGE[pmmlFileForKey]
            # print ('PMMLMODELSTORAGE after >>>>>>>>>>> ',PMMLMODELSTORAGE)
            # print (modelInformation)

            toexp = TrainingViewModels().restructureModelInforForExportDict(
                modelInformation)
            # print ('toexp'*20)
            # print ('toexportDictN >>>>>>>> ',toexp)

            import copy, json

            tempSec = {
                "name": "Section",
                "layerId": "Section",
                "children": [],
                "itemType": "FOLDING",
                "icon": "mdi mdi-group",
                "class": "wide",
                "modelType": "Workflow",
                "id": "id",
                "sectionId": "modName",
                "layerIndex": None,
                'connectionLayerId': None
            }

            tempData = {
                "name": "Data",
                "icon": "mdi mdi-database-plus",
                "itemType": "DATA",
                "layerId": None,
                "trainable": False,
                "modelType": "Workflow",
                "id": None,
                "layerIndex": None,
                "connectionLayerId": None,
                "url": None,
                "filePath": None
            }

            tempCode = {
                "name": "Code",
                "icon": "mdi mdi-code-braces",
                "itemType": "CODE",
                "layerId": None,
                "trainable": False,
                "modelType": "Workflow",
                "id": "K2PVI4HZ3NBGF",
                "layerIndex": None,
                "connectionLayerId": None,
                "url": None,
                "filePath": None,
                "taskType": None,
                "scriptOutput": None,
                "scriptPurpose": None
            }

            tempModel = {
                "name": "Model",
                "icon": "mdi mdi-xml",
                "itemType": "MODEL",
                "layerId": None,
                "trainable": False,
                "modelType": "Workflow",
                "id": None,
                "layerIndex": None,
                "connectionLayerId": None,
                "url": None,
                "filePath": None,
                "taskType": None
            }

            # toexp={'K2PSSUKYFRSMF': {'hyperparameters': None,
            #                    'data': 'C:/Users/swsh/Desktop/ZMODGit/ZMOD/ZMOD/Data/newData2',
            #                    'preProcessingScript': {'scripts': ['def addVal(x):\n    return x\n'], 'scriptpurpose': ['trainAndscore'], 'scriptOutput': ['DATA'], 'scriptPath': ['C:/Users/swsh/Desktop/ZMODGit/ZMOD/ZMOD/Code/scriptToTest.py']},
            #                    'modelObj': None,
            #                    'pipelineObj': None,
            #                    'featuresUsed': ['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration'],
            #                    'targetName': 'mpg',
            #                    'postProcessingScript': {'scripts': [], 'scriptpurpose': [], 'scriptOutput': [], 'scriptPath': []},
            #                    'taskType': 'trainAndscore',
            #                    'modelPath': 'C:\\Users\\swsh\\Desktop\\ZMODGit\\ZMOD\\ZMOD\\Models\\autoML2.pmml'}}

            workflowArch = []
            for modTemp in list(toexp.keys()):
                temSecCop = copy.deepcopy(tempSec)  #.copy()
                temSecCop['sectionId'] = modTemp
                temSecCop["layerId"] = modTemp
                if toexp[modTemp]['data'] != None:
                    dataInfo = copy.deepcopy(tempData)
                    import pathlib
                    fileName = pathlib.Path(toexp[modTemp]['data']).name
                    dataInfo['layerId'] = fileName
                    dataInfo['url'] = '/Data/' + fileName
                    dataInfo['filePath'] = toexp[modTemp]['data']
                    temSecCop['children'].append(dataInfo)
                for numSc, sC in enumerate(
                        toexp[modTemp]['preProcessingScript']['scriptPath']):
                    codeInfo = copy.deepcopy(tempCode)
                    fileName = pathlib.Path(
                        toexp[modTemp]['preProcessingScript']['scriptPath']
                        [numSc]).name
                    codeInfo['layerId'] = fileName
                    codeInfo['url'] = '/Code/' + fileName
                    codeInfo['filePath'] = toexp[modTemp][
                        'preProcessingScript']['scriptPath'][numSc]
                    codeInfo['taskType'] = 'PREPROCESSING'
                    codeInfo['scriptOutput'] = toexp[modTemp][
                        'preProcessingScript']['scriptOutput'][numSc]
                    codeInfo['scriptPurpose'] = toexp[modTemp][
                        'preProcessingScript']['scriptpurpose'][numSc]
                    temSecCop['children'].append(codeInfo)

                modtempC = copy.deepcopy(tempModel)
                fileName = pathlib.Path(toexp[modTemp]['modelPath']).name
                modtempC['layerId'] = fileName
                modtempC['url'] = '/Model/' + fileName
                modtempC['filePath'] = toexp[modTemp]['modelPath']
                modtempC['taskType'] = toexp[modTemp]['taskType']
                temSecCop['children'].append(modtempC)

                for numSc, sC in enumerate(
                        toexp[modTemp]['postProcessingScript']['scriptPath']):
                    codeInfo = copy.deepcopy(tempCode)
                    fileName = pathlib.Path(
                        toexp[modTemp]['postProcessingScript']['scriptPath']
                        [numSc]).name
                    codeInfo['layerId'] = fileName
                    codeInfo['url'] = '/Code/' + fileName
                    codeInfo['filePath'] = toexp[modTemp][
                        'postProcessingScript']['scriptPath'][numSc]
                    codeInfo['taskType'] = 'POSTPROCESSING'
                    codeInfo['scriptOutput'] = toexp[modTemp][
                        'postProcessingScript']['scriptOutput'][numSc]
                    codeInfo['scriptPurpose'] = toexp[modTemp][
                        'postProcessingScript']['scriptpurpose'][numSc]
                    temSecCop['children'].append(codeInfo)

                workflowArch.append(temSecCop)

            from random import choice
            from string import ascii_uppercase

            for num, i in enumerate(workflowArch):
                if i['itemType'] == 'FOLDING':
                    i['layerIndex'] = num
                    i['id'] = ''.join(
                        choice(ascii_uppercase) for i in range(12))
                    for num2, j in enumerate(i['children']):
                        j['layerIndex'] = num2
                        j['id'] = ''.join(
                            choice(ascii_uppercase) for i in range(12))
                else:
                    i['layerIndex'] = num
                    i['id'] = ''.join(
                        choice(ascii_uppercase) for i in range(12))
            # print ('l'*200)
            # print ('workflowArch',workflowArch)
            return workflowArch
        else:
            overAll = []

            deepObject = pmmlDictObj['DeepNetwork'][0]
            listOfNetworkLayer = deepObject.NetworkLayer
            for lay in listOfNetworkLayer:
                networkDict = lay.__dict__
                tempDict = {}
                tempDict['layerParam'] = {}
                tempDict['netParam'] = {}
                for j in networkDict:
                    if networkDict[j] is not None:
                        if j not in [
                                'original_tagname_', 'LayerWeights',
                                'LayerParameters', 'Extension', 'LayerBias'
                        ]:
                            tempDict['netParam'][j] = networkDict[j]
                        layerDict = networkDict['LayerParameters'].__dict__
                        for kk in layerDict:
                            if layerDict[kk] is not None:
                                if kk not in [
                                        'original_tagname_', 'Extension'
                                ]:
                                    try:
                                        evalVal = list(
                                            ast.literal_eval(layerDict[kk]))
                                    except:
                                        evalVal = layerDict[kk]
                                    tempDict['layerParam'][kk] = evalVal
                        tempDict['layerParam'][
                            'trainable'] = False if layerDict[
                                'trainable'] == False else True

                        if len(networkDict['Extension']) > 0:
                            ttt = networkDict['Extension'][0]
                            sectionVal = ttt.get_value()
                            import ast
                            tempDict['sectionId'] = ast.literal_eval(
                                sectionVal)['sectionId']
                        else:
                            tempDict['sectionId'] = None
                overAll.append(tempDict)

            allLayers = MEMORY_OF_LAYERS['layerinfo'][0]['layers']
            listOFLayersName = [
                j['name'] for j in MEMORY_OF_LAYERS['layerinfo'][0]['layers']
            ]
            architecture = []
            for tempLay in overAll:
                import copy
                tempSpace = copy.deepcopy(allLayers[listOFLayersName.index(
                    tempLay['netParam']['layerType'])])

                layerPARA = tempLay['layerParam']
                netWorkPARA = tempLay['netParam']
                for j in netWorkPARA:
                    try:
                        tempSpace[j] = netWorkPARA[j]
                    except:
                        pass

                for k in layerPARA:
                    for k2 in tempSpace['properties']:
                        if k2['id'] == k:
                            k2['value'] = layerPARA[k]

                try:
                    tempSpace['sectionId'] = tempLay['sectionId']
                except:
                    pass
                tempSpace['trainable'] = layerPARA['trainable']
                architecture.append(tempSpace)

            forLoopSection = [j['sectionId'] for j in architecture]
            # print ('forLoopSection $$$$$$$$$$$$$$$',forLoopSection)
            tempSection = {
                'children': [],
                'class': 'wide',
                'icon': 'mdi mdi-group',
                'id': '',
                'itemType': 'FOLDING',
                'layerId': 'Section',
                'layerIndex': '',
                'name': 'Section',
                'sectionId': '',
                "sectionCollapse": True
            }

            import copy

            newarchitecture = []
            tempSectionA = copy.deepcopy(tempSection)
            for num, secInfo in enumerate(forLoopSection):
                if secInfo is None:
                    newarchitecture.append(architecture[num])
                else:
                    if (num + 1 < len(forLoopSection)) and (
                            forLoopSection[num] == forLoopSection[num + 1]):
                        tempSectionA['children'].append(architecture[num])
                    else:
                        tempSectionA['children'].append(architecture[num])
                        tempSectionA['sectionId'] = secInfo
                        tempSectionA['layerId'] = 'Section_' + str(num)
                        tempSectionA['name'] = 'Section_' + str(num)
                        newarchitecture.append(tempSectionA)
                        tempSectionA = copy.deepcopy(tempSection)

            hd = pmmlDictObj['Header']
            scrptVal = pmmlDictObj['script']
            DataVal = pmmlDictObj['Data']
            import ast, pathlib
            try:
                try:
                    dataUrl = DataVal[0].filePath
                except:
                    dataUrl = 'Some issue'
                print('$$$$$$$$$$$$$$$$$$$$$$', dataUrl)
                if dataUrl != 'Some issue':
                    fObj = pathlib.Path(dataUrl)
                    dataCon = {
                        'icon': 'mdi mdi-database-plus',
                        'id': 'NNN',
                        'itemType': 'DATA',
                        'layerId': fObj.name,
                        'layerIndex': 0,
                        'name': 'Data',
                        'url': dataUrl
                    }
                    newarchitecture.insert(0, dataCon)

                    for counT, sc in enumerate(scrptVal):
                        import pathlib
                        scriptPurpose = sc.scriptPurpose
                        modelVal = sc.for_
                        classVal = sc.class_
                        filePathUrl = sc.filePath
                        fObjScrpt = pathlib.Path(filePathUrl)
                        scriptCon = {
                            "name": "Code",
                            "icon": "mdi mdi-code-braces",
                            "itemType": "CODE",
                            "modelFor": modelVal,
                            "layerId": fObjScrpt.name,
                            "scriptPurpose": scriptPurpose,
                            'url': filePathUrl,
                            "layerIndex": "NA",
                            'useFor': classVal
                        }
                        newarchitecture.insert(counT + 1, scriptCon)
                else:
                    pass

            except Exception as e:
                for counT, sc in enumerate(scrptVal):
                    scriptUrl = sc.class_
                    import pathlib
                    fObjScrpt = pathlib.Path(scriptUrl)
                    scriptCon = {
                        "name": "Code",
                        "icon": "mdi mdi-code-braces",
                        "itemType": "CODE",
                        "layerId": fObjScrpt.name,
                        'url': scriptUrl,
                        "layerIndex": "NA"
                    }
                    newarchitecture.insert(counT, scriptCon)
                print(e, 'some error occured')

            for num, i in enumerate(newarchitecture):
                if i['itemType'] == 'FOLDING':
                    i['layerIndex'] = num
                    i['id'] = ''.join(
                        choice(ascii_uppercase) for i in range(12))
                    for num2, j in enumerate(i['children']):
                        j['layerIndex'] = num2
                        j['id'] = ''.join(
                            choice(ascii_uppercase) for i in range(12))
                else:
                    i['layerIndex'] = num
                    from random import choice
                    from string import ascii_uppercase
                    i['id'] = ''.join(
                        choice(ascii_uppercase) for i in range(12))

            return newarchitecture
Esempio n. 16
0
    def getPmml(self, architecture):

        fName = 'classification'
        lenOfArch = len(architecture)
        mName = 'Keras Model'

        netWorkInfo = []
        scriptVal = []
        extensionInfoForData = [pml.Extension(value=[], anytypeobjs_=[''])]
        dataVal = {}
        for counta, j in enumerate(architecture):
            if counta == 0:
                someConectionId = 'na'
            else:
                someConectionId = tempConId
            if j['itemType'] in ['CODE']:
                # print ('##################',j)
                scriptFile = open(j['filePath'], 'r')
                scriptCode = scriptFile.read()
                scriptCode = scriptCode.replace('<', '&lt;')
                scriptInfo = {}
                scrptVal = []
                # dataVal['scriptUrl']=j['url']
                useFor = j['useFor']
                extensionInfoForScript = [
                    pml.Extension(value=scrptVal, anytypeobjs_=[''])
                ]
                scrp = pml.script(content=scriptCode,
                                  Extension=extensionInfoForScript,
                                  for_=useFor)
                scriptVal.append(scrp)
                tempConId = None
            elif j['itemType'] in ['DATA']:
                try:
                    dataVal['dataUrl'] = j['filePath']
                    extensionInfoForData = [
                        pml.Extension(value=dataVal, anytypeobjs_=[''])
                    ]
                except:
                    pass
                tempConId = None

            elif j['itemType'] == 'FOLDING':
                #         print (j)
                for k in j['children']:
                    tempdata7 = self.convertToStandardJson(k)
                    tempdata7['connectionLayerId'] = someConectionId
                    tempConId = tempdata7['layerId']
                    pp = addLayer(tempdata7)
                    netWorkInfo.append(pp)
                    someConectionId = tempConId
            else:
                # print ('Start of tamasha$$$$$$$$$$',j)
                tempdata7 = self.convertToStandardJson(j)
                tempdata7['connectionLayerId'] = someConectionId
                tempConId = tempdata7['layerId']
                # print ('pakda', tempdata7)
                pp = self.addLayer(tempdata7)
                netWorkInfo.append(pp)

        kk = pml.DeepNetwork(modelName=mName,
                             functionName=fName,
                             NetworkLayer=netWorkInfo,
                             numberOfLayers=lenOfArch)
        tt = pml.Timestamp(datetime.now())
        hd = pml.Header(copyright="Copyright (c) 2018 Software AG",
                        Extension=extensionInfoForData,
                        description="Neural Network Model",
                        Timestamp=pml.Timestamp(datetime.now()))

        jj = pml.PMML(version="4.3Ext",
                      script=scriptVal,
                      Header=hd,
                      DeepNetwork=[kk])
        return jj
Esempio n. 17
0
    def addLayer(self, tempData):
        # print ('addLayerPMML 00000111>>>> ',tempData)
        tempLayerBiasobject = pml.LayerBias(content=[])
        tempBia = tempLayerBiasobject.__dict__
        tempBia['weightsFlattenAxis'] = "0"

        tempLayerWeightobject = pml.LayerWeights(content=[])
        tempWei = tempLayerWeightobject.__dict__
        tempWei['weightsFlattenAxis'] = "0"

        if tempData['layerType'] in ['Input']:
            tempData['properties']['outputDimension'] = self.outputForInput(
                tempData)
        if tempData['layerType'] in ['Dropout']:
            if tempData['properties']['dropoutRate'] == '':
                tempData['properties']['dropoutRate'] = '0'
            tempData['properties']['outputDimension'] = self.outputForInput(
                tempData)
        elif tempData['layerType'] in ['BatchNormalization']:
            tempData['properties']['outputDimension'] = self.outputForInput(
                tempData)
            tempWei['weightsShape'] = self.weightBatchNormalization(tempData)
        elif tempData['layerType'] in ['Activation']:
            tempData['properties']['outputDimension'] = tempData['properties'][
                'inputDimension']
        elif tempData['layerType'] in ['Conv2D', 'DepthwiseConv2D']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
            tempWei['weightsShape'] = self.weightConvo(tempData)
        elif tempData['layerType'] in ['Flatten', 'GlobalAveragePooling2D']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
        elif tempData['layerType'] in ['MaxPooling2D', 'AveragePooling2D']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
        elif tempData['layerType'] in ['MaxPooling1D', 'AveragePooling1D']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
            tempData['properties']['poolSize'] = str(
                tempData['properties']['poolSize'])
            tempData['properties']['stride'] = str(
                tempData['properties']['stride'])
        elif tempData['layerType'] in ['Reshape']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
            tempData['properties']['reshapeTarget'] = str(
                tuple(tempData['properties']['reshapeTarget']))
        elif tempData['layerType'] in ['ZeroPadding2D', 'ZeroPadding1D']:
            tempData['properties']['outputDimension'] = self.outputForFlatten(
                tempData)
            if len(tempData['properties']['paddingDims']) == 4:
                top_pad, bottom_pad, left_pad, right_pad = tempData[
                    'properties']['paddingDims']
                tempData['properties']['paddingDims'] = ((top_pad, bottom_pad),
                                                         (left_pad, right_pad))
            elif len(tempData['properties']['paddingDims']) == 1:
                pad = tempData['properties']['paddingDims'][0]
                tempData['properties']['paddingDims'] = (pad, pad)
            tempData['properties']['paddingDims'] = str(
                tuple(tempData['properties']['paddingDims']))
        elif tempData['layerType'] in ['Dense']:
            if tempData['properties']['activationFunction'] == "relu":
                tempData['properties']['activationFunction'] = "rectifier"
            elif tempData['properties']['activationFunction'] == "tanh":
                tempData['properties']['activationFunction'] = "tanch"

            tempData['properties']['outputDimension'] = self.outputForDense(
                tempData)
            tempWei['weightsShape'] = self.weightBiasDense(tempData)[0]
            tempBia['weightsShape'] = self.weightBiasDense(tempData)[1]
        # print ('$$$$$$$$$$$$$$$tempData',tempData)
        tempLayerobject = pml.LayerParameters()
        tempvals = tempLayerobject.__dict__
        for j in list(tempvals.keys()):
            try:
                tempvals[j] = tempData['properties'][j]
            except:
                pass

        if tempData['sectionId'] == None:

            if tempData['layerType'] == 'Input':
                input_filed_name = "base64String"
                kk = pml.NetworkLayer(
                    inputFieldName=input_filed_name,
                    layerType=tempData['layerType'],
                    layerId=tempData['layerId'],
                    connectionLayerId=tempData['connectionLayerId'],
                    LayerParameters=tempLayerobject)
            elif tempData['layerType'] == 'Dense':

                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject,LayerWeights=tempLayerWeightobject,LayerBias=tempLayerBiasobject)
            elif tempData['layerType'] in [
                    'Conv2D', 'DepthwiseConv2D', 'BatchNormalization'
            ]:
                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject,LayerWeights=tempLayerWeightobject)
            else:
                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject)

        else:
            extensionInfo = [
                pml.Extension(value={'sectionId': tempData['sectionId']},
                              anytypeobjs_=[''])
            ]
            if tempData['layerType'] == 'Input':
                input_filed_name = "base64String"
                kk=pml.NetworkLayer(inputFieldName=input_filed_name,Extension=[extensionInfo], layerType=tempData['layerType'],\
                                   layerId=tempData['layerId'],connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject)
            elif tempData['layerType'] == 'Dense':

                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],Extension=extensionInfo,\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject,LayerWeights=tempLayerWeightobject,LayerBias=tempLayerBiasobject)
            elif tempData['layerType'] in [
                    'Conv2D', 'DepthwiseConv2D', 'BatchNormalization'
            ]:

                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],Extension=extensionInfo,\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject,LayerWeights=tempLayerWeightobject)
            else:
                kk=pml.NetworkLayer(layerType=tempData['layerType'],layerId=tempData['layerId'],Extension=extensionInfo,\
                                   connectionLayerId=tempData['connectionLayerId'],\
                                   LayerParameters=tempLayerobject)

        return kk
Esempio n. 18
0
    def loadPMMLmodel(self, filepath, idforData=None):
        def readScriptFromPMML(scrptCode, useForVal):
            code = None
            for num, sc in enumerate(scrptCode):
                useFor = sc.for_
                # print ('>>>>>>>UUUUUU>>>>>>>',useFor,useForVal)

                if useFor == useForVal:
                    scripCode = sc.get_valueOf_()
                    # print (scripCode)
                    code = scripCode.lstrip('\n')
                    lines = []
                    code = scripCode.lstrip('\n')
                    leading_spaces = len(code) - len(code.lstrip(' '))
                    for line in code.split('\n'):
                        lines.append(line[leading_spaces:])
                    code = '\n'.join(lines)
            return code

        global PMMLMODELSTORAGE
        try:
            print('step 1', filepath)
            pmmlName = os.path.basename(filepath).split('.')[0]
            nyoka_pmml_obj = ny.parse(filepath, True)
            pmmlObj = nyoka_pmml_obj.__dict__
            try:
                checkMRCNN = nyoka_pmml_obj.DeepNetwork[0].Extension[
                    0].name == 'config'
            except:
                checkMRCNN = False
            #MaskRcnn model
            if (nyoka_pmml_obj.DeepNetwork) and (checkMRCNN == True):
                from nyoka.mrcnn import pmml_to_maskrcnn
                from nyoka.mrcnn import model as modellib
                predClasses = self.getPredClasses(nyoka_pmml_obj)
                modelFolder = './logs/MaskRCNNWei_' + ''.join(
                    choice(ascii_uppercase) for i in range(12)) + '/'
                self.checkCreatePath(modelFolder)
                model_graph = Graph()
                with model_graph.as_default():
                    tf_session = Session()
                    with tf_session.as_default():
                        modelRecon = pmml_to_maskrcnn.GenerateMaskRcnnModel(
                            nyoka_pmml_obj)
                        weight_file = modelFolder + '/dumpedWeights.h5'
                        modelRecon.model.keras_model.save_weights(weight_file)
                        MODEL_DIR = modelFolder
                        model = modellib.MaskRCNN(mode="inference",
                                                  model_dir=MODEL_DIR,
                                                  config=modelRecon.config)
                        model.load_weights(weight_file, by_name=True)
                        model_graph = tf.get_default_graph()
                # pmmlName = pmmlName
                PMMLMODELSTORAGE[pmmlName] = {}
                PMMLMODELSTORAGE[pmmlName]['model'] = model
                PMMLMODELSTORAGE[pmmlName]['modelType'] = 'MRCNN'
                PMMLMODELSTORAGE[pmmlName]['model_graph'] = model_graph
                PMMLMODELSTORAGE[pmmlName]['predClasses'] = list(predClasses)
                PMMLMODELSTORAGE[pmmlName]['tf_session'] = tf_session
                modelType = 'MRCNN'

            #DeepNetwokr Model
            elif nyoka_pmml_obj.DeepNetwork:
                hdInfo = pmmlObj['Header']
                try:
                    hdExtDet = ast.literal_eval(
                        hdInfo.Extension[0].get_value())
                except:
                    pass

                print('step 2')
                predClasses = self.getPredClasses(nyoka_pmml_obj)
                print('step 3')
                newNet = nyoka_pmml_obj.DeepNetwork[0]
                print('step 4')
                scrptCode = nyoka_pmml_obj.script
                # print ('Step 4.1 ',scrptCode)

                try:
                    preCode = readScriptFromPMML(scrptCode, 'TEST')
                except:
                    preCode = None
                try:
                    postCode = readScriptFromPMML(scrptCode, 'POSTPROCESSING')
                except:
                    postCode = None

                model_graph = Graph()
                with model_graph.as_default():
                    tf_session = Session()
                    with tf_session.as_default():
                        print('step 5')
                        from nyoka.keras.pmml_to_keras_model import GenerateKerasModel
                        print('step 5.1')
                        model_net = GenerateKerasModel(nyoka_pmml_obj)
                        print('step 5.2')
                        model = model_net.model
                        model_graph = tf.get_default_graph()
                        print('step 6')
                inputShapevals = [
                    inpuShape.value for inpuShape in list(model.input.shape)
                ]
                PMMLMODELSTORAGE[pmmlName] = {}
                PMMLMODELSTORAGE[pmmlName]['model'] = model
                PMMLMODELSTORAGE[pmmlName]['predClasses'] = predClasses
                PMMLMODELSTORAGE[pmmlName]['preProcessScript'] = preCode
                PMMLMODELSTORAGE[pmmlName]['postProcessScript'] = postCode
                try:
                    PMMLMODELSTORAGE[pmmlName]['scriptOutput'] = hdExtDet[
                        'scriptOutput']
                except:
                    PMMLMODELSTORAGE[pmmlName]['scriptOutput'] = ''
                print('step 7')
                try:
                    PMMLMODELSTORAGE[pmmlName]['inputShape'] = inputShapevals
                except:
                    PMMLMODELSTORAGE[pmmlName]['inputShape'] = 'CheckSomeissue'
                PMMLMODELSTORAGE[pmmlName]['status'] = 'loaded'
                # print ('step 8')
                PMMLMODELSTORAGE[pmmlName]['model_graph'] = model_graph
                PMMLMODELSTORAGE[pmmlName]['tf_session'] = tf_session
                PMMLMODELSTORAGE[pmmlName]['modelType'] = 'kerasM'
                modelType = 'kerasM'
                # print ('###################',PMMLMODELSTORAGE)
            #Sklearn Model
            else:
                print('Next Step 2 >>>>>>>>>>>>')
                from nyoka.reconstruct.pmml_to_pipeline_model import generate_skl_model
                print('Next Step 3 >>>>>>>>>>>>')
                sklModelPipeline = generate_skl_model(filepath)
                print('Next Step 4 >>>>>>>>>>>>')
                # if hasattr(sklModelPipeline.steps[-1][-1],'classes_'):
                #     print ('sklModelPipeline.steps[-1][-1] >>> ',sklModelPipeline.steps[-1][-1])
                #     predClasses=sklModelPipeline.steps[-1][-1].classes_
                # else:
                try:
                    predClasses = self.getPredClasses(nyoka_pmml_obj)
                except:
                    predClasses = []
                print('Next Step 5 >>>>>>>>>>>>')
                targetVar = self.getTargetVariable(nyoka_pmml_obj)
                PMMLMODELSTORAGE[pmmlName] = {}
                PMMLMODELSTORAGE[pmmlName]['model'] = sklModelPipeline
                PMMLMODELSTORAGE[pmmlName]['predClasses'] = list(predClasses)
                PMMLMODELSTORAGE[pmmlName]['targetVar'] = targetVar
                PMMLMODELSTORAGE[pmmlName]['modelType'] = 'sklearnM'
                modelType = 'sklearnM'
            return (pmmlName, 'Success', modelType)
        except Exception as e:
            print(str(e))
            import traceback
            print(str(traceback.format_exc()))
            return (pmmlName, 'Failure', None)
Esempio n. 19
0
    def loadExecutionModel(self, pmmlFile):
        pmmlFileObj = pathlib.Path(pmmlFile)
        pmmlFileForKey = pmmlFileObj.name.replace(pmmlFileObj.suffix, '')
        from nyoka import PMML43Ext as ny
        pmmlObj = ny.parse(pmmlFile, silence=True)

        modelObj = []
        for inMod in modelObjectToCheck:
            if len(pmmlObj.__dict__[inMod]) > 0:
                modPMMLObj = pmmlObj.__dict__[inMod]
                if inMod == 'DeepNetwork':
                    for ininMod in modPMMLObj:
                        colInfo = self.getTargetAndColumnsName(ininMod)
                        modelObj.append({
                            'modelArchType': 'NNModel',
                            'pmmlModelObject': ininMod,
                            'recoModelObj': None,
                            'listOFColumns': None,
                            'targetCol': colInfo[1]
                        })
                else:
                    for ininMod in modPMMLObj:
                        colInfo = self.getTargetAndColumnsName(ininMod)
                        #                 recoModelObj=generateModelfromPMML(ininMod)
                        modelObj.append({
                            'modelArchType': 'SKLModel',
                            'pmmlModelObject': ininMod,
                            'recoModelObj': None,
                            'listOFColumns': colInfo[0],
                            'targetCol': colInfo[1]
                        })

        tempDict = {}
        tempDict['train'] = {}

        tempDict['score'] = {}

        for singMod in modelObj:
            if singMod['pmmlModelObject'].taskType == 'trainAndscore':
                tempDict['train'][singMod['pmmlModelObject'].modelName] = {}
                tempDict['train'][
                    singMod['pmmlModelObject'].modelName]['modelObj'] = singMod
                tempDict['train'][singMod['pmmlModelObject'].modelName][
                    'modelObj']['pmmlDdicObj'] = pmmlObj.DataDictionary
                tempDict['train'][singMod['pmmlModelObject'].modelName][
                    'modelObj']['pmmlNyokaObj'] = self.nyObjOfModel(
                        pmmlObj, singMod)
                tempDict['score'][singMod['pmmlModelObject'].modelName] = {}
                tempDict['score'][
                    singMod['pmmlModelObject'].modelName]['modelObj'] = singMod
                tempDict['score'][singMod['pmmlModelObject'].modelName][
                    'modelObj']['pmmlDdicObj'] = pmmlObj.DataDictionary
                tempDict['train'][singMod['pmmlModelObject'].modelName][
                    'modelObj']['pmmlNyokaObj'] = self.nyObjOfModel(
                        pmmlObj, singMod)
            else:
                tempDict[singMod['pmmlModelObject'].taskType][
                    singMod['pmmlModelObject'].modelName] = {}
                tempDict[singMod['pmmlModelObject'].taskType][
                    singMod['pmmlModelObject'].modelName]['modelObj'] = singMod
                tempDict[singMod['pmmlModelObject'].taskType][
                    singMod['pmmlModelObject'].modelName]['modelObj'][
                        'pmmlDdicObj'] = pmmlObj.DataDictionary
                tempDict[singMod['pmmlModelObject'].taskType][
                    singMod['pmmlModelObject'].
                    modelName]['modelObj']['pmmlNyokaObj'] = self.nyObjOfModel(
                        pmmlObj, singMod)

        tempDict2 = {}
        for taType in tempDict:
            tempTa = list(tempDict[taType].keys())
            tempTa.sort()

            for taTTemp in tempTa:
                if taType in tempDict2:
                    pass
                else:
                    tempDict2[taType] = {}
                tempDict2[taType][taTTemp] = tempDict[taType][taTTemp]

        tempDict = tempDict2.copy()

        for sc1 in pmmlObj.script:
            if sc1.scriptPurpose == 'trainAndscore':
                tempDict['train'][sc1.for_][sc1.class_] = {}
                tempDict['train'][sc1.for_][sc1.class_ +
                                            '_code'] = self.getCode(
                                                sc1.valueOf_)
                tempDict['train'][sc1.for_][
                    sc1.class_] = self.getCodeObjectToProcess(
                        self.getCode(sc1.valueOf_))
                tempDict['score'][sc1.for_][sc1.class_] = {}
                tempDict['score'][sc1.for_][sc1.class_ +
                                            '_code'] = self.getCode(
                                                sc1.valueOf_)
                tempDict['score'][sc1.for_][
                    sc1.class_] = self.getCodeObjectToProcess(
                        self.getCode(sc1.valueOf_))
            else:
                tempDict[sc1.scriptPurpose][sc1.for_][sc1.class_] = {}
                tempDict[sc1.scriptPurpose][sc1.for_][sc1.class_ +
                                                      '_code'] = self.getCode(
                                                          sc1.valueOf_)
                tempDict[sc1.scriptPurpose][sc1.for_][
                    sc1.class_] = self.getCodeObjectToProcess(
                        self.getCode(sc1.valueOf_))

        taskTypesName = list(tempDict.keys())
        listOfModelNames = set([k for j in tempDict for k in tempDict[j]])

        hyperParDict = {}
        for extObj in pmmlObj.MiningBuildTask.Extension:
            if extObj.name == 'hyperparameters':
                hyperParDict[extObj.for_] = ast.literal_eval(extObj.value)

        try:
            miningBuildTaskList = pmmlObj.MiningBuildTask.__dict__['Extension']
            for bTask in miningBuildTaskList:
                if bTask.__dict__['for_'] in listOfModelNames:
                    for tT in taskTypesName:
                        for modInd in listOfModelNames:
                            tempDict[tT][modInd]['modelObj'][
                                'miningExtension'] = bTask
        except:
            pass

        modelLoadStatus = []

        for taskT in tempDict:
            print(taskT)
            for mO in tempDict[taskT]:
                if tempDict[taskT][mO]['modelObj'][
                        'modelArchType'] == "NNModel":
                    modelProp = tempDict[taskT][mO]['modelObj']['pmmlNyokaObj']

                    model_graph = Graph()
                    with model_graph.as_default():
                        tf_session = Session()
                        with tf_session.as_default():
                            print('step 5')
                            from nyoka.reconstruct.pmml_to_pipeline_model import generate_skl_model
                            print('step 5.1')
                            model_net = generate_skl_model(modelProp)
                            print('step 5.2')
                            model = model_net.model
                            model_graph = tf.get_default_graph()
                            print('step 6')
                    inputShapevals = [
                        inpuShape.value
                        for inpuShape in list(model.input.shape)
                    ]
                    if str(model_net) != 'None':
                        tempDict[taskT][mO]['modelObj'][
                            'recoModelObj'] = model_net
                        tempDict[taskT][mO]['modelObj'][
                            'model_graph'] = model_graph
                        tempDict[taskT][mO]['modelObj'][
                            'tf_session'] = tf_session
                        tempDict[taskT][mO]['modelObj'][
                            'inputShape'] = inputShapevals
                        modelLoadStatus.append(1)
                    else:
                        modelLoadStatus.append(0)
                    try:
                        tempDict[taskT][mO]['modelObj'][
                            'hyperparameters'] = hyperParDict[mO]
                    except:
                        tempDict[taskT][mO]['modelObj'][
                            'hyperparameters'] = None
                elif tempDict[taskT][mO]['modelObj'][
                        'modelArchType'] == "SKLModel":
                    modelProp = tempDict[taskT][mO]['modelObj']['pmmlNyokaObj']
                    from nyoka.reconstruct.pmml_to_pipeline_model import generate_skl_model
                    recoModelObj = generate_skl_model(modelProp)
                    if recoModelObj != None:
                        tempDict[taskT][mO]['modelObj'][
                            'recoModelObj'] = recoModelObj
                        modelLoadStatus.append(1)
                    else:
                        modelLoadStatus.append(0)
                    try:
                        tempDict[taskT][mO]['modelObj'][
                            'hyperparameters'] = hyperParDict[mO]
                    except:
                        tempDict[taskT][mO]['modelObj'][
                            'hyperparameters'] = None

        # print('*'*100)

        # print(tempDict['score']['model2'])
        # print('*'*100)

        PMMLMODELSTORAGE[pmmlFileForKey] = tempDict
        if 0 in modelLoadStatus:
            messageToWorld = "Model load failed, please connect with admin"
        else:
            messageToWorld = "Model Loaded Successfully"

        resultResp = {'message': messageToWorld, 'keytoModel': pmmlFileForKey}
        return JsonResponse(resultResp, status=200)
Esempio n. 20
0
    def test_allTreeModels(self):
        model1 = GradientBoostingClassifier()
        model2 = GradientBoostingRegressor()
        model3 = DecisionTreeClassifier()

        sk_model1, feature_names1, target_name1 = iris_dataset_for_classification(
            model1)
        sk_model2, feature_names2, target_name2 = auto_dataset_for_regression(
            model2)
        sk_model3, feature_names3, target_name3 = iris_dataset_for_classification(
            model3)
        mining_imp_val = ()
        categoric_values = ()

        self.assertEqual(
            sklToPmml.get_node(sk_model3, feature_names3).__class__.__name__,
            pml.Node().__class__.__name__)

        self.assertEqual(
            sklToPmml.get_ensemble_models(
                sk_model1, feature_names1, feature_names1, target_name1,
                mining_imp_val, categoric_values)[0].get_functionName(),
            'classification')

        self.assertEqual(
            len(
                sklToPmml.get_ensemble_models(
                    sk_model1, feature_names1, feature_names1, target_name1,
                    mining_imp_val,
                    categoric_values)[0].get_MiningSchema().get_MiningField()),
            len(feature_names1) + 1)

        self.assertEqual(
            len(
                sklToPmml.get_ensemble_models(
                    sk_model1, feature_names1, feature_names1, target_name1,
                    mining_imp_val,
                    categoric_values)[0].get_MiningSchema().get_MiningField()),
            len(feature_names1) + 1)

        self.assertEqual(
            sklToPmml.get_ensemble_models(sk_model1, feature_names1,
                                          feature_names1, target_name1,
                                          mining_imp_val, categoric_values)
            [0].get_Segmentation().get_multipleModelMethod(), 'modelChain')

        self.assertEqual(
            len(
                sklToPmml.get_ensemble_models(
                    sk_model1, feature_names1, feature_names1, target_name1,
                    mining_imp_val,
                    categoric_values)[0].get_Segmentation().get_Segment()),
            len(model1.classes_) + 1)

        self.assertEqual(
            sklToPmml.get_outer_segmentation(
                sk_model2, feature_names2, feature_names2, target_name2,
                mining_imp_val, categoric_values).get_multipleModelMethod(),
            'sum')

        self.assertEqual(
            len(
                sklToPmml.get_inner_segments(sk_model2, feature_names2,
                                             feature_names2, 0)),
            model2.n_estimators)

        self.assertEqual(
            len(
                sklToPmml.get_segments_for_gbc(sk_model1, feature_names1,
                                               feature_names1, target_name1,
                                               mining_imp_val,
                                               categoric_values)),
            len(sk_model1.classes_) + 1)