Skip to content

Commit 41d9b4e

Browse files
committed
Rebase conflict resolved
1 parent 2935e46 commit 41d9b4e

19 files changed

Lines changed: 274 additions & 203 deletions

carvekit/api/high.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
3+
34
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
5+
46
License: Apache License 2.0
57
"""
68
import warnings
@@ -31,26 +33,26 @@ def __init__(
3133
Initializes High Level interface.
3234
3335
Args:
34-
object_type: Interest object type. Can be "object" or "hairs-like".
35-
matting_mask_size: The size of the input image for the matting neural network.
36-
seg_mask_size: The size of the input image for the segmentation neural network.
37-
batch_size_seg: Number of images processed per one segmentation neural network call.
38-
batch_size_matting: Number of images processed per one matting neural network call.
39-
device: Processing device
40-
fp16: Use half precision. Reduce memory usage and increase speed. Experimental support
41-
trimap_prob_threshold: Probability threshold at which the prob_filter and prob_as_unknown_area operations will be applied
42-
trimap_dilation: The size of the offset radius from the object mask in pixels when forming an unknown area
43-
trimap_erosion_iters: The number of iterations of erosion that the object's mask will be subjected to before forming an unknown area
36+
object_type (str, default=object): Interest object type. Can be "object" or "hairs-like".
37+
matting_mask_size (int, default=2048): The size of the input image for the matting neural network.
38+
seg_mask_size (int, default=640): The size of the input image for the segmentation neural network.
39+
batch_size_seg (int, default=2): Number of images processed per one segmentation neural network call.
40+
batch_size_matting (int, matting=1): Number of images processed per one matting neural network call.
41+
device (Literal[cpu, cuda], default=cpu): Processing device
42+
fp16 (bool, default=False): Use half precision. Reduce memory usage and increase speed.
43+
.. CAUTION:: ⚠️ **Experimental support**
44+
trimap_prob_threshold (int, default=231): Probability threshold at which the prob_filter and prob_as_unknown_area operations will be applied
45+
trimap_dilation (int, default=30): The size of the offset radius from the object mask in pixels when forming an unknown area
46+
trimap_erosion_iters (int, default=5): The number of iterations of erosion that the object's mask will be subjected to before forming an unknown area
4447
45-
Notes:
46-
1. Changing seg_mask_size may cause an out-of-memory error if the value is too large, and it may also
47-
result in reduced precision. I do not recommend changing this value. You can change matting_mask_size in
48-
range from (1024 to 4096) to improve object edge refining quality, but it will cause extra large RAM and
48+
.. NOTE::
49+
1. Changing seg_mask_size may cause an `out-of-memory` error if the value is too large, and it may also
50+
result in reduced precision. I do not recommend changing this value. You can change `matting_mask_size` in
51+
range from `(1024 to 4096)` to improve object edge refining quality, but it will cause extra large RAM and
4952
video memory consume. Also, you can change batch size to accelerate background removal, but it also causes
5053
extra large video memory consume, if value is too big.
51-
52-
2. Changing trimap_prob_threshold, trimap_kernel_size, trimap_erosion_iters may improve object edge
53-
refining quality,
54+
2. Changing `trimap_prob_threshold`, `trimap_kernel_size`, `trimap_erosion_iters` may improve object edge
55+
refining quality.
5456
"""
5557
if object_type == "object":
5658
self.u2net = TracerUniversalB7(

carvekit/api/interface.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def __init__(
3131
Initializes an object for interacting with pipelines and other components of the CarveKit framework.
3232
3333
Args:
34-
pre_pipe: Initialized pre-processing pipeline object
35-
seg_pipe: Initialized segmentation network object
36-
post_pipe: Initialized postprocessing pipeline object
37-
device: The processing device that will be used to apply the masks to the images.
34+
pre_pipe (Union[U2NET, BASNET, DeepLabV3, TracerUniversalB7]): Initialized pre-processing pipeline object
35+
seg_pipe (Optional[Union[PreprocessingStub]]): Initialized segmentation network object
36+
post_pipe (Optional[Union[MattingMethod]]): Initialized postprocessing pipeline object
37+
device (Literal[cpu, cuda], default=cpu): The processing device that will be used to apply the masks to the images.
3838
"""
3939
self.device = device
4040
self.preprocessing_pipeline = pre_pipe

carvekit/ml/files/models_loc.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
3+
34
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
5+
46
License: Apache License 2.0
57
"""
68
import pathlib
@@ -12,7 +14,7 @@ def u2net_full_pretrained() -> pathlib.Path:
1214
"""Returns u2net pretrained model location
1315
1416
Returns:
15-
pathlib.Path to model location
17+
pathlib.Path: model location
1618
"""
1719
return downloader("u2net.pth")
1820

@@ -21,7 +23,7 @@ def basnet_pretrained() -> pathlib.Path:
2123
"""Returns basnet pretrained model location
2224
2325
Returns:
24-
pathlib.Path to model location
26+
pathlib.Path: model location
2527
"""
2628
return downloader("basnet.pth")
2729

@@ -30,7 +32,7 @@ def deeplab_pretrained() -> pathlib.Path:
3032
"""Returns basnet pretrained model location
3133
3234
Returns:
33-
pathlib.Path to model location
35+
pathlib.Path: model location
3436
"""
3537
return downloader("deeplab.pth")
3638

@@ -39,7 +41,7 @@ def fba_pretrained() -> pathlib.Path:
3941
"""Returns basnet pretrained model location
4042
4143
Returns:
42-
pathlib.Path to model location
44+
pathlib.Path: model location
4345
"""
4446
return downloader("fba_matting.pth")
4547

@@ -48,7 +50,7 @@ def tracer_b7_pretrained() -> pathlib.Path:
4850
"""Returns TRACER with EfficientNet v1 b7 encoder pretrained model location
4951
5052
Returns:
51-
pathlib.Path to model location
53+
pathlib.Path: model location
5254
"""
5355
return downloader("tracer_b7.pth")
5456

@@ -57,7 +59,7 @@ def tracer_hair_pretrained() -> pathlib.Path:
5759
"""Returns TRACER with EfficientNet v1 b7 encoder model for hair segmentation location
5860
5961
Returns:
60-
pathlib.Path to model location
62+
pathlib.Path: model location
6163
"""
6264
return downloader("tracer_hair.pth")
6365

carvekit/ml/wrap/basnet.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
3+
34
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
5+
46
License: Apache License 2.0
57
"""
68
import pathlib
@@ -34,12 +36,11 @@ def __init__(
3436
Initialize the BASNET model
3537
3638
Args:
37-
device: processing device
38-
input_image_size: input image size
39-
batch_size: the number of images that the neural network processes in one run
40-
load_pretrained: loading pretrained model
41-
fp16: use fp16 precision // not supported at this moment
42-
39+
device (Literal[cpu, cuda], default=cpu): processing device
40+
input_image_size (Union[List[int], int], default=320): input image size
41+
batch_size (int, default=10): the number of images that the neural network processes in one run
42+
load_pretrained (bool, default=True): loading pretrained model
43+
fp16 (bool, default=True): use fp16 precision **not supported at this moment**
4344
"""
4445
super(BASNET, self).__init__(n_channels=3, n_classes=1)
4546
self.device = device
@@ -60,10 +61,10 @@ def data_preprocessing(self, data: PIL.Image.Image) -> torch.Tensor:
6061
Transform input image to suitable data format for neural network
6162
6263
Args:
63-
data: input image
64+
data (PIL.Image.Image): input image
6465
6566
Returns:
66-
input for neural network
67+
torch.Tensor: input for neural network
6768
6869
"""
6970
resized = data.resize(self.input_image_size)
@@ -81,18 +82,18 @@ def data_preprocessing(self, data: PIL.Image.Image) -> torch.Tensor:
8182

8283
@staticmethod
8384
def data_postprocessing(
84-
data: torch.tensor, original_image: PIL.Image.Image
85+
data: torch.Tensor, original_image: PIL.Image.Image
8586
) -> PIL.Image.Image:
8687
"""
8788
Transforms output data from neural network to suitable data
8889
format for using with other components of this framework.
8990
9091
Args:
91-
data: output data from neural network
92-
original_image: input image which was used for predicted data
92+
data (torch.Tensor): output data from neural network
93+
original_image (PIL.Image.Image): input image which was used for predicted data
9394
9495
Returns:
95-
Segmentation mask as PIL Image instance
96+
PIL.Image.Image: Segmentation mask as `PIL Image` instance
9697
9798
"""
9899
data = data.unsqueeze(0)
@@ -109,13 +110,13 @@ def __call__(
109110
self, images: List[Union[str, pathlib.Path, PIL.Image.Image]]
110111
) -> List[PIL.Image.Image]:
111112
"""
112-
Passes input images through neural network and returns segmentation masks as PIL.Image.Image instances
113+
Passes input images through neural network and returns segmentation masks as `PIL.Image.Image` instances
113114
114115
Args:
115-
images: input images
116+
images (List[Union[str, pathlib.Path, PIL.Image.Image]]): input images
116117
117118
Returns:
118-
segmentation masks as for input images, as PIL.Image.Image instances
119+
List[PIL.Image.Image]: segmentation masks as for input images, as `PIL.Image.Image` instances
119120
120121
"""
121122
collect_masks = []

carvekit/ml/wrap/deeplab_v3.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
3+
34
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
5+
46
License: Apache License 2.0
57
"""
68
import pathlib
@@ -29,14 +31,14 @@ def __init__(
2931
fp16: bool = False,
3032
):
3133
"""
32-
Initialize the DeepLabV3 model
34+
Initialize the `DeepLabV3` model
3335
3436
Args:
35-
device: processing device
36-
input_image_size: input image size
37-
batch_size: the number of images that the neural network processes in one run
38-
load_pretrained: loading pretrained model
39-
fp16: use half precision
37+
device (Literal[cpu, cuda], default=cpu): processing device
38+
input_image_size (): input image size
39+
batch_size (int, default=10): the number of images that the neural network processes in one run
40+
load_pretrained (bool, default=True): loading pretrained model
41+
fp16 (bool, default=False): use half precision
4042
4143
"""
4244
self.device = device
@@ -69,9 +71,7 @@ def to(self, device: str):
6971
Moves neural network to specified processing device
7072
7173
Args:
72-
device (:class:`torch.device`): the desired device.
73-
Returns:
74-
None
74+
device (Literal[cpu, cuda]): the desired device.
7575
7676
"""
7777
self.network.to(device)
@@ -81,10 +81,10 @@ def data_preprocessing(self, data: PIL.Image.Image) -> torch.Tensor:
8181
Transform input image to suitable data format for neural network
8282
8383
Args:
84-
data: input image
84+
data (PIL.Image.Image): input image
8585
8686
Returns:
87-
input for neural network
87+
torch.Tensor: input for neural network
8888
8989
"""
9090
copy = data.copy()
@@ -93,18 +93,18 @@ def data_preprocessing(self, data: PIL.Image.Image) -> torch.Tensor:
9393

9494
@staticmethod
9595
def data_postprocessing(
96-
data: torch.tensor, original_image: PIL.Image.Image
96+
data: torch.Tensor, original_image: PIL.Image.Image
9797
) -> PIL.Image.Image:
9898
"""
9999
Transforms output data from neural network to suitable data
100100
format for using with other components of this framework.
101101
102102
Args:
103-
data: output data from neural network
104-
original_image: input image which was used for predicted data
103+
data (torch.Tensor): output data from neural network
104+
original_image (PIL.Image.Image): input image which was used for predicted data
105105
106106
Returns:
107-
Segmentation mask as PIL Image instance
107+
PIL.Image.Image: Segmentation mask as `PIL Image` instance
108108
109109
"""
110110
return (
@@ -115,13 +115,13 @@ def __call__(
115115
self, images: List[Union[str, pathlib.Path, PIL.Image.Image]]
116116
) -> List[PIL.Image.Image]:
117117
"""
118-
Passes input images though neural network and returns segmentation masks as PIL.Image.Image instances
118+
Passes input images though neural network and returns segmentation masks as `PIL.Image.Image` instances
119119
120120
Args:
121-
images: input images
121+
images (List[Union[str, pathlib.Path, PIL.Image.Image]]): input images
122122
123123
Returns:
124-
segmentation masks as for input images, as PIL.Image.Image instances
124+
List[PIL.Image.Image]: segmentation masks as for input images, as `PIL.Image.Image` instances
125125
126126
"""
127127
collect_masks = []

carvekit/ml/wrap/fba_matting.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ def __init__(
4343
Initialize the FBAMatting model
4444
4545
Args:
46-
device: processing device
47-
input_tensor_size: input image size
48-
batch_size: the number of images that the neural network processes in one run
49-
encoder: neural network encoder head
50-
load_pretrained: loading pretrained model
51-
fp16: use half precision
46+
device (Literal[cpu, cuda], default=cpu): processing device
47+
input_tensor_size (Union[List[int], int], default=2048): input image size
48+
batch_size (int, default=2): the number of images that the neural network processes in one run
49+
encoder (str, default=resnet50_GN_WS): neural network encoder head
50+
.. TODO::
51+
Add more encoders to documentation as Literal typehint.
52+
load_pretrained (bool, default=True): loading pretrained model
53+
fp16 (bool, default=False): use half precision
5254
5355
"""
5456
super(FBAMatting, self).__init__(encoder=encoder)
@@ -71,10 +73,10 @@ def data_preprocessing(
7173
Transform input image to suitable data format for neural network
7274
7375
Args:
74-
data: input image
76+
data (Union[PIL.Image.Image, np.ndarray]): input image
7577
7678
Returns:
77-
input for neural network
79+
Tuple[torch.FloatTensor, torch.FloatTensor]: input for neural network
7880
7981
"""
8082
resized = data.copy()
@@ -114,18 +116,18 @@ def data_preprocessing(
114116

115117
@staticmethod
116118
def data_postprocessing(
117-
data: torch.tensor, trimap: PIL.Image.Image
119+
data: torch.Tensor, trimap: PIL.Image.Image
118120
) -> PIL.Image.Image:
119121
"""
120122
Transforms output data from neural network to suitable data
121123
format for using with other components of this framework.
122124
123125
Args:
124-
data: output data from neural network
125-
trimap: Map with the area we need to refine
126+
data (torch.Tensor): output data from neural network
127+
trimap (PIL.Image.Image): Map with the area we need to refine
126128
127129
Returns:
128-
Segmentation mask as PIL Image instance
130+
PIL.Image.Image: Segmentation mask
129131
130132
"""
131133
if trimap.mode != "L":
@@ -149,11 +151,11 @@ def __call__(
149151
Passes input images though neural network and returns segmentation masks as PIL.Image.Image instances
150152
151153
Args:
152-
images: input images
153-
trimaps: Maps with the areas we need to refine
154+
images (List[Union[str, pathlib.Path, PIL.Image.Image]]): input images
155+
trimaps (List[Union[str, pathlib.Path, PIL.Image.Image]]): Maps with the areas we need to refine
154156
155157
Returns:
156-
segmentation masks as for input images, as PIL.Image.Image instances
158+
List[PIL.Image.Image]: segmentation masks as for input images
157159
158160
"""
159161

0 commit comments

Comments
 (0)