-
Notifications
You must be signed in to change notification settings - Fork 370
Training Mask R CNN
- Clone the Mask R-CNN repository from github: https://github.com/matterport/Mask_RCNN.
- Create the folder
Mask_RCNN/samples/neurons
and copy the configuration file fromcaiman/source_extraction/volpy/mrcnn/neurons.py
file to the folder. - Create
Mask_RCNN/datasets/neurons/train
andMask_RCNN/datasets/neurons/val
folders for storing training and validation 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.
- Open the .tif file of summary images in ImageJ
- Open ROI Manager under Analyze > Tools
- Use Cell Magic Wand plugin and click on neurons in the image
- Click Add button in ROI Manager to add the selected neuron
- Repeat (3) and (4) until all neurons with clear spatial footprints are selected, click More > Save to save the result into .zip file
- 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)
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.