xwhy.models.TorchvisionSegmentation¶
Bases: BaseSegmentation
Segmentation backend for standard torchvision models.
Supports dynamic loading of models like DeepLabV3+, FCN, and LRASPP.
Source code in src/xwhy/models/segmentation/torchvision_models.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
model
property
¶
Read-only property to access the underlying raw segmentation model.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has not been loaded yet. |
Returns:
| Type | Description |
|---|---|
Any
|
The loaded torchvision segmentation model. |
preprocess_fn
property
¶
Read-only property to access the preprocessing transform function.
Returns:
| Type | Description |
|---|---|
Callable[..., Any] | None
|
The torchvision transform function configured for the segmentation model. |
class_names
property
¶
Read-only property to access the segmentation semantic class names.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of class names supported by the loaded model. |
__init__(*, settings, model_name='deeplabv3_resnet101', seed=42, device=None, **kwargs)
¶
Initialize the Torchvision segmentation backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings
|
Global application settings for cache directories. |
required |
model_name
|
str
|
The torchvision segmentation model identifier. |
'deeplabv3_resnet101'
|
seed
|
int
|
Random seed for reproducible inference. |
42
|
device
|
device | str | None
|
Target computation device. |
None
|
**kwargs
|
Any
|
Additional arbitrary keyword arguments. |
{}
|
Source code in src/xwhy/models/segmentation/torchvision_models.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
__call__(inputs)
¶
Execute the forward pass of the segmentation model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
A PyTorch tensor containing the preprocessed images. Expected shape is typically (B, C, H, W). |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model has not been loaded yet. |
Returns:
| Type | Description |
|---|---|
Tensor
|
A PyTorch tensor containing the segmentation logits or masks |
Tensor
|
of shape (B, num_classes, H, W). |
Source code in src/xwhy/models/segmentation/torchvision_models.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
load()
¶
Load the specified model and preprocessing transforms into memory.
Returns:
| Type | Description |
|---|---|
tuple[Any, Any]
|
A tuple containing the initialized (preprocess_transforms, model). |
Source code in src/xwhy/models/segmentation/torchvision_models.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
predict(inputs)
¶
Run segmentation on preprocessed tensor inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
A preprocessed image tensor of shape (B, C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of logits/masks (B, num_classes, H, W). |
Source code in src/xwhy/models/segmentation/torchvision_models.py
227 228 229 230 231 232 233 234 235 236 237 | |