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('<', '<') 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
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