This repository contains the code to experiment with our framework of Causality-driven Convolutional Neural Networks:
[ESWA 2024 (Journal paper)] [ICCV 2023 (Conference paper)]
We present a new plug-in attention-inspired module to discover and exploit weak causal signals directly from images via neural networks for improving CNN classifiers. We model how the presence of a feature in one part of the image affects the appearance of another feature in a different part of the image. Our Mulcat method consists of a CNN backbone and a causality-factors extractor module, which computes weights to enhance each feature map according to its causal influence in the scene. We demonstrated the effectiveness of our method both in fully-supervised and few-shot learning and for different medical image classification tasks. Our lightweight block is architecture-independent and easy to add to existing convolutional models, including attention-based solutions, where it enables synergy. With this method, the overall classification performance can be increased and more robust predictions (which focus on relevant parts of the image) are produced.
To Get Started with the coding, just skip to the section at the bottom!
The rationale behind the whole project is the concept of causal disposition from Lopez-Paz, D. (2017). Given an image dataset, we can have insights into observable footprints that reveal the dispositions of the object categories appearing in the images. For instance, if two objects/artifacts A and B are present in the images, we can define the causal disposition of A w.r.t. B by counting the number of images in the dataset where if we remove A then B also disappear.
Intuition: any causal disposition induces a set of conditional asymmetries between the artifacts from an image (features, object categories, etc.) that represent (weak) causality signals regarding the real-world scene. Can computer vision models infer such asymmetries autonomously?
Terziyan and Vitko (2023) suggests a way to compute estimates for possible causal relationships within images via CNNs.
When a feature map
Given a pair of feature maps
On the other hand, Lehmer method entails computing
We propose a new lightweight module - causality factors extractor - to compute weights for the feature maps. Specifically, we propose two new schemes, namely Mulcat and Mulcatbool, to embed causal information in CNNs.
Instead of concatenating the flattened causality map to the flattened set of feature maps just before the classifier as in Terziyan and Vitko (2023), here we propose to use the causality map to compute a vector of causality factors that multiply (i.e., weighs) the feature maps so that each feature map is strengthened according to its causal influence within the image's scene.
After multiplication, the obtained causality-driven version of the feature maps is flattened and concatenated to the flattened original ones, producing a
At the core of the mulcat option stands our causality factors extractor module, which yields the vector of weights needed to multiply the feature maps. The main idea here is to look for asymmetries between elements opposite the main diagonal of the causality map, as they represent conditional asymmetries entailing possible cause-effect relationships. Indeed, some features may be more often found on the left side of the arrow (i.e.,
In this project, we have utilized Docker to run our experiments on a multi-GPU cluster at our Institute. Therefore, you'll find side files to assist in the launching of the experiments, such as the 'docker_run_[].sh' bash scripts and the 'Dockerfile' files. If you're used to utilizing Docker too, then simply build your Image and Container following our instructions. Instead, if you want to execute the framework on your local machine without any Docker, just install the required dependencies and packages.
To make things easier and tidier, we'll build two separate docker images: one for the training experiments (Dockerfile) and the other for the test/inference execution (Dockerfile_test). As you can see, they both inherit (i.e., "FROM" command) the "nvcr.io/nvidia/pytorch" image. In addition, they optionally 'pip install' some packages such as 'tqdm, einops, seaborn, scikit-learn' in the "RUN" command, and, finally, specify the correct "ENTRYPOINT" for the container.
Log in to your Docker and build two images with the above-mentioned Dockerfiles. Use a meaningful tag for them.
The 'docker_run_loop_inner.sh' bash script is used to run the causality-driven and baseline versions of the networks with a hyperparameters optimization in a loop. The loop itself is actually performed within the python script itself, but here in the bash script, you must specify the lists of possible hyperparameter values.
That bash script ultimately executes the 'docker run' command on the training docker image built at the previous step. Please, follow the instructions contained in that bash script to customise it and to set everything you need about the GPU device(s), accordingly to your system setup.
Upon completion of the model's training, you will obtain a new folder with the training results. See the screenshot below for a visual example:
where caumps will contain the causality maps obtained during validation epochs (in numpy format), ftrmps will contain the feature maps extracted from the hooked inner layers of the network during validation epochs (in numpy format), and orgimg optionally contains a sample of the input validation images onto which feature maps and causality maps are extracted.
According to the models' saving policy, you will find .pth files for the Top-k best-performing models obtained during training. As of now, the saving policy is set to the Top-3 models, and the metric defining the best performers is a lower loss on the validation set. You can modify the saving policy and the metric at your ease.
To facilitate model tracking, during the whole training process, a Tensorboard events.out.tfevents. object is stored. It will keep track of relevant curves and metrics during the epochs, to conveniently visualize them. You may follow the 'tutorial' in the next subsection to know how to use TensorboardX events.
- On your local machine, create a new conda environment with torch 2.0, tensorboard 2.13, tensorboardX 2.6.1. For the latter, you could just 'pip install tensorboardX', refer to the official page.
- In a shell, locate (cd) the result folder obtained after training (which contains the events.out.tfevents. file)
- Type the command: 'tensorboard --logdir . --port 6001'. That will prompt you with a URL link, just CTRL+click on it, and a browser page will open. Important IN CASE THAT COMMAND DID NOT WORK (i.e., the link opens an error page), try the command: 'tensorboard --logdir . --host localhost --port 8088'.
Please cite our paper if you found this code useful for your research:
@article{carloni2024exploiting,
title={Exploiting causality signals in medical images: A pilot study with empirical results},
author={Carloni, Gianluca and Colantonio, Sara},
journal={Expert Systems with Applications},
pages={123433},
year={2024},
publisher={Elsevier}
}