Commit
·
a8e8282
0
Parent(s):
Add PathoGen model: attention weights and configs
Browse files- .gitattributes +2 -0
- README.md +129 -0
- attention.pt +3 -0
- config.json +36 -0
- scheduler/scheduler_config.json +13 -0
.gitattributes
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: diffusers
|
| 4 |
+
tags:
|
| 5 |
+
- diffusion
|
| 6 |
+
- inpainting
|
| 7 |
+
- histopathology
|
| 8 |
+
- medical-imaging
|
| 9 |
+
- pathology
|
| 10 |
+
- pytorch
|
| 11 |
+
pipeline_tag: image-to-image
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# PathoGen - Histopathology Image Inpainting
|
| 15 |
+
|
| 16 |
+
PathoGen is a diffusion-based model for histopathology image inpainting. It enables realistic tissue pattern generation for filling masked regions in pathology whole slide images (WSI).
|
| 17 |
+
|
| 18 |
+
## Model Description
|
| 19 |
+
|
| 20 |
+
- **Model Type:** Diffusion model with custom attention processors
|
| 21 |
+
- **Task:** Image inpainting for histopathology images
|
| 22 |
+
- **Architecture:** UNet2DConditionModel with custom SkipAttnProcessor
|
| 23 |
+
- **Input Size:** 512x512 pixels
|
| 24 |
+
- **Framework:** PyTorch, Diffusers, PyTorch Lightning
|
| 25 |
+
|
| 26 |
+
## Usage
|
| 27 |
+
|
| 28 |
+
### Installation
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
git clone https://github.com/mkoohim/PathoGen.git
|
| 32 |
+
cd PathoGen
|
| 33 |
+
pip install -r requirements.txt
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
### Download Weights
|
| 37 |
+
|
| 38 |
+
Download the attention weights and place them in your checkpoint directory:
|
| 39 |
+
|
| 40 |
+
```python
|
| 41 |
+
from huggingface_hub import hf_hub_download
|
| 42 |
+
|
| 43 |
+
# Download attention weights
|
| 44 |
+
hf_hub_download(
|
| 45 |
+
repo_id="mkoohim/PathoGen",
|
| 46 |
+
filename="attention.pt",
|
| 47 |
+
local_dir="./checkpoints"
|
| 48 |
+
)
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
### Inference
|
| 52 |
+
|
| 53 |
+
```python
|
| 54 |
+
from src.models.pathogen import PathoGenModel
|
| 55 |
+
from omegaconf import OmegaConf
|
| 56 |
+
from PIL import Image
|
| 57 |
+
|
| 58 |
+
# Load configuration
|
| 59 |
+
config = OmegaConf.load("configs/config.yaml")
|
| 60 |
+
|
| 61 |
+
# Initialize model
|
| 62 |
+
model = PathoGenModel(config)
|
| 63 |
+
model.load_attention_weights("./checkpoints/attention.pt")
|
| 64 |
+
model.eval()
|
| 65 |
+
|
| 66 |
+
# Load images
|
| 67 |
+
image = Image.open("your_wsi_crop.jpg")
|
| 68 |
+
mask = Image.open("your_mask.jpg")
|
| 69 |
+
condition = Image.open("your_source_image.jpg")
|
| 70 |
+
|
| 71 |
+
# Run inference
|
| 72 |
+
result = model(image, mask, condition)
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
### Training
|
| 76 |
+
|
| 77 |
+
```bash
|
| 78 |
+
python train.py
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
See the [GitHub repository](https://github.com/mkoohim/PathoGen) for full training instructions.
|
| 82 |
+
|
| 83 |
+
## Model Files
|
| 84 |
+
|
| 85 |
+
| File | Description | Size |
|
| 86 |
+
|------|-------------|------|
|
| 87 |
+
| `attention.pt` | Trained attention module weights | ~190MB |
|
| 88 |
+
|
| 89 |
+
## Training Details
|
| 90 |
+
|
| 91 |
+
- **Base Model:** Stable Diffusion Inpainting UNet
|
| 92 |
+
- **Training Data:** Histopathology whole slide image crops
|
| 93 |
+
- **Optimizer:** AdamW
|
| 94 |
+
- **Learning Rate:** 1e-5
|
| 95 |
+
- **Precision:** Mixed precision (FP16)
|
| 96 |
+
|
| 97 |
+
## Intended Use
|
| 98 |
+
|
| 99 |
+
This model is designed for:
|
| 100 |
+
- Histopathology image inpainting and augmentation
|
| 101 |
+
- Research in computational pathology
|
| 102 |
+
- Data augmentation for pathology AI training
|
| 103 |
+
|
| 104 |
+
## Limitations
|
| 105 |
+
|
| 106 |
+
- Optimized for 512x512 input images
|
| 107 |
+
- Best results on H&E stained tissue images
|
| 108 |
+
- Requires GPU for reasonable inference speed
|
| 109 |
+
|
| 110 |
+
## Citation
|
| 111 |
+
|
| 112 |
+
```bibtex
|
| 113 |
+
@misc{pathogen2024,
|
| 114 |
+
title={PathoGen: Histopathology Image Inpainting with Diffusion Models},
|
| 115 |
+
author={mkoohim},
|
| 116 |
+
year={2024},
|
| 117 |
+
url={https://huggingface.co/mkoohim/PathoGen}
|
| 118 |
+
}
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
## License
|
| 122 |
+
|
| 123 |
+
This model is released under the MIT License.
|
| 124 |
+
|
| 125 |
+
## Links
|
| 126 |
+
|
| 127 |
+
- **GitHub:** [https://github.com/mkoohim/PathoGen](https://github.com/mkoohim/PathoGen)
|
| 128 |
+
- **Hugging Face:** [https://huggingface.co/mkoohim/PathoGen](https://huggingface.co/mkoohim/PathoGen)
|
| 129 |
+
|
attention.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b722cdedabdfae97012dfb713f91d740b7945e6ccaae61745cfdf4ec5853fb0e
|
| 3 |
+
size 198342750
|
config.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "UNet2DConditionModel",
|
| 3 |
+
"_diffusers_version": "0.6.0.dev0",
|
| 4 |
+
"act_fn": "silu",
|
| 5 |
+
"attention_head_dim": 8,
|
| 6 |
+
"block_out_channels": [
|
| 7 |
+
320,
|
| 8 |
+
640,
|
| 9 |
+
1280,
|
| 10 |
+
1280
|
| 11 |
+
],
|
| 12 |
+
"center_input_sample": false,
|
| 13 |
+
"cross_attention_dim": 768,
|
| 14 |
+
"down_block_types": [
|
| 15 |
+
"CrossAttnDownBlock2D",
|
| 16 |
+
"CrossAttnDownBlock2D",
|
| 17 |
+
"CrossAttnDownBlock2D",
|
| 18 |
+
"DownBlock2D"
|
| 19 |
+
],
|
| 20 |
+
"downsample_padding": 1,
|
| 21 |
+
"flip_sin_to_cos": true,
|
| 22 |
+
"freq_shift": 0,
|
| 23 |
+
"in_channels": 9,
|
| 24 |
+
"layers_per_block": 2,
|
| 25 |
+
"mid_block_scale_factor": 1,
|
| 26 |
+
"norm_eps": 1e-05,
|
| 27 |
+
"norm_num_groups": 32,
|
| 28 |
+
"out_channels": 4,
|
| 29 |
+
"sample_size": 64,
|
| 30 |
+
"up_block_types": [
|
| 31 |
+
"UpBlock2D",
|
| 32 |
+
"CrossAttnUpBlock2D",
|
| 33 |
+
"CrossAttnUpBlock2D",
|
| 34 |
+
"CrossAttnUpBlock2D"
|
| 35 |
+
]
|
| 36 |
+
}
|
scheduler/scheduler_config.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "DDIMScheduler",
|
| 3 |
+
"_diffusers_version": "0.6.0.dev0",
|
| 4 |
+
"beta_end": 0.012,
|
| 5 |
+
"beta_schedule": "scaled_linear",
|
| 6 |
+
"beta_start": 0.00085,
|
| 7 |
+
"clip_sample": false,
|
| 8 |
+
"num_train_timesteps": 1000,
|
| 9 |
+
"set_alpha_to_one": false,
|
| 10 |
+
"steps_offset": 1,
|
| 11 |
+
"trained_betas": null,
|
| 12 |
+
"skip_prk_steps": true
|
| 13 |
+
}
|