Skip to content

Commit

Permalink
🚧 chore(preprocess&docs): remove unnecessary preprocess steps and upd…
Browse files Browse the repository at this point in the history
…ate README.md
  • Loading branch information
zhujiajian98 committed Sep 10, 2024
1 parent 3ab2632 commit 625ae1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

## News
- 2024-08-08: Removed dependencies related to torch. Now you can only use it with numpy and opencv.
- 2024-09-10: Accelerate the preprocess of Depth-Anything v2 by removing the unnecessary steps.

## Requirments

Expand Down
25 changes: 16 additions & 9 deletions models/utils_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
from typing import Tuple


class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
Expand All @@ -24,6 +25,7 @@ def __call__(self, sample):
sample["image"] = (sample["image"] - self.__mean) / self.__std
return sample


class PrepareForNet(object):
"""Prepare sample for usage as network input."""

Expand All @@ -34,17 +36,18 @@ def __call__(self, sample):
if "mask" in sample:
sample["mask"] = sample["mask"].astype(np.float32)
sample["mask"] = np.ascontiguousarray(sample["mask"])

if "depth" in sample:
depth = sample["depth"].astype(np.float32)
sample["depth"] = np.ascontiguousarray(depth)

if "semseg_mask" in sample:
sample["semseg_mask"] = sample["semseg_mask"].astype(np.float32)
sample["semseg_mask"] = np.ascontiguousarray(sample["semseg_mask"])

return sample


def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA):
"""Rezise the sample to ensure the given size. Keeps aspect ratio."""
shape = list(sample["disparity"].shape)
Expand Down Expand Up @@ -78,6 +81,7 @@ def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA):

return tuple(shape)


class Resize(object):
"""Resize sample to given size (width, height)."""

Expand Down Expand Up @@ -126,7 +130,9 @@ def get_size(self, width, height):
else:
scale_width = scale_height
else:
raise ValueError(f"resize_method {self.__resize_method} not implemented")
raise ValueError(
f"resize_method {self.__resize_method} not implemented"
)

if self.__resize_method == "lower_bound":
new_height = self.constrain_to_multiple_of(
Expand Down Expand Up @@ -179,9 +185,9 @@ def __call__(self, sample):
sample["semseg_mask"] = cv2.resize(
sample["semseg_mask"].astype(np.float32),
(width, height),
interpolation=cv2.INTER_NEAREST
interpolation=cv2.INTER_NEAREST,
)

if "mask" in sample:
sample["mask"] = cv2.resize(
sample["mask"].astype(np.float32),
Expand All @@ -194,12 +200,13 @@ def __call__(self, sample):

def load_image(image) -> Tuple[np.ndarray, Tuple[int, int]]:
orig_shape = image.shape[:2]
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
image = image.astype(np.float32) / 255.0
image = transform({"image": image})["image"] # C, H, W
image = image[np.newaxis, ...] # B, C, H, W
return image, orig_shape


transform = Compose(
[
Resize(
Expand All @@ -211,7 +218,7 @@ def load_image(image) -> Tuple[np.ndarray, Tuple[int, int]]:
resize_method="lower_bound",
image_interpolation_method=cv2.INTER_CUBIC,
),
NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
# NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
PrepareForNet(),
]
)
)

0 comments on commit 625ae1a

Please sign in to comment.