From 7b7f7f38c817021e37594d0bd7de81ebe369530f Mon Sep 17 00:00:00 2001 From: Felix Dittrich Date: Mon, 30 Sep 2024 12:03:39 +0200 Subject: [PATCH] [build] NumPy 2.0 support (#1709) --- .conda/meta.yaml | 2 +- doctr/transforms/functional/pytorch.py | 2 +- doctr/utils/metrics.py | 2 +- pyproject.toml | 4 +-- tests/conftest.py | 39 ++++++++++++++++---------- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/.conda/meta.yaml b/.conda/meta.yaml index fcac492132..7feb3a1bf9 100644 --- a/.conda/meta.yaml +++ b/.conda/meta.yaml @@ -20,7 +20,7 @@ requirements: - setuptools run: - - numpy >=1.16.0, <2.0.0 + - numpy >=1.16.0, <3.0.0 - scipy >=1.4.0, <2.0.0 - pillow >=9.2.0 - h5py >=3.1.0, <4.0.0 diff --git a/doctr/transforms/functional/pytorch.py b/doctr/transforms/functional/pytorch.py index 65649ea2c8..740769d99c 100644 --- a/doctr/transforms/functional/pytorch.py +++ b/doctr/transforms/functional/pytorch.py @@ -89,7 +89,7 @@ def rotate_sample( rotated_geoms[..., 0] = rotated_geoms[..., 0] / rotated_img.shape[2] rotated_geoms[..., 1] = rotated_geoms[..., 1] / rotated_img.shape[1] - return rotated_img, np.clip(rotated_geoms, 0, 1) + return rotated_img, np.clip(np.around(rotated_geoms, decimals=15), 0, 1) def crop_detection( diff --git a/doctr/utils/metrics.py b/doctr/utils/metrics.py index faea10a3ab..6947298ede 100644 --- a/doctr/utils/metrics.py +++ b/doctr/utils/metrics.py @@ -149,7 +149,7 @@ def box_iou(boxes_1: np.ndarray, boxes_2: np.ndarray) -> np.ndarray: right = np.minimum(r1, r2.T) bot = np.minimum(b1, b2.T) - intersection = np.clip(right - left, 0, np.Inf) * np.clip(bot - top, 0, np.Inf) + intersection = np.clip(right - left, 0, np.inf) * np.clip(bot - top, 0, np.inf) union = (r1 - l1) * (b1 - t1) + ((r2 - l2) * (b2 - t2)).T - intersection iou_mat = intersection / union diff --git a/pyproject.toml b/pyproject.toml index c208d98652..c0b209f535 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ dynamic = ["version"] dependencies = [ # For proper typing, mypy needs numpy>=1.20.0 (cf. https://github.com/numpy/numpy/pull/16515) # Additional typing support is brought by numpy>=1.22.4, but core build sticks to >=1.16.0 - "numpy>=1.16.0,<2.0.0", + "numpy>=1.16.0,<3.0.0", "scipy>=1.4.0,<2.0.0", "h5py>=3.1.0,<4.0.0", "opencv-python>=4.5.0,<5.0.0", @@ -75,7 +75,6 @@ contrib = [ testing = [ "pytest>=5.3.2", "coverage[toml]>=4.5.4", - "hdf5storage>=0.1.18", "onnxruntime>=1.11.0", "requests>=2.20.0", "psutil>=5.9.5" @@ -112,7 +111,6 @@ dev = [ # Testing "pytest>=5.3.2", "coverage[toml]>=4.5.4", - "hdf5storage>=0.1.18", "onnxruntime>=1.11.0", "requests>=2.20.0", "psutil>=5.9.5", diff --git a/tests/conftest.py b/tests/conftest.py index 61bceeb392..9757b4eaad 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,6 @@ from io import BytesIO import cv2 -import hdf5storage import numpy as np import pytest import requests @@ -257,24 +256,34 @@ def mock_imgur5k(tmpdir_factory, mock_image_stream): def mock_svhn_dataset(tmpdir_factory, mock_image_stream): root = tmpdir_factory.mktemp("datasets") svhn_root = root.mkdir("svhn") + train_root = svhn_root.mkdir("train") file = BytesIO(mock_image_stream) + + # NOTE: hdf5storage seems not to be maintained anymore, ref.: https://github.com/frejanordsiek/hdf5storage/pull/134 + # Instead we download the mocked data which was generated using the following code: # ascii image names - first = np.array([[49], [46], [112], [110], [103]], dtype=np.int16) # 1.png - second = np.array([[50], [46], [112], [110], [103]], dtype=np.int16) # 2.png - third = np.array([[51], [46], [112], [110], [103]], dtype=np.int16) # 3.png + # first = np.array([[49], [46], [112], [110], [103]], dtype=np.int16) # 1.png + # second = np.array([[50], [46], [112], [110], [103]], dtype=np.int16) # 2.png + # third = np.array([[51], [46], [112], [110], [103]], dtype=np.int16) # 3.png # labels: label is also ascii - label = { - "height": [35, 35, 35, 35], - "label": [1, 1, 3, 7], - "left": [116, 128, 137, 151], - "top": [27, 29, 29, 26], - "width": [15, 10, 17, 17], - } - - matcontent = {"digitStruct": {"name": [first, second, third], "bbox": [label, label, label]}} + # label = { + # "height": [35, 35, 35, 35], + # "label": [1, 1, 3, 7], + # "left": [116, 128, 137, 151], + # "top": [27, 29, 29, 26], + # "width": [15, 10, 17, 17], + # } + + # matcontent = {"digitStruct": {"name": [first, second, third], "bbox": [label, label, label]}} # Mock train data - train_root = svhn_root.mkdir("train") - hdf5storage.write(matcontent, filename=train_root.join("digitStruct.mat")) + # hdf5storage.write(matcontent, filename=train_root.join("digitStruct.mat")) + + # Downloading the mocked data + url = "https://github.com/mindee/doctr/releases/download/v0.9.0/digitStruct.mat" + response = requests.get(url) + with open(train_root.join("digitStruct.mat"), "wb") as f: + f.write(response.content) + for i in range(3): fn = train_root.join(f"{i + 1}.png") with open(fn, "wb") as f: