Example #1
0
def optimize(config):
    """Creates pipeline of compression algorithms and optimize its parameters"""

    if logger.progress_bar_disabled:
        print_algo_configs(config.compression.algorithms)

    # load custom model
    model = load_model(config.model,
                       target_device=config.compression.target_device)

    data_loader = None
    # create custom data loader in case of custom Engine
    if config.engine.type != 'accuracy_checker':
        data_loader = create_data_loader(config.engine, model)

    engine = create_engine(config.engine, data_loader=data_loader, metric=None)

    pipeline = create_pipeline(config.compression.algorithms, engine, 'CLI')

    compressed_model = pipeline.run(model)

    if not config.model.keep_uncompressed_weights:
        compress_model_weights(compressed_model)

    save_model(compressed_model,
               os.path.join(config.model.exec_log_dir, 'optimized'),
               model_name=config.model.model_name)

    # evaluating compressed model if need
    if config.engine.evaluate:
        return pipeline.evaluate(compressed_model)

    return None
Example #2
0
def main():
    args = parse_args()
    out_dir = os.path.expanduser(args.output)
    if not os.path.isdir(out_dir):
        os.makedirs(out_dir)
    init_logger(level=args.log_level,
                file_name=os.path.join(out_dir, 'log.txt'))
    compressed_model = optimize_model(args)
    save_model(compressed_model, out_dir)
Example #3
0
    def run(self, model):
        """ this function applies quantization algorithm
         :param model: model to apply algo
         :return model with inserted and filled FakeQuantize nodes
         """
        activation_statistics = self._stats_collector.get_statistics_for_algorithm(
            self.name)
        model = fqut.get_quantized_model(model, self.create_stats_layout,
                                         activation_statistics,
                                         self.fill_fq_range, self._config)

        if self._config.get('dump_intermediate_model', False):
            save_model(model,
                       os.path.join(self._config.get('exec_log_dir'),
                                    'optimized'),
                       model_name=model.name)
        return model
Example #4
0
def test_per_channel_activations_for_depthwise(tmp_path, models, model_name,
                                               model_framework,
                                               hardware_config_path):
    model = models.get(model_name, model_framework, tmp_path)
    model = load_model(model.model_params)
    hardware_config = HardwareConfig.from_json(hardware_config_path.as_posix())
    model = GraphTransformer(hardware_config).insert_fake_quantize(model)
    fq_configurations = read_all_fake_quantize_configurations(
        ALGORITHM_CONFIG, hardware_config, model)
    ALGORITHM_CONFIG.preset = ALGORITHM_CONFIG.params.preset
    ALGORITHM_CONFIG.target_device = ALGORITHM_CONFIG.params.target_device
    fq_configuration = get_configurations_by_preset(ALGORITHM_CONFIG, model,
                                                    fq_configurations)
    fq_dw_names = [
        'Conv_4/WithoutBiases/fq_input_0', 'Conv_13/WithoutBiases/fq_input_0',
        'Conv_22/WithoutBiases/fq_input_0', 'Conv_32/WithoutBiases/fq_input_0',
        'Conv_41/WithoutBiases/fq_input_0', 'Conv_51/WithoutBiases/fq_input_0',
        'Conv_61/WithoutBiases/fq_input_0', 'Conv_70/WithoutBiases/fq_input_0',
        'Conv_80/WithoutBiases/fq_input_0', 'Conv_90/WithoutBiases/fq_input_0',
        'Conv_100/WithoutBiases/fq_input_0',
        'Conv_109/WithoutBiases/fq_input_0',
        'Conv_119/WithoutBiases/fq_input_0',
        'Conv_129/WithoutBiases/fq_input_0',
        'Conv_138/WithoutBiases/fq_input_0',
        'Conv_148/WithoutBiases/fq_input_0',
        'Conv_158/WithoutBiases/fq_input_0'
    ]
    dw_config = None
    for config_by_type in hardware_config:
        if config_by_type['type'] == 'DepthWiseConvolution':
            dw_config = config_by_type['quantization']['activations'][0]

    if not dw_config:
        raise Exception('DepthWise missing at hardware configuration')

    save_model(model, tmp_path.as_posix(), model_name)

    for fq_name in fq_configuration:
        if fq_name in fq_dw_names:
            fq_config = fq_configuration[fq_name]['activations']
            assert fq_config == dw_config
Example #5
0
def main():
    argparser = get_common_argparser()
    argparser.add_argument(
        '-a',
        '--annotation-file',
        help='File with Imagenet annotations in .txt format',
        required=True
    )

    # Steps 1-7: Model optimization
    args = argparser.parse_args()
    compressed_model, pipeline = optimize_model(args)

    # Step 8: Save the compressed model to the desired path.
    save_model(compressed_model, os.path.join(os.path.curdir, 'optimized'))

    # Step 9 (Optional): Evaluate the compressed model. Print the results.
    metric_results = pipeline.evaluate(compressed_model)
    if metric_results:
        for name, value in metric_results.items():
            print('{: <27s}: {}'.format(name, value))
Example #6
0
def test_sample_compression(_sample_params, tmp_path, models):
    model_name, model_framework, algorithm, preset, expected_accuracy, custom_mo_config = _sample_params

    # hack for sample imports because sample app works only from sample directory
    pot_dir = Path(__file__).parent.parent
    sys.path.append(str(pot_dir / 'sample'))
    # pylint: disable=C0415
    from openvino.tools.pot.api.samples.classification.classification_sample import optimize_model

    model = models.get(model_name,
                       model_framework,
                       tmp_path,
                       custom_mo_config=custom_mo_config)
    data_source, annotations = get_dataset_info('imagenet_1001_classes')

    args = Dict({
        'model': model.model_params.model,
        'dataset': data_source,
        'annotation_file': annotations['annotation_file']
    })

    model_, _ = optimize_model(args)

    paths = save_model(model_, tmp_path.as_posix(), model_name)
    model_xml = os.path.join(tmp_path.as_posix(), '{}.xml'.format(model_name))
    weights = os.path.join(tmp_path.as_posix(), '{}.bin'.format(model_name))

    assert os.path.exists(model_xml)
    assert os.path.exists(weights)

    algorithm_config = make_algo_config(algorithm, preset)
    engine_config = get_engine_config(model_name)
    config = merge_configs(model.model_params, engine_config, algorithm_config)
    config.engine = get_engine_config(model_name)

    metrics = evaluate(config=config, subset=range(1000), paths=paths)

    metrics = OrderedDict([(metric.name, np.mean(metric.evaluated_value))
                           for metric in metrics])

    for metric_name, metric_val in metrics.items():
        print('{}: {:.4f}'.format(metric_name, metric_val))
        if metric_name == 'accuracy@top1':
            assert {
                metric_name: metric_val
            } == pytest.approx(expected_accuracy, abs=0.006)
def run_algo(model, model_name, algorithm_config, tmp_path, reference_name):
    engine_config = get_engine_config(model_name)
    config = merge_configs(model.model_params, engine_config, algorithm_config)

    model = load_model(model.model_params)
    data_loader = create_data_loader(engine_config, model)
    engine = create_engine(engine_config, data_loader=data_loader, metric=None)
    pipeline = create_pipeline(algorithm_config.algorithms, engine)

    with torch.backends.mkldnn.flags(enabled=False):
        model = pipeline.run(model)
    paths = save_model(model, tmp_path.as_posix(), reference_name)
    engine.set_model(model)
    metrics = evaluate(config=config, subset=range(1000), paths=paths)
    metrics = OrderedDict([(metric.name, np.mean(metric.evaluated_value))
                           for metric in metrics])

    return metrics, model
Example #8
0
def main():
    parser = get_common_argparser()
    parser.add_argument('--mask-dir',
                        help='Path to the directory with segmentation masks',
                        required=True)

    args = parser.parse_args()
    if not args.weights:
        args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])

    model_config = Dict({
        'model_name': 'brain-tumor-segmentation-0002',
        'model': os.path.expanduser(args.model),
        'weights': os.path.expanduser(args.weights)
    })

    engine_config = Dict({
        'device': 'CPU',
        'stat_requests_number': 4,
        'eval_requests_number': 4
    })

    dataset_config = Dict({
        'data_source': os.path.expanduser(args.dataset),
        'mask_dir': os.path.expanduser(args.mask_dir),
        'modality_order': [1, 2, 3, 0],
        'size': (128, 128, 128)
    })

    algorithms = [{
        'name': 'DefaultQuantization',
        'params': {
            'target_device': 'ANY',
            'preset': 'performance',
            'stat_subset_size': 200
        }
    }]

    # Step 1: Load the model.
    model = load_model(model_config)

    # Step 2: Initialize the data loader.
    data_loader = BRATSDataLoader(dataset_config)

    # Step 3 (Optional. Required for AccuracyAwareQuantization): Initialize the metric.
    metric = DiceIndex(num_classes=4)

    # Step 4: Initialize the engine for metric calculation and statistics collection.
    engine = SegmentationEngine(config=engine_config,
                                data_loader=data_loader,
                                metric=metric)

    # Step 5: Create a pipeline of compression algorithms.
    pipeline = create_pipeline(algorithms, engine)

    # Step 6: Execute the pipeline.
    compressed_model = pipeline.run(model)

    # Step 7 (Optional):  Compress model weights to quantized precision
    #                     in order to reduce the size of final .bin file.
    compress_model_weights(compressed_model)

    # Step 8: Save the compressed model to the desired path.
    save_model(compressed_model, os.path.join(os.path.curdir, 'optimized'))

    # Step 9 (Optional): Evaluate the compressed model. Print the results.
    metric_results = pipeline.evaluate(compressed_model)
    if metric_results:
        for name, value in metric_results.items():
            print('{: <27s}: {}'.format(name, value))
Example #9
0
def main():
    parser = get_common_argparser()
    parser.add_argument('--annotation-path',
                        help='Path to the directory with annotation file',
                        required=True)
    args = parser.parse_args()
    if not args.weights:
        args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])

    model_config = Dict({
        'model_name': 'ssd_mobilenet_v1_fpn',
        'model': os.path.expanduser(args.model),
        'weights': os.path.expanduser(args.weights)
    })

    engine_config = Dict({'device': 'CPU'})

    dataset_config = Dict({
        'images_path':
        os.path.expanduser(args.dataset),
        'annotation_path':
        os.path.expanduser(args.annotation_path),
    })
    algorithms = [{
        'name': 'AccuracyAwareQuantization',
        'params': {
            'target_device': 'ANY',
            'preset': 'mixed',
            'stat_subset_size': 300,
            'maximal_drop': 0.004
        }
    }]

    # Step 1: Load the model.
    model = load_model(model_config)

    # Step 2: Initialize the data loader.
    data_loader = COCOLoader(dataset_config)
    # Step 3 (Optional. Required for AccuracyAwareQuantization): Initialize the metric.
    metric = MAP(91, data_loader.labels)

    # Step 4: Initialize the engine for metric calculation and statistics collection.
    engine = IEEngine(config=engine_config,
                      data_loader=data_loader,
                      metric=metric)

    # Step 5: Create a pipeline of compression algorithms.
    pipeline = create_pipeline(algorithms, engine)

    # Step 6: Execute the pipeline.
    compressed_model = pipeline.run(model)

    # Step 7 (Optional): Compress model weights to quantized precision
    #                    in order to reduce the size of final .bin file.
    compress_model_weights(compressed_model)

    # Step 8: Save the compressed model to the desired path.
    save_model(compressed_model, os.path.join(os.path.curdir, 'optimized'))

    # Step 9 (Optional): Evaluate the compressed model. Print the results.
    metric_results = pipeline.evaluate(compressed_model)
    if metric_results:
        for name, value in metric_results.items():
            print('{: <27s}: {}'.format(name, value))