Skip to content

Commit

Permalink
fix issue #I8B8SV, #I7RZSB, and bug of exporting visual results
Browse files Browse the repository at this point in the history
  • Loading branch information
panshaowu committed Oct 31, 2023
1 parent 0e707b3 commit a727a7d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion deploy/py_infer/src/utils/visual_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/cn/datasets/ccpd.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion docs/en/datasets/ccpd.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 3 additions & 1 deletion mindocr/optim/adamw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.assignadd(self.global_step, self.global_step_increase_tensor)
gradients = scale_grad(gradients, self.reciprocal_scale)
if self.clip:
gradients = ops.clip_by_global_norm(gradients, 5.0, None)
Expand Down
4 changes: 3 additions & 1 deletion mindocr/optim/adan.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.assignadd(self.global_step, self.global_step_increase_tensor)

# TODO: currently not support dist
success = self.map_(
Expand Down
4 changes: 3 additions & 1 deletion mindocr/optim/lion.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.assignadd(self.global_step, self.global_step_increase_tensor)
gradients = scale_grad(gradients, self.reciprocal_scale)
if self.clip:
gradients = ops.clip_by_global_norm(gradients, 5.0, None)
Expand Down
4 changes: 3 additions & 1 deletion mindocr/optim/nadam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.assignadd(self.global_step, self.global_step_increase_tensor)
params = self.parameters
step = self.global_step + _scaler_one
gradients = self.decay_weight(gradients)
Expand Down
12 changes: 10 additions & 2 deletions mindocr/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tools/dataset_converters/ccpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a727a7d

Please sign in to comment.