From bb5ff82f59e31667175ffbd49f0d78853a55ab81 Mon Sep 17 00:00:00 2001 From: panshaowu Date: Sat, 28 Oct 2023 16:20:05 +0800 Subject: [PATCH] fix issue #I8B8SV, #I7RZSB, and bug of exporting visual results --- deploy/py_infer/src/utils/visual_utils.py | 3 ++- docs/cn/datasets/ccpd.md | 4 ++-- docs/en/datasets/ccpd.md | 2 +- mindocr/optim/adamw.py | 4 +++- mindocr/optim/adan.py | 4 +++- mindocr/optim/lion.py | 4 +++- mindocr/optim/nadam.py | 4 +++- mindocr/utils/misc.py | 12 ++++++++++-- tools/dataset_converters/ccpd.py | 2 +- 9 files changed, 28 insertions(+), 11 deletions(-) diff --git a/deploy/py_infer/src/utils/visual_utils.py b/deploy/py_infer/src/utils/visual_utils.py index 059f73b60..bccc59f4b 100644 --- a/deploy/py_infer/src/utils/visual_utils.py +++ b/deploy/py_infer/src/utils/visual_utils.py @@ -69,14 +69,15 @@ def vis_bbox_text(image, box_list, text_list, font_path): def draw_box_txt_fine(img_size, box, txt, font_path): box_height = int(math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)) box_width = int(math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)) - img_text = Image.new("RGB", (box_width, box_height), (255, 255, 255)) # RGB or BGR doesn't matter if box_height > 2 * box_width and box_height > 30: + img_text = Image.new("RGB", (box_height, box_width), (255, 255, 255)) # RGB or BGR doesn't matter draw_text = ImageDraw.Draw(img_text) if txt: font = create_font(txt, (box_height, box_width), font_path) draw_text.text([0, 0], txt, fill=(0, 0, 0), font=font) img_text = img_text.transpose(Image.ROTATE_270) else: + img_text = Image.new("RGB", (box_width, box_height), (255, 255, 255)) # RGB or BGR doesn't matter draw_text = ImageDraw.Draw(img_text) if txt: font = create_font(txt, (box_width, box_height), font_path) diff --git a/docs/cn/datasets/ccpd.md b/docs/cn/datasets/ccpd.md index 6aa3e73e6..edf0d8a8f 100644 --- a/docs/cn/datasets/ccpd.md +++ b/docs/cn/datasets/ccpd.md @@ -35,8 +35,8 @@ CCPD2019 ```bash python tools/dataset_converters/convert.py \ --dataset_name ccpd --task det \ - --image_dir path/to/CCPD2019/ \ - --label_dir path/to/CCPD2019/splits/train.txt + --image_dir path/to/CCPD2019 \ + --label_dir path/to/CCPD2019/splits/train.txt \ --output_path path/to/CCPD2019/det_gt.txt ``` diff --git a/docs/en/datasets/ccpd.md b/docs/en/datasets/ccpd.md index 6b08957b1..15b1bc154 100644 --- a/docs/en/datasets/ccpd.md +++ b/docs/en/datasets/ccpd.md @@ -34,7 +34,7 @@ To prepare the data for text detection, you can run the following commands: ```bash python tools/dataset_converters/convert.py \ --dataset_name ccpd --task det \ - --image_dir path/to/CCPD2019/ \ + --image_dir path/to/CCPD2019 \ --label_dir path/to/CCPD2019/splits/train.txt \ --output_path path/to/CCPD2019/det_gt.txt ``` diff --git a/mindocr/optim/adamw.py b/mindocr/optim/adamw.py index 1c9b17ca0..2e43f3be1 100644 --- a/mindocr/optim/adamw.py +++ b/mindocr/optim/adamw.py @@ -3,7 +3,7 @@ import numpy as np import mindspore as ms -from mindspore import ops +from mindspore import ops, version from mindspore.common.initializer import initializer from mindspore.common.parameter import Parameter from mindspore.common.tensor import Tensor @@ -174,6 +174,8 @@ def __init__( @jit def construct(self, gradients): lr = self.get_lr() + if version.__version__ >= "2.2" and self._is_dynamic_lr_or_weight_decay(): + self.global_step += 1 gradients = scale_grad(gradients, self.reciprocal_scale) if self.clip: gradients = ops.clip_by_global_norm(gradients, 5.0, None) diff --git a/mindocr/optim/adan.py b/mindocr/optim/adan.py index dec3ea13a..9ff2017e0 100644 --- a/mindocr/optim/adan.py +++ b/mindocr/optim/adan.py @@ -1,6 +1,6 @@ """adan""" import mindspore as ms -from mindspore import ops +from mindspore import ops, version from mindspore.common import dtype as mstype from mindspore.common.tensor import Tensor from mindspore.nn.optim.optimizer import Optimizer, opt_init_args_register @@ -163,6 +163,8 @@ def construct(self, gradients): gradients = self.scale_grad(gradients) gradients = self._grad_sparse_indices_deduplicate(gradients) lr = self.get_lr() + if version.__version__ >= "2.2" and self._is_dynamic_lr_or_weight_decay(): + self.global_step += 1 # TODO: currently not support dist success = self.map_( diff --git a/mindocr/optim/lion.py b/mindocr/optim/lion.py index dfe540de4..e6f8227fa 100644 --- a/mindocr/optim/lion.py +++ b/mindocr/optim/lion.py @@ -1,7 +1,7 @@ import numpy as np import mindspore as ms -from mindspore import ops +from mindspore import ops, version from mindspore.common.initializer import initializer from mindspore.common.parameter import Parameter from mindspore.common.tensor import Tensor @@ -158,6 +158,8 @@ def __init__( @jit def construct(self, gradients): lr = self.get_lr() + if version.__version__ >= "2.2" and self._is_dynamic_lr_or_weight_decay(): + self.global_step += 1 gradients = scale_grad(gradients, self.reciprocal_scale) if self.clip: gradients = ops.clip_by_global_norm(gradients, 5.0, None) diff --git a/mindocr/optim/nadam.py b/mindocr/optim/nadam.py index d585df4b0..7a5b02b30 100644 --- a/mindocr/optim/nadam.py +++ b/mindocr/optim/nadam.py @@ -2,7 +2,7 @@ import numpy as np import mindspore as ms -from mindspore import ops +from mindspore import ops, version from mindspore.common.initializer import initializer from mindspore.common.parameter import Parameter from mindspore.common.tensor import Tensor @@ -67,6 +67,8 @@ def __init__( @jit def construct(self, gradients): lr = self.get_lr() + if version.__version__ >= "2.2" and self._is_dynamic_lr_or_weight_decay(): + self.global_step += 1 params = self.parameters step = self.global_step + _scaler_one gradients = self.decay_weight(gradients) diff --git a/mindocr/utils/misc.py b/mindocr/utils/misc.py index 874767bdf..a34baaabf 100644 --- a/mindocr/utils/misc.py +++ b/mindocr/utils/misc.py @@ -35,10 +35,18 @@ def fetch_optimizer_lr(opt): if opt.is_group_lr: lr = () for learning_rate in opt.learning_rate: - cur_dynamic_lr = learning_rate(opt.global_step - 1).reshape(()) + cur_dynamic_lr = ( + learning_rate(opt.global_step - 1).reshape(()) + if ms.version.__version__ < "2.2" + else learning_rate(opt.global_step).reshape(()) + ) lr += (cur_dynamic_lr,) else: - lr = opt.learning_rate(opt.global_step - 1).reshape(()) + lr = ( + opt.learning_rate(opt.global_step - 1).reshape(()) + if ms.version.__version__ < "2.2" + else opt.learning_rate(opt.global_step).reshape(()) + ) # print(f"After, global step: {opt.global_step}") return lr diff --git a/tools/dataset_converters/ccpd.py b/tools/dataset_converters/ccpd.py index 38024f16d..a00d676ec 100644 --- a/tools/dataset_converters/ccpd.py +++ b/tools/dataset_converters/ccpd.py @@ -149,7 +149,7 @@ def _format_det_label(self, image_dir: Path, label_path: Path, output_path: str) if label_path: with open(label_path, "r") as f: for line in f.readlines(): - img_path = image_dir / line.strip() + img_path = image_dir / line.strip().split("/")[1] assert img_path.exists(), f"Image {img_path} not found." img_paths.append(img_path) else: