5. Left-Ventricle Segmentationο
This chapter covers PredictSegmentation.py β the all-in-one script that
segments the left ventricle in echocardiogram videos and .npy frames.
5.1. Configure the pathsο
Edit the configuration block near the top of 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"
Variable |
Meaning |
|---|---|
|
Folder scanned for |
|
DeepLabV3-ResNet50 checkpoint ( |
|
Folder where all results are written (created automatically) |
To run on your own clips, point INPUT_DIR at your folder. OUTPUT_DIR is
created automatically.
The script also reads the MEAN/STD block at the top of the file. Both the
image (.npy) and video modules now use the same statistics. Keep the
defaults for EchoNet-style data, or recompute them for your own dataset β see
Normalization Statistics.
5.2. Run the scriptο
conda activate echonet
python scripts/PredictSegmentation.py
The script prints the device it is using:
π Running device: cuda
π§ Loading DeepLabV3 model onto GPU...
π Scan complete: found 1 videos and 0 .npy files.
GPU vs CPU
If no GPU is found it falls back to CPU automatically and warns you. CPU works but is much slower because the video module does frame-by-frame inference.
5.3. What happens under the hoodο
5.3.1. Modelο
model = torchvision.models.segmentation.deeplabv3_resnet50(pretrained=False, aux_loss=False)
model.classifier[-1] = torch.nn.Conv2d(..., 1, ...) # 1 output channel = LV mask
The checkpointβs state_dict is loaded, stripping any module. prefix left over
from multi-GPU training.
5.3.2. Module A β .npy framesο
For each .npy file the script:
Normalises the array to a
112 Γ 112RGB image.Applies the configured
MEAN/STDnormalisation and runs the model.Builds a mask where the model output is positive (
pred > 0).Computes LV area as the number of mask pixels.
Saves a 448Γ448 layout image β the original and the green-masked image side by side, with the LV area printed underneath β into
segmentation_images/, named<name>_segmentation.png.Appends a row to
image_lv_area.csv.
5.3.3. Module B β videosο
For each video the script:
Reads and resizes every frame to
112 Γ 112.Runs the model in batches of 20 frames to limit memory use.
Computes the LV area (size) for every frame.
Detects systole frames as the troughs (negative peaks) of the area curve using
scipy.signal.find_peaksβ peaks at least 20 frames apart, with a prominence of 50 % of the spread between then**0.05andn**0.95order statistics of the per-frame sizes (the original EchoNet heuristic, not the 5th/95th percentiles).Writes an annotated
<name>_segmentation.avi(original + red LV overlaya moving white size trace) into
segmentation_videos/usingechonet.utils.savevideo.
Saves a
<name>_area_curve.pdfplot of LV size vs. time with systole markers intoarea_curves/.Appends one row per frame to
video_lv_area.csv.
Systole detection needs enough frames
Peak detection only runs when a video has more than 10 frames. Very short clips will simply report sizes with no systole flags.
5.4. Outputsο
After a run, OUTPUT_DIR contains (sub-folders are created on demand β you
only get the ones relevant to your inputs):
OUTPUT_DIR/
βββ segmentation_videos/ # <name>_segmentation.avi annotated videos
βββ area_curves/ # <name>_area_curve.pdf LV area-vs-time curves
βββ segmentation_images/ # <name>_segmentation.png per-image mask layouts
βββ video_lv_area.csv # Filename, Frame, Size, ComputerSystole
βββ image_lv_area.csv # Filename, LV_Area_Pixels(112x112_scale), Status
5.4.1. Example outputsο
Here is what PredictSegmentation.py produced on the bundled demo.
Video (Module B) β the annotated clip overlays the LV in red and draws a moving white trace of the area curve; the grey band marks detected systole:
Fig. 5.4.1 segmentation_videos/0X243FDE8AE0A05B6F_segmentation.avi, shown here as a GIF.ο
The matching area curve is roughly periodic, with the three detected systole frames (32, 75, 119) marked by dashed lines:
Fig. 5.4.2 area_curves/0X243FDE8AE0A05B6F_area_curve.pdf.ο
Single frame (Module A) β each .npy frame becomes an original-vs-mask
layout with the LV area printed underneath:
Fig. 5.4.3 segmentation_images/frame_0046_segmentation.png (LV area 1800 px).ο
5.4.2. video_lv_area.csv columnsο
Column |
Meaning |
|---|---|
|
Source video name |
|
Frame index (0-based) |
|
LV area in pixels at that frame (112Γ112 scale) |
|
|
5.4.3. image_lv_area.csv columnsο
Column |
Meaning |
|---|---|
|
Source |
|
LV area in pixels |
|
Always |
See Results & Troubleshooting for how to interpret and verify these outputs.