Update README.md
Browse files
README.md
CHANGED
|
@@ -1,147 +1,172 @@
|
|
| 1 |
# CellFM-800M
|
| 2 |
|
| 3 |
-
##
|
| 4 |
|
| 5 |
-
CellFM
|
| 6 |
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
|
| 13 |
-
##
|
| 14 |
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
- **Dropout**: 0.1
|
| 19 |
-
-
|
| 20 |
|
| 21 |
-
##
|
| 22 |
|
| 23 |
-
###
|
| 24 |
|
| 25 |
```python
|
| 26 |
from perturblab.model.cellfm import CellFMModel
|
| 27 |
|
| 28 |
-
#
|
| 29 |
model = CellFMModel.from_pretrained('cellfm-800m')
|
| 30 |
|
| 31 |
-
#
|
|
|
|
|
|
|
|
|
|
| 32 |
model = CellFMModel.from_pretrained('./weights/cellfm-800m')
|
| 33 |
```
|
| 34 |
|
| 35 |
-
###
|
| 36 |
|
| 37 |
```python
|
| 38 |
import scanpy as sc
|
| 39 |
|
| 40 |
-
#
|
| 41 |
adata = sc.read_h5ad('your_data.h5ad')
|
| 42 |
|
| 43 |
-
#
|
| 44 |
adata = CellFMModel.prepare_data(adata)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
embeddings = model.predict_embeddings(
|
| 48 |
adata,
|
| 49 |
-
batch_size=8, #
|
| 50 |
return_cls_token=True,
|
| 51 |
)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
cell_embeddings = embeddings['cell_embeddings'] # Shape: (n_cells,
|
| 55 |
```
|
| 56 |
|
| 57 |
-
###
|
| 58 |
|
| 59 |
```python
|
| 60 |
from perturblab.model.cellfm import CellFMModel, CellFMConfig
|
| 61 |
|
| 62 |
-
#
|
| 63 |
config = CellFMConfig(
|
| 64 |
model_name='800M',
|
| 65 |
-
n_genes=
|
| 66 |
-
enc_dims=
|
| 67 |
-
enc_nlayers=
|
| 68 |
-
enc_num_heads=
|
| 69 |
-
num_cls=10, #
|
| 70 |
)
|
| 71 |
model = CellFMModel(config, for_finetuning=True)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
model.load_weights('./weights/cellfm-800m/model.pt')
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
```
|
| 79 |
|
| 80 |
-
###
|
| 81 |
|
| 82 |
```python
|
| 83 |
from perturblab.model.cellfm import CellFMPerturbationModel
|
| 84 |
from perturblab.data import PerturbationData
|
| 85 |
|
| 86 |
-
#
|
| 87 |
data = PerturbationData.from_anndata(adata)
|
| 88 |
data.split_data(train=0.7, val=0.15, test=0.15)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
model = CellFMPerturbationModel.from_pretrained('cellfm-800m')
|
| 92 |
|
| 93 |
-
#
|
| 94 |
model.init_perturbation_head_from_dataset(data)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
model.train_model(data, epochs=20, batch_size=
|
| 98 |
|
| 99 |
-
#
|
| 100 |
predictions = model.predict_perturbation(data, split='test')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
```
|
| 102 |
|
| 103 |
-
##
|
| 104 |
|
| 105 |
-
-
|
| 106 |
-
-
|
| 107 |
-
-
|
| 108 |
-
-
|
| 109 |
|
| 110 |
-
##
|
| 111 |
|
| 112 |
-
-
|
| 113 |
-
-
|
| 114 |
-
-
|
| 115 |
-
-
|
| 116 |
-
-
|
| 117 |
-
-
|
| 118 |
-
-
|
| 119 |
-
-
|
| 120 |
-
-
|
| 121 |
|
| 122 |
-
##
|
| 123 |
|
| 124 |
-
|
|
| 125 |
-
|
| 126 |
-
|
|
| 127 |
-
|
|
| 128 |
-
|
|
| 129 |
-
|
|
| 130 |
-
|
|
| 131 |
-
|
|
| 132 |
-
|
|
|
|
|
| 133 |
|
| 134 |
-
800M
|
| 135 |
|
| 136 |
-
##
|
| 137 |
|
| 138 |
-
- `config.json`:
|
| 139 |
-
- `model.pt`: PyTorch
|
| 140 |
-
- `README.md`:
|
|
|
|
| 141 |
|
| 142 |
-
##
|
| 143 |
|
| 144 |
-
|
| 145 |
|
| 146 |
```bibtex
|
| 147 |
@article{cellfm2024,
|
|
@@ -152,15 +177,15 @@ predictions = model.predict_perturbation(data, split='test')
|
|
| 152 |
}
|
| 153 |
```
|
| 154 |
|
| 155 |
-
##
|
| 156 |
|
| 157 |
-
-
|
| 158 |
-
- PyTorch
|
| 159 |
-
-
|
| 160 |
|
| 161 |
-
##
|
| 162 |
|
| 163 |
-
-
|
| 164 |
-
-
|
| 165 |
-
-
|
| 166 |
-
-
|
|
|
|
| 1 |
# CellFM-800M
|
| 2 |
|
| 3 |
+
## Model Description
|
| 4 |
|
| 5 |
+
CellFM is a large-scale foundation model pre-trained on transcriptomics of 100 million human cells using a retention-based architecture (MAE Autobin).
|
| 6 |
|
| 7 |
+
- **Model Size**: 800M parameters
|
| 8 |
+
- **Pre-training Data**: 100M human cells
|
| 9 |
+
- **Architecture**: Retention-based Transformer (MAE Autobin)
|
| 10 |
+
- **Vocabulary**: 24,072 genes
|
| 11 |
+
- **Pre-training Task**: Masked Autoencoding (MAE)
|
| 12 |
|
| 13 |
+
## Model Details
|
| 14 |
|
| 15 |
+
- **Source**: [biomed-AI/CellFM](https://github.com/biomed-AI/CellFM)
|
| 16 |
+
- **Original Framework**: MindSpore
|
| 17 |
+
- **Converted to**: PyTorch (PerturbLab format)
|
| 18 |
+
- **License**: See original repository for details
|
| 19 |
+
|
| 20 |
+
## Architecture Specifications
|
| 21 |
+
|
| 22 |
+
- **Hidden Dimension**: 1536
|
| 23 |
+
- **Number of Layers**: 40
|
| 24 |
+
- **Number of Attention Heads**: 48
|
| 25 |
- **Dropout**: 0.1
|
| 26 |
+
- **Max Sequence Length**: 2048 genes
|
| 27 |
|
| 28 |
+
## Usage
|
| 29 |
|
| 30 |
+
### Load Model
|
| 31 |
|
| 32 |
```python
|
| 33 |
from perturblab.model.cellfm import CellFMModel
|
| 34 |
|
| 35 |
+
# Load pretrained model (automatically downloads if needed)
|
| 36 |
model = CellFMModel.from_pretrained('cellfm-800m')
|
| 37 |
|
| 38 |
+
# Or use short name
|
| 39 |
+
model = CellFMModel.from_pretrained('800m')
|
| 40 |
+
|
| 41 |
+
# Or from local path
|
| 42 |
model = CellFMModel.from_pretrained('./weights/cellfm-800m')
|
| 43 |
```
|
| 44 |
|
| 45 |
+
### Generate Cell Embeddings
|
| 46 |
|
| 47 |
```python
|
| 48 |
import scanpy as sc
|
| 49 |
|
| 50 |
+
# Load your data
|
| 51 |
adata = sc.read_h5ad('your_data.h5ad')
|
| 52 |
|
| 53 |
+
# Preprocess
|
| 54 |
adata = CellFMModel.prepare_data(adata)
|
| 55 |
|
| 56 |
+
# Get embeddings (use smaller batch size for 800M model)
|
| 57 |
embeddings = model.predict_embeddings(
|
| 58 |
adata,
|
| 59 |
+
batch_size=8, # Smaller batch size for larger model
|
| 60 |
return_cls_token=True,
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Access cell embeddings
|
| 64 |
+
cell_embeddings = embeddings['cell_embeddings'] # Shape: (n_cells, 1536)
|
| 65 |
```
|
| 66 |
|
| 67 |
+
### Fine-tune for Classification
|
| 68 |
|
| 69 |
```python
|
| 70 |
from perturblab.model.cellfm import CellFMModel, CellFMConfig
|
| 71 |
|
| 72 |
+
# Initialize model with classification head
|
| 73 |
config = CellFMConfig(
|
| 74 |
model_name='800M',
|
| 75 |
+
n_genes=24072,
|
| 76 |
+
enc_dims=1536,
|
| 77 |
+
enc_nlayers=40,
|
| 78 |
+
enc_num_heads=48,
|
| 79 |
+
num_cls=10, # Number of cell types
|
| 80 |
)
|
| 81 |
model = CellFMModel(config, for_finetuning=True)
|
| 82 |
|
| 83 |
+
# Load pretrained weights
|
| 84 |
model.load_weights('./weights/cellfm-800m/model.pt')
|
| 85 |
|
| 86 |
+
# Get dataloaders
|
| 87 |
+
train_loader = model.get_dataloader(train_data, batch_size=4)['train']
|
| 88 |
+
val_loader = model.get_dataloader(val_data, batch_size=4)['train']
|
| 89 |
+
|
| 90 |
+
# Train
|
| 91 |
+
model.train_model(
|
| 92 |
+
train_dataloader=train_loader,
|
| 93 |
+
val_dataloader=val_loader,
|
| 94 |
+
num_epochs=10,
|
| 95 |
+
learning_rate=1e-4,
|
| 96 |
+
)
|
| 97 |
```
|
| 98 |
|
| 99 |
+
### Perturbation Prediction
|
| 100 |
|
| 101 |
```python
|
| 102 |
from perturblab.model.cellfm import CellFMPerturbationModel
|
| 103 |
from perturblab.data import PerturbationData
|
| 104 |
|
| 105 |
+
# Load perturbation data
|
| 106 |
data = PerturbationData.from_anndata(adata)
|
| 107 |
data.split_data(train=0.7, val=0.15, test=0.15)
|
| 108 |
|
| 109 |
+
# Initialize model
|
| 110 |
model = CellFMPerturbationModel.from_pretrained('cellfm-800m')
|
| 111 |
|
| 112 |
+
# Initialize perturbation head from dataset
|
| 113 |
model.init_perturbation_head_from_dataset(data)
|
| 114 |
|
| 115 |
+
# Train (use smaller batch size)
|
| 116 |
+
model.train_model(data, epochs=20, batch_size=4)
|
| 117 |
|
| 118 |
+
# Predict
|
| 119 |
predictions = model.predict_perturbation(data, split='test')
|
| 120 |
+
|
| 121 |
+
# Evaluate
|
| 122 |
+
metrics = model.evaluate(data, split='test')
|
| 123 |
+
print(f"Pearson correlation: {metrics['pearson']:.4f}")
|
| 124 |
```
|
| 125 |
|
| 126 |
+
## Performance Notes
|
| 127 |
|
| 128 |
+
- **Memory Requirements**: ~3-4GB GPU memory for inference (batch_size=8)
|
| 129 |
+
- **Recommended Batch Size**: 4-8 for training, 8-16 for inference
|
| 130 |
+
- **Inference Speed**: ~2-3x slower than 80M model
|
| 131 |
+
- **Loading Time**: ~5-10 seconds
|
| 132 |
|
| 133 |
+
## Model Architecture
|
| 134 |
|
| 135 |
+
- **Encoder**: Retention-based Transformer (MAE Autobin)
|
| 136 |
+
- Auto-discretization embedding layer
|
| 137 |
+
- 40 retention layers with 48 attention heads each
|
| 138 |
+
- Hidden dimension: 1536
|
| 139 |
+
- Layer normalization and residual connections
|
| 140 |
+
- **Pre-training**: Masked Autoencoding (MAE)
|
| 141 |
+
- Masks 50% of genes
|
| 142 |
+
- Reconstructs masked gene expression
|
| 143 |
+
- **Output**: Gene-level embeddings + CLS token (1536-dimensional)
|
| 144 |
|
| 145 |
+
## Comparison with 80M Model
|
| 146 |
|
| 147 |
+
| Feature | 80M | 800M |
|
| 148 |
+
|---------|-----|------|
|
| 149 |
+
| Parameters | 80M | 800M |
|
| 150 |
+
| Hidden Dim | 1536 | 1536 |
|
| 151 |
+
| Layers | 2 | 40 |
|
| 152 |
+
| Heads | 48 | 48 |
|
| 153 |
+
| Genes | 27,855 | 24,072 |
|
| 154 |
+
| Memory (Inference) | ~1-2GB | ~3-4GB |
|
| 155 |
+
| Speed | Faster | Slower |
|
| 156 |
+
| Performance | Good | Better |
|
| 157 |
|
| 158 |
+
The 800M model provides significantly better representation quality due to its deeper architecture (40 layers vs 2 layers), at the cost of increased computational requirements.
|
| 159 |
|
| 160 |
+
## Files
|
| 161 |
|
| 162 |
+
- `config.json`: Model configuration
|
| 163 |
+
- `model.pt`: Model weights (PyTorch state dict, ~3.0GB)
|
| 164 |
+
- `README.md`: This file
|
| 165 |
+
- `.gitattributes`: Git LFS configuration
|
| 166 |
|
| 167 |
+
## Citation
|
| 168 |
|
| 169 |
+
If you use CellFM in your research, please cite:
|
| 170 |
|
| 171 |
```bibtex
|
| 172 |
@article{cellfm2024,
|
|
|
|
| 177 |
}
|
| 178 |
```
|
| 179 |
|
| 180 |
+
## References
|
| 181 |
|
| 182 |
+
- Original Repository: https://github.com/biomed-AI/CellFM
|
| 183 |
+
- PyTorch Version: https://github.com/biomed-AI/CellFM-torch
|
| 184 |
+
- Paper: [Link to paper when available]
|
| 185 |
|
| 186 |
+
## Notes
|
| 187 |
|
| 188 |
+
- This model was converted from the original MindSpore checkpoint
|
| 189 |
+
- The gene vocabulary (24,072 genes) may differ from the 80M model (27,855 genes)
|
| 190 |
+
- For best results, ensure your data preprocessing matches the model's expected input format
|
| 191 |
+
- Use `CellFMModel.prepare_data()` to automatically preprocess your data
|