1. Introductionο
1.1. What this tutorial coversο
This tutorial documents two prediction scripts β plus an optional statistics helper β for automated analysis of echocardiograms (cardiac ultrasound). Both prediction scripts are thin, easy-to-edit wrappers around the deep-learning models released with the EchoNet-Dynamic project from Stanford.
Script |
Task |
Model |
Main output |
|---|---|---|---|
|
Left-ventricle (LV) segmentation |
DeepLabV3-ResNet50 |
Masks, area curves, annotated videos, CSV |
|
Ejection-fraction (EF) prediction |
R(2+1)D-18 video network |
One EF value per video, CSV |
|
Per-channel |
β |
Printed stats to paste into the two scripts |
Who is this for?
Anyone who has a folder of echocardiogram videos (or .npy frames) and wants to
(a) trace the left-ventricle contour over time, or (b) estimate the ejection
fraction β without retraining any model. You only edit a few path variables
and run the script.
1.2. Background: what the models doο
1.2.1. Left-ventricle segmentationο
The left ventricle is the heartβs main pumping chamber. Segmentation means labelling, pixel by pixel, which part of each ultrasound frame belongs to the LV cavity. From the segmented mask we can measure the LV area in every frame. As the heart beats, this area rises (filling, diastole) and falls (contraction, systole). This curve can be used to calculate the heart rate by Fast Fourier transform (FFT).
1.2.2. Ejection fraction (EF)ο
The ejection fraction is the percentage of blood pumped out of the left ventricle with each beat:
It is one of the most important clinical measures of heart function. A normal EF
is roughly 50β70 %; lower values can indicate heart failure. PredictEF.py
predicts EF end-to-end from a video, so it frees people from measuring volumes by hand.
Note
The Status column in the EF output uses simple thresholds: Low (< 50 %),
Normal (50β70 %), or High (> 70 %). This is a coarse screening heuristic,
not a clinical diagnosis.
1.3. The end-to-end workflowο
(optional) CalculateStats.py βββΊ MEAN / STD
β paste into both scripts
βΌ
Echo videos / .npy frames
β
βββββββββββββββΊ PredictSegmentation.py βββΊ masks + LV area + systole + CSV
β
βββββββββββββββΊ PredictEF.py βββββββββββββΊ predicted EF (%) + CSV
The two scripts are independent β you can run either one on its own. They adopt different weights, named deeplabv3_resnet50_random.pt and r2plus1d_18_32_2_pretrained.pt, respectively.
1.4. How the scripts are configuredο
Neither script takes command-line arguments β you edit a small path block at
the top of the file. Paths are written as ...\EchoNet-dynamic\β¦; replace the
leading ... with the folder that holds EchoNet-dynamic on your machine. For
example, in PredictSegmentation.py:
INPUT_DIR = r"...\EchoNet-dynamic\demo\video"
WEIGHTS = r"...\EchoNet-dynamic\stats\deeplabv3_resnet50_random.pt"
OUTPUT_DIR = r"...\EchoNet-dynamic\demo_output"
and in PredictEF.py:
INPUT_DIR = r"...\EchoNet-dynamic\demo\video"
WEIGHTS = r"...\EchoNet-dynamic\stats\r2plus1d_18_32_2_pretrained.pt"
OUTPUT_CSV = r"...\EchoNet-dynamic\demo_output\EFprediction.csv"
1.5. What you need before startingο
A Python environment with PyTorch (see Installation)
The two pretrained weight files (
r2plus1d_18_32_2_pretrained.pt,deeplabv3_resnet50_random.pt)A folder of echocardiogram videos (
.avi/.mp4) and/or.npyframes(Optional but recommended) an NVIDIA GPU with CUDA