Example: Doing Grad-CAM on the Food Scoring (FS) modelΒΆ

The code below is used to perform Grad-CAM on the FS model.

You can refer to Tutorial: Doing Grad-CAM on the Food Scoring (FS) model for an in-depth explanation for intuition behind the code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import XAI

ds = XAI.datasets.DatasetFromCSV(path_to_csv="/path/to/desired/csv/data.csv")

fs_gc = XAI.FSGradCam(
    model_1=model_1,
    model_2=model_2,
    model_3=model_3,
    target_layer_name="conv2d_7b",
)

fs_results_df = fs_gc.do_gradcam(
    image_dataset=ds,
    batch_size=2,
    relu_attributions=False,
    return_metadata=True,
    output_folder="/output_folder",
    plot_gradcam=True
)

You can also execute the plot_gradcam() function independently from the do_gradcam() function by setting plot_gradcam=False and doing the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import XAI

ds = XAI.datasets.DatasetFromCSV(path_to_csv="/path/to/desired/csv/data.csv")

fs_gc = XAI.FSGradCam(
    model_1=model_1,
    model_2=model_2,
    model_3=model_3,
    target_layer_name="conv2d_7b",
)

fs_results_df = fs_gc.do_gradcam(
    image_dataset=ds,
    batch_size=2,
    relu_attributions=False,
    return_metadata=True,
    output_folder="/output_folder",
    plot_gradcam=False
)

fs_results_df = fs_gc.plot_gradcam(
    do_gradcam_df=fs_results_df,
    savefig_dir="/output_folder",
    savefig=True,
    plt_show=True,
    save_csv=True
)

After performing Grad-CAM on the FS model as shown above, you can generate a PowerPoint report of the Grad-CAM results using the XAI.report_generation.generate_pptx() function. You can refer to Example: Generating PPTX Report for Grad-CAM Results for the code.