explainer
BasePipelineExplainer
¶
Bases: ABC
Core base class to explain all DiffusionPipeline: text2img, img2img and inpaint pipelines
Source code in diffusers_interpret/explainer.py
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 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 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
pipe = pipe
instance-attribute
¶
verbose = verbose
instance-attribute
¶
gradient_checkpointing = gradient_checkpointing
instance-attribute
¶
__init__(pipe: DiffusionPipeline, verbose: bool = True, gradient_checkpointing: bool = False) -> None
¶
Source code in diffusers_interpret/explainer.py
27 28 29 30 31 32 33 34 35 36 | |
__call__(prompt: str, init_image: Optional[Union[torch.FloatTensor, Image]] = None, mask_image: Optional[Union[torch.FloatTensor, Image]] = None, attribution_method: Union[str, AttributionMethods] = None, explanation_2d_bounding_box: Optional[Tuple[Tuple[int, int], Tuple[int, int]]] = None, consider_special_tokens: bool = False, clean_token_prefixes_and_suffixes: bool = True, run_safety_checker: bool = False, n_last_diffusion_steps_to_consider_for_attributions: Optional[int] = None, get_images_for_all_inference_steps: bool = True, output_type: Optional[str] = 'pil', **kwargs) -> Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]
¶
Calls a DiffusionPipeline and generates explanations for a given prompt.
Parameters:
-
prompt
(
`str`) –Input string for the diffusion model
-
init_image
(
`torch.FloatTensor` or `PIL.Image.Image`, *optional*) –Image, or tensor representing an image batch, that will be used as the starting point for the process. If provided, output will be of typePipelineImg2ImgExplainerOutputorPipelineImg2ImgExplainerForBoundingBoxOutputOutput. -
mask_image
(
`torch.FloatTensor` or `PIL.Image.Image`, *optional*) –Image, or tensor representing an image batch, to maskinit_image. White pixels in the mask will be replaced by noise and therefore repainted, while black pixels will be preserved. The mask image will be converted to a single channel (luminance) before use. -
attribution_method
(
`Union[str, AttributionMethods]`, *optional*) –AttributionMethodsorstrwith the attribution algorithms to compute. Only one algorithm per type of attribution. Ifstris provided, the same algorithm will be applied to calculate both token and pixel attributions. -
explanation_2d_bounding_box
(
`Tuple[Tuple[int, int], Tuple[int, int]]`, *optional*) –Tuple with the bounding box coordinates to calculate attributions for. The tuple is like (upper left corner, bottom right corner). Example:
((0, 0), (300, 300))If this argument is provided, the output will be of typePipelineExplainerForBoundingBoxOutputorPipelineImg2ImgExplainerForBoundingBoxOutputOutput- -
consider_special_tokens
(
bool, defaults to `True`) –If True, token attributions will also show attributions for
pipe.tokenizer.SPECIAL_TOKENS_ATTRIBUTES -
clean_token_prefixes_and_suffixes
(
bool, defaults to `True`) –If True, tries to clean prefixes and suffixes added by the
pipe.tokenizer. -
run_safety_checker
(
bool, defaults to `False`) –If True, will run the NSFW checker and return a black image if the safety checker says so.
-
n_last_diffusion_steps_to_consider_for_attributions
(
int, *optional*) –If not provided, it will calculate explanations for the output image based on all the diffusion steps. If given a number, it will only use the last provided diffusion steps. Set to
n_last_diffusion_steps_to_consider_for_attributions=0for deactivating attributions calculation. -
get_images_for_all_inference_steps
(
bool, defaults to `True`) –If True, will return all the images during diffusion in
output.all_images_during_generation -
output_type
(
str, *optional*, defaults to `"pil"`) –The output format of the generated image. Choose between PIL:
PIL.Image.Imageortorch.Tensor. -
**kwargs
–
Used to pass more arguments to DiffusionPipeline.call.
Returns:
-
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineExplainerOutput], [PipelineExplainerForBoundingBoxOutput], -
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineImg2ImgExplainerOutput] or [PipelineImg2ImgExplainerForBoundingBoxOutputOutput] -
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineExplainerOutput] ifinit_image=Noneandexplanation_2d_bounding_box=None -
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineExplainerForBoundingBoxOutput] ifinit_image=Noneandexplanation_2d_bounding_box is not None -
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineImg2ImgExplainerOutput] ifinit_image is not Noneandexplanation_2d_bounding_box=None -
Union[PipelineExplainerOutput, PipelineExplainerForBoundingBoxOutput, PipelineImg2ImgExplainerOutput, PipelineImg2ImgExplainerForBoundingBoxOutputOutput]–[
PipelineImg2ImgExplainerForBoundingBoxOutputOutput] ifinit_image is not Noneandexplanation_2d_bounding_box is not None
Source code in diffusers_interpret/explainer.py
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 | |
special_tokens_attributes() -> Set[str]
property
¶
Source code in diffusers_interpret/explainer.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
gradient_checkpointing_enable() -> None
¶
Source code in diffusers_interpret/explainer.py
333 334 | |
gradient_checkpointing_disable() -> None
¶
Source code in diffusers_interpret/explainer.py
336 337 | |
tokenizer() -> PreTrainedTokenizerBase
abstractmethod
property
¶
Source code in diffusers_interpret/explainer.py
339 340 341 342 | |
get_prompt_tokens_token_ids_and_embeds(prompt: Union[str, List[str]]) -> Tuple[List[List[str]], BatchEncoding, torch.Tensor]
abstractmethod
¶
Source code in diffusers_interpret/explainer.py
344 345 346 | |
BasePipelineImg2ImgExplainer
¶
Bases: BasePipelineExplainer
Core base class to explain img2img and inpaint pipelines
Source code in diffusers_interpret/explainer.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | |
Created: October 5, 2022