Esempio n. 1
0
def get_1st_feature_extractor_tensors(
        detection_graph: tf.Graph) -> (dict, dict):
    allDict = {}
    allDictShape = {}
    currentBlockID = 0
    currentUnitID = 0
    convNum = 1

    for i, n in enumerate(detection_graph.as_graph_def().node):
        if "FirstStageFeatureExtractor" in n.name:
            if (CONV_CST in n.name.lower()
                    or SHORTCUT_CST in n.name) and n.name.endswith('weights'):
                blockFind = n.name.find(BLOCK_CST)
                tensor = getTensor(detection_graph, n.name)
                if blockFind == -1:
                    allDict[CONV_CST + str(convNum)] = tensor
                    allDictShape[CONV_CST + str(convNum)] = tensor.shape
                else:
                    blockID, unitID, convID = getBUC(n.name)
                    if blockID != currentBlockID:
                        allDict[blockID] = {}
                        allDictShape[blockID] = {}
                        currentBlockID = blockID
                        currentUnitID = 0
                    if unitID != currentUnitID:
                        allDict[blockID][unitID] = {}
                        allDictShape[blockID][unitID] = {}
                        currentUnitID = unitID
                    if SHORTCUT_CST in n.name:
                        convID = SHORTCUT_CST
                    allDict[blockID][unitID][convID] = tensor
                    allDictShape[blockID][unitID][convID] = tensor.shape
    return allDict, allDictShape
Esempio n. 2
0
def get_2nd_feature_extractor_weights(detection_graph: tf.Graph,
                                      sess: tf.Session) -> dict:
    allDict = {}

    prevName = ""
    currentBlockID = 0
    currentUnitID = 0
    currentConvID = 0
    shortcut = False
    c2 = 0
    convNum = 1

    for i, n in enumerate(detection_graph.as_graph_def().node):
        if "SecondStageFeatureExtractor" in n.name:
            if (CONV_CST in n.name.lower()
                    or SHORTCUT_CST in n.name) and n.name.endswith('weights'):
                blockFind = n.name.find(BLOCK_CST)
                tensor = getTensor(detection_graph, n.name)
                weights = getWeightsByTensor(sess, tensor)
                if blockFind == -1:
                    allDict[n.name] = weights
                else:
                    blockID, unitID, convID = getBUC(n.name)
                    if blockID != currentBlockID:
                        allDict[blockID] = {}
                        currentBlockID = blockID
                        currentUnitID = 0
                    if unitID != currentUnitID:
                        allDict[blockID][unitID] = {}
                        currentUnitID = unitID
                    if SHORTCUT_CST in n.name:
                        convID = SHORTCUT_CST
                    allDict[blockID][unitID][convID] = weights
    return allDict
Esempio n. 3
0
def _run_tf_optimizer(config: ConfigProto, graph: tf.Graph,
                      signature_def: SignatureDef) -> GraphDef:
    """Run the TF optimizer ("grappler") on a graph"""
    graph_def = graph.as_graph_def()
    meta_graph = export_meta_graph(graph_def=graph_def, graph=graph)
    meta_graph.signature_def['not_used_key'].CopyFrom(signature_def)
    return tf_optimizer.OptimizeGraph(config, meta_graph)
Esempio n. 4
0
def get_1st_feature_extractor_bn(detection_graph: tf.Graph,
                                 sess: tf.Session) -> dict:
    bnDict = {}
    allDict = {}

    prevName = ""
    currentBlockID = 0
    currentUnitID = 0
    currentConvID = 0
    shortcut = False
    c2 = 0
    convNum = 1

    for i, n in enumerate(detection_graph.as_graph_def().node):
        if BN_CST in n.name:
            if (CONV_CST in n.name.lower() or SHORTCUT_CST in n.name):
                if n.name.endswith(BETA_CST):
                    bnDict[n.name] = getWeightsByName(detection_graph, n.name,
                                                      sess)
                elif n.name.endswith(GAMMA_CST):
                    bnDict[n.name] = getWeightsByName(detection_graph, n.name,
                                                      sess)
                elif n.name.endswith(MOVING_MEAN_CST):
                    bnDict[n.name] = getWeightsByName(detection_graph, n.name,
                                                      sess)
                elif n.name.endswith(MOVING_VAR_CST):
                    bnDict[n.name] = getWeightsByName(detection_graph, n.name,
                                                      sess)
    for k, v in bnDict.items():
        blockFind = k.find(BLOCK_CST)
        if k.endswith(BETA_CST):
            key = BETA_CST
        elif k.endswith(GAMMA_CST):
            key = GAMMA_CST
        elif k.endswith(MOVING_MEAN_CST):
            key = MOVING_MEAN_CST
        elif k.endswith(MOVING_VAR_CST):
            key = MOVING_VAR_CST

        if blockFind == -1:
            if not CONV_CST + str(convNum) in allDict:
                allDict[CONV_CST + str(convNum)] = {}
            allDict[CONV_CST + str(convNum)][key] = v
        else:
            blockID, unitID, convID = getBUC(k)
            if blockID != currentBlockID:
                allDict[blockID] = {}
                currentBlockID = blockID
                currentUnitID = 0
            if unitID != currentUnitID:
                allDict[blockID][unitID] = {}
                currentUnitID = unitID
            if SHORTCUT_CST in k:
                convID = SHORTCUT_CST
            if not convID in allDict[blockID][unitID]:
                allDict[blockID][unitID][convID] = {}
            allDict[blockID][unitID][convID][key] = v

    return allDict
Esempio n. 5
0
def get_by_name(graph: tf.Graph, name: str):
    """Return op in Graph with name or None if not found.

    Parameters
    ----------
    graph : tf.Graph
        A Tensorflow Graph

    Returns
    -------
    tf.Operation or None
    """
    for node in graph.as_graph_def().node:
        if node.name == name:
            return graph.as_graph_element(node.name)
    return None
Esempio n. 6
0
def _process_graph(graph: tf.Graph) -> List[str]:
    """
    Gets the list of the output nodes present in the graph for inference
    :return: list of node names
    """
    all_nodes = [x.name for x in graph.as_graph_def().node]
    print("############")
    print(all_nodes)
    nodes = [x for x in all_nodes if x in POSSIBLE_OUTPUT_NODES | MODEL_CONSTANTS]
    print("List of nodes to export for brain TODO(oleguer put name here)")
    print("############")
    print(nodes)
    print("############")
    for n in nodes:
        print("\t" + n)
    return nodes
Esempio n. 7
0
def get_all_graph_nodes_names(graph: tf.Graph) -> List[str]:
    return [n.name for n in graph.as_graph_def().node]
Esempio n. 8
0
def getComputationalCostInFlops(graph: tf.Graph,
                                keywords: List[Union[str, List]] = None,
                                exclude_keywords: bool = True) -> int:
    """Calculate the number of flops required to run the graph, while excluding any nodes that contain the supplied
    keywords.

    Parameters
    ----------
    graph : tf.Graph
        Tensorflow graph object to analyze.
    keywords : list(str or list)
        Select the nodes containing the specified keywords in the node names. An individual "keyword" item can be either a
        string, or a list of strings. When a list of strings is supplied as a keyword, the node is selected using a
        logical and operator, i.e., all the strings must be present in the node name. When `exclude_keywords` is
        set to `True`, the total flops returned excludes the flops used for the specified nodes.
        When `exclude_keywords` is set to `False`, the return value only contains the total flops used for the
        specified keywords. Default value of `None` returns the total flops for the entire computational graph.
    exclude_keywords : bool
        Whether to exclude (or include only) the specified keywords from the total flops counted.
    Returns
    -------
    flops : int
        Total number of flops required for one pass through the graph.
    Notes
    -----
    Calculates the flops using the estimates from `sopt.benchmarks.ops.tensorflow.flops_registry_custom`. The
    estimates used might not include all the operations used in the supplied graph.

    I went carefully through the nodes for the simple case of ePIE reconstruction, where I found that this function
    did actually correctly include all the relevant nodes.
    """

    from tensorflow.python.framework import graph_util
    import sopt.benchmarks.ops.tensorflow.flops_registry_custom
    from sopt.benchmarks.ops.tensorflow.graph_utils_custom import get_flops_for_node_list

    with graph.as_default():
        run_meta = tf.RunMetadata()
        opts = tf.profiler.ProfileOptionBuilder.float_operation()
        profile = tf.profiler.profile(run_meta=run_meta,
                                      cmd="scope",
                                      options=opts)
        flops_all_ops = profile.total_float_ops

    graph_def = graph.as_graph_def()

    if keywords is None:
        return flops_all_ops

    keywords_flops = 0

    def _checkWordListInName(word_or_list, name):
        if isinstance(word_or_list, str):
            return word_or_list in name

        for word in word_or_list:
            if not word in name:
                return False
        return True

    for word_or_list in keywords:
        keywords_nodes = [
            node for node in graph_def.node
            if _checkWordListInName(word_or_list, node.name)
        ]
        flops = get_flops_for_node_list(graph, keywords_nodes)
        keywords_flops += flops

    return_flops = flops_all_ops - keywords_flops if exclude_keywords else keywords_flops
    return return_flops