<reponame>devkicks/TensorflowModelAnalysis
"""
Created on Mon Mar 12 18:00:27 2018

Helper libraries for analysing a tensorflow network

- The functions here require end_points as well as a tensorflow graph

@author: muhammad.asad
"""

import tensorflow as tf
import os
modelParamsFolder = "model_params_analysis"

if not os.path.exists(modelParamsFolder):
    os.makedirs(modelParamsFolder)

def convertTensorNameAndShapeToText(inTensor):
    outStr = inTensor.name+','
    
    # get the tensor shape
    tShape = inTensor.shape
    
    mulVal = 1 # container for parameters multiplier value
    
    # iterate through the shape and add them to the outStr
    for i in range(len(tShape)):
        cVal = tShape[i].value
        
        # check for None and replace with -1
        if cVal is None:
            cVal = 1
        
        #print(cVal)
        outStr += str(cVal)
        mulVal *= cVal
        # check if not the last element - if not then add a ,
#        if(i != len(tShape)-1):
        outStr += ','
#    print(outStr)
    outStr += str(mulVal)
    return outStr

def convertEndpointNameAndShapeToText(inName, inShape):
    outStr = inName+','
    mulVal = 1 # container for parameters multiplier value
    # iterate through the shape and add them to the outStr
    for i in range(len(inShape)):
        cVal = inShape[i]
        
        # check for None and replace with -1
        if cVal is None:
            cVal = 1
        
        #print(cVal)
        outStr += str(cVal)
        mulVal *= cVal
        # check if not the last element - if not then add a ,
#        if(i != len(inShape)-1):
        outStr += ','
#    print(outStr)
    outStr += str(mulVal)
    return outStr

## save list of trainable parameters to a CSV file
## function using sess.graph._nodes_by_name.keys() and 
## sess.graph.get_tensor_by_name(name)
#def createCSVTrainParamsFromGraph(networkName, sess):    
#    networkParamsList = list()
#    networkParamsEndpoint = list()
#    
#    names =[n.name for n in tf.get_default_graph().as_graph_def().node]
#    
##    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
#    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
#        
#        tempStr = convertTensorNameAndShapeToText(variable)
#        networkParamsList.append(tempStr)
#        print(tempStr)
#        tempTensor = sess.graph.get_tensor_by_name(variable.name)
#        
##        sess = tf.get_default_session()
#        tempNumpy = tempTensor.eval(session=sess)
#        
#        tempStr = convertEndpointNameAndShapeToText(variable.name, tempNumpy.shape)
#        networkParamsEndpoint.append(tempStr)
#        print(tempStr)
#    
#    fileName = os.path.join(modelParamsFolder, networkName + '_Trainable' + '.csv')
#    with open(fileName, 'w') as f:
#        f.write("Model: " + networkName + "\n")
#        for item in networkParamsList:
#            f.write("%s\n" % item)           
#            

# Help regarding tf.GraphKeys
# https://www.tensorflow.org/api_docs/python/tf/GraphKeys
    
# save list of trainable parameters to a CSV file
def createCSVTrainParamsFromGraph(networkName):    
    networkParamsList = list()
#    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        tempStr = convertTensorNameAndShapeToText(variable)
        networkParamsList.append(tempStr)
    
    fileName = os.path.join(modelParamsFolder, networkName + '_Trainable' + '.csv')
    with open(fileName, 'w') as f:
        f.write("Model: " + networkName + "\n")
        for item in networkParamsList:
            f.write("%s\n" % item)           
            

# save list of model parameters to a CSV file      
def createCSVModelParamsFromEndpoints(networkName, end_points):    
    networkParamsList = list()
    for variable in end_points:
        tempStr = convertEndpointNameAndShapeToText(variable, end_points[variable].shape)
        networkParamsList.append(tempStr)
#        print(variable)
    
    fileName = os.path.join(modelParamsFolder, networkName + '_Endpoints' + '.csv')
    with open(fileName, 'w') as f:
        f.write("Model: " + networkName + "\n")
        for item in networkParamsList:
            f.write("%s\n" % item)            




"""
Created on Mon Mar 12 18:00:27 2018

Helper libraries for analysing a tensorflow network

- The functions here require end_points as well as a tensorflow graph

@author: muhammad.asad
"""

import tensorflow as tf
import os
modelParamsFolder = "model_params_analysis"

if not os.path.exists(modelParamsFolder):
    os.makedirs(modelParamsFolder)

def convertTensorNameAndShapeToText(inTensor):
    outStr = inTensor.name+','
    
    # get the tensor shape
    tShape = inTensor.shape
    
    mulVal = 1 # container for parameters multiplier value
    
    # iterate through the shape and add them to the outStr
    for i in range(len(tShape)):
        cVal = tShape[i].value
        
        # check for None and replace with -1
        if cVal is None:
            cVal = 1
        
        #print(cVal)
        outStr += str(cVal)
        mulVal *= cVal
        # check if not the last element - if not then add a ,
#        if(i != len(tShape)-1):
        outStr += ','
#    print(outStr)
    outStr += str(mulVal)
    return outStr

def convertEndpointNameAndShapeToText(inName, inShape):
    outStr = inName+','
    mulVal = 1 # container for parameters multiplier value
    # iterate through the shape and add them to the outStr
    for i in range(len(inShape)):
        cVal = inShape[i]
        
        # check for None and replace with -1
        if cVal is None:
            cVal = 1
        
        #print(cVal)
        outStr += str(cVal)
        mulVal *= cVal
        # check if not the last element - if not then add a ,
#        if(i != len(inShape)-1):
        outStr += ','
#    print(outStr)
    outStr += str(mulVal)
    return outStr

## save list of trainable parameters to a CSV file
## function using sess.graph._nodes_by_name.keys() and 
## sess.graph.get_tensor_by_name(name)
#def createCSVTrainParamsFromGraph(networkName, sess):    
#    networkParamsList = list()
#    networkParamsEndpoint = list()
#    
#    names =[n.name for n in tf.get_default_graph().as_graph_def().node]
#    
##    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
#    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
#        
#        tempStr = convertTensorNameAndShapeToText(variable)
#        networkParamsList.append(tempStr)
#        print(tempStr)
#        tempTensor = sess.graph.get_tensor_by_name(variable.name)
#        
##        sess = tf.get_default_session()
#        tempNumpy = tempTensor.eval(session=sess)
#        
#        tempStr = convertEndpointNameAndShapeToText(variable.name, tempNumpy.shape)
#        networkParamsEndpoint.append(tempStr)
#        print(tempStr)
#    
#    fileName = os.path.join(modelParamsFolder, networkName + '_Trainable' + '.csv')
#    with open(fileName, 'w') as f:
#        f.write("Model: " + networkName + "\n")
#        for item in networkParamsList:
#            f.write("%s\n" % item)           
#            

# Help regarding tf.GraphKeys
# https://www.tensorflow.org/api_docs/python/tf/GraphKeys
    
# save list of trainable parameters to a CSV file
def createCSVTrainParamsFromGraph(networkName):    
    networkParamsList = list()
#    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        tempStr = convertTensorNameAndShapeToText(variable)
        networkParamsList.append(tempStr)
    
    fileName = os.path.join(modelParamsFolder, networkName + '_Trainable' + '.csv')
    with open(fileName, 'w') as f:
        f.write("Model: " + networkName + "\n")
        for item in networkParamsList:
            f.write("%s\n" % item)           
            

# save list of model parameters to a CSV file      
def createCSVModelParamsFromEndpoints(networkName, end_points):    
    networkParamsList = list()
    for variable in end_points:
        tempStr = convertEndpointNameAndShapeToText(variable, end_points[variable].shape)
        networkParamsList.append(tempStr)
#        print(variable)
    
    fileName = os.path.join(modelParamsFolder, networkName + '_Endpoints' + '.csv')
    with open(fileName, 'w') as f:
        f.write("Model: " + networkName + "\n")
        for item in networkParamsList:
            f.write("%s\n" % item)
Created on Mon Mar 12 18:00:27 2018  Helper libraries for analysing a tensorflow network  - The functions here require end_points as well as a tensorflow graph  @author: muhammad.asad # get the tensor shape # container for parameters multiplier value # iterate through the shape and add them to the outStr # check for None and replace with -1 #print(cVal) # check if not the last element - if not then add a , #        if(i != len(tShape)-1): #    print(outStr) # container for parameters multiplier value # iterate through the shape and add them to the outStr # check for None and replace with -1 #print(cVal) # check if not the last element - if not then add a , #        if(i != len(inShape)-1): #    print(outStr) ## save list of trainable parameters to a CSV file ## function using sess.graph._nodes_by_name.keys() and ## sess.graph.get_tensor_by_name(name) #def createCSVTrainParamsFromGraph(networkName, sess): #    networkParamsList = list() #    networkParamsEndpoint = list() # #    names =[n.name for n in tf.get_default_graph().as_graph_def().node] # ##    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES): #    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES): # #        tempStr = convertTensorNameAndShapeToText(variable) #        networkParamsList.append(tempStr) #        print(tempStr) #        tempTensor = sess.graph.get_tensor_by_name(variable.name) # ##        sess = tf.get_default_session() #        tempNumpy = tempTensor.eval(session=sess) # #        tempStr = convertEndpointNameAndShapeToText(variable.name, tempNumpy.shape) #        networkParamsEndpoint.append(tempStr) #        print(tempStr) # #    fileName = os.path.join(modelParamsFolder, networkName + '_Trainable' + '.csv') #    with open(fileName, 'w') as f: #        f.write("Model: " + networkName + "\n") #        for item in networkParamsList: #            f.write("%s\n" % item) # # Help regarding tf.GraphKeys # https://www.tensorflow.org/api_docs/python/tf/GraphKeys # save list of trainable parameters to a CSV file #    for variable in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES): # save list of model parameters to a CSV file #        print(variable)