Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_statistics(self, statistics: dict):
network_stats = {}
for layer_name, node_statistic in statistics.items():
network_stats[layer_name] = ie.LayerStats(min=tuple(node_statistic.min_outputs),
max=tuple(node_statistic.max_outputs))
self.ie_network.stats.update(network_stats)
debug("create network #{} for layer {} ({}) -> {} ({})".format(index, layer_to_clone.name, layer_to_clone.type, activation_layer.name, activation_layer.type))
else:
debug("create network #{} for layer {} ({})".format(index, layer_to_clone.name, layer_to_clone.type))
layer_network, reference_output_layer_name = self._normalizer.create_network_for_layer(
self._configuration.weights,
layer_to_clone,
layer_to_clone_info,
activation_layer)
Network.reshape(layer_network, self._configuration.batch_size)
network_stats = {}
# TODO: initialize only neccessary statistic
for layer_name, node_statistic in statistics.items():
network_stats[layer_name] = ie.LayerStats(min=tuple(node_statistic.min_outputs), max=tuple(node_statistic.max_outputs))
layer_network.stats.update(network_stats)
params = layer_network.layers[layer_to_clone.name].params
params["quantization_level"] = 'I8' if self._configuration.precision == 'INT8' else self._configuration.precision
layer_network.layers[layer_to_clone.name].params = params
exec_network = self._plugin.load(network=layer_network, config={ "EXCLUSIVE_ASYNC_REQUESTS": "YES" })
if len(layer_network.inputs) != 1:
raise ValueError("created network has several inputs")
network_input_layer_name = next(iter(layer_network.inputs.keys()))
with SingleLayerNetwork(
network=layer_network,
exec_network=exec_network,
debug("create network #{} for layer {} ({}) -> {} ({})".format(layer_index, layer_to_clone.name, layer_to_clone.type, activation_layer.name, activation_layer.type))
else:
debug("create network #{} for layer {} ({})".format(layer_index, layer_to_clone.name, layer_to_clone.type))
layer_network, reference_output_layer_name = self._normalizer.create_network_for_layer(
self._configuration.weights,
layer_to_clone,
layer_to_clone_info,
activation_layer)
Network.reshape(layer_network, self._configuration.batch_size)
network_stats = {}
# TODO: initialize only neccessary statistic
for layer_name, node_statistic in stat.items():
network_stats[layer_name] = ie.LayerStats(min=tuple(node_statistic.min_outputs), max=tuple(node_statistic.max_outputs))
layer_network.stats.update(network_stats)
params = layer_network.layers[layer_to_clone.name].params
params["quantization_level"] = 'I8' if self._configuration.precision == 'INT8' else self._configuration.precision
layer_network.layers[layer_to_clone.name].params = params
exec_network = self._plugin.load(network=layer_network, config={ "EXCLUSIVE_ASYNC_REQUESTS": "YES" })
if len(layer_network.inputs) != 1:
raise ValueError("created network has several inputs")
network_input_layer_name = next(iter(layer_network.inputs.keys()))
single_layer_networks[layer_to_clone.name] = SingleLayerNetwork(
network = layer_network,
exec_network = exec_network,
else:
debug("create network #{} for layer {} ({})".format(index, layer.name,
layer.type))
layer_network, reference_output_layer_name = self._normalizer.create_network_for_layer(
None,
layer,
layer_info,
activation_layer)
Network.reshape(layer_network, self._configuration.batch_size)
network_stats = {}
# TODO: initialize only neccessary statistic
for layer_name, node_statistic in self._per_layer_statistics.items():
network_stats[layer_name] = ie.LayerStats(min=tuple(node_statistic.min_outputs),
max=tuple(node_statistic.max_outputs))
layer_network.stats.update(network_stats)
params = layer_network.layers[layer.name].params
params["quantization_level"] = 'I8' if self._configuration.precision == 'INT8' else self._configuration.precision
layer_network.layers[layer.name].params = params
exec_network = self._normalizer.plugin.load(
network=layer_network,
config={"EXCLUSIVE_ASYNC_REQUESTS": "YES"})
if len(layer_network.inputs) != 1:
raise ValueError("created network has several inputs")
network_input_layer_name = next(iter(layer_network.inputs.keys()))
def save(self, model_file_path: str, weights_file_path: str, quantization_level: dict, statistics):
'''
Save calibration results.
'''
if not statistics:
raise ValueError("statistics is empy")
network = self.create_network()
network_stats = {}
for layer_name, node_statistic in statistics.items():
network_stats[layer_name] = ie.LayerStats(min=tuple(node_statistic.min_outputs),
max=tuple(node_statistic.max_outputs))
network.stats.update(network_stats)
for layer in network.layers.values():
if self.is_quantization_supported(layer.type) and layer.name in quantization_level:
params = layer.params
params["quantization_level"] = quantization_level[layer.name]
layer.params = params
network.serialize(model_file_path, weights_file_path)