3. Data Preparationο
Both scripts read a folder of files and process everything they find. This chapter explains the accepted formats and how each script scans the folder.
3.1. Accepted input formatsο
Format |
Extension |
Segmentation |
EF prediction |
|---|---|---|---|
Video |
|
β |
β |
NumPy array (single frame) |
|
β |
β |
Key difference
PredictSegmentation.py auto-detects both videos and .npy files in the
same folder and handles each with a dedicated module. PredictEF.py only looks
for .avi/.mp4 videos β EF is a temporal measurement and needs a full clip.
3.2. How each script scans the folderο
3.2.1. Segmentationο
all_files = glob.glob(os.path.join(input_dir, "*"))
video_files = [f for f in all_files if f.lower().endswith(('.avi', '.mp4'))]
npy_files = [f for f in all_files if f.lower().endswith('.npy')]
So a single INPUT_DIR may mix videos and .npy frames; each group is processed
separately. (There are two modules in the code. Module A for .npy, Module B for videos.)
3.2.2. EF predictionο
self.video_paths = glob.glob(os.path.join(video_dir, "*.avi")) + \
glob.glob(os.path.join(video_dir, "*.mp4"))
Only top-level .avi/.mp4 files are picked up. Sub-folders are not searched
recursively β put your videos directly inside the input folder.
3.3. Image / video assumptionsο
The scripts were built around EchoNet-Dynamic data, so they expect:
Apical-4-chamber (A4C) echocardiogram views for meaningful EF.
Frames are internally resized to 112 Γ 112 pixels, so the source resolution does not need to match β but very low quality inputs degrade accuracy.
Videos are read with OpenCV; for EF, each frame is converted to grayscale and back to RGB (the model expects 3 identical channels).
3.3.1. .npy frame handling (segmentation only)ο
Module A is deliberately tolerant of .npy layouts. It normalises any array to a
(H, W, 3) uint8 image:
2-D
(H, W)grayscale β stacked into 3 channelschannel-first
(C, H, W)β transposed to(H, W, C)single-channel
(H, W, 1)β repeated to 3 channelspixel values are minβmax scaled to
0β255
Anything with an unsupported number of dimensions is skipped with a warning.
3.4. Normalisation constantsο
Every frame is scaled to [0, 1] and then normalised with a per-channel mean
and standard deviation, defined in a single MEAN/STD block at the top of
each script:
MEAN = [0.128, 0.129, 0.130]
STD = [0.196, 0.196, 0.197]
These same statistics are used consistently across all paths:
Script / module |
Mean (RGB) |
Std (RGB) |
Scale |
|---|---|---|---|
Segmentation β |
|
|
0β1 |
Segmentation β video |
|
|
0β1 |
EF β video |
|
|
0β1 |
Recompute for your own data
The defaults come from the EchoNet-Dynamic training set. If your data looks
different, recompute MEAN/STD with CalculateStats.py and paste the result
into both scripts β see Normalization Statistics.
3.5. Project folder layoutο
If you keep this layout, the paths in the scripts will match your files as-is:
EchoNet-dynamic/
βββ scripts/ # the three .py scripts
βββ stats/ # model weights
β βββ deeplabv3_resnet50_random.pt
β βββ r2plus1d_18_32_2_pretrained.pt
βββ demo/ # input data (replace with your own)
βββ video/ *.avi / *.mp4
βββ npy/ *.npy # only used by the segmentation script
To run on your own study, you can easily drop your videos into demo/video/ (and any .npy
frames into demo/npy/), or just point INPUT_DIR at a folder of your choice.
With the data in place, continue to LV Segmentation or EF Prediction.