Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / progress_reporters.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 """
16
17 import time
18
19 from tqdm import tqdm
20
21 from .dependency import ClassProvider
22 from .logging import print_info
23
24
25 class ProgressReporter(ClassProvider):
26     __provider_type__ = 'progress_reporter'
27
28     def __init__(self, dataset_size=None):
29         self.finished = True
30         self.dataset_size = None
31         self.start_time = None
32         self.prev_time = None
33         if dataset_size is not None:
34             self.reset(dataset_size)
35
36     def finish(self, objects_processed=True):
37         self.finished = True
38         if not objects_processed:
39             return
40
41         process_time = time.time() - self.start_time
42         print_info('{} objects processed in {:.3f} seconds'.format(self.dataset_size, process_time))
43
44     def reset(self, dataset_size):
45         if not self.finished:
46             self.finish(objects_processed=False)
47
48         self.dataset_size = dataset_size
49         self.start_time = time.time()
50         self.finished = False
51
52
53 class PrintProgressReporter(ProgressReporter):
54     __provider__ = 'print'
55
56     def __init__(self, dataset_size=None, print_interval=1000):
57         super().__init__(dataset_size)
58         self.print_interval = print_interval
59
60     def reset(self, dataset_size):
61         self.dataset_size = dataset_size
62         print_info('Total dataset size: {}'.format(dataset_size))
63         self.start_time = time.time()
64         self.prev_time = self.start_time
65
66     def update(self, batch_id, batch_size):
67         if (batch_id + 1) % self.print_interval != 0:
68             return
69
70         now = time.time()
71         batch_time = now - self.prev_time
72         self.prev_time = now
73
74         print_info('{} / {} processed in {:.3f}s'.format((batch_id + 1) * batch_size, self.dataset_size, batch_time))
75
76
77 class TQDMReporter(ProgressReporter):
78     __provider__ = 'bar'
79
80     def update(self, _batch_id, batch_size):
81         self.tqdm.update(batch_size)
82
83     def finish(self, objects_processed=True):
84         self.tqdm.close()
85         super().finish(objects_processed)
86
87     def reset(self, dataset_size):
88         super().reset(dataset_size)
89         self.tqdm = tqdm(
90             total=self.dataset_size, unit='frames', leave=False,
91             bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]'
92         )