Downloads · 30 days
2.8K
1% of all-time downloads
project-lighter/ct_fm_feature_extractor
ct_fm_feature_extractor is a machine learning model from project-lighter. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for project-lighter. The card lists the license as apache-2.0.
This model is a feature extractor for CT-FM, a model pre-trained using contrastive self-supervised learning on a huge dataset of 148,000 CT scans from the Imaging Data Commons.
Downloads · 30 days
2.8K
1% of all-time downloads
All-time downloads
263K
Public
Parameters
77.8M
311 MB on disk
Likes
13
Trending 1
Click a slice to open those files.
.safetensors311 MB · 100%
From the Hugging Face model README
This model is a feature extractor for CT-FM, a model pre-trained using contrastive self-supervised learning on a huge dataset of 148,000 CT scans from the Imaging Data Commons.
The backbone is based on a SegResNet, a 3D U-Net variant. If you want to just load the model and fine-tune, ignore the feature extraction workflow.
This notebook demonstrates how to:
Install requirements and import necessary packages
# Install lighter_zoo package
%pip install lighter_zoo -U -qq
Note: you may need to restart the kernel to use updated packages.
# Imports
import torch
from lighter_zoo import SegResEncoder
from monai.transforms import (
Compose, LoadImage, EnsureType, Orientation,
ScaleIntensityRange, CropForeground
)
from monai.inferers import SlidingWindowInferer
Download and initialize the pre-trained model from HuggingFace Hub
# Load pre-trained model
model = SegResEncoder.from_pretrained(
"project-lighter/ct_fm_feature_extractor"
)
model.eval()
Define preprocessing transforms
# Preprocessing pipeline
preprocess = Compose([
LoadImage(ensure_channel_first=True), # Load image and ensure channel dimension
EnsureType(), # Ensure correct data type
Orientation(axcodes="SPL"), # Standardize orientation
# Scale intensity to [0,1] range, clipping outliers
ScaleIntensityRange(
a_min=-1024, # Min HU value
a_max=2048, # Max HU value
b_min=0, # Target min
b_max=1, # Target max
clip=True # Clip values outside range
),
CropForeground() # Remove background to reduce computation
])
monai.transforms.croppad.array CropForeground.__init__:allow_smaller: Current default value of argument `allow_smaller=True` has been deprecated since version 1.2. It will be changed to `allow_smaller=False` in version 1.5.
Process an input CT scan and extract features
# Input path
input_path = "/home/suraj/Repositories/lighter-ct-fm/semantic-search-app/assets/scans/s0114.nii.gz"
# Preprocess input
input_tensor = preprocess(input_path)
# Run inference
with torch.no_grad():
output = model(input_tensor.unsqueeze(0))[-1]
# Average pooling compressed the feature vector across all patches. If this is not desired, remove this line and
# use the output tensor directly which will give you the feature maps in a low-dimensional space.
avg_output = torch.nn.functional.adaptive_avg_pool3d(output, 1).squeeze()
print("✅ Feature extraction completed")
print(f"Output shape: {avg_output.shape}")
✅ Feature extraction completed
Output shape: torch.Size([512])
# Plot distribution of features
import matplotlib.pyplot as plt
_ = plt.hist(avg_output.cpu().numpy(), bins=100)
