def test_useful_crops(self):
        graph = build_graph({'placeholder_in': {'kind': 'op', 'op': 'Parameter'},
                             'in_node': {'kind': 'data', 'shape': [1, 130]},
                             'crop1': {'kind': 'op', 'op': 'Crop', 'offset': 0, 'dim': 26, 'axis': -1},
                             'crop_data_1': {'kind': 'data', 'shape': [1, 26]},
                             'crop2': {'kind': 'op', 'op': 'Crop', 'offset': 26, 'dim': 26, 'axis': -1},
                             'crop_data_2': {'kind': 'data', 'shape': [1, 26]},
                             'crop4': {'kind': 'op', 'op': 'Crop', 'offset': 78, 'dim': 26, 'axis': -1},
                             'crop_data_4': {'kind': 'data', 'shape': [1, 26]},
                             'crop5': {'kind': 'op', 'op': 'Crop', 'offset': 104, 'dim': 26, 'axis': -1},
                             'crop_data_5': {'kind': 'data', 'shape': [1, 26]},
                             'concat': {'kind': 'op', 'op': 'Concat'},
                             'concat_data': {'kind': 'data', 'shape': [1, 104]},
                             'placeholder': {'kind': 'op', 'op': 'Parameter'},
                             },
                            [('placeholder_in', 'in_node'),
                             ('in_node', 'crop1'), ('crop1', 'crop_data_1'),
                             ('in_node', 'crop2'), ('crop2', 'crop_data_2'),
                             ('in_node', 'crop4'), ('crop4', 'crop_data_4'),
                             ('in_node', 'crop5'), ('crop5', 'crop_data_5'),
                             ('crop_data_1', 'concat'),
                             ('crop_data_2', 'concat'),
                             ('crop_data_4', 'concat'),
                             ('crop_data_5', 'concat'),
                             ('concat', 'concat_data'),
                             ('concat_data', 'placeholder')])

        RemoveUselessCropsPattern().find_and_replace_pattern(graph)
        ref_graph = build_graph({'placeholder_in': {'kind': 'op', 'op': 'Placeholder'},
                                 'in_node': {'kind': 'data', 'shape': [1, 130]},
                                 'crop1': {'kind': 'op', 'op': 'Crop', 'offset': 0, 'dim': 26, 'axis': -1},
                                 'crop_data_1': {'kind': 'data', 'shape': [1, 26]},
                                 'crop2': {'kind': 'op', 'op': 'Crop', 'offset': 26, 'dim': 26, 'axis': -1},
                                 'crop_data_2': {'kind': 'data', 'shape': [1, 26]},
                                 'crop4': {'kind': 'op', 'op': 'Crop', 'offset': 78, 'dim': 26, 'axis': -1},
                                 'crop_data_4': {'kind': 'data', 'shape': [1, 26]},
                                 'crop5': {'kind': 'op', 'op': 'Crop', 'offset': 104, 'dim': 26, 'axis': -1},
                                 'crop_data_5': {'kind': 'data', 'shape': [1, 26]},
                                 'concat': {'kind': 'op', 'op': 'Concat'},
                                 'concat_data': {'kind': 'data', 'shape': [1, 104]},
                                 'placeholder': {'kind': 'op', 'op': 'Placeholder'},
                                 },
                                [('placeholder_in', 'in_node'),
                                 ('in_node', 'crop1'), ('crop1', 'crop_data_1'),
                                 ('in_node', 'crop2'), ('crop2', 'crop_data_2'),
                                 ('in_node', 'crop4'), ('crop4', 'crop_data_4'),
                                 ('in_node', 'crop5'), ('crop5', 'crop_data_5'),
                                 ('crop_data_1', 'concat'),
                                 ('crop_data_2', 'concat'),
                                 ('crop_data_4', 'concat'),
                                 ('crop_data_5', 'concat'),
                                 ('concat', 'concat_data'),
                                 ('concat_data', 'placeholder')]
                                )
        (flag, resp) = compare_graphs(graph, ref_graph, 'placeholder')
        self.assertTrue(flag, resp)
Exemple #2
0
def driver(argv, input_model, output_model_name, output_dir):
    log_step(argv.steps, 'LOAD')
    meta_info = get_meta_info(argv)

    EltwiseChecker.enabled = False

    try:
        graph = load_kaldi_model(input_model)
    except Exception as e:
        raise Error('Model Optimizer is not able to parse Kaldi model {}. '.format(input_model) +
                    refer_to_faq_msg(91)) from e
    graph.check_empty_graph('load_kaldi_nnet_model')
    graph.graph['cmd_params'] = argv
    graph.graph['fw'] = 'kaldi'

    if graph.graph['cmd_params'].generate_experimental_IR_V10:
        version = 10
    else:
        version = 6
    graph.graph['ir_version'] = 2 if argv.generate_deprecated_IR_V2 else version

    update_extractors_with_extensions(kaldi_type_extractors)
    extract_node_attrs(graph, lambda node: kaldi_extractor(node))

    # --------------------------------- LOAD END ------------------------------------------------------
    log_step(argv.steps, 'FRONT')
    ReplaceLSTMNodePattern().find_and_replace_pattern(graph)
    class_registration.apply_replacements(graph, class_registration.ClassType.FRONT_REPLACER)
    log_step(argv.steps, 'MIDDLE')
    graph = partial_infer(graph)

    ReplacePNormNodePattern().find_and_replace_pattern(graph)
    ReplaceMemoryOffsetNodePattern().find_and_replace_pattern(graph)
    ReplaceMemoryOffsetWithMemoryNodePattern().find_and_replace_pattern(graph)
    RemoveMemoryDuplicationPattern().find_and_replace_pattern(graph)
    MergeNeighborSplicePattern().find_and_replace_pattern(graph)
    RemoveUselessCropsPattern().find_and_replace_pattern(graph)
    RemoveIdentity().find_and_replace_pattern(graph)
    graph_clean_up(graph)

    AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)

    ReplaceSpliceNodePattern().find_and_replace_pattern(graph)
    graph_clean_up(graph)

    # The order is intentional, firstly eliminate repeated, then remove redundant
    FuseRepeatedReshapes().find_and_replace_pattern(graph)
    EliminateRedundantReshape().find_and_replace_pattern(graph)
    graph_clean_up(graph)
    graph.check_empty_graph('partial_infer')
    if argv.counts:
        try:
            counts = read_counts_file(argv.counts)
        except Exception as e:
            raise Error('Model Optimizer is not able to read counts file {}'.format(argv.counts) +
                        refer_to_faq_msg(92)) from e

        apply_biases_to_last_layer(graph, counts)

    if argv.remove_output_softmax:
        RemoveLastSoftMaxPattern().find_and_replace_pattern(graph)
        graph_clean_up(graph)
        log.debug("After removing softmax")
        graph.print_graph_stat()

    log_step(argv.steps, 'BACK')
    LeakyReluToReluWithNegativeSlope().find_and_replace_pattern(graph)
    TransposeToPermute().find_and_replace_pattern(graph)
    DivideToEltwises().find_and_replace_pattern(graph)
    SubtractToEltwises().find_and_replace_pattern(graph)
    SimpleEltwiseToEltwiseOp().find_and_replace_pattern(graph)
    for_graph_and_each_sub_graph_recursively(graph, convert_matmul_to_fully_connected)

    # Intentionally after all transformations
    if argv.remove_memory:
        CutMemory().find_and_replace_pattern(graph)
        graph_clean_up(graph)
    ParameterToInput().find_and_replace_pattern(graph)

    KaldiRemoveMemoryOutputBackReplacementPattern().find_and_replace_pattern(graph)
    ForceStrictPrecision().find_and_replace_pattern(graph)
    remove_const_ops(graph)
    CreateConstNodesReplacement().find_and_replace_pattern(graph)

    remove_output_ops(graph)
    log_step(argv.steps, 'EMIT')
    prepare_emit_ir(graph, argv.data_type, output_dir, output_model_name, meta_info=meta_info)
    return 0