XAI package

Submodules

XAI.XAI module

XAI.XAI.count_pth(path)[source]

This function counts the number of checkpoint files (.pth) in a given directory path. This is used to ensure that there are 3 .pth files when using this XAI library for Food Scoring (FS) and 1 .pth file when using this XAI library for Food-Non-Food (FNF).

Parameters

path (str) – The directory to check.

Returns

  • count (int) – The number of .pth files in the directory.

  • pth_paths (list of paths) – A list containing the paths of all the .pth files that exists in the given path directory.

XAI.XAI.load_model(num_classes, pretrained_path=None, as_extractor=False, tuning_layers=None, resume=None)[source]

Helper function to load model.

Source code for load_model() can be found here: https://github.com/ChuaHanChong/FoodDX_P2/blob/dev2/src/modules/FoodScoring/__init__.py#L18

Parameters
  • num_classes (int) – The number of classes in the output layer. Food-non-food requires 2 classes. Food scoring requires 1 class.

  • pretrained_path (str, optional) – Path of an ImageNet pre-trained weight, by default None.

  • as_extractor (bool, optional) – Fix ConvNets as feature extractor, by default False.

  • tuning_layers (list, optional) – ConvNet layers to be fine-tuned in training, by default None.

  • resume (str, optional) – Path of a training checkpoint, by default None.

Returns

model – The loaded model with pretrained weights. A food scoring or food-non-food model.

Return type

lib..InceptionResNetV2 object

XAI.XAI.main()[source]
XAI.XAI.path_exists(path)[source]

This function checks if a given path is a valid path. This function is used as argparse.ArgumentParser(…, type=path_exists, …).

Returns

path – The original input path if it is a path.

Return type

str

Raises

NotADirectoryError – If path is not a directory, this function raises the NotADirectoryError.

Module contents

This paragraph describes the contents for the XAI package.

The XAI package has the following structure:

XAI (package)
│
├── XAI.py (module)
│
├── utils (subpackage)
│   ├── datasets.py (module)
│   ├── inception_resnet_v2.py (module)
│   ├── load_model_worker.py (module)
│   └── plot_helpers.py (module)
│
└── gradcam (subpackage)
    └── gradcam.py (module)

The subpackages and modules within those subpackages listed above are what makes performing the Grad-CAM on the Food-Non-Food (FNF) and Food Scoring (FS) models possible. The XAI.XAI module drives the Command Line Interface (CLI) tool.