Manuscript Offprint
arXiv:2611.06556LungMend: A Healthy-Only Latent Diffusion Shape Prior for Lung Mask Correction in Severe Tuberculosis CT
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
ABSTRACT
Lung segmentation models such as R231 are widely used, but they fail on severe tuberculosis (TB) CT: cavities and consolidations look like non-lung tissue and are left out of the mask. We present LungMend, a post-processing model that corrects such masks using only healthy anatomy. A latent diffusion model, trained on healthy lungs with synthetically corrupted masks, learns to recover the complete lung from the faulty mask and the bone anatomy, without any pathological data or CT intensities. LungMend recovers cavities and consolidations that R231 misses and improves accuracy on expert-annotated slices. Code: noureddinekhiati/LungMend
Index Terms— Lung segmentation, latent diffusion, shape prior
1. INTRODUCTION
Lung segmentation is the first step of most quantitative CT pipelines. The most widely used model is the open-source U-Net R231 [1], the de facto standard in 3D Slicer. It was trained on a large and diverse set of 231 CT scans that includes consolidation and several other pathological patterns. Yet it still fails on TB, where large cavities and extensive consolidations have soft-tissue density and are excluded from the mask. We observe three recurrent failure modes: (i) loss of large contiguous regions, (ii) erosion along the chest wall where disease is subpleural, and (iii) slice-to-slice inconsistency, since R231 works in 2D and its 3D masks are fragmented along the craniocaudal axis.
Related work. Dedicated pathological lung segmentation combines intensity, texture and shape cues [2, 3, 4], but it needs pathological annotations, which are costly and never exhaustive. Shape-based post-processing, from statistical shape models [5] to autoencoder priors [6, 7, 8], projects a mask onto a single plausible shape and struggles when large regions are missing. Diffusion models [9, 10] have been used to refine segmentations [11, 12, 13], but they condition on image intensities, which are what misleads the segmenter in diseased lungs. Latent diffusion [14] makes 3D medical generation tractable [15, 16], and shape diffusion enables 3D completion in vision [17, 18, 19, 20]. To our knowledge, no
prior work uses a generative shape prior learned from healthy anatomy to correct pathological lung masks.
-
Training without pathology. We introduce a corruption model that reproduces the failure modes of texture-based segmenters, which makes it possible to train a correction model on healthy lungs alone.
-
Disease-independent anchoring. We show that bone anatomy gives a reliable cue for completing large missing regions, since the rib cage is not affected by parenchymal disease.
-
Validation on real TB. We evaluate on expert-annotated TB cases and show improvements over R231 and shape-prior baselines, including 3D consistency.
2. METHOD
Given an incomplete lung mask (e.g. the R231 output) and a bone mask , LungMend estimates the complete lung mask . A model that has only seen healthy lungs knows what a lung should look like, so it can fill the regions where disease made the mask deviate from that shape. To do this, the lung is represented as a smooth shape rather than as image intensities, which are what misleads the segmenter, and the missing region is anchored by the bone anatomy, which disease does not alter. LungMend is trained in two stages, both on healthy lungs only (Fig. 1): Stage 1 learns a compact space of healthy shapes, and Stage 2 learns to recover a complete lung from a corrupted one. No pathological scan, mask or CT intensity is used.
Data and shape representation. Both stages use the same 57 healthy post-COVID scans from COVALUNG (CO-VALUNG dataset, Caen University Hospital, France). Each lung is cropped (bounding box +15 mm), cleaned (two largest components, hole filling) and resampled to ; the bone mask gets the same crop. Instead of binary masks, we use the

Fig. 1. Overview of LungMend. Training (Stage 2): the SDT of a healthy lung mask is encoded by the frozen VAE encoder (pre-trained in Stage 1) into . A pathology-mimicking corruption of the mask gives , and bone occupancy and distance give . The U-Net takes the noisy latent and (18 channels, ) and predicts . Inference: N samples are decoded by and averaged into the corrected mask and an uncertainty map. CFG denotes: classifier-free guidance.
signed distance transform (SDT),
with the Euclidean distance transform, the complement of and mm. We write for a complete mask and for an incomplete one. Each voxel stores its signed distance to the lung surface (positive inside), and a mask is recovered as . The SDT is smooth, so it can be compressed and denoised without the isolated-voxel artifacts of binary masks.
2.1. Stage 1: shape autoencoder
A 3D VAE [21] encodes ( ) into a latent ( ) and decodes it to ( ). The encoder is fully convolutional, with three stride-2 convolutions (total stride 8), so the latent is a spatial grid that keeps the layout of the lung, not a single global vector. The loss is
The first term preserves the global distance field. is a soft Dice on sigmoid-relaxed SDTs [22] that sharpens the surface. KL is the Kullback–Leibler divergence between the encoder distribution and a standard Gaussian , which regularizes the latent space. We use and . The VAE is then frozen ( , ); having seen only healthy lungs, its latent space describes healthy shapes only.
2.2. Stage 2: anatomy-conditioned latent diffusion
Pathology-mimicking corruption. Without pathological ground truth, we create (faulty, true) mask pairs from healthy
lungs. For each training lung , a corrupted mask is generated on the fly with the failure modes of texture-based segmenters: region drop (a contiguous region removed from the apex, base or lateral wall), medial drop, subpleural erosion, cavities (internal holes) and truncation (a lung cut by an oblique plane). Occasionally a leak dilates the mask, so the model also learns to remove voxels. Most corruptions are mild, so the model learns to keep correct regions unchanged, and a new corruption is drawn at each epoch.
Bone conditioning. When a large region is missing, the mask alone admits many completions. We add information that does not depend on the disease: ribs and chest wall are unaffected by parenchymal lesions and tightly bound the lung. We provide the bone occupancy and a distance-to-bone field , both pooled to , as (2 channels). Thin ribs largely vanish after pooling, so the smooth carries the distance to the chest wall.
Diffusion model. The frozen encoder gives and . We add noise to the clean latent, , where is the diffusion time, , and , are the signal and noise scales of a cosine schedule [23]. A 3D U-Net [24] receives (8 channels) and the condition (8+2 channels), i.e. 18 input channels at , and predicts the velocity [25] (8 channels). It is trained to match the target velocity :
During training, is zeroed with probability 0.2 (bone channels always kept), so the same network also learns to predict from the bone alone. This prevents the model from copying its input and enables classifier-free guidance [26]: at inference we combine the two predictions as , where uses the input mask and uses only the bone prior. The guidance weight sets the balance: gives the full prediction, and larger keeps the output closer to the input mask.
Inference and uncertainty. The input mask obtained with R231 and the bone mask are cropped, and is converted to and encoded into . We draw latents with DDIM [10] (50 steps, guided with ) and decode each with into an SDT . The mean of the decoded SDTs, , is upsampled trilinearly to the native grid and thresholded at zero, and the two largest components form the corrected mask . The standard deviation of the same SDTs, , gives a voxel-wise uncertainty map: it is close to zero where the samples agree, for example where the input mask is kept, and high where they differ, i.e. where several completions are plausible.
3. EXPERIMENTS
3.1. Data
Training data. The VAE (Stage 1) and the diffusion model (Stage 2) are trained on the same private COVALUNG cohort: 57 healthy post-COVID patients with no consolidation or other parenchymal lesion (90/10% patient-level split). No pathological scan or mask is used at any point.
Test data. We use 20 TB cases from ImageCLEF TB [27], selected with the DeepPulmo annotations [28] as the most severe in cavity burden, since large cavities and consolidations are where R231 fails most. Cavities cover 3.1–16.1% of the lung volume (largest cavity 57–962 mL, 4–36 cavities per case) and consolidation 2.0–21.9%. Patient lists and details are provided in the code repository. The DeepPulmo lung masks are not accurate enough to serve as a reference (examples in the repository), so we use only its lesion labels. To obtain a reliable reference, a thoracic radiologist corrected the R231 mask on 200 axial slices, 10 per case. Finally, we assess LungMend qualitatively on TB CT scans of 18 patients from Avicenne Hospital, Bobigny, France.
3.2. Evaluation
We evaluate in two ways:
-
Expert slices: Dice and average surface distance (ASD) on the 200 radiologist-corrected slices.
-
Lesion recovery: using the DeepPulmo cavity and consolidation labels, the fraction of each lesion included in the predicted lung mask (recall), over the 200 slices.
In addition, we assess LungMend qualitatively on the Avicenne cohort.
Table 1. Results on the 200 expert slices of severe TB cases (mean; ASD and HD95 in mm). Best in bold.
| Method | Expert slices | Lesion recall | |||
|---|---|---|---|---|---|
| Dice↑ | ASD↓ | HD95↓ | Cav.↑ | Cons.↑ | |
| R231 [1] | 0.909 | 4.38 | 27.4 | 0.869 | 0.732 |
| R231 + closing | 0.929 | 3.79 | 22.9 | 0.934 | 0.868 |
| Post-DAE [7] | 0.768 | 11.87 | 36.5 | 0.968 | 0.942 |
| Det. 3D U-Net | 0.896 | 5.04 | 26.1 | 0.557 | 0.731 |
| TotalSegmentator | 0.919 | 4.29 | 24.7 | 0.951 | 0.723 |
| VISTA3D | 0.847 | 7.61 | 38.8 | 0.726 | 0.364 |
| LungMend (ours) | 0.986 | 0.57 | 5.5 | 0.993 | 0.981 |
Table 2. Ablations on the 200 expert slices (ASD in mm). A: one training component removed at a time, with the model retrained using the same protocol as the full recipe; all rows use plain sampling (1 sample, ). B: inference steps added one at a time to the final model. R231 input: Dice 0.909.
| Dice↑ | ASD↓ | |
|---|---|---|
| A. Training components | ||
| Full recipe (reference) | 0.964 | 2.34 |
| Latent → voxel-space diffusion | 0.917 | 4.54 |
| SDT → binary mask | 0.795 | 15.02 |
| No Dice term in VAE | 0.935 | 3.68 |
| No bone mask | 0.929 | 3.47 |
| No distance-to-bone | 0.926 | 3.75 |
| No lung-channel dropout | 0.939 | 3.32 |
| No random-blob corruptions | 0.923 | 4.02 |
| No medial corruption | 0.938 | 3.16 |
| No leak corruption | 0.937 | 3.49 |
| B. Inference (final model, cumulative) | ||
| Plain sampling (1 sample, ) | 0.964 | 2.34 |
| + guidance | 0.967 | 2.15 |
| + average of 5 samples | 0.976 | 0.93 |
| + smooth SDT upsampling (= LungMend) | 0.986 | 0.57 |
3.3. Baselines and implementation
Baselines. Segmentation models applied to the CT: R231, a U-Net trained on diverse routine CT including pathological cases; TotalSegmentator [29], an nnU-Net-based model trained on 1204 CT examinations of clinical routine, with varied pathologies, scanners and sites (lung output: union of the lung lobes); and VISTA3D [30], a MONAI segmentation foundation model with a SegResNet backbone, trained on 11,454 CT volumes with anatomical and lesion annotations. Mask correction: R231 with morphological closing (10 mm ball) and hole filling; Post-DAE [7] trained on the same healthy masks and corruptions; and a deterministic 3D U-Net that takes the CT, the R231 mask and the bone mask and predicts the corrected mask, trained on the same healthy scans and corruptions. It has the same conditioning as LungMend plus the CT, but no diffusion step, which isolates the effect of the generative formulation.

Fig. 2. Qualitative results on severe TB. (a) ImageCLEF TB axial slices: R231+closing (red, top) and LungMend (blue, bottom) masks overlaid on the CT, with the annotation in yellow. The region added by LungMend is green; the volume added over R231 is given under each case. (b) Avicenne TB: coronal slices and 3D renderings for R231 and LungMend. LungMend recovers cavities and consolidated lung that R231 excludes, and removes the stair-step fragmentation of the R231 3D surface.
4. RESULTS AND DISCUSSION
Comparison with baselines. LungMend is the best method on every metric of Table 1, with the largest margin on HD95 (5.5 mm vs. 22.9 mm for the strongest baseline, R231 + closing): the baselines are accurate on most of the lung but leave large regions out, which dominates the worst-case distance. General segmentation models, even when trained on pathological CT, still decide from appearance, and more training data does not fix this (VISTA3D does worse than TotalSegmentator). Closing improves the surface but cannot recreate missing regions, and Post-DAE recovers lesions but over-segments, since it has no anatomical anchor. The deterministic U-Net has all the inputs of LungMend plus the CT, so it isolates the diffusion step: when several completions are plausible, a deterministic model returns a single estimate and loses the cavities, whereas diffusion samples plausible completions and averages them.
Number of samples and inference time. We tested several values of and chose : with fewer samples the averaged map is sparse and unstable across runs, while beyond 5 we observed no noticeable change. The cost grows linearly with , but the samples are independent and can run in parallel on the GPU, so remains practical as a one-off post-processing step per scan.
Ablation. The ablation (Table 2, single runs) shows that the information given to the model matters more than architectural choices. Bone conditioning helps where the mask alone leaves a missing region ambiguous, and pathology-mimicking corruptions are the condition for transfer: a model trained on random blobs loses the most lesion recall, because a healthy-only prior is useful only if the simulated failures
resemble the real ones. Latent diffusion on the SDT beats voxel-space diffusion and binary masks. The medial and leak corruptions and the conditioning dropout have a clear effect here. At inference, guidance and averaging reduce the variability between samples, and a single sample is already competitive.
Qualitative results. Fig. 2 compares LungMend with R231 + closing on cases selected by the volume LungMend adds. The added regions are the large cavities and consolidations on the diseased side; further examples on Avicenne scans are in Fig. 2b and in the repository.
Limitations. A healthy prior may over-complete lungs with genuine volume loss (fibrotic retraction, destroyed lung), so the uncertainty map should be read together with the input mask. The gain depends on corruptions that resemble real failures, and the resolution limits precision near the hilum. Lesion recall depends on the DeepPulmo labels, and the expert slices cover only part of the lung.
5. CONCLUSION
We presented LungMend, a post-processing model that corrects lung masks in severe TB CT using only healthy anatomy. A latent diffusion prior trained on healthy lungs, together with a corruption model that reproduces the failure modes of texture-based segmenters and a bone-anchored condition, recovers cavities and consolidations that R231 excludes, without any pathological training data or CT intensities. Repeated sampling also provides an uncertainty map that shows which regions of the corrected mask were reconstructed rather than kept from the input.
6. ACKNOWLEDGMENTS
This work was carried out within a ANRS-MIE project No. ANRS00885-R-PR and ANR MLQ-CT project No. ANR-23-CE17-0046
7. REFERENCES
[1] J. Hofmanninger et al., “Automatic lung segmentation in routine imaging is primarily a data diversity problem,” Eur. Radiol. Exp., 2020.
[2] I. Sluimer et al., “Toward automated segmentation of the pathological lung in CT,” IEEE TMI, 2005.
[3] A. Mansoor et al., “A generic approach to pathological lung segmentation,” IEEE TMI, 2014.
[4] A. P. Harrison et al., “Progressive and multi-path holistically nested networks for pathological lung segmentation,” in MICCAI, 2017.
[5] T. Heimann and H.-P. Meinzer, “Statistical shape models for 3D medical image segmentation,” Med. Image Anal., 2009.
[6] O. Oktay et al., “Anatomically constrained neural networks (ACNNs),” IEEE TMI, 2018.
[7] A. J. Larrazabal et al., “Post-DAE: anatomically plausible segmentation via denoising autoencoders,” IEEE TMI, 2020.
[8] N. Painchaud et al., “Cardiac segmentation with strong anatomical guarantees,” IEEE TMI, 2020.
[9] J. Ho et al., “Denoising diffusion probabilistic models,” in NeurIPS, 2020.
[10] J. Song et al., “Denoising diffusion implicit models,” in ICLR, 2021.
[11] J. Wolleb et al., “Diffusion models for implicit image segmentation ensembles,” in MIDL, 2022.
[12] J. Wu et al., “MedSegDiff: medical image segmentation with diffusion models,” in MIDL, 2023.
[13] M. Wang et al., “SegRefiner: model-agnostic segmentation refinement with discrete diffusion,” in NeurIPS, 2023.
[14] R. Rombach et al., “High-resolution image synthesis with latent diffusion models,” in CVPR, 2022.
[15] W. H. L. Pinaya et al., “Brain imaging generation with latent diffusion models,” in DGM4MICCAI, 2022.
[16] F. Khader et al., “Denoising diffusion probabilistic models for 3D medical image generation,” Sci. Rep., 2023.
[17] J. J. Park et al., “DeepSDF: learning continuous signed distance functions,” in CVPR, 2019.
[18] L. Zhou et al., “3D shape generation and completion through point-voxel diffusion,” in ICCV, 2021.
[19] Y.-C. Cheng et al., “SDFusion: multimodal 3D shape completion, reconstruction, and generation,” in CVPR, 2023.
[20] B. Zhang et al., “3DShape2VecSet: a 3D shape representation for neural fields,” ACM Trans. Graph., 2023.
[21] D. P. Kingma and M. Welling, “Auto-encoding variational Bayes,” in ICLR, 2014.
[22] Y. Xue et al., “Shape-aware organ segmentation by predicting signed distance maps,” in AAAI, 2020.
[23] A. Q. Nichol and P. Dhariwal, “Improved denoising diffusion probabilistic models,” in ICML, 2021.
[24] Ozgun Cicek, “3D U-Net: learning dense volumetric segmentation from sparse annotation,” in MICCAI, 2016.
[25] T. Salimans and J. Ho, “Progressive distillation for fast sampling of diffusion models,” in ICLR, 2022.
[26] J. Ho and T. Salimans, “Classifier-free diffusion guidance,” arXiv:2207.12598, 2022.
[27] S. Kozlovski et al., “Overview of ImageCLEF tuberculosis 2021,” in CLEF, 2021.
[28] Z. Tan et al., “DeepPulmoTB: benchmark dataset for tuberculosis lesions in lung CT,” Heliyon, 2024.
[29] J. Wasserthal et al., “TotalSegmentator: robust segmentation of 104 anatomic structures in CT,” Radiol. Artif. Intell., 2023.
[30] Y. He et al., “VISTA3D: a unified segmentation foundation model for 3D imaging,” in CVPR, 2025.