MD.ai

Getting Started

Connect to MD.ai, download a project, and prepare data for training

Connect to MD.ai

Create a client with your MD.ai domain and an access token:

import mdai

mdai_client = mdai.Client(domain="public.md.ai", access_token="YOUR_ACCESS_TOKEN")
Successfully authenticated to public.md.ai.

The domain must be an MD.ai domain (*.md.ai). An invalid token raises Authorization error. Make sure your access token is valid.

Treat your access token like a password. Don't commit it to code or share it in notebooks; read it from an environment variable instead.

In the annotator, Notebooks in the project sidebar has a ready-made snippet for the project, with the domain and IDs filled in. See Notebooks.

Download a project

Pass the project ID, which is in the project URL (https://public.md.ai/annotator/project/<project_id>) and in the annotator's Info panel:

p = mdai_client.project("LxR6zdR2", path="./lesson3-data")
Using path './lesson3-data' for data.
Preparing annotations export for project LxR6zdR2...
Preparing images export for project LxR6zdR2...
Using cached images data for project LxR6zdR2.
Using cached annotations data for project LxR6zdR2.

This downloads the project's images and annotations to path, and returns a Project for preparing data. Data that's already downloaded and up to date is reused.

ArgumentDefaultDescription
project_idThe project ID (required)
dataset_idNoneDownload only this dataset
label_group_idNoneDownload only this label group's annotations
path"."Where to save the data
force_downloadFalseDownload again even if the data is up to date
annotations_onlyFalseDownload annotations only. Returns None instead of a Project.
extract_imagesTrueExtract the downloaded image archives

Image export may be turned off for the project, or limited to admins. If images don't download, ask your project admin.

To download only annotations, model outputs or DICOM metadata, see Download data.

Use images you already have

If you have the images locally, download only the annotations and point the Project at your images. The images must be organized as StudyInstanceUID/SeriesInstanceUID/SOPInstanceUID.dcm.

mdai_client.project(PROJECT_ID, path="./data", annotations_only=True)

p = mdai.preprocess.Project(
    annotations_fp="./data/<downloaded annotations file>.json",
    images_dir="path/to/images",
)

Prepare data for training

Choose labels and classes

List the label groups and their labels, then map the label IDs you want to train on to class IDs:

p.show_label_groups()

labels_dict = {
    "L_ylR0L8": 0,  # No Lung Opacity
    "L_DlqEAl": 1,  # Lung Opacity
}
p.set_labels_dict(labels_dict)

Get a dataset

p.show_datasets()

dataset = p.get_dataset_by_name("Train")  # or p.get_dataset_by_id("D_ao3XWQ")
dataset.prepare()
dataset.show_classes()
Label id: L_ylR0L8, Class id: 0, Class text: No Lung Opacity
Label id: L_DlqEAl, Class id: 1, Class text: Lung Opacity

Split into training and validation sets

train_dataset, valid_dataset = mdai.common_utils.train_test_split(dataset)

The data is shuffled and split 90/10 by default. Change this with shuffle and validation_split.

Visualize

The mdai.visualize module displays images with their annotations:

from mdai import visualize

image_ids = dataset.get_image_ids()
visualize.display_images(image_ids[:6], cols=3)
Annotation modes visualized Displayed images

Next steps

On this page