Exemplo n.º 1
0
    def test_convert_lenet(self, output):
        """Test LeNet script of the PyTorch convert to MindSpore script"""
        script_filename = "lenet_script.py"
        expect_filename = "lenet_converted.py"
        files_config = {
            'root_path': self.script_dir,
            'in_files': [os.path.join(self.script_dir, script_filename)],
            'outfile_dir': output,
            'report_dir': output
        }
        main(files_config)

        assert os.path.isfile(os.path.join(output, script_filename))

        with open(os.path.join(output, script_filename)) as converted_f:
            converted_source = converted_f.readlines()

        with open(os.path.join(self.script_dir, expect_filename)) as expect_f:
            expect_source = expect_f.readlines()

        diff = difflib.ndiff(converted_source, expect_source)
        diff_lines = 0
        for line in diff:
            if line.startswith('+'):
                diff_lines += 1

        converted_ratio = 100 - (diff_lines * 100) / (len(expect_source))
        assert converted_ratio >= 80
Exemplo n.º 2
0
def _run(in_files, out_dir, in_module, report):
    """
    Run converter command.

    Args:
        in_files (str): The file path or directory to convert.
        out_dir (str): The output directory to save converted file.
        in_module (str): The module name to convert.
        report (str): The report file path.
    """
    files_config = {
        'root_path': in_files if in_files else '',
        'in_files': [],
        'outfile_dir': out_dir,
        'report_dir': report,
        'in_module': in_module
    }
    if os.path.isfile(in_files):
        files_config['root_path'] = os.path.dirname(in_files)
        files_config['in_files'] = [in_files]
    else:
        for root_dir, _, files in os.walk(in_files):
            for file in files:
                files_config['in_files'].append(os.path.join(root_dir, file))
    main(files_config)
Exemplo n.º 3
0
def _run(in_files, model_file, shape, input_nodes, output_nodes, out_dir,
         report, project_path):
    """
    Run converter command.

    Args:
        in_files (str): The file path or directory to convert.
        model_file(str): The pytorch .pth to convert on graph based schema.
        shape(list): The input tensor shape of module_file.
        input_nodes(str): The input node(s) name of Tensorflow model, split by ','.
        output_nodes(str): The output node(s) name of Tensorflow model, split by ','.
        out_dir (str): The output directory to save converted file.
        report (str): The report file path.
        project_path(str): Pytorch scripts project path.
    """
    if in_files:
        files_config = {
            'root_path': in_files,
            'in_files': [],
            'outfile_dir': out_dir,
            'report_dir': report if report else out_dir
        }

        if os.path.isfile(in_files):
            files_config['root_path'] = os.path.dirname(in_files)
            files_config['in_files'] = [in_files]
        else:
            for root_dir, _, files in os.walk(in_files):
                for file in files:
                    files_config['in_files'].append(
                        os.path.join(root_dir, file))
        main(files_config)
        log_console.info("\n")
        log_console.info("MindConverter: conversion is completed.")
        log_console.info("\n")

    elif model_file:
        file_config = {
            'model_file': model_file,
            'shape': shape if shape else [],
            'input_nodes': input_nodes,
            'output_nodes': output_nodes,
            'outfile_dir': out_dir,
            'report_dir': report if report else out_dir
        }
        if project_path:
            paths = sys.path
            if project_path not in paths:
                sys.path.append(project_path)

        main_graph_base_converter(file_config)
        log_console.info("\n")
        log_console.info("MindConverter: conversion is completed.")
        log_console.info("\n")
    else:
        error_msg = "`--in_file` and `--model_file` should be set at least one."
        error = FileNotFoundError(error_msg)
        log.error(str(error))
        log.exception(error)
        raise error
Exemplo n.º 4
0
def _run(in_files, model_file, shape, input_nodes, output_nodes, out_dir,
         report):
    """
    Run converter command.

    Args:
        in_files (str): The file path or directory to convert.
        model_file (str): The model to convert on graph based schema.
        shape (Sequence[tuple]): The input tensor shape of the model.
        input_nodes (Sequence[str]): The input node(s) name of model.
        output_nodes (Sequence[str]): The output node(s) name of model.
        out_dir (str): The output directory to save converted file.
        report (str): The report file path.
    """
    if in_files:
        files_config = {
            'root_path': in_files,
            'in_files': [],
            'outfile_dir': out_dir,
            'report_dir': report if report else out_dir
        }
        if os.path.isfile(in_files):
            files_config['root_path'] = os.path.dirname(in_files)
            files_config['in_files'] = [in_files]
        else:
            for root_dir, _, files in os.walk(in_files):
                for file in files:
                    files_config['in_files'].append(
                        os.path.join(root_dir, file))
        main(files_config)
        log_console.info("MindConverter: conversion is completed.")

    elif model_file:
        file_config = {
            'model_file': model_file,
            'shape': shape if shape else [],
            'input_nodes': input_nodes,
            'output_nodes': output_nodes,
            'outfile_dir': out_dir,
            'report_dir': report if report else out_dir
        }
        main_graph_base_converter(file_config)
        log_console.info("MindConverter: conversion is completed.")
    else:
        error_msg = "`--in_file` and `--model_file` should be set at least one."
        error = FileNotFoundError(error_msg)
        log.error(str(error))
        log_console.error(str(error))
        sys.exit(-1)