From da10c34e41d251aaf79668f152d276f2348bff05 Mon Sep 17 00:00:00 2001 From: Nikita Savelyev Date: Mon, 25 Sep 2023 18:10:40 +0200 Subject: [PATCH 1/5] Added intel colors for progress bar text --- .../openvino/mobilenet_v2/main.py | 24 ++++++ nncf/common/logging/track_progress.py | 83 +++++++++++++++++-- 2 files changed, 98 insertions(+), 9 deletions(-) diff --git a/examples/post_training_quantization/openvino/mobilenet_v2/main.py b/examples/post_training_quantization/openvino/mobilenet_v2/main.py index 2cc6ab0329f..245e41f1a43 100644 --- a/examples/post_training_quantization/openvino/mobilenet_v2/main.py +++ b/examples/post_training_quantization/openvino/mobilenet_v2/main.py @@ -102,6 +102,30 @@ def get_model_size(ir_path: str, m_type: str = "Mb", verbose: bool = True) -> fl ) val_data_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1, shuffle=False) +class Loader: + def __init__(self, dataloader): + self.dataloader = dataloader + self.i = 0 + + def __iter__(self): + self.i = 0 + self.iter = self.dataloader.__iter__() + return self + + def __next__(self): + if self.i > 100: + raise StopIteration + self.i += 1 + return next(self.iter) + + # def __len__(self): + # return 101 + +# from time import sleep +# from nncf.common.logging.track_progress import track +# for n in track(Loader(val_data_loader)): +# sleep(0.1) + path_to_model = download(MODEL_URL, MODEL_PATH) ov_model = ov.Core().read_model(path_to_model / "mobilenet_v2_fp32.xml") diff --git a/nncf/common/logging/track_progress.py b/nncf/common/logging/track_progress.py index 9827bc72085..cc2d2121576 100644 --- a/nncf/common/logging/track_progress.py +++ b/nncf/common/logging/track_progress.py @@ -8,7 +8,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +from datetime import timedelta from typing import Callable, Iterable, List, Optional, Sequence, Union from rich.console import Console @@ -20,20 +20,23 @@ from rich.progress import Task from rich.progress import TaskProgressColumn from rich.progress import TextColumn -from rich.progress import TimeElapsedColumn -from rich.progress import TimeRemainingColumn from rich.style import StyleType from rich.text import Text +# INTEL_BLUE_COLOR = (0, 174, 239) +INTEL_BLUE_COLOR = (0, 113, 197) + class IterationsColumn(ProgressColumn): + def __init__(self, style: Union[str, StyleType]): + super().__init__() + self.style = style + def render(self, task: Task) -> Text: if task.total is None: return Text("") text = f"{int(task.completed)}/{int(task.total)}" - if task.finished: - return Text(text, style="progress.elapsed") - return Text(text, style="progress.remaining") + return Text(text, style=self.style) class SeparatorColumn(ProgressColumn): @@ -47,6 +50,65 @@ def render(self, task: Task) -> Text: return Text("•") +class TimeElapsedColumnWithStyle(ProgressColumn): + """Renders time elapsed.""" + + def __init__(self, style: Union[str, StyleType]): + super().__init__() + self.style = style + + def render(self, task: "Task") -> Text: + """Show time elapsed.""" + elapsed = task.finished_time if task.finished else task.elapsed + if elapsed is None: + return Text("-:--:--", style=self.style) + delta = timedelta(seconds=max(0, int(elapsed))) + return Text(str(delta), style=self.style) + + +class TimeRemainingColumnWithStyle(ProgressColumn): + """Renders estimated time remaining.""" + + # Only refresh twice a second to prevent jitter + max_refresh = 0.5 + + def __init__( + self, + style: Union[str, StyleType], + compact: bool = False, + elapsed_when_finished: bool = False, + table_column: Optional[Column] = None, + ): + self.style = style + self.compact = compact + self.elapsed_when_finished = elapsed_when_finished + super().__init__(table_column=table_column) + + def render(self, task: "Task") -> Text: + """Show time remaining.""" + if self.elapsed_when_finished and task.finished: + task_time = task.finished_time + else: + task_time = task.time_remaining + + if task.total is None: + return Text("", style=self.style) + + if task_time is None: + return Text("--:--" if self.compact else "-:--:--", style=self.style) + + # Based on https://github.com/tqdm/tqdm/blob/master/tqdm/std.py + minutes, seconds = divmod(int(task_time), 60) + hours, minutes = divmod(minutes, 60) + + if self.compact and not hours: + formatted = f"{minutes:02d}:{seconds:02d}" + else: + formatted = f"{hours:d}:{minutes:02d}:{seconds:02d}" + + return Text(formatted, style=self.style) + + class track: def __init__( self, @@ -95,6 +157,8 @@ def __init__( self.update_period = update_period self.task = None + text_style = f"rgb{INTEL_BLUE_COLOR}".replace(' ', '') + self.columns: List[ProgressColumn] = ( [TextColumn("[progress.description]{task.description}")] if description else [] ) @@ -105,13 +169,14 @@ def __init__( complete_style=complete_style, finished_style=finished_style, pulse_style=pulse_style, + bar_width=None ), TaskProgressColumn(show_speed=show_speed), - IterationsColumn(), + IterationsColumn(style=text_style), SeparatorColumn(), - TimeElapsedColumn(), + TimeElapsedColumnWithStyle(style=text_style), SeparatorColumn(disable_if_no_total=True), # disable because time remaining will be empty - TimeRemainingColumn(), + TimeRemainingColumnWithStyle(style=text_style), ) ) From 6ebe7496ec1cbcd63934267cabc883fc46e19e65 Mon Sep 17 00:00:00 2001 From: Nikita Savelyev Date: Mon, 25 Sep 2023 18:16:53 +0200 Subject: [PATCH 2/5] Removed irrelevant changes --- .../openvino/mobilenet_v2/main.py | 24 ------------------- nncf/common/logging/track_progress.py | 17 +++++++++---- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/examples/post_training_quantization/openvino/mobilenet_v2/main.py b/examples/post_training_quantization/openvino/mobilenet_v2/main.py index 245e41f1a43..2cc6ab0329f 100644 --- a/examples/post_training_quantization/openvino/mobilenet_v2/main.py +++ b/examples/post_training_quantization/openvino/mobilenet_v2/main.py @@ -102,30 +102,6 @@ def get_model_size(ir_path: str, m_type: str = "Mb", verbose: bool = True) -> fl ) val_data_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1, shuffle=False) -class Loader: - def __init__(self, dataloader): - self.dataloader = dataloader - self.i = 0 - - def __iter__(self): - self.i = 0 - self.iter = self.dataloader.__iter__() - return self - - def __next__(self): - if self.i > 100: - raise StopIteration - self.i += 1 - return next(self.iter) - - # def __len__(self): - # return 101 - -# from time import sleep -# from nncf.common.logging.track_progress import track -# for n in track(Loader(val_data_loader)): -# sleep(0.1) - path_to_model = download(MODEL_URL, MODEL_PATH) ov_model = ov.Core().read_model(path_to_model / "mobilenet_v2_fp32.xml") diff --git a/nncf/common/logging/track_progress.py b/nncf/common/logging/track_progress.py index cc2d2121576..9914c4b79ff 100644 --- a/nncf/common/logging/track_progress.py +++ b/nncf/common/logging/track_progress.py @@ -23,7 +23,6 @@ from rich.style import StyleType from rich.text import Text -# INTEL_BLUE_COLOR = (0, 174, 239) INTEL_BLUE_COLOR = (0, 113, 197) @@ -51,7 +50,11 @@ def render(self, task: Task) -> Text: class TimeElapsedColumnWithStyle(ProgressColumn): - """Renders time elapsed.""" + """ + Renders time elapsed. + + Similar to TimeElapsedColumn, but with addition of style parameter. + """ def __init__(self, style: Union[str, StyleType]): super().__init__() @@ -67,7 +70,11 @@ def render(self, task: "Task") -> Text: class TimeRemainingColumnWithStyle(ProgressColumn): - """Renders estimated time remaining.""" + """ + Renders estimated time remaining. + + Similar to TimeRemainingColumn, but with addition of style parameter. + """ # Only refresh twice a second to prevent jitter max_refresh = 0.5 @@ -157,7 +164,7 @@ def __init__( self.update_period = update_period self.task = None - text_style = f"rgb{INTEL_BLUE_COLOR}".replace(' ', '') + text_style = f"rgb({INTEL_BLUE_COLOR[0]},{INTEL_BLUE_COLOR[1]},{INTEL_BLUE_COLOR[2]})" self.columns: List[ProgressColumn] = ( [TextColumn("[progress.description]{task.description}")] if description else [] @@ -169,7 +176,7 @@ def __init__( complete_style=complete_style, finished_style=finished_style, pulse_style=pulse_style, - bar_width=None + bar_width=None, ), TaskProgressColumn(show_speed=show_speed), IterationsColumn(style=text_style), From 5b0feb18846ece116f14a87ea2fb617da47e3b6e Mon Sep 17 00:00:00 2001 From: Nikita Savelyev Date: Wed, 27 Sep 2023 15:50:06 +0200 Subject: [PATCH 3/5] Simplified custom column classes --- nncf/common/logging/track_progress.py | 85 ++++----------------------- 1 file changed, 13 insertions(+), 72 deletions(-) diff --git a/nncf/common/logging/track_progress.py b/nncf/common/logging/track_progress.py index 9914c4b79ff..17de41a0ecb 100644 --- a/nncf/common/logging/track_progress.py +++ b/nncf/common/logging/track_progress.py @@ -20,22 +20,20 @@ from rich.progress import Task from rich.progress import TaskProgressColumn from rich.progress import TextColumn +from rich.progress import TimeElapsedColumn +from rich.progress import TimeRemainingColumn from rich.style import StyleType from rich.text import Text -INTEL_BLUE_COLOR = (0, 113, 197) +INTEL_BLUE_COLOR = "#0068b5" class IterationsColumn(ProgressColumn): - def __init__(self, style: Union[str, StyleType]): - super().__init__() - self.style = style - def render(self, task: Task) -> Text: if task.total is None: return Text("") text = f"{int(task.completed)}/{int(task.total)}" - return Text(text, style=self.style) + return Text(text, style=INTEL_BLUE_COLOR) class SeparatorColumn(ProgressColumn): @@ -49,71 +47,16 @@ def render(self, task: Task) -> Text: return Text("•") -class TimeElapsedColumnWithStyle(ProgressColumn): - """ - Renders time elapsed. - - Similar to TimeElapsedColumn, but with addition of style parameter. - """ - - def __init__(self, style: Union[str, StyleType]): - super().__init__() - self.style = style - +class TimeElapsedColumnWithStyle(TimeElapsedColumn): def render(self, task: "Task") -> Text: - """Show time elapsed.""" - elapsed = task.finished_time if task.finished else task.elapsed - if elapsed is None: - return Text("-:--:--", style=self.style) - delta = timedelta(seconds=max(0, int(elapsed))) - return Text(str(delta), style=self.style) - + text = super().render(task) + return Text(text._text[0], style=INTEL_BLUE_COLOR) -class TimeRemainingColumnWithStyle(ProgressColumn): - """ - Renders estimated time remaining. - - Similar to TimeRemainingColumn, but with addition of style parameter. - """ - - # Only refresh twice a second to prevent jitter - max_refresh = 0.5 - - def __init__( - self, - style: Union[str, StyleType], - compact: bool = False, - elapsed_when_finished: bool = False, - table_column: Optional[Column] = None, - ): - self.style = style - self.compact = compact - self.elapsed_when_finished = elapsed_when_finished - super().__init__(table_column=table_column) +class TimeRemainingColumnWithStyle(TimeRemainingColumn): def render(self, task: "Task") -> Text: - """Show time remaining.""" - if self.elapsed_when_finished and task.finished: - task_time = task.finished_time - else: - task_time = task.time_remaining - - if task.total is None: - return Text("", style=self.style) - - if task_time is None: - return Text("--:--" if self.compact else "-:--:--", style=self.style) - - # Based on https://github.com/tqdm/tqdm/blob/master/tqdm/std.py - minutes, seconds = divmod(int(task_time), 60) - hours, minutes = divmod(minutes, 60) - - if self.compact and not hours: - formatted = f"{minutes:02d}:{seconds:02d}" - else: - formatted = f"{hours:d}:{minutes:02d}:{seconds:02d}" - - return Text(formatted, style=self.style) + text = super().render(task) + return Text(text._text[0], style=INTEL_BLUE_COLOR) class track: @@ -164,8 +107,6 @@ def __init__( self.update_period = update_period self.task = None - text_style = f"rgb({INTEL_BLUE_COLOR[0]},{INTEL_BLUE_COLOR[1]},{INTEL_BLUE_COLOR[2]})" - self.columns: List[ProgressColumn] = ( [TextColumn("[progress.description]{task.description}")] if description else [] ) @@ -179,11 +120,11 @@ def __init__( bar_width=None, ), TaskProgressColumn(show_speed=show_speed), - IterationsColumn(style=text_style), + IterationsColumn(), SeparatorColumn(), - TimeElapsedColumnWithStyle(style=text_style), + TimeElapsedColumnWithStyle(), SeparatorColumn(disable_if_no_total=True), # disable because time remaining will be empty - TimeRemainingColumnWithStyle(style=text_style), + TimeRemainingColumnWithStyle(), ) ) From 9807d5e874d0dfee949ff20608da7357628181a6 Mon Sep 17 00:00:00 2001 From: Nikita Savelyev Date: Wed, 27 Sep 2023 15:52:56 +0200 Subject: [PATCH 4/5] Removed not used import --- nncf/common/logging/track_progress.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nncf/common/logging/track_progress.py b/nncf/common/logging/track_progress.py index 17de41a0ecb..b82a95f32bf 100644 --- a/nncf/common/logging/track_progress.py +++ b/nncf/common/logging/track_progress.py @@ -8,7 +8,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from datetime import timedelta + from typing import Callable, Iterable, List, Optional, Sequence, Union from rich.console import Console From 28558adcba1c467aa81be9f07bb0b75c0e20cd04 Mon Sep 17 00:00:00 2001 From: Nikita Savelyev Date: Wed, 27 Sep 2023 16:19:37 +0200 Subject: [PATCH 5/5] pylint --- nncf/common/logging/track_progress.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nncf/common/logging/track_progress.py b/nncf/common/logging/track_progress.py index b82a95f32bf..b852c372c87 100644 --- a/nncf/common/logging/track_progress.py +++ b/nncf/common/logging/track_progress.py @@ -50,13 +50,13 @@ def render(self, task: Task) -> Text: class TimeElapsedColumnWithStyle(TimeElapsedColumn): def render(self, task: "Task") -> Text: text = super().render(task) - return Text(text._text[0], style=INTEL_BLUE_COLOR) + return Text(text._text[0], style=INTEL_BLUE_COLOR) # pylint: disable=protected-access class TimeRemainingColumnWithStyle(TimeRemainingColumn): def render(self, task: "Task") -> Text: text = super().render(task) - return Text(text._text[0], style=INTEL_BLUE_COLOR) + return Text(text._text[0], style=INTEL_BLUE_COLOR) # pylint: disable=protected-access class track: