Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
@@ -4,8 +4,126 @@ tags:
|
|
4 |
- lighter
|
5 |
- model_hub_mixin
|
6 |
- pytorch_model_hub_mixin
|
|
|
|
|
|
|
7 |
---
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
- lighter
|
5 |
- model_hub_mixin
|
6 |
- pytorch_model_hub_mixin
|
7 |
+
language: en
|
8 |
+
license: apache-2.0
|
9 |
+
arxiv: 2501.09001
|
10 |
---
|
11 |
|
12 |
+
# CT-FM SegResNet
|
13 |
+
|
14 |
+
This model is a SegResNet containing the weights of the pre-trained CT-FM, using contrastive self-supervised learning on a huge dataset of 148,000 CT scans from the Imaging Data Commons.
|
15 |
+
|
16 |
+
|
17 |
+
## Running instructions
|
18 |
+
|
19 |
+
|
20 |
+
# CT-FM SegResNet Fine-tuning
|
21 |
+
|
22 |
+
This notebook demonstrates how to:
|
23 |
+
1. Load a SSL pre-trained model into a SegResNet
|
24 |
+
2. Recommended preprocessing and postprocessing steps that were used during pre-training
|
25 |
+
3. Finetuning instructions overview
|
26 |
+
|
27 |
+
## Setup
|
28 |
+
Install requirements and import necessary packages
|
29 |
+
|
30 |
+
|
31 |
+
```python
|
32 |
+
# Install lighter_zoo package
|
33 |
+
%pip install lighter_zoo -U -qq
|
34 |
+
```
|
35 |
+
|
36 |
+
Note: you may need to restart the kernel to use updated packages.
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
```python
|
41 |
+
# Imports
|
42 |
+
import torch
|
43 |
+
from lighter_zoo import SegResNet
|
44 |
+
from monai.transforms import (
|
45 |
+
Compose, LoadImage, EnsureType, Orientation,
|
46 |
+
ScaleIntensityRange, CropForeground, Invert,
|
47 |
+
Activations, AsDiscrete, KeepLargestConnectedComponent,
|
48 |
+
SaveImage
|
49 |
+
)
|
50 |
+
from monai.inferers import SlidingWindowInferer
|
51 |
+
```
|
52 |
+
|
53 |
+
## Load Model
|
54 |
+
Download and initialize the pre-trained model from HuggingFace Hub
|
55 |
+
|
56 |
+
|
57 |
+
```python
|
58 |
+
# Load pre-trained model
|
59 |
+
model = SegResNet.from_pretrained(
|
60 |
+
"project-lighter/ct_fm_segresnet"
|
61 |
+
)
|
62 |
+
```
|
63 |
+
|
64 |
+
## Setup Processing Pipelines
|
65 |
+
Define preprocessing and postprocessing transforms
|
66 |
+
|
67 |
+
|
68 |
+
```python
|
69 |
+
# Preprocessing pipeline
|
70 |
+
preprocess = Compose([
|
71 |
+
LoadImage(ensure_channel_first=True), # Load image and ensure channel dimension
|
72 |
+
EnsureType(), # Ensure correct data type
|
73 |
+
Orientation(axcodes="SPL"), # Standardize orientation
|
74 |
+
# Scale intensity to [0,1] range, clipping outliers
|
75 |
+
ScaleIntensityRange(
|
76 |
+
a_min=-1024, # Min HU value
|
77 |
+
a_max=2048, # Max HU value
|
78 |
+
b_min=0, # Target min
|
79 |
+
b_max=1, # Target max
|
80 |
+
clip=True # Clip values outside range
|
81 |
+
),
|
82 |
+
CropForeground() # Remove background to reduce computation
|
83 |
+
])
|
84 |
+
```
|
85 |
+
|
86 |
+
## Run Inference
|
87 |
+
Process an input CT scan and extract features
|
88 |
+
|
89 |
+
|
90 |
+
```python
|
91 |
+
# Configure sliding window inference
|
92 |
+
inferer = SlidingWindowInferer(
|
93 |
+
roi_size=[96, 160, 160], # Size of patches to process
|
94 |
+
sw_batch_size=2, # Number of windows to process in parallel
|
95 |
+
overlap=0.625, # Overlap between windows (reduces boundary artifacts)
|
96 |
+
mode="gaussian" # Gaussian weighting for overlap regions
|
97 |
+
)
|
98 |
+
|
99 |
+
# Input path
|
100 |
+
input_path = "/home/suraj/Repositories/semantic-search-app/assets/scans/s0114.nii.gz"
|
101 |
+
|
102 |
+
# Preprocess input
|
103 |
+
input_tensor = preprocess(input_path)
|
104 |
+
|
105 |
+
# Run inference
|
106 |
+
with torch.no_grad():
|
107 |
+
model = model.to("cuda")
|
108 |
+
input_tensor = input_tensor.to("cuda")
|
109 |
+
output = inferer(input_tensor.unsqueeze(dim=0), model)[0]
|
110 |
+
output = output.to("cpu")
|
111 |
+
|
112 |
+
|
113 |
+
print(output.shape)
|
114 |
+
```
|
115 |
+
|
116 |
+
torch.Size([2, 227, 181, 258])
|
117 |
+
|
118 |
+
|
119 |
+
## Fine-tuning instructions
|
120 |
+
|
121 |
+
The above model does not have a trained decoder which means the predictions you will get are nonsensical.
|
122 |
+
|
123 |
+
You can however use the pre-trained encoder and the model architecture to finetune on your own datasets - especially if they are small sized.
|
124 |
+
|
125 |
+
A very simple way to fit this into your pipelines is to take the model loaded above using model = SegResNet.from_pretrained('project-lighter/ct_fm_segresnet') and replace the model in your training pipeline with this.
|
126 |
+
|
127 |
+
Using Auto3DSeg with our model is the recommended approach, follow the instructions here: https://project-lighter.github.io/CT-FM/replication-guide/downstream/#tumor-segmentation-with-auto3dseg
|
128 |
+
|
129 |
+
|