コード例 #1
0
def predictor_from_cntk_model_using_new_engine(modelFile, plotModel=True):
    """
    Loads a CNTK model and returns an ell.neural.NeuralNetworkPredictor
    """

    _logger = logger.get()
    _logger.info("Loading...")
    z = load_model(modelFile)
    _logger.info("\nFinished loading.")

    if plotModel:
        filename = os.path.join(os.path.dirname(modelFile), os.path.basename(modelFile) + ".svg")
        cntk_utilities.plot_model(z, filename)

    try:
        _logger.info("Pre-processing...")
        # Get the relevant nodes from CNTK that make up the model
        importer_nodes = cntk_utilities.Utilities.get_model_nodes(z)
        _logger.info("\nFinished pre-processing.")

        # Create an ImporterModel from the CNTK nodes
        importer_model = import_nodes(importer_nodes)
        # Use the common importer engine to drive conversion of the
        # ImporterModel to ELL layers
        importer_engine = common.importer.ImporterEngine()
        ell_layers = importer_engine.convert(importer_model)
        # Create an ELL neural network predictor from the layers
        predictor = ell.neural.NeuralNetworkPredictor(ell_layers)
    except BaseException as exception:
        _logger.error("Error occurred attempting to convert cntk layers to ELL layers: " + str(exception))
        raise exception

    return predictor
コード例 #2
0
def predictor_from_cntk_model(modelFile, plotModel=False):
    """Loads a CNTK model and returns an ell.neural.NeuralNetworkPredictor"""

    _logger = logger.get()
    _logger.info("Loading...")
    z = load_model(modelFile)
    _logger.info("\nFinished loading.")

    if plotModel:
        filename = os.path.join(os.path.dirname(modelFile), os.path.basename(modelFile) + ".svg")
        cntk_utilities.plot_model(z, filename)

    _logger.info("Pre-processing...")
    modelLayers = cntk_utilities.get_model_layers(z)

    # Get the relevant CNTK layers that we will convert to ELL
    layersToConvert = cntk_layers.get_filtered_layers_list(modelLayers)
    _logger.info("\nFinished pre-processing.")

    predictor = None

    try:
        # Create a list of ELL layers from the CNTK layers
        ellLayers = cntk_layers.convert_cntk_layers_to_ell_layers(
            layersToConvert)
        # Create an ELL neural network predictor from the layers
        predictor = ell.neural.NeuralNetworkPredictor(ellLayers)
    except BaseException as exception:
        _logger.error("Error occurred attempting to convert cntk layers to ELL layers: " + str(exception))
        raise exception

    return predictor
コード例 #3
0
def map_from_cntk_model_using_new_engine(modelFile, step_interval_msec=None, lag_threshold_msec=None, plot_model=False,
                                         verify_model={"audio": False, "vision": False}):
    """
    Loads a CNTK model and returns an ell.model.Map
    """

    _logger = logger.get()
    _logger.info("Loading...")
    cntk_model = load_model(modelFile)
    _logger.info("\nFinished loading.")

    if plot_model:
        filename = os.path.join(os.path.dirname(modelFile), os.path.basename(modelFile) + ".svg")
        cntk_utilities.plot_model(cntk_model, filename)

    try:
        _logger.info("Pre-processing...")
        # Get the relevant nodes from CNTK that make up the model
        cntk_nodes = cntk_utilities.Utilities.get_model_nodes(cntk_model)
        _logger.info("\nFinished pre-processing.")

        # Create an ImporterModel from the CNTK nodes
        importer_model = import_nodes(cntk_nodes)
        # Use the common importer engine to drive conversion of the
        # ImporterModel to ELL layers
        importer_engine = common.importer.ImporterEngine(step_interval_msec=step_interval_msec,
                                                         lag_threshold_msec=lag_threshold_msec)
        ell_map = importer_engine.convert_nodes(importer_model)

    except BaseException as exception:
        _logger.error("Error occurred attempting to convert cntk layers to ELL model: " + str(exception))
        raise exception

    if verify_model["audio"]:
        _logger.info("Verification of audio models is not supported at this time, skipping verification")
    elif verify_model["vision"]:
        testing_info = {"apply_softmax": False}
        try:
            ordered_importer_nodes, node_mapping = importer_engine.get_importer_node_to_ell_mapping()
            # RESTORE:
            # verify_ell_nodes_in_vision_model(ell_map, cntk_model, cntk_nodes, ordered_importer_nodes, node_mapping,
            # testing_info)
            verify_compiled_ell_nodes_in_vision_model(modelFile, cntk_model, cntk_nodes, ordered_importer_nodes,
                                                      verify_model=verify_model)
            verify_ell_output_in_vision_model(ell_map, cntk_model, testing_info)
        except BaseException as exception:
            _logger.error("Error occurred verifying imported model")
            basename, ext = os.path.splitext(modelFile)
            ell_map.Save(basename + ".ell.verification_failed")
            raise exception

    return ell_map