classification
automatic_batch_size_computation(datamodule, backbone, starting_batch_size)
¶
Find the optimal batch size for feature extraction. This algorithm works from the largest batch size possible and divide by 2 until it finds the largest batch size that fits in memory.
Parameters:
-
datamodule
(
SklearnClassificationDataModule | PatchSklearnClassificationDataModule
) –Datamodule used for feature extraction
-
backbone
(
ModelSignatureWrapper
) –Backbone used for feature extraction
-
starting_batch_size
(
int
) –Starting batch size to use for the search
Returns:
-
int
–Optimal batch size
Source code in quadra/utils/classification.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
|
filter_with_file(list_of_full_paths, file_path, root_path)
¶
Filter a list of items using a file containing the items to keep. Paths inside file should be relative to root_path not absolute to avoid user related issues.
Parameters:
-
list_of_full_paths
(
list[str]
) –list of items to filter
-
file_path
(
str
) –path to the file containing the items to keep
-
root_path
(
str
) –root path of the dataset
Returns:
Source code in quadra/utils/classification.py
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 |
|
find_images_and_targets(folder, types=None, class_to_idx=None, leaf_name_only=True, sort=True, exclude_filter=None, include_filter=None, label_map=None)
¶
Given a folder, extract the absolute path of all the files with a valid extension. Then assign a label based on subfolder name.
Parameters:
-
folder
(
str
) –path to main folder
-
types
(
list | None
, default:None
) –valid file extentions
-
class_to_idx
(
dict[str, int] | None
, default:None
) –dictionary of conversion btw folder name and index. Only file whose label is in dictionary key list will be considered. If None all files will be considered and a custom conversion is created.
-
leaf_name_only
(
bool
, default:True
) –if True use only the leaf folder name as label, otherwise use the full path
-
sort
(
bool
, default:True
) –if True sort the images and labels based on the image name
-
exclude_filter
(
list | None
, default:None
) –list of string filter to be used to exclude images. If None no filter will be applied.
-
include_filter
(
list | None
, default:None
) –list of string filder to be used to include images. Only images that satisfied at list one of the filter will be included.
-
label_map
(
dict[str, Any] | None
, default:None
) –dictionary of conversion btw folder name and label.
Source code in quadra/utils/classification.py
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 |
|
find_test_image(folder, types=None, exclude_filter=None, include_filter=None, include_none_class=True, test_split_file=None, label_map=None)
¶
Given a path extract images and labels with filters, labels are based on the parent folder name of the images Args: folder: root directory containing the images types: only choose images with the extensions specified, if None use default extensions exclude_filter: list of string filter to be used to exclude images. If None no filter will be applied. include_filter: list of string filter to be used to include images. If None no filter will be applied. include_none_class: if set to True convert all 'None' labels to None, otherwise ignore the image test_split_file: if defined use the split defined inside the file Returns: Two lists, one containing the images path and the other one containing the labels. Labels can be None.
Source code in quadra/utils/classification.py
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 |
|
get_file_condition(file_name, root, exclude_filter=None, include_filter=None)
¶
Check if a file should be included or excluded based on the filters provided.
Parameters:
-
file_name
(
str
) –Name of the file
-
root
(
str
) –Root directory of the file
-
exclude_filter
(
list[str] | None
, default:None
) –List of string filter to be used to exclude images. If None no filter will be applied.
-
include_filter
(
list[str] | None
, default:None
) –List of string filter to be used to include images. If None no filter will be applied.
Source code in quadra/utils/classification.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
get_results(test_labels, pred_labels, idx_to_labels=None, cl_rep_digits=3)
¶
Get prediction results from predicted and test labels.
Parameters:
-
test_labels
–
test labels
-
pred_labels
–
predicted labels
-
idx_to_labels
–
dictionary mapping indices to labels
-
cl_rep_digits
–
number of digits to use in the classification report. Default: 3
Returns:
-
str | dict
–A tuple that contains classification report as dictionary,
cm
is a pd.Dataframe representing -
DataFrame
–the Confusion Matrix, acc is the computed accuracy
Source code in quadra/utils/classification.py
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 |
|
get_split(image_dir, exclude_filter=None, include_filter=None, test_size=0.3, random_state=42, class_to_idx=None, label_map=None, n_splits=1, include_none_class=False, limit_training_data=None, train_split_file=None)
¶
Given a folder, extract the absolute path of all the files with a valid extension and name and split them into train/test.
Parameters:
-
image_dir
(
str
) –Path to the folder containing the images
-
exclude_filter
(
list[str] | None
, default:None
) –List of file name filter to be excluded: If None no filter will be applied
-
include_filter
(
list[str] | None
, default:None
) –List of file name filter to be included: If None no filter will be applied
-
test_size
(
float
, default:0.3
) –Percentage of data to be used for test
-
random_state
(
int
, default:42
) –Random state to be used for reproducibility
-
class_to_idx
(
dict[str, int] | None
, default:None
) –Dictionary of conversion btw folder name and index. Only file whose label is in dictionary key list will be considered. If None all files will be considered and a custom conversion is created.
-
label_map
(
dict | None
, default:None
) –Dictionary of conversion btw folder name and label.
-
n_splits
(
int
, default:1
) –Number of dataset subdivision (default 1 -> train/test)
-
include_none_class
(
bool
, default:False
) –If set to True convert all 'None' labels to None
-
limit_training_data
(
int | None
, default:None
) –If set to a value, limit the number of training samples to this value
-
train_split_file
(
str | None
, default:None
) –If set to a path, use the file to split the dataset
Source code in quadra/utils/classification.py
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 |
|
group_labels(labels, class_mapping)
¶
Group labels based on class_mapping.
Raises:
-
ValueError
–if a label is not in class_mapping
-
ValueError
–if a label is in class_mapping but has no corresponding value
Returns:
Example
grouped_labels, class_to_idx = group_labels(labels, class_mapping={"Good": "A", "Bad": None})
assert grouped_labels.count("Good") == labels.count("A")
assert len(class_to_idx.keys()) == 2
grouped_labels, class_to_idx = group_labels(labels, class_mapping={"Good": "A", "Defect": "B", "Bad": None})
assert grouped_labels.count("Bad") == labels.count("C") + labels.count("D")
assert len(class_to_idx.keys()) == 3
grouped_labels, class_to_idx = group_labels(labels, class_mapping={"Good": "A", "Bad": ["B", "C", "D"]})
assert grouped_labels.count("Bad") == labels.count("B") + labels.count("C") + labels.count("D")
assert len(class_to_idx.keys()) == 2
Source code in quadra/utils/classification.py
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 |
|
natural_key(string_)
¶
See http://www.codinghorror.com/blog/archives/001018.html.
Source code in quadra/utils/classification.py
57 58 59 |
|
save_classification_result(results, output_folder, test_dataloader, config, output, accuracy=None, confmat=None, grayscale_cams=None)
¶
Save csv results, confusion matrix and example images.
Parameters:
-
results
(
DataFrame
) –Dataframe containing the results
-
output_folder
(
str
) –Path to the output folder
-
confmat
(
DataFrame | None
, default:None
) –Confusion matrix in a pandas dataframe, may be None if all test labels are unknown
-
accuracy
(
float | None
, default:None
) –Accuracy of the model, is None if all test labels are unknown
-
test_dataloader
(
DataLoader
) –Dataloader used for testing
-
config
(
DictConfig
) –Configuration file
-
output
(
DictConfig
) –Output configuration
-
grayscale_cams
(
ndarray | None
, default:None
) –List of grayscale grad_cam outputs ordered as the results
Source code in quadra/utils/classification.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 |
|