Self-supervision

The idea of this workflow is to pretrain the backbone model by solving a so-called pretext task without labels. This way, the model learns a representation that can be later transferred to solve a downstream task in a labeled (but smaller) dataset.

  • Input:

    • Image (single-channel or multi-channel). E.g. image with shape (500, 500, 1) (y, x, channels) in 2D or (100, 500, 500, 1) (z, y, x, channels) in 3D.

  • Output:

    • Pretrained model.

In the figure below an example of crappify pretext task input images are depicted:

../_images/lucchi_train_0.png

Input image.

../_images/lucchi_train_0_crap.png

Worstened image.

Data preparation

To ensure the proper operation of the library the data directory tree should be something like this:

Expand directory tree
dataset/
├── train
│   └── x
│       ├── training-0001.tif
│       ├── training-0002.tif
│       ├── . . .
│       └── training-9999.tif
└── test
    └── x
       ├── testing-0001.tif
       ├── testing-0002.tif
       ├── . . .
       └── testing-9999.tif

Problem resolution

In BiaPy we adopt two pretext tasks that you will need to choose with pretext_task variable below (controlled with PROBLEM.SELF_SUPERVISED.PRETEXT_TASK):

  • crappify: Firstly, a pre-processing step is done where the input images are worstened by adding Gaussian noise and downsampling and upsampling them so the resolution gets worsen. This way, the images are stored in DATA.TRAIN.SSL_SOURCE_DIR, DATA.VAL.SSL_SOURCE_DIR and DATA.TEST.SSL_SOURCE_DIR for train, validation and test data respectively. This way, the model will be input with the worstened version of images and will be trained to map it to its good version (as in [FBPTGonzalezM+22]).

  • masking: The model undergoes training by acquiring the skill to restore a concealed input image. This occurs in real-time during training, where random portions of the images are automatically obscured ([HCX+22]).

After this training, the model should have learned some features of the images, which will be a good starting point in another training process. This way, if you re-train the model loading those learned model’s weigths, which can be done enabling MODEL.LOAD_CHECKPOINT if you call BiaPy with the same --name option or setting PATHS.CHECKPOINT_FILE variable to point the file directly otherwise, the training process will be easier and faster than training from scratch.

Configuration file

Find in templates/self-supervised folder of BiaPy a few YAML configuration templates for this workflow.

Special workflow configuration

Metrics

During the inference phase the performance of the test data is measured using different metrics if test masks were provided (i.e. ground truth) and, consequently, DATA.TEST.LOAD_GT is True. In the case of super-resolution the Peak signal-to-noise ratio (PSNR) metrics is calculated when the worstened image is reconstructed from individual patches.

Run

Select self-supervised learning workflow during the creation of a new configuration file:

https://raw.githubusercontent.com/BiaPyX/BiaPy-doc/master/source/img/gui/biapy_gui_ssl.jpg

Results

The results are placed in results folder under --result_dir directory with the --name given. An example of this workflow is depicted below:

../_images/pred_ssl.png

Predicted image.

../_images/lucchi_train_0.png

Original image.

Following the example, you should see that the directory /home/user/exp_results/my_2d_self-supervised has been created. If the same experiment is run 5 times, varying --run_id argument only, you should find the following directory tree:

Expand directory tree
my_2d_self-supervised/
├── config_files/
│   └── my_2d_self-supervised.yaml
├── checkpoints
│   └── my_2d_self-supervised_1-checkpoint-best.pth
└── results
   ├── my_2d_self-supervised_1
    ├── . . .
    └── my_2d_self-supervised_5
        ├── aug
           └── .tif files
       ├── charts
           ├── my_2d_self-supervised_1_*.png
           ├── my_2d_self-supervised_1_loss.png
           └── model_plot_my_2d_self-supervised_1.png
       ├── MAE_checks
           └── .tif files
       ├── per_image
           └── .tif files
        ├── tensorboard
        └── train_logs

  • config_files: directory where the .yaml filed used in the experiment is stored.

    • my_2d_self-supervised.yaml: YAML configuration file used (it will be overwrited every time the code is run).

  • checkpoints: directory where model’s weights are stored.

    • my_2d_self-supervised_1-checkpoint-best.pth: checkpoint file (best in validation) where the model’s weights are stored among other information.

    • normalization_mean_value.npy: normalization mean value (only created if DATA.NORMALIZATION.TYPE is custom). Is saved to not calculate it everytime and to use it in inference.

    • normalization_std_value.npy: normalization std value (only created if DATA.NORMALIZATION.TYPE is custom). Is saved to not calculate it everytime and to use it in inference.

  • results: directory where all the generated checks and results will be stored. There, one folder per each run are going to be placed.

    • my_2d_self-supervised_1: run 1 experiment folder.

      • aug: image augmentation samples.

      • charts:

        • my_2d_self-supervised_1_*.png: Plot of each metric used during training.

        • my_2d_self-supervised_1_loss.png: Loss over epochs plot (when training is done).

        • model_plot_my_2d_self-supervised_1.png: plot of the model.

      • MAE_checks: MAE predictions. Only available if PROBLEM.SELF_SUPERVISED.PRETEXT_TASK is masking.

        • *_original.tif: Original image.

        • *_masked.tif: Masked image inputed to the model.

        • *_reconstruction.tif: Reconstructed image.

        • *_reconstruction_and_visible.tif: Reconstructed image with the visible parts copied.

      • per_image:

        • .tif files: reconstructed images from patches.

      • tensorboard: Tensorboard logs.

      • train_logs: each row represents a summary of each epoch stats. Only avaialable if training was done.

Note

Here, for visualization purposes, only my_2d_self-supervised_1 has been described but my_2d_self-supervised_2, my_2d_self-supervised_3, my_2d_self-supervised_4 and my_2d_self-supervised_5 will follow the same structure.