Skip to content

Commit 7958afd

Browse files
authored
AC: port bugfixes to release (#2513)
1 parent 36c38ab commit 7958afd

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

tools/accuracy_checker/.pylintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ disable = C0103,
2020

2121
max-line-length = 120
2222
ignore-docstrings = yes
23-
extension-pkg-whitelist=inference_engine,cv2,numpy,mxnet,tensorflow,pycocotools,onnxruntime,kenlm,paddle
24-
ignored-modules = numpy,cv2,openvino.inference_engine,caffe,mxnet,tensorflow,pycocotools,onnxruntime,torch,kenlm,paddle.fluid.core
23+
extension-pkg-whitelist=inference_engine,cv2,numpy,mxnet,tensorflow,pycocotools,onnxruntime,kenlm,paddle,torchvision
24+
ignored-modules = numpy,cv2,openvino.inference_engine,caffe,mxnet,tensorflow,pycocotools,onnxruntime,torch,kenlm,paddle.fluid.core,torchvision
2525
load-plugins = pylint_checkers
2626
ignored-classes = pathlib.PurePath
2727
jobs=0

tools/accuracy_checker/accuracy_checker/evaluators/custom_evaluators/lpcnet_evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def predict(self, cfeats, features, chunk_size, order=16, callback=None):
270270
# Cut off the tail of the remaining distribution
271271
p = np.maximum(p - 0.002, 0).astype('float64')
272272
p = p / (1e-8 + np.sum(p))
273-
274-
fexc[0, 0, 2] = np.argmax(np.random.multinomial(1, p[0, 0, :], 1))
273+
rng = np.random.default_rng(12345)
274+
fexc[0, 0, 2] = np.argmax(rng.multinomial(1, p[0, 0, :], 1))
275275
pcm[pcm_start_index] = pred + ulaw2lin(fexc[0, 0, 2])
276276
fexc[0, 0, 0] = lin2ulaw(pcm[pcm_start_index])
277277
mem.append(coef * mem_ + pcm[pcm_start_index])

tools/accuracy_checker/accuracy_checker/metrics/image_quality_assessment.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"""
22
Copyright (c) 2018-2021 Intel Corporation
3-
43
Licensed under the Apache License, Version 2.0 (the "License");
54
you may not use this file except in compliance with the License.
65
You may obtain a copy of the License at
7-
86
http://www.apache.org/licenses/LICENSE-2.0
9-
107
Unless required by applicable law or agreed to in writing, software
118
distributed under the License is distributed on an "AS IS" BASIS,
129
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1310
See the License for the specific language governing permissions and
1411
limitations under the License.
1512
"""
1613
import math
14+
import tempfile
15+
from pathlib import Path
1716

1817
import cv2
1918
import numpy as np
@@ -407,8 +406,8 @@ def configure(self):
407406
self.color_scale = 255 if not self.normalized_images else 1
408407
if isinstance(lpips, UnsupportedPackage):
409408
lpips.raise_error(self.__provider__)
410-
self.loss = lpips.LPIPS(net=self.get_value_from_config('net'))
411409
self.dist_threshold = self.get_value_from_config('distance_threshold')
410+
self.loss = self._create_loss()
412411

413412
def lpips_differ(self, annotation_image, prediction_image):
414413
if self.color_order == 'BGR':
@@ -425,3 +424,37 @@ def evaluate(self, annotations, predictions):
425424
self.meta['names'].append('ratio_greater_{}'.format(self.dist_threshold))
426425
results += (invalid_ratio, )
427426
return results
427+
428+
def _create_loss(self):
429+
import torch # pylint: disable=C0415
430+
import torchvision # pylint: disable=C0415
431+
net = self.get_value_from_config('net')
432+
model_weights = {
433+
'alex': ('https://download.pytorch.org/models/alexnet-owt-7be5be79.pth',
434+
'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth'
435+
),
436+
'squeeze': 'https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth',
437+
'vgg': 'https://download.pytorch.org/models/vgg16-397923af.pth'
438+
}
439+
model_classes = {
440+
'alex': torchvision.models.alexnet,
441+
'squeeze': torchvision.models.squeezenet1_1,
442+
'vgg': torchvision.models.vgg16
443+
}
444+
with tempfile.TemporaryDirectory(prefix='lpips_model', dir=Path.cwd()) as model_dir:
445+
weights = model_weights[net]
446+
if isinstance(weights, tuple):
447+
weights = weights[1] if torch.__version__ <= '1.6.0' else weights[0]
448+
preloaded_weights = torch.utils.model_zoo.load_url(
449+
weights, model_dir=model_dir, progress=False, map_location='cpu'
450+
)
451+
model = model_classes[net](pretrained=False)
452+
model.load_state_dict(preloaded_weights)
453+
feats = model.features
454+
loss = lpips.LPIPS(pnet_rand=True)
455+
for slice_id in range(1, loss.net.N_slices + 1):
456+
sl = getattr(loss.net, 'slice{}'.format(slice_id))
457+
for module_id in sl._modules: # pylint: disable=W0212
458+
sl._modules[module_id] = feats[int(module_id)] # pylint: disable=W0212
459+
setattr(loss.net, 'slice{}'.format(slice_id), sl)
460+
return loss

tools/accuracy_checker/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tqdm>=4.54.1
55
# scikit-image version will be changed to 0.18 after fixing tests (compare_ssim method has been removed in this version)
66
# note: python 3.6 wheels is not available since 0.18
77
scikit-image~=0.17.2
8+
imagecodecs
89

910
# reid
1011
scikit-learn>=0.24.1

tools/accuracy_checker/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ ignore = F401
55
[isort]
66
line_length = 120
77
use_parentheses = True
8-
known_third_party = openvino.inference_engine,caffe,cv2,mxnet,tensorflow,torch
8+
known_third_party = openvino.inference_engine,caffe,cv2,mxnet,tensorflow,torch,torchvision
99
extension-pkg-whitelist = kenlm

0 commit comments

Comments
 (0)