Skip to content

Training Mask R CNN

Changjia edited this page Nov 21, 2020 · 2 revisions

Guide for training your own datasets with Mask R-CNN for neuron detection.

Step1. Clone repository

  1. Clone the Mask R-CNN repository from github: https://github.com/matterport/Mask_RCNN.
  2. Create the folder Mask_RCNN/samples/neurons and copy the configuration file from caiman/source_extraction/volpy/mrcnn/neurons.py file to the folder.
  3. Create Mask_RCNN/datasets/neurons/train and Mask_RCNN/datasets/neurons/val folders for storing training and validation datasets.

Step2. Train/val datasets

Each dataset should be first motion corrected. Following the demo, a three-channel summary image in .tif format is saved. The .tif file will be further used for manual annotating the neurons. Summary images in .npz files are needed for training Mask R-CNN. To do this, run the following code:

np.savez(save_folder + name +'.npz', img = summary_images)

where save_folder is the train/val folder mentioned before; name is the name of the dataset; summary_images is a 3D matrix loaded from the .tif file.

Step3. Manual annotation using ImageJ

  1. Open the .tif file of summary images in ImageJ
  2. Open ROI Manager under Analyze > Tools
  3. Use Cell Magic Wand plugin and click on neurons in the image
  4. Click Add button in ROI Manager to add the selected neuron
  5. Repeat (3) and (4) until all neurons with clear spatial footprints are selected, click More > Save to save the result into .zip file
  6. Use the following code to transform .zip file into _mask.npz file so that Mask R-CNN can read. Save the output file also to the datasets folder
import zipfile

from caiman.base.rois import nf_read_roi

with zipfile.ZipFile(zip_file_name) as zf:

    names = zf.namelist()

    coords = [nf_read_roi(zf.open(n) for n in names]

    polygons = [{'name': 'polygon','all_points_x':i[:,1],'all_points_y':i[:,0]} for i in coords]

    np.savez(mask_npz_file_name, polygons)

Step4. Train your data!

You may need to first download the weights pretrained on coco datasets by following the instruction from the Mask R-CNN github repository. To train the network, Type the following code in the terminal:

python3 neurons.py train --dataset=Mask_RCNN/datasets/neurons/train --weights=coco

You can test and see the result following the code in the demo afterwards.