Skip to content

Commit 3e21320

Browse files
authored
[misc] Code quality fixes (#1890)
1 parent 5402948 commit 3e21320

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: no-commit-to-branch
1717
args: ['--branch', 'main']
1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.9.3
19+
rev: v0.9.10
2020
hooks:
2121
- id: ruff
2222
args: [ --fix ]

doctr/datasets/coco_text.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class COCOTEXT(AbstractDataset):
2222
COCO-Text dataset from `"COCO-Text: Dataset and Benchmark for Text Detection and Recognition in Natural Images"
2323
<https://arxiv.org/pdf/1601.07140v2>`_ |
2424
`"homepage" <https://bgshih.github.io/cocotext/>`_.
25-
25+
2626
>>> # NOTE: You need to download the dataset first.
2727
>>> from doctr.datasets import COCOTEXT
2828
>>> train_set = COCOTEXT(train=True, img_folder="/path/to/coco_text/train2014/",
@@ -31,7 +31,7 @@ class COCOTEXT(AbstractDataset):
3131
>>> test_set = COCOTEXT(train=False, img_folder="/path/to/coco_text/train2014/",
3232
>>> label_path = "/path/to/coco_text/cocotext.v2.json")
3333
>>> img, target = test_set[0]
34-
34+
3535
Args:
3636
img_folder: folder with all the images of the dataset
3737
label_path: path to the annotations file of the dataset
@@ -55,7 +55,7 @@ def __init__(
5555
super().__init__(
5656
img_folder, pre_transforms=convert_target_to_relative if not recognition_task else None, **kwargs
5757
)
58-
# Task check
58+
# Task check
5959
if recognition_task and detection_task:
6060
raise ValueError(
6161
" 'recognition' and 'detection task' cannot be set to True simultaneously. "
@@ -73,14 +73,16 @@ def __init__(
7373

7474
with open(label_path, "r") as file:
7575
data = json.load(file)
76-
76+
7777
# Filter images based on the set
7878
img_items = [img for img in data["imgs"].items() if (img[1]["set"] == "train") == train]
79+
box: list[float] | np.ndarray
7980

8081
for img_id, img_info in tqdm(img_items, desc="Preparing and Loading COCOTEXT", total=len(img_items)):
8182
img_path = os.path.join(img_folder, img_info["file_name"])
8283

83-
if not os.path.exists(img_path):
84+
# File existence check
85+
if not os.path.exists(img_path): # pragma: no cover
8486
raise FileNotFoundError(f"Unable to locate {img_path}")
8587

8688
# Get annotations for the current image (only legible text)
@@ -90,11 +92,12 @@ def __init__(
9092
if ann["image_id"] == int(img_id) and ann["legibility"] == "legible"
9193
]
9294

93-
if not annotations: # Some images have no annotations with readable text
95+
# Some images have no annotations with readable text
96+
if not annotations: # pragma: no cover
9497
continue
9598

9699
_targets = []
97-
100+
98101
for annotation in annotations:
99102
x, y, w, h = annotation["bbox"]
100103
if use_polygons:
@@ -133,4 +136,4 @@ def __init__(
133136
self.root = tmp_root
134137

135138
def extra_repr(self) -> str:
136-
return f"train={self.train}"
139+
return f"train={self.train}"

doctr/models/detection/fast/pytorch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ def reparameterize(model: FAST | nn.Module) -> FAST:
302302
if last_conv is None:
303303
continue
304304
conv_w = last_conv.weight
305-
conv_b = last_conv.bias if last_conv.bias is not None else torch.zeros_like(child.running_mean)
305+
conv_b = last_conv.bias if last_conv.bias is not None else torch.zeros_like(child.running_mean) # type: ignore[arg-type]
306306

307-
factor = child.weight / torch.sqrt(child.running_var + child.eps)
307+
factor = child.weight / torch.sqrt(child.running_var + child.eps) # type: ignore
308308
last_conv.weight = nn.Parameter(conv_w * factor.reshape([last_conv.out_channels, 1, 1, 1]))
309309
last_conv.bias = nn.Parameter((conv_b - child.running_mean) * factor + child.bias)
310-
model._modules[last_conv_name] = last_conv
310+
model._modules[last_conv_name] = last_conv # type: ignore[index]
311311
model._modules[name] = nn.Identity()
312312
last_conv = None
313313
elif isinstance(child, nn.Conv2d):

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,4 +780,4 @@ def mock_cocotext_dataset(tmpdir_factory, mock_image_stream):
780780
fn = image_folder.join(f"{img_name}")
781781
with open(fn, "wb") as f:
782782
f.write(file.getbuffer())
783-
return str(image_folder), str(annotation_file)
783+
return str(image_folder), str(annotation_file)

0 commit comments

Comments
 (0)