Basnet
BASNet saliency detection package.
BASNetConfig ¶
Bases: PretrainedConfig
Configuration for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id2label
|
Mapping[int | str, str] | None
|
Public label mapping persisted with the model. |
None
|
input_size
|
int
|
Square side length used by the image processor. |
256
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
conversion_report
|
Mapping[str, str | int | float | bool | list[str]] | None
|
Conversion metadata persisted in configs. |
None
|
kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Returns:
| Type | Description |
|---|---|
|
BASNet configuration instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> config = BASNetConfig(input_size=256)
>>> config.model_type
'basnet'
Source code in models/basnet/src/basnet/configuration_basnet.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | |
__init__ ¶
__init__(
*,
id2label: Mapping[int | str, str] | None = None,
input_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
conversion_report: Mapping[
str, str | int | float | bool | list[str]
]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize BASNet configuration.
Source code in models/basnet/src/basnet/configuration_basnet.py
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 | |
BASNetImageProcessor ¶
Bases: BaseImageProcessor
Prepare BASNet image tensors and image-space saliency maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Square side length used for model inputs. |
256
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
Returns:
| Type | Description |
|---|---|
|
BASNet image processor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> processor = BASNetImageProcessor(input_size=32)
>>> batch = processor.preprocess(Image.new("RGB", (16, 20)))
>>> tuple(batch["pixel_values"].shape)
(1, 3, 32, 32)
Source code in models/basnet/src/basnet/image_processing_basnet.py
19 20 21 22 23 24 25 26 27 28 29 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 | |
__init__ ¶
__init__(
input_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image processor settings.
Source code in models/basnet/src/basnet/image_processing_basnet.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
from_config
classmethod
¶
from_config(config: BASNetConfig) -> 'BASNetImageProcessor'
Build an image processor from BASNet configuration.
Source code in models/basnet/src/basnet/image_processing_basnet.py
58 59 60 61 62 63 64 65 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess images for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If an image input type is unsupported. |
Source code in models/basnet/src/basnet/image_processing_basnet.py
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 | |
postprocess_saliency ¶
postprocess_saliency(
saliency: Float[Tensor, "height width"]
| Float[Tensor, "batch height width"],
*,
output_size: tuple[int, int]
| Sequence[tuple[int, int]],
) -> (
Float[torch.Tensor, "height width"]
| Float[torch.Tensor, "batch height width"]
)
Resize normalized saliency maps through the PNG-space path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
saliency
|
Float[Tensor, 'height width'] | Float[Tensor, 'batch height width']
|
Normalized saliency map shaped |
required |
output_size
|
tuple[int, int] | Sequence[tuple[int, int]]
|
Target |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'height width'] | Float[Tensor, 'batch height width']
|
Resized saliency tensor in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If batch sizes and output sizes do not match. |
Examples:
>>> processor = BASNetImageProcessor()
>>> out = processor.postprocess_saliency(torch.zeros(4, 4), output_size=(8, 6))
>>> tuple(out.shape)
(8, 6)
Source code in models/basnet/src/basnet/image_processing_basnet.py
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 | |
BASNetModel ¶
Bases: PreTrainedModel
BASNet saliency predictor.
Source code in models/basnet/src/basnet/modeling_basnet.py
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
__init__ ¶
__init__(config: BASNetConfig) -> None
Initialize the BASNet architecture.
Source code in models/basnet/src/basnet/modeling_basnet.py
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
forward ¶
forward(
pixel_values: Float[
Tensor, "batch channels height width"
],
return_dict: bool | None = None,
) -> (
BASNetSaliencyOutput
| tuple[Shaped[torch.Tensor, "..."], ...]
)
Predict saliency with the BASNet forward path.
Source code in models/basnet/src/basnet/modeling_basnet.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
BASNetSaliencyOutput
dataclass
¶
Bases: ModelOutput
Output of BASNetModel.forward.
Source code in models/basnet/src/basnet/modeling_basnet.py
17 18 19 20 21 22 23 24 | |
convert_original_checkpoint ¶
convert_original_checkpoint(
*,
checkpoint: Path,
output_dir: Path,
config: BASNetConfig,
) -> dict[str, str | int | list[str]]
Convert a raw BASNet checkpoint into a save_pretrained directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
Path
|
Raw checkpoint path. |
required |
output_dir
|
Path
|
Output model directory. |
required |
config
|
BASNetConfig
|
BASNet config. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str | int | list[str]]
|
Conversion report dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If converted keys do not strictly match the target model. |
Source code in models/basnet/src/basnet/conversion.py
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 | |
normalize_saliency ¶
normalize_saliency(
pred: Float[Tensor, "... height width"],
) -> Float[torch.Tensor, "... height width"]
Normalize saliency maps independently over each spatial map.
Source code in models/basnet/src/basnet/modeling_basnet.py
149 150 151 152 153 154 155 | |
configuration_basnet ¶
Configuration for BASNet saliency detection.
BASNetConfig ¶
Bases: PretrainedConfig
Configuration for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id2label
|
Mapping[int | str, str] | None
|
Public label mapping persisted with the model. |
None
|
input_size
|
int
|
Square side length used by the image processor. |
256
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
conversion_report
|
Mapping[str, str | int | float | bool | list[str]] | None
|
Conversion metadata persisted in configs. |
None
|
kwargs
|
str | int | float | bool | None
|
Extra |
{}
|
Returns:
| Type | Description |
|---|---|
|
BASNet configuration instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> config = BASNetConfig(input_size=256)
>>> config.model_type
'basnet'
Source code in models/basnet/src/basnet/configuration_basnet.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | |
__init__ ¶
__init__(
*,
id2label: Mapping[int | str, str] | None = None,
input_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
conversion_report: Mapping[
str, str | int | float | bool | list[str]
]
| None = None,
**kwargs: str | int | float | bool | None,
) -> None
Initialize BASNet configuration.
Source code in models/basnet/src/basnet/configuration_basnet.py
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 | |
conversion ¶
Checkpoint conversion helpers for BASNet.
strip_module_prefix ¶
strip_module_prefix(
state_dict: Mapping[str, Shaped[Tensor, "..."]],
) -> dict[str, Shaped[torch.Tensor, "..."]]
Remove DataParallel module. prefixes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Mapping[str, Shaped[Tensor, '...']]
|
Raw PyTorch state dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[Tensor, '...']]
|
State dict with prefixes removed. |
Examples:
>>> strip_module_prefix({"module.a": torch.tensor(1)})["a"].item()
1
Source code in models/basnet/src/basnet/conversion.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
file_sha256 ¶
file_sha256(path: Path) -> str
Compute SHA256 for a local file.
Source code in models/basnet/src/basnet/conversion.py
38 39 40 41 42 43 44 | |
convert_original_checkpoint ¶
convert_original_checkpoint(
*,
checkpoint: Path,
output_dir: Path,
config: BASNetConfig,
) -> dict[str, str | int | list[str]]
Convert a raw BASNet checkpoint into a save_pretrained directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
Path
|
Raw checkpoint path. |
required |
output_dir
|
Path
|
Output model directory. |
required |
config
|
BASNetConfig
|
BASNet config. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str | int | list[str]]
|
Conversion report dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If converted keys do not strictly match the target model. |
Source code in models/basnet/src/basnet/conversion.py
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 | |
image_processing_basnet ¶
Image processor for BASNet saliency detection.
BASNetImageProcessor ¶
Bases: BaseImageProcessor
Prepare BASNet image tensors and image-space saliency maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Square side length used for model inputs. |
256
|
rgb_mean
|
Sequence[float]
|
RGB normalization mean. |
(0.485, 0.456, 0.406)
|
rgb_std
|
Sequence[float]
|
RGB normalization standard deviation. |
(0.229, 0.224, 0.225)
|
Returns:
| Type | Description |
|---|---|
|
BASNet image processor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> processor = BASNetImageProcessor(input_size=32)
>>> batch = processor.preprocess(Image.new("RGB", (16, 20)))
>>> tuple(batch["pixel_values"].shape)
(1, 3, 32, 32)
Source code in models/basnet/src/basnet/image_processing_basnet.py
19 20 21 22 23 24 25 26 27 28 29 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 | |
__init__ ¶
__init__(
input_size: int = 256,
rgb_mean: Sequence[float] = (0.485, 0.456, 0.406),
rgb_std: Sequence[float] = (0.229, 0.224, 0.225),
**kwargs: str | int | float | bool | None,
) -> None
Initialize image processor settings.
Source code in models/basnet/src/basnet/image_processing_basnet.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
from_config
classmethod
¶
from_config(config: BASNetConfig) -> 'BASNetImageProcessor'
Build an image processor from BASNet configuration.
Source code in models/basnet/src/basnet/image_processing_basnet.py
58 59 60 61 62 63 64 65 | |
preprocess ¶
preprocess(
images: ImageInput | Sequence[ImageInput],
*,
return_tensors: Literal["pt"] = "pt",
**kwargs: str | int | float | bool | None,
) -> BatchFeature
Preprocess images for BASNet saliency prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
ImageInput | Sequence[ImageInput]
|
RGB image or image batch. |
required |
return_tensors
|
Literal['pt']
|
Tensor framework. Only |
'pt'
|
kwargs
|
str | int | float | bool | None
|
Ignored compatibility kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
BatchFeature
|
Batch feature with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If an image input type is unsupported. |
Source code in models/basnet/src/basnet/image_processing_basnet.py
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 | |
postprocess_saliency ¶
postprocess_saliency(
saliency: Float[Tensor, "height width"]
| Float[Tensor, "batch height width"],
*,
output_size: tuple[int, int]
| Sequence[tuple[int, int]],
) -> (
Float[torch.Tensor, "height width"]
| Float[torch.Tensor, "batch height width"]
)
Resize normalized saliency maps through the PNG-space path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
saliency
|
Float[Tensor, 'height width'] | Float[Tensor, 'batch height width']
|
Normalized saliency map shaped |
required |
output_size
|
tuple[int, int] | Sequence[tuple[int, int]]
|
Target |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, 'height width'] | Float[Tensor, 'batch height width']
|
Resized saliency tensor in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If batch sizes and output sizes do not match. |
Examples:
>>> processor = BASNetImageProcessor()
>>> out = processor.postprocess_saliency(torch.zeros(4, 4), output_size=(8, 6))
>>> tuple(out.shape)
(8, 6)
Source code in models/basnet/src/basnet/image_processing_basnet.py
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 | |
resize_basnet_rgb ¶
resize_basnet_rgb(
image: Image, input_size: int = 256
) -> Float[np.ndarray, "height width channels"]
Resize an RGB image to the BASNet square input size.
Source code in models/basnet/src/basnet/image_processing_basnet.py
156 157 158 159 160 161 162 163 164 165 166 167 | |
modeling_basnet ¶
BASNet saliency model.
BASNetSaliencyOutput
dataclass
¶
Bases: ModelOutput
Output of BASNetModel.forward.
Source code in models/basnet/src/basnet/modeling_basnet.py
17 18 19 20 21 22 23 24 | |
BASNetModel ¶
Bases: PreTrainedModel
BASNet saliency predictor.
Source code in models/basnet/src/basnet/modeling_basnet.py
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
__init__ ¶
__init__(config: BASNetConfig) -> None
Initialize the BASNet architecture.
Source code in models/basnet/src/basnet/modeling_basnet.py
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
forward ¶
forward(
pixel_values: Float[
Tensor, "batch channels height width"
],
return_dict: bool | None = None,
) -> (
BASNetSaliencyOutput
| tuple[Shaped[torch.Tensor, "..."], ...]
)
Predict saliency with the BASNet forward path.
Source code in models/basnet/src/basnet/modeling_basnet.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
normalize_saliency ¶
normalize_saliency(
pred: Float[Tensor, "... height width"],
) -> Float[torch.Tensor, "... height width"]
Normalize saliency maps independently over each spatial map.
Source code in models/basnet/src/basnet/modeling_basnet.py
149 150 151 152 153 154 155 | |