[POT] Renaming NXModel (#10168)
* NXModel -> CompressedModel renaming * Update references & remove Dicts * Pylint fixes
This commit is contained in:
@@ -109,7 +109,7 @@ def compute_stats_layouts(config, model, qscheme=None):
|
||||
"""
|
||||
Compute stats layouts and hardware configuration
|
||||
:param config: dictionary with params algo section from toolkit config
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:return: configuration dictionary
|
||||
"""
|
||||
hardware_config = load_hardware_config(config)
|
||||
@@ -168,7 +168,7 @@ def compute_levels(fq_config, is_weights):
|
||||
def insert_fake_quantize_nodes(config, model, qscheme=None):
|
||||
""" Inserts fake quantize nodes, fill them according config
|
||||
:param config: dictionary with params algo section from toolkit config
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:param qscheme: The quantization scheme generated from the space
|
||||
:return None
|
||||
"""
|
||||
@@ -301,7 +301,7 @@ def get_quantized_model(model, create_stats_collector, activations_statistics,
|
||||
2. Calculate quantization config for FQ nodes
|
||||
3. Collect the weight stats based on config
|
||||
4. Calibrate [min, max] for inserted fq nodes
|
||||
:param model: original model (NXModel instance)
|
||||
:param model: original model (CompressedModel instance)
|
||||
:param create_stats_collector: functor to create function for stats collector callback
|
||||
:param activations_statistics: precomputed statistics for activations layers
|
||||
:param fill_fq_range: functor to generate min and max range for fake quantize node
|
||||
@@ -327,7 +327,7 @@ def get_quantized_model(model, create_stats_collector, activations_statistics,
|
||||
|
||||
def compute_weights_stats(model, stats_layout):
|
||||
""" Computes weights statistic from provided statistics layout
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:param stats_layout: dictionary with layer names as keys and
|
||||
functions list with rules how to compute statistics as values
|
||||
:return dictionary with layers names as keys and list of evaluated statistics as values"""
|
||||
@@ -392,7 +392,7 @@ def set_rescaling_factors(config, model, scaling_factor=2.0):
|
||||
for further rescaling of weights/FQs.
|
||||
Skip if target device is not CPU.
|
||||
:param config: algo config
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:param scaling_factor: rescaling factor for first convolution nodes
|
||||
"""
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ def read_all_fake_quantize_configurations(config, hardware_config, model):
|
||||
every fake quantize node based on toolkit config file and sub graph of every fake quantize node
|
||||
:param config: dictionary with compression section from toolkit config file
|
||||
:param hardware_config: dictionary with hardware config
|
||||
:param model: NXModel instance to quantize
|
||||
:param model: CompressedModel instance to quantize
|
||||
:return dictionary with fake quantize names as keys and
|
||||
list of corresponding configurations as values
|
||||
"""
|
||||
@@ -168,7 +168,7 @@ def add_range_estimator_configs(fq_to_hw_confs, config):
|
||||
def get_configurations_by_preset(config, model, fq_to_hw_confs):
|
||||
""" Choose fake quantize configuration by preset
|
||||
:param config: dictionary with params algo section from toolkit config
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:param fq_to_hw_confs: dictionary with fake quantize names as keys and
|
||||
list of its configurations as values (read_all_fake_quantize_configurations(..) return value)
|
||||
:return dictionary with fake quantize nodes names as keys and
|
||||
|
||||
@@ -67,7 +67,7 @@ class MinMaxQuantization(Algorithm):
|
||||
def __get_activations_statistics_layout(self, model):
|
||||
"""
|
||||
Compute statistics layout for activations
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:return: statistics layout in format {node_name: [stat_1, stat_2] .. }
|
||||
"""
|
||||
fake_quantize_config = fqut.compute_stats_layouts(self._config, model)
|
||||
@@ -81,7 +81,7 @@ class MinMaxQuantization(Algorithm):
|
||||
def create_stats_layout(fake_quantize_config, model, for_weights=True, inplace_statistics=True):
|
||||
""" Creates weights layout based on model and config dictionary
|
||||
:param fake_quantize_config: dictionary with fake quantize names as a key and its settings as values
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:param for_weights: whether statistic layout is calculated for weights or for activations.
|
||||
:return weights or activations statistics layout. Layout is a dictionary with layer name as
|
||||
key and dictionary with statistics {stats_name: stats_fn} as values
|
||||
|
||||
@@ -55,7 +55,7 @@ class RangeOptimization(OptimizationAlgorithm):
|
||||
|
||||
def _get_parameter_values(self, model):
|
||||
""" returns activation ranges from FakeQuantize nodes
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:return dictionary with FQ names as keys and ranges as values
|
||||
"""
|
||||
out = OrderedDict()
|
||||
|
||||
@@ -45,7 +45,7 @@ class TunableQuantization(MinMaxQuantization):
|
||||
def __get_activations_statistics_layout(self, model, qscheme=None):
|
||||
"""
|
||||
Compute statistics layout for activations
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
:return: statistics layout in format {node_name: [stat_1, stat_2] .. }
|
||||
"""
|
||||
fake_quantize_config = fqut.compute_stats_layouts(self._config, model, qscheme=qscheme)
|
||||
|
||||
@@ -112,7 +112,7 @@ The engine provides model inference, statistics collection for activations and c
|
||||
All subclasses should override the following methods:
|
||||
- `set_model(model)` - sets/resets a model.<br><br>
|
||||
*Parameters*
|
||||
- `model` - `NXModel` instance for inference (see details below).
|
||||
- `model` - `CompressedModel` instance for inference (see details below).
|
||||
|
||||
- `predict(stats_layout=None, sampler=None, metric_per_sample=False, print_progress=False)` - performs model inference
|
||||
on the specified subset of data.<br><br>
|
||||
@@ -201,15 +201,15 @@ Metric values returned by a `Metric` instance are expected to be in the format:
|
||||
|
||||
In order to implement a custom `Engine` class you may need to get familiar with the following interfaces:
|
||||
|
||||
### NXModel
|
||||
### CompressedModel
|
||||
|
||||
The Python* POT API provides the `NXModel` class as one interface for working with single and cascaded DL model.
|
||||
The Python* POT API provides the `CompressedModel` class as one interface for working with single and cascaded DL model.
|
||||
It is used to load, save and access the model, in case of the cascaded model, access each model of the cascaded model.
|
||||
|
||||
```
|
||||
class openvino.tools.pot.graph.nx_model.NXModel(**kwargs)
|
||||
class openvino.tools.pot.graph.nx_model.CompressedModel(**kwargs)
|
||||
```
|
||||
The NXModel class provides a representation of the DL model. A single model and cascaded model can be
|
||||
The CompressedModel class provides a representation of the DL model. A single model and cascaded model can be
|
||||
represented as an instance of this class. The cascaded model is stored as a list of models.
|
||||
|
||||
*Properties*
|
||||
@@ -230,15 +230,15 @@ openvino.tools.pot.graph.model_utils.load_model(model_config)
|
||||
|
||||
Example of `model_config` for a single model:
|
||||
```
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'mobilenet_v2',
|
||||
'model': '<PATH_TO_MODEL>/mobilenet_v2.xml',
|
||||
'weights': '<PATH_TO_WEIGHTS>/mobilenet_v2.bin'
|
||||
})
|
||||
}
|
||||
```
|
||||
Example of `model_config` for a cascaded model:
|
||||
```
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'mtcnn',
|
||||
'cascade': [
|
||||
{
|
||||
@@ -257,11 +257,11 @@ openvino.tools.pot.graph.model_utils.load_model(model_config)
|
||||
'weights': '<PATH_TO_WEIGHTS>/onet.bin'
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
*Returns*
|
||||
- `NXModel` instance
|
||||
- `CompressedModel` instance
|
||||
|
||||
#### Saving model to IR
|
||||
The Python* POT API provides the utility function to save model in the OpenVINO™ Intermediate Representation (IR):
|
||||
@@ -269,7 +269,7 @@ The Python* POT API provides the utility function to save model in the OpenVINO&
|
||||
openvino.tools.pot.graph.model_utils.save_model(model, save_path, model_name=None, for_stat_collection=False)
|
||||
```
|
||||
*Parameters*
|
||||
- `model` - `NXModel` instance.
|
||||
- `model` - `CompressedModel` instance.
|
||||
- `save_path` - path to save the model.
|
||||
- `model_name` - name under which the model will be saved.
|
||||
- `for_stat_collection` - whether model is saved to be used for statistic collection or for normal inference
|
||||
@@ -324,7 +324,7 @@ Pipeline class represents the optimization pipeline.
|
||||
*Parameters*
|
||||
- `engine` - instance of `Engine` class for model inference.
|
||||
|
||||
The pipeline can be applied to the DL model by calling `run(model)` method where `model` is the `NXModel` instance.
|
||||
The pipeline can be applied to the DL model by calling `run(model)` method where `model` is the `CompressedModel` instance.
|
||||
|
||||
#### Create a pipeline
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from addict import Dict
|
||||
|
||||
|
||||
class DataLoader(ABC):
|
||||
@@ -16,7 +17,7 @@ class DataLoader(ABC):
|
||||
""" Constructor
|
||||
:param config: data loader specific config
|
||||
"""
|
||||
self.config = config
|
||||
self.config = config if isinstance(config, Dict) else Dict(config)
|
||||
|
||||
@abstractmethod
|
||||
def __getitem__(self, index):
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from addict import Dict
|
||||
|
||||
from ..statistics.statistic_graph_builder import StatisticGraphBuilder
|
||||
|
||||
@@ -17,16 +18,16 @@ class Engine(ABC):
|
||||
:param data_loader: entity responsible for communication with dataset
|
||||
:param metric: entity for metric calculation
|
||||
"""
|
||||
self.config = config
|
||||
self.config = config if isinstance(config, Dict) else Dict(config)
|
||||
self._data_loader = data_loader
|
||||
self._metric = metric
|
||||
self._statistic_graph_builder = StatisticGraphBuilder()
|
||||
self._stat_requests_number = config.get('stat_requests_number', None)
|
||||
self._eval_requests_number = config.get('eval_requests_number', None)
|
||||
self._stat_requests_number = self.config.get('stat_requests_number', None)
|
||||
self._eval_requests_number = self.config.get('eval_requests_number', None)
|
||||
|
||||
def set_model(self, model):
|
||||
""" Set/reset model to instance of engine class
|
||||
:param model: NXModel instance for inference
|
||||
:param model: CompressedModel instance for inference
|
||||
"""
|
||||
|
||||
@property
|
||||
|
||||
@@ -6,7 +6,6 @@ import os
|
||||
import nibabel as nib
|
||||
import numpy as np
|
||||
from scipy.ndimage import interpolation
|
||||
from addict import Dict
|
||||
|
||||
from openvino.tools.pot import Metric, DataLoader, IEEngine, \
|
||||
load_model, save_model, compress_model_weights, create_pipeline
|
||||
@@ -23,8 +22,6 @@ class BRATSDataLoader(DataLoader):
|
||||
|
||||
# Required methods:
|
||||
def __init__(self, config):
|
||||
if not isinstance(config, Dict):
|
||||
config = Dict(config)
|
||||
super().__init__(config)
|
||||
self._img_ids = sorted(os.listdir(self.config.data_source))
|
||||
|
||||
@@ -246,24 +243,24 @@ def main():
|
||||
if not args.weights:
|
||||
args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'brain-tumor-segmentation-0002',
|
||||
'model': os.path.expanduser(args.model),
|
||||
'weights': os.path.expanduser(args.weights)
|
||||
})
|
||||
}
|
||||
|
||||
engine_config = Dict({
|
||||
engine_config = {
|
||||
'device': 'CPU',
|
||||
'stat_requests_number': 4,
|
||||
'eval_requests_number': 4
|
||||
})
|
||||
}
|
||||
|
||||
dataset_config = Dict({
|
||||
dataset_config = {
|
||||
'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 = [
|
||||
{
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import os
|
||||
import numpy as np
|
||||
from addict import Dict
|
||||
from cv2 import imread, resize as cv2_resize
|
||||
|
||||
from openvino.tools.pot import Metric, DataLoader, IEEngine, \
|
||||
@@ -20,10 +19,8 @@ init_logger(level='INFO')
|
||||
class ImageNetDataLoader(DataLoader):
|
||||
|
||||
def __init__(self, config):
|
||||
if not isinstance(config, Dict):
|
||||
config = Dict(config)
|
||||
super().__init__(config)
|
||||
self._annotations, self._img_ids = self._read_img_ids_annotations(config)
|
||||
self._annotations, self._img_ids = self._read_img_ids_annotations(self.config)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._img_ids)
|
||||
@@ -158,16 +155,16 @@ def get_configs(args):
|
||||
if not args.weights:
|
||||
args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'sample_model',
|
||||
'model': os.path.expanduser(args.model),
|
||||
'weights': os.path.expanduser(args.weights)
|
||||
})
|
||||
engine_config = Dict({
|
||||
}
|
||||
engine_config = {
|
||||
'device': 'CPU',
|
||||
'stat_requests_number': 4,
|
||||
'eval_requests_number': 4
|
||||
})
|
||||
}
|
||||
dataset_config = {
|
||||
'data_source': os.path.expanduser(args.dataset),
|
||||
'annotation_file': os.path.expanduser(args.annotation_file),
|
||||
|
||||
@@ -6,7 +6,6 @@ from argparse import ArgumentParser
|
||||
from functools import partial
|
||||
from time import time
|
||||
|
||||
from addict import Dict
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
@@ -27,8 +26,6 @@ class WiderFaceLoader(DataLoader):
|
||||
|
||||
# Required methods:
|
||||
def __init__(self, config):
|
||||
if not isinstance(config, Dict):
|
||||
config = Dict(config)
|
||||
super().__init__(config)
|
||||
self._min_height_ann = 60
|
||||
self._img_ids, self._annotations = self._read_image_ids_annotations(config.annotation_file)
|
||||
@@ -95,7 +92,7 @@ class MTCNNEngine(IEEngine):
|
||||
|
||||
def set_model(self, model):
|
||||
""" Loads NetworkX model into InferenceEngine and stores it in Engine class
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
"""
|
||||
# save graph to IR and use it to initialize IE Network
|
||||
self._model = self._set_model(model)
|
||||
@@ -322,7 +319,7 @@ def main():
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'mtcnn',
|
||||
'cascade': [
|
||||
{
|
||||
@@ -344,20 +341,20 @@ def main():
|
||||
args.onet_model.replace('.xml', '.bin'))
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
engine_config = Dict({
|
||||
engine_config = {
|
||||
'device': 'CPU',
|
||||
'outputs': {
|
||||
'probabilities': ['prob1', 'prob1', 'prob1'],
|
||||
'regions': ['conv4-2', 'conv5-2', 'conv6-2']
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
dataset_config = Dict({
|
||||
dataset_config = {
|
||||
'data_source': os.path.expanduser(args.dataset),
|
||||
'annotation_file': os.path.expanduser(args.annotation_file)
|
||||
})
|
||||
}
|
||||
|
||||
algorithms = [
|
||||
{
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import os
|
||||
|
||||
from addict import Dict
|
||||
|
||||
from openvino.tools.pot import IEEngine, load_model, save_model, compress_model_weights, create_pipeline
|
||||
from openvino.tools.pot.utils.logger import init_logger
|
||||
from openvino.tools.pot.api.samples.utils.argument_parser import get_common_argparser
|
||||
@@ -25,20 +23,20 @@ def main():
|
||||
if not args.weights:
|
||||
args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'ssd_mobilenet_v1_fpn',
|
||||
'model': os.path.expanduser(args.model),
|
||||
'weights': os.path.expanduser(args.weights)
|
||||
})
|
||||
}
|
||||
|
||||
engine_config = Dict({
|
||||
engine_config = {
|
||||
'device': 'CPU'
|
||||
})
|
||||
}
|
||||
|
||||
dataset_config = Dict({
|
||||
dataset_config = {
|
||||
'images_path': os.path.expanduser(args.dataset),
|
||||
'annotation_path': os.path.expanduser(args.annotation_path),
|
||||
})
|
||||
}
|
||||
algorithms = [
|
||||
{
|
||||
'name': 'AccuracyAwareQuantization',
|
||||
|
||||
@@ -6,7 +6,6 @@ import math
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from addict import Dict
|
||||
|
||||
from openvino.tools.pot import Metric, DataLoader, IEEngine, \
|
||||
load_model, save_model, compress_model_weights, create_pipeline
|
||||
@@ -33,8 +32,6 @@ class VOCSegmentationLoader(DataLoader):
|
||||
|
||||
# Required methods:
|
||||
def __init__(self, config):
|
||||
if not isinstance(config, Dict):
|
||||
config = Dict(config)
|
||||
super().__init__(config)
|
||||
self._image_size = config.image_size
|
||||
self._img_ids = self._read_img_ids(config)
|
||||
@@ -187,24 +184,24 @@ def main():
|
||||
if not args.weights:
|
||||
args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'deeplabv3',
|
||||
'model': os.path.expanduser(args.model),
|
||||
'weights': os.path.expanduser(args.weights)
|
||||
})
|
||||
}
|
||||
|
||||
engine_config = Dict({
|
||||
engine_config = {
|
||||
'device': 'CPU',
|
||||
'stat_requests_number': 4,
|
||||
'eval_requests_number': 4
|
||||
})
|
||||
}
|
||||
|
||||
dataset_config = Dict({
|
||||
dataset_config = {
|
||||
'data_source': os.path.expanduser(args.dataset),
|
||||
'mask_dir': os.path.expanduser(args.mask_dir),
|
||||
'imageset_file': os.path.expanduser(args.imageset_file),
|
||||
'image_size': 513
|
||||
})
|
||||
}
|
||||
|
||||
algorithms = [
|
||||
{
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
from addict import Dict
|
||||
|
||||
from openvino.tools.pot import load_model, save_model, create_pipeline
|
||||
from openvino.tools.pot.utils.logger import init_logger
|
||||
@@ -67,15 +66,15 @@ def get_configs(args):
|
||||
if not args.weights:
|
||||
args.weights = '{}.bin'.format(os.path.splitext(args.model)[0])
|
||||
|
||||
model_config = Dict({
|
||||
model_config = {
|
||||
'model_name': 'gna_model',
|
||||
'model': os.path.expanduser(args.model),
|
||||
'weights': os.path.expanduser(args.weights),
|
||||
'exec_log_dir': args.output
|
||||
})
|
||||
engine_config = Dict({
|
||||
}
|
||||
engine_config = {
|
||||
'device': 'CPU'
|
||||
})
|
||||
}
|
||||
dataset_config = {
|
||||
'data_source': os.path.expanduser(args.dataset),
|
||||
'type': 'simplified',
|
||||
|
||||
@@ -10,7 +10,7 @@ def create_data_loader(config, model):
|
||||
"""
|
||||
Factory to create instance of engine class based on config
|
||||
:param config: engine config section from toolkit config file
|
||||
:param model: NXModel instance to find out input shape
|
||||
:param model: CompressedModel instance to find out input shape
|
||||
:return: instance of DataLoader descendant class
|
||||
"""
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class ACEngine(Engine):
|
||||
|
||||
def set_model(self, model):
|
||||
""" Load NetworkX model into InferenceEngine and stores it in Engine class
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
"""
|
||||
def _set_model(path):
|
||||
tmp_model_name = 'tmp_model'
|
||||
|
||||
@@ -38,7 +38,7 @@ class IEEngine(Engine):
|
||||
|
||||
def set_model(self, model):
|
||||
""" Loads NetworkX model into InferenceEngine and stores it in Engine class
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
"""
|
||||
if model.is_cascade:
|
||||
raise Exception('Cascade models are not supported in current engine')
|
||||
@@ -48,8 +48,8 @@ class IEEngine(Engine):
|
||||
self._output_layers = [get_clean_name(output.get_node().friendly_name) for output in self._model.outputs]
|
||||
|
||||
def _set_model(self, model):
|
||||
"""Creates IENetwork instances from NetworkX models in NXModel.
|
||||
:param: model: NXModel instance
|
||||
"""Creates IENetwork instances from NetworkX models in CompressedModel.
|
||||
:param: model: CompressedModel instance
|
||||
:return: list of dictionaries:
|
||||
[
|
||||
{
|
||||
|
||||
@@ -6,21 +6,21 @@ from openvino.tools.mo.graph.graph import Node
|
||||
from openvino.tools.mo.middle.passes.infer import type_infer
|
||||
|
||||
from . import editor as ge, builder as gb
|
||||
from .nx_model import NXModel
|
||||
from .nx_model import CompressedModel
|
||||
from .passes import compress_weights
|
||||
from ..utils.utils import convert_output_key
|
||||
|
||||
|
||||
def load_model(model_config, target_device='ANY'):
|
||||
""" Loads model from specified path
|
||||
:return NXModel instance
|
||||
:return CompressedModel instance
|
||||
"""
|
||||
return NXModel(config=model_config, target_device=target_device)
|
||||
return CompressedModel(config=model_config, target_device=target_device)
|
||||
|
||||
|
||||
def save_model(model: NXModel, save_path, model_name=None, for_stat_collection=False):
|
||||
def save_model(model: CompressedModel, save_path, model_name=None, for_stat_collection=False):
|
||||
""" Save model as IR in specified path
|
||||
:param model: NXModel instance to save
|
||||
:param model: CompressedModel instance to save
|
||||
:param save_path: path to save the model
|
||||
:param model_name: name under which the model will be saved
|
||||
:param for_stat_collection: whether model is saved to be used
|
||||
@@ -52,16 +52,16 @@ def add_outputs(models, node_names):
|
||||
return outputs_per_model
|
||||
|
||||
|
||||
def compress_model_weights(model: NXModel):
|
||||
def compress_model_weights(model: CompressedModel):
|
||||
"""Apply transformations to save model weights to INT8."""
|
||||
for model_dict in model.models:
|
||||
compress_weights(model_dict['model'])
|
||||
|
||||
|
||||
# TODO: set recursively = True to enable subgraphs quantization
|
||||
def get_nodes_by_type(model: NXModel, types: list, recursively: bool = False):
|
||||
def get_nodes_by_type(model: CompressedModel, types: list, recursively: bool = False):
|
||||
""" Returns all nodes with type from types collection
|
||||
:param model: NXModel model
|
||||
:param model: CompressedModel model
|
||||
:param types: list of required types
|
||||
:param recursively: whether return all nodes from the model
|
||||
and each subgraph or only from the external model
|
||||
@@ -71,9 +71,9 @@ def get_nodes_by_type(model: NXModel, types: list, recursively: bool = False):
|
||||
for node in ge.get_nodes_by_type(model_dict['model'], types, recursively)]
|
||||
|
||||
|
||||
def get_node_by_name(model: NXModel, name: str) -> Node:
|
||||
def get_node_by_name(model: CompressedModel, name: str) -> Node:
|
||||
""" Returns node by name found in the graph and each subgraph
|
||||
:param model: NXModel model
|
||||
:param model: CompressedModel model
|
||||
:param name: name of the node
|
||||
:return node from model (of type Node or None if there's no such node)
|
||||
"""
|
||||
@@ -87,9 +87,9 @@ def get_node_by_name(model: NXModel, name: str) -> Node:
|
||||
|
||||
|
||||
# TODO: set recursively = True to enable subgraphs quantization
|
||||
def get_all_operation_nodes(model: NXModel, recursively: bool = False):
|
||||
def get_all_operation_nodes(model: CompressedModel, recursively: bool = False):
|
||||
""" Returns sequence of all nodes in all graphs
|
||||
:param model: NXModel model
|
||||
:param model: CompressedModel model
|
||||
:param recursively: whether return all nodes from the model
|
||||
and each subgraph or only from the external model
|
||||
:return list of all nodes
|
||||
@@ -100,16 +100,16 @@ def get_all_operation_nodes(model: NXModel, recursively: bool = False):
|
||||
|
||||
def build_model_for_node(nx_model, input_name, input_shape, node, remove_bias=False,
|
||||
remove_fake_quantize=False, target_device='ANY'):
|
||||
""" Build Model containing Subgraph of NXModel (input - node - output).
|
||||
""" Build Model containing Subgraph of CompressedModel (input - node - output).
|
||||
The Convolution, FullyConnected node types are supported.
|
||||
:param nx_model: NXModel model
|
||||
:param nx_model: CompressedModel model
|
||||
:param input_name: name of the input node in the generated graph
|
||||
:param input_shape: shape of the input node in the generated graph
|
||||
:param node: node for which graph (input - node - output) will be generated
|
||||
:param remove_bias: remove bias in the generated graph
|
||||
:param remove_fake_quantize: remove fake quantize nodes in the generated graph
|
||||
:param target_device: device for processing
|
||||
:return: generated NXModel instance.
|
||||
:return: generated CompressedModel instance.
|
||||
"""
|
||||
candidates = [model_dict['model'] for model_dict in nx_model.models
|
||||
if ge.get_node_by_name(model_dict['model'], input_name)]
|
||||
@@ -117,12 +117,12 @@ def build_model_for_node(nx_model, input_name, input_shape, node, remove_bias=Fa
|
||||
raise RuntimeError('Name collision: {}'.format(input_name))
|
||||
model = candidates[0]
|
||||
op_graph = gb.build_graph_for_node(model, input_name, input_shape, node, remove_bias, remove_fake_quantize)
|
||||
return NXModel(graph=op_graph, target_device=target_device)
|
||||
return CompressedModel(graph=op_graph, target_device=target_device)
|
||||
|
||||
|
||||
def models_union(first_model, second_model):
|
||||
""" Return the union of NXModel models
|
||||
:return NXModel instance - union of first_model and second_model
|
||||
""" Return the union of CompressedModel models
|
||||
:return CompressedModel instance - union of first_model and second_model
|
||||
"""
|
||||
union = first_model
|
||||
union_models = union.models
|
||||
@@ -135,7 +135,7 @@ def models_union(first_model, second_model):
|
||||
return union
|
||||
|
||||
def nx_type_infer(model):
|
||||
""" Apply type_infer for each model in NXModel wrapper
|
||||
""" Apply type_infer for each model in CompressedModel wrapper
|
||||
"""
|
||||
for model_dict in model.models:
|
||||
type_infer(model_dict['model'])
|
||||
|
||||
@@ -15,7 +15,7 @@ from openvino.tools.pot.utils.logger import get_logger, stdout_redirect
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class NXModel:
|
||||
class CompressedModel:
|
||||
"""
|
||||
Class encapsulating the logic of graph operations handling
|
||||
for multiple NetworkX models (Model Optimizer representation).
|
||||
@@ -47,8 +47,7 @@ class NXModel:
|
||||
ge.add_fullname_for_nodes(model['model'])
|
||||
|
||||
def _from_config(self, model_config, target_device='ANY'):
|
||||
if not isinstance(model_config, Dict):
|
||||
model_config = Dict(model_config)
|
||||
model_config = model_config if isinstance(model_config, Dict) else Dict(model_config)
|
||||
if model_config.cascade:
|
||||
for model_dict in model_config.cascade:
|
||||
model_config_ = model_config.deepcopy()
|
||||
|
||||
@@ -49,7 +49,7 @@ class StatisticsCollector:
|
||||
def compute_statistics(self, model):
|
||||
"""
|
||||
Compute statistics for registered statistics
|
||||
:param model: NXModel instance
|
||||
:param model: CompressedModel instance
|
||||
"""
|
||||
# checks that statistics layouts for registered algorithms are not empty
|
||||
if all([not stats_layout for stats_layout in self._layout_by_algo.values()]):
|
||||
|
||||
@@ -26,7 +26,7 @@ class IELauncher:
|
||||
|
||||
def set_model(self, model, output_names=None, md_shapes=None):
|
||||
""" Set/reset model to instance of engine class
|
||||
:param model: NXModel instance for inference
|
||||
:param model: CompressedModel instance for inference
|
||||
"""
|
||||
if model.is_cascade:
|
||||
raise Exception('Cascade models are not supported in current launcher')
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:d8c2f05f2c5e1f15e7ba34d11db1533fc2728512672edf741bf9458722fdc10e
|
||||
size 400700
|
||||
oid sha256:4d7c7d92edd844993b787195b4736fbe7204c99ef46782a0aad86c05fd674ca7
|
||||
size 430170
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:9ec67ca9c35448b2d6043ae007fbe40d0ecc7ccc2073a6a62f910a957d6d29f3
|
||||
size 138666
|
||||
oid sha256:0b1aba0f47e6ddfa79e50ee881ec35c892ddfa4a0f9f1a52d857c8d145a060f1
|
||||
size 168136
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:a756a350fdceae199a797bcf254f8c7b6ec73976367f4654635fe42a26ef5039
|
||||
size 48284
|
||||
oid sha256:4cbb5506a2aa95c3dfdba0f2e6f3c7a69eb4879504d499077805718f280834f1
|
||||
size 48464
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:5d6d0149bb449eaac1c4ffb01394d497464f411277366d708199a963ab91dc99
|
||||
size 30173
|
||||
oid sha256:4087cdc57c73e461d6341f1493f1c6dbe87350cab8477942aea3b4fba591f735
|
||||
size 30334
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:eaf9864e73d4dba5b98632a00f338a853158ee207a9eb0ab9fe39308c03cd189
|
||||
size 27879
|
||||
oid sha256:66cd71d8f24f840b8840223a811deab944ea54e25398befba9498ac0daed1da1
|
||||
size 28059
|
||||
|
||||
@@ -10,7 +10,7 @@ from openvino.tools.mo.utils.ir_reader.restore_graph import restore_graph_from_i
|
||||
|
||||
from openvino.tools.pot.app.run import optimize
|
||||
from openvino.tools.pot import MagnitudeSparsity
|
||||
from openvino.tools.pot.graph.nx_model import NXModel
|
||||
from openvino.tools.pot.graph.nx_model import CompressedModel
|
||||
from openvino.tools.pot.graph.node_utils import get_node_value
|
||||
from openvino.tools.pot.utils.logger import stdout_redirect
|
||||
from tests.utils.config import get_engine_config, merge_configs
|
||||
@@ -64,7 +64,7 @@ def test_sparsity_algo(test_models, tmp_path, models):
|
||||
output_model, meta = stdout_redirect(restore_graph_from_ir, xml_path, bin_path)
|
||||
output_model.meta_data = meta
|
||||
|
||||
assert check_sparsity_level(NXModel(graph=output_model), config, sparsity_level)
|
||||
assert check_sparsity_level(CompressedModel(graph=output_model), config, sparsity_level)
|
||||
check_graph(tmp_path, output_model, model_name + ref_name, model_framework, check_weights=True)
|
||||
|
||||
TEST_SPARSITY_MODELS = [
|
||||
@@ -117,4 +117,4 @@ def test_sparsity(test_models, tmp_path, models):
|
||||
|
||||
# Check resulting sparsity level
|
||||
model, _ = stdout_redirect(restore_graph_from_ir, xml_path, bin_path)
|
||||
assert check_sparsity_level(NXModel(graph=model), config, sparsity_level)
|
||||
assert check_sparsity_level(CompressedModel(graph=model), config, sparsity_level)
|
||||
|
||||
Reference in New Issue
Block a user