Пример #1
0
    def test_model_converted_from_kaldi(self, mocker):
        mock = mocker.patch(
            'accuracy_checker.launcher.dlsdk_launcher.convert_model',
            return_value=('converted_model', 'converted_weights'))

        config = {
            'framework': 'dlsdk',
            'kaldi_model': '/path/to/source_models/custom_model',
            'device': 'cpu',
            '_models_prefix': '/path/to/source_models',
            'adapter': 'classification',
            'should_log_cmd': False
        }
        DLSDKLauncher(config)

        mock.assert_called_once_with('custom_model',
                                     '/path/to/source_models/custom_model',
                                     '',
                                     '',
                                     FrameworkParameters('kaldi', False), [],
                                     None,
                                     None,
                                     None,
                                     None,
                                     should_log_cmd=False)
Пример #2
0
    def test_model_converted_with_mo_flags(self, mocker):
        mock = mocker.patch(
            'accuracy_checker.launcher.dlsdk_launcher.convert_model',
            return_value=('converted_model', 'converted_weights'))

        config = {
            'framework': 'dlsdk',
            'caffe_model': '/path/to/source_models/custom_model',
            'caffe_weights': '/path/to/source_models/custom_weights',
            'device': 'cpu',
            'bitstream': Path('custom_bitstream'),
            '_models_prefix': '/path/to/source_models',
            'mo_flags': ['reverse_input_channels'],
            'adapter': 'classification',
            'should_log_cmd': False
        }

        DLSDKLauncher(config)

        mock.assert_called_once_with('custom_model',
                                     '/path/to/source_models/custom_model',
                                     '/path/to/source_models/custom_weights',
                                     '',
                                     FrameworkParameters('caffe', False), [],
                                     None, ['reverse_input_channels'],
                                     None,
                                     None,
                                     should_log_cmd=False)
Пример #3
0
    def test_model_converted_with_mo_params(self, mocker):
        mock = mocker.patch(
            'accuracy_checker.launcher.dlsdk_launcher.convert_model',
            return_value=('converted_model', 'converted_weights')
        )

        config = {
            'framework': "dlsdk",
            'caffe_model': '/path/to/source_models/custom_model',
            'caffe_weights': '/path/to/source_models/custom_weights',
            'device': 'cpu',
            'bitstream': Path('custom_bitstream'),
            'mo_params': {'data_type': 'FP16'},
            'adapter': 'classification',
            'should_log_cmd': False
        }
        DLSDKLauncher(config)

        mock.assert_called_once_with(
            'custom_model', '/path/to/source_models/custom_model', '/path/to/source_models/custom_weights', '',
            FrameworkParameters('caffe', False),
            [], {'data_type': 'FP16'}, None, None, None, None, should_log_cmd=False
        )
Пример #4
0
    def process() -> CalibrationConfiguration:
        args, unknown_args = CommandLineReader.parser().parse_known_args()
        if unknown_args:
            info("unknown command line arguments: {0}".format(unknown_args))

        if not args.simplified_mode:
            args.target_framework = "dlsdk"
            args.aocl = None

            merged_config, mode = ConfigReader.merge(args)
            updated_config = ConfigurationFilter.filter(
                merged_config, args.metric_name, args.metric_type,
                default_logger)

            if len(updated_config['models']) > 1:
                raise ValueError("too much models")

            if len(updated_config['models'][0]['launchers']) > 1:
                raise ValueError("too much launchers")

            launcher = updated_config['models'][0]['launchers'][0]
            if 'caffe_model' in launcher or 'tf_model' in launcher or 'tf_meta' in launcher or 'mxnet_weights' in launcher or 'onnx_model' in launcher:
                if args.converted_models:
                    tmp_directory = None
                else:
                    tmp_directory = tempfile.mkdtemp(".converted_models")
                    launcher['mo_params']['output_dir'] = tmp_directory

                if 'caffe_model' in launcher:
                    framework = FrameworkParameters('caffe', False)
                    output_model = Path.get_model(
                        str(launcher['caffe_model']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                    output_weights = Path.get_weights(
                        str(launcher['caffe_weights']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                elif 'tf_model' in launcher:
                    framework = FrameworkParameters('tf', False)
                    output_model = Path.get_model(
                        str(launcher['tf_model']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                    output_weights = Path.get_weights(
                        str(launcher['tf_model']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                elif 'tf_meta' in launcher:
                    framework = FrameworkParameters('tf', True)
                    output_model = Path.get_model(
                        str(launcher['tf_meta']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                    output_weights = Path.get_weights(
                        str(launcher['tf_meta']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                elif 'mxnet_weights' in launcher:
                    framework = FrameworkParameters('mxnet', False)
                    output_model = Path.get_model(
                        str(launcher['mxnet_weights']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                    output_weights = Path.get_weights(
                        str(launcher['mxnet_weights']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                elif 'onnx_model' in launcher:
                    framework = FrameworkParameters('onnx', False)
                    output_model = Path.get_model(
                        str(launcher['onnx_model']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                    output_weights = Path.get_weights(
                        str(launcher['onnx_model']), "_i8",
                        str(args.output_dir) if args.output_dir else None)
                else:
                    raise ValueError("unknown model framework")

                model, weights = DLSDKLauncher.convert_model(
                    launcher, framework)
                launcher['model'] = model
                launcher['weights'] = weights

                launcher.pop('caffe_model', None)
                launcher.pop('caffe_weights', None)
                launcher.pop('tf_model', None)
                launcher.pop('tf_meta', None)
                launcher.pop('mxnet_weights', None)
                launcher.pop('onnx_model', None)
            else:
                model = launcher['model']
                output_model = Path.get_model(
                    str(model), "_i8",
                    str(args.output_dir) if args.output_dir else None)
                weights = launcher['weights']
                output_weights = Path.get_weights(
                    str(weights), "_i8",
                    str(args.output_dir) if args.output_dir else None)
                tmp_directory = None

            batch_size = args.batch_size if args.batch_size else (
                launcher['batch'] if 'batch' in launcher else None)
            if not batch_size:
                with Network(str(launcher['model']),
                             str(launcher['weights'])) as network:
                    batch_size = network.ie_network.batch_size

            if 'cpu_extensions' in launcher:
                cpu_extension = DLSDKLauncher.get_cpu_extension(
                    launcher['cpu_extensions'], args.cpu_extensions_mode)
                launcher['cpu_extensions'] = cpu_extension
            else:
                cpu_extension = None

            if not args.calibrate_fully_connected:
                if args.ignore_layer_types is None:
                    args.ignore_layer_types = []
                args.ignore_layer_types.append("FullyConnected")

            return CalibrationConfiguration(
                config=updated_config,
                precision=args.precision,
                model=str(model),
                weights=str(weights),
                tmp_directory=tmp_directory,
                output_model=output_model,
                output_weights=output_weights,
                cpu_extension=str(cpu_extension) if cpu_extension else None,
                gpu_extension=str(launcher['gpu_extensions'])
                if 'gpu_extensions' in launcher else None,
                device=launcher['device'],
                batch_size=batch_size,
                threshold=args.threshold,
                ignore_layer_types=args.ignore_layer_types,
                ignore_layer_types_path=args.ignore_layer_types_path,
                ignore_layer_names=args.ignore_layer_names,
                ignore_layer_names_path=args.ignore_layer_names_path,
                benchmark_iterations_count=args.benchmark_iterations_count,
                progress=(None if args.progress == 'None' else args.progress),
                threshold_step=args.threshold_step,
                threshold_boundary=args.threshold_boundary,
                simplified_mode=args.simplified_mode)
        else:
            file_name = ntpath.basename(str(args.models))
            model = os.path.splitext(file_name)
            output_model = model[0] + "_i8"
            if args.output_dir:
                output_model = str(args.output_dir.joinpath(output_model))
            batch_size = args.batch_size if args.batch_size else 0
            precision = args.precision if args.precision.lower() in [
                'fp16', 'fp32'
            ] else ''
            return CalibrationConfiguration(
                config=args,
                precision=precision,
                model=str(args.models),
                weights=None,
                tmp_directory=None,
                output_model=output_model,
                output_weights=None,
                cpu_extension=str(args.extensions) if args.extensions else '',
                gpu_extension=None,
                device=args.target_devices,
                batch_size=batch_size,
                threshold=None,
                ignore_layer_types=None,
                ignore_layer_types_path=None,
                ignore_layer_names=None,
                ignore_layer_names_path=None,
                benchmark_iterations_count=None,
                progress=(None if args.progress == 'None' else args.progress),
                threshold_step=None,
                threshold_boundary=None,
                simplified_mode=args.simplified_mode)