npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mri-deface-detector

v0.1.4

Published

Detects if NIFTI image has been defaced

Downloads

8

Readme

MRI Deface Detector

The de-identification of Brain MRI (Magnetic Resonance Imaging) scans, which is an important ethical step while publicly sharing datasets, is called defacing. Defacing involves removing or masking the part of the image corresponding to the face, while simultaneously ensuring brain data is not lost in the process.

This javascript tool intends to serve a trained deep learning model to do the job of detecting deface in an MRI scan quickly and efficiently. The tool will be easy to integrate into automatic validators such as the BIDS Validator.

The deep learning model consists of only 34,937 parameters, hence taking a mere 156 KB overhead on storage, while producing lighting fast predictions on the browser.

This tool can be used in the following ways:

  • Installed and used as a node package through npm
  • Run on the browser directly
  • Build custom model and run your own detector

Requirements

  • Node>=8.9.0
  • Python3 (For training custom model)

Install

Install with npm :

$ npm install mri-deface-detector

Basic Usage

const detector = require('mri-deface-detector')

// file passed as an ArrayBuffer
mri_scan = detector.readNifti(file)

// result is a prediction indicating probability of deface
detector.detectDeface(mri_scan)
	.then(result => console.log(result))

How to Run the Detector on your Browser

  1. Clone the github repository
  2. cd into the cloned repository
  3. $ npm install
  4. $ npm run watch
  5. Upload a NIFTI file to see results

Build your own Detector

This requires two steps:

  • Train and Export the Deep Learning Model
  • Using Custom Model in the Deface Detector Tool

Training the Deep Learning Model

Dataset Preparation

The existing model has been trained on the IXI-Dataset, which would formulate the Original dataset. To create the defaced dataset, a defacing tool such as pydeface was used to create the corresponding defaced dataset. Another popular defacing tool which can be used to add variabality is mri_deface.

The Dataset should be structured in the following way :

Dataset
│
└───Original
│   │	image1.nii.gz 
│   │	image2.nii.gz 
│   │	...
│
└───Defaced
    │	image3.nii.gz 
    │	image4.nii.gz
    │	...

The three-dimensional MRI scan is preprocessed to obtain 3 two-dimensional cross-sections using one of mean or slice methods. Examples for each of the two are given below.

Mean

Arithmetic mean along each of the dimensions

mean

Slice

Center slice along each of the dimensions

slice

Note : The existing model uses slice preprocessing. This is provides a faster preprocessing step and simultaneously achieves a good sensitivity and specificity. On the other hand, the mean preprocessing blurs out the intricate details of the neural anatomy, and thus will potentially allow the model to give emphasis to the actual shape of the structure, which is essential for deface detection.

For faster experimentation, the mri data is first stored as numpy(npz) files.

To do this run :

python load_dataset.py --load_path path/to/dataset1 path/to/dataset2 .. \
		       --save_path path/to/save/npz/files \
		       --preprocess [Optional] mean/slice

As this step takes substantial time due to big sizes of MRI scans, the script allows you keep appending more data as acquired to the numpy storage by just running load_dataset using the same save_path repeatedly.

Train the Model

To train the model :

python detector.py --load_path path/to/npz/files \
		   --augment_images False \
		   --export_js False
Export to JS

Set the --export_js flag to True for automatic conversion of the best model to a TensorFlowJS usable form.

Augmentations

Set the --augment_images to True to augment images, while training, using the transformations below:

  • Gaussian Blur
  • Contrast Normalization
  • Multiply
  • Flips
  • Rotations
Model Architecture

The figure below represents the simplified model architecture:

Model Architecture

Note : The weight sharing and elementwise addition features were used specifically to ensure the independence of model performance on the order in which the cross-sections are passed to the model, thus making it more robust.

The model only contains 34,937 parameters making the saved keras model only 495 KB. The size is further optimized to 156 KB while converting to the JS form.

Metrics

In concordance to the neuroimaging community, the trained model is validated using sensitivity and specificity metrics, where :

Sensitivity = TP / (TP + FN)
Specificity = TN / (TN + FP)

Note : The existing model has a sensitivity = 0.9898 and specificity = 0.9849 on the Test Set.

Using the Custom Model in the Deface Detector Tool

The model trained using detector.py (with export_js=True) will be stored in /models as model_js

const detector = require('mri-deface-detector')
var model = detector.loadModel('path/to/model_js/model.json')

// file passed as an ArrayBuffer
mri_scan = detector.readNifti(file)

detector.detectDefaceCustom(model, mri_scan, preprocess_method, input_size, callback)
	.then(result => console.log(result))

Next Steps

  • ~~Fix the NIFTI file read, which is corrupted right now.~~
  • ~~Implement mean preprocessing in javascript detector tool.~~
  • ~~Make integratable into other validators.~~
  • Add unit tests