Example: Doing Grad-CAM on the Food-Non-Food (FNF) modelΒΆ

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

You can refer to Tutorial: Doing Grad-CAM on the Food-Non-Food (FNF) 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
import XAI

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

fnf_gc = XAI.FNFGradCam(
    model=model,
    target_layer_name="conv2d_7b",
)

fnf_results_df = fnf_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
import XAI

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

fnf_gc = XAI.FNFGradCam(
    model=model,
    target_layer_name="conv2d_7b",
)

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

fnf_results_df = fnf_gc.plot_gradcam(
    do_gradcam_df=fnf_results_df,
    savefig_dir="/output_folder",
    savefig=True,
    plt_show=True,
    save_csv=True
)

After performing Grad-CAM on the FNF 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.