Skip to content

Commit

Permalink
Quantization with openvino_2021.1 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
likholat committed Oct 16, 2020
1 parent c0f76f6 commit 62c5d1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ python3 evaluate.py --engine opvn --xml resnet_v2_101_299_opt.xml --dataset ILSV
To quantizes the model to int8 model run:

```bash
python3 calibration.py --xml resnet_v2_101_299_opt.xml --data ILSVRC2012_img_val --annotation ILSVRC2012_img_val/val.txt
python3 quantize.py --xml resnet_v2_101_299_opt.xml --data ILSVRC2012_img_val --annotation ILSVRC2012_img_val/val.txt
```

This script created ```/model/optimised``` folder in current folder with quantized model.
Expand Down
38 changes: 27 additions & 11 deletions calibration.py → quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from addict import Dict
from compression.graph import load_model, save_model
from compression.data_loaders.data_loader import DataLoader
from compression.api.data_loader import DataLoader
from compression.engines.ie_engine import IEEngine
from compression.custom.metric import Metric
from compression.api.metric import Metric
from compression.pipeline.initializer import create_pipeline

parser = argparse.ArgumentParser(description="Quantizes OpenVino model to int8.",
Expand Down Expand Up @@ -111,39 +111,55 @@ def higher_better(self):
def get_attributes(self):
return {}

# Dictionary with the FP32 model info
model_config = Dict({
'model_name': argv.model_name,
"model": argv.xml,
"weights": argv.xml.split('.xml')[0] + '.bin'
})
engine_config = {

# Dictionary with the engine parameters
engine_config = Dict({
'device': 'CPU',
'stat_requests_number': 4,
'eval_requests_number': 4
}
dataset_config = {
})

# Dictionary witn input dataset info
dataset_config = Dict({
'data_source': argv.data,
'annotation_file': argv.annotation,
}
})

# Quantization algorithm settings
algorithms = [
{
'name': 'DefaultQuantization',
'name': 'DefaultQuantization', # Optimization algorithm name
'params': {
'target_device': 'CPU',
'preset': 'performance',
'stat_subset_size': 300
'preset': 'performance', # Preset [performance (default), accuracy] which controls the quantization mode
# (symmetric and asymmetric respectively)
'stat_subset_size': 300 # Size of subset to calculate activations statistics that can be used
# for quantization parameters calculation.
}
}
]

# Load the model.
model = load_model(model_config)

# Initialize the data loader and metric.
data_loader = DatasetsDataLoader(dataset_config)
metric = AccuracyMetric()

loss = None
engine = IEEngine(engine_config, data_loader, metric, loss)
# Initialize the engine for metric calculation and statistics collection.
engine = IEEngine(engine_config, data_loader, metric)

# Initialize the engine for metric calculation and statistics collection.
pipeline = create_pipeline(algorithms, engine)

# Execute the pipeline.
compressed_model = pipeline.run(model)

# Save the compressed model.
save_model(compressed_model, argv.int8_dir)

0 comments on commit 62c5d1f

Please sign in to comment.