Skip to content

Commit 8fb64b9

Browse files
authored
AC: update style transfer converter (#3446)
1 parent 3286b10 commit 8fb64b9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

tools/accuracy_checker/openvino/tools/accuracy_checker/annotation_converters/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ The main difference between this converter and `super_resolution` in data organi
618618
* `data_dir` - directory, where input images and annotation files in MATLAB format stored.
619619
* `style_transfer` - converts images to `StyleTransferAnnotation`.
620620
* `images_dir` - path to images directory.
621+
* `annotation_file` - path to file with annotation in format: `[input_img1] [ref_img]` if you have single input model or `[input_img1] [input_img2] [ref_img]` if you have 2 inputs.
621622
* `ade20k` - converts ADE20K dataset to `SegmentationAnnotation`.
622623
* `images_dir` - path to directory with images (e.g. `ADEChallengeData2016/images/validation`).
623624
* `annotations_dir` - path to directory with annotations (e.g. `ADEChallengeData2016/annotations/validation`).

tools/accuracy_checker/openvino/tools/accuracy_checker/annotation_converters/style_transfer.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ..config import PathField
1818
from ..representation import StyleTransferAnnotation
1919
from .format_converter import BaseFormatConverter, ConverterReturn
20+
from ..utils import read_txt, check_file_existence
2021

2122

2223
class StyleTransferConverter(BaseFormatConverter):
@@ -30,23 +31,62 @@ def parameters(cls):
3031
'images_dir': PathField(
3132
optional=False, is_directory=True,
3233
description="Path to directory with images."
33-
)
34+
),
35+
'annotation_file': PathField(optional=True, description='File with used images declaration')
3436
})
3537
return parameters
3638

3739
def configure(self):
3840
self.image_dir = self.get_value_from_config('images_dir')
41+
self.annotation_file = self.get_value_from_config('annotation_file')
3942

4043
def convert(self, check_content=False, progress_callback=None, progress_interval=100, **kwargs):
4144
content_check_errors = [] if check_content else None
4245
annotations = []
46+
if self.annotation_file:
47+
return self._convert_using_annotation(check_content, progress_callback, progress_interval)
4348
images = [
4449
im for im in self.image_dir.iterdir()
4550
if im.name.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))
4651
]
47-
for image in images:
52+
num_iteration = len(images)
53+
for idx, image in enumerate(images):
4854
identifiers = image.name
4955
annotation = StyleTransferAnnotation(identifiers, image.name)
5056
annotations.append(annotation)
57+
if progress_callback and idx % progress_interval:
58+
progress_callback(idx * 100 / num_iteration)
5159

5260
return ConverterReturn(annotations, None, content_check_errors)
61+
62+
def _convert_using_annotation(self, check_content=False, progress_callback=None, progress_interval=100):
63+
list_images = read_txt(self.annotation_file)
64+
num_lines = len(list_images)
65+
annotation = []
66+
content_errors = [] if check_content else None
67+
for line_id, line in enumerate(list_images):
68+
data = line.split()
69+
if len(data) == 1:
70+
identifier = data[0]
71+
reference = data[0]
72+
elif len(data) == 2:
73+
identifier = data[0]
74+
reference = data[1]
75+
else:
76+
identifier = data[:-1]
77+
reference = data[-1]
78+
if check_content:
79+
if isinstance(identifier, str):
80+
if not check_file_existence(self.image_dir / identifier):
81+
content_errors.append(f'{self.image_dir / identifier}: does not exists')
82+
else:
83+
for img in identifier:
84+
if not check_file_existence(self.image_dir / img):
85+
content_errors.append(f'{self.image_dir / identifier}: does not exists')
86+
if not check_file_existence(self.image_dir / reference):
87+
content_errors.append(f'{self.image_dir/reference}: does not exist')
88+
ann = StyleTransferAnnotation(identifier, reference)
89+
if progress_callback and line_id % progress_interval:
90+
progress_callback(line_id * 100 / num_lines)
91+
annotation.append(ann)
92+
return ConverterReturn(annotation, None, content_errors)

0 commit comments

Comments
 (0)