Upload folder using huggingface_hub
Browse files- README.md +159 -46
- preprocessor_config.json +14 -0
README.md
CHANGED
|
@@ -6,6 +6,7 @@ tags:
|
|
| 6 |
- cervical-cancer
|
| 7 |
- pytorch
|
| 8 |
- safetensors
|
|
|
|
| 9 |
datasets:
|
| 10 |
- custom
|
| 11 |
metrics:
|
|
@@ -15,7 +16,7 @@ pipeline_tag: image-classification
|
|
| 15 |
library_name: pytorch
|
| 16 |
---
|
| 17 |
|
| 18 |
-
# Cervical
|
| 19 |
|
| 20 |
## Model Description
|
| 21 |
|
|
@@ -27,29 +28,112 @@ This model classifies cervical images into 3 transformation zone types, which is
|
|
| 27 |
| 1 | Type 2 | Transformation zone partially visible (extends into endocervical canal) |
|
| 28 |
| 2 | Type 3 | Transformation zone not visible (entirely within endocervical canal) |
|
| 29 |
|
|
|
|
|
|
|
| 30 |
## Model Architecture
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
```
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
```
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
| Parameter | Value |
|
| 55 |
|-----------|-------|
|
|
@@ -57,7 +141,11 @@ FC(128β3) β Output
|
|
| 57 |
| Batch Size | 32 |
|
| 58 |
| Dropout | 0.4 |
|
| 59 |
| Optimizer | Adam |
|
|
|
|
| 60 |
| Epochs | 50 |
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
## Performance
|
| 63 |
|
|
@@ -66,23 +154,25 @@ FC(128β3) β Output
|
|
| 66 |
| **Validation Accuracy** | 61.69% |
|
| 67 |
| **Macro F1 Score** | 61.81% |
|
| 68 |
|
| 69 |
-
### Per-Class
|
| 70 |
|
| 71 |
-
| Type | F1 Score |
|
| 72 |
-
|
| 73 |
-
| Type 1 | 68.32% |
|
| 74 |
-
| Type 2 | 56.41% |
|
| 75 |
-
| Type 3 | 60.69% |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
## Usage
|
| 78 |
|
| 79 |
### Installation
|
| 80 |
|
| 81 |
```bash
|
| 82 |
-
pip install torch torchvision safetensors
|
| 83 |
```
|
| 84 |
|
| 85 |
-
### Quick Start
|
| 86 |
|
| 87 |
```python
|
| 88 |
import torch
|
|
@@ -114,51 +204,74 @@ print(f"Prediction: {labels[prediction]}")
|
|
| 114 |
print(f"Confidence: {probabilities[0][prediction]:.2%}")
|
| 115 |
```
|
| 116 |
|
| 117 |
-
###
|
| 118 |
|
| 119 |
```python
|
| 120 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 121 |
import torch
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
# Download
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
# Load
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
|
| 133 |
-
import json
|
| 134 |
-
|
| 135 |
-
with open(config_path) as f:
|
| 136 |
config = json.load(f)
|
| 137 |
|
| 138 |
-
model = BaseCNN(**config['model_config'])
|
| 139 |
-
model.load_state_dict(
|
| 140 |
model.eval()
|
|
|
|
|
|
|
| 141 |
```
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
## Limitations
|
| 144 |
|
| 145 |
- Model was trained on a specific dataset and may not generalize to all cervical imaging equipment
|
| 146 |
- Type 2 classification has lower accuracy (56.41% F1) as it represents an intermediate state
|
| 147 |
-
- Input images should be
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
|
| 153 |
```bibtex
|
| 154 |
-
@misc{
|
| 155 |
-
title={Cervical
|
| 156 |
-
author={
|
| 157 |
year={2026},
|
| 158 |
-
howpublished={\url{https://huggingface.co/
|
| 159 |
}
|
| 160 |
```
|
| 161 |
|
|
|
|
|
|
|
| 162 |
## License
|
| 163 |
|
| 164 |
MIT License
|
|
|
|
| 6 |
- cervical-cancer
|
| 7 |
- pytorch
|
| 8 |
- safetensors
|
| 9 |
+
- cnn
|
| 10 |
datasets:
|
| 11 |
- custom
|
| 12 |
metrics:
|
|
|
|
| 16 |
library_name: pytorch
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# CerviGuard - Cervical Transformation Zone Classifier
|
| 20 |
|
| 21 |
## Model Description
|
| 22 |
|
|
|
|
| 28 |
| 1 | Type 2 | Transformation zone partially visible (extends into endocervical canal) |
|
| 29 |
| 2 | Type 3 | Transformation zone not visible (entirely within endocervical canal) |
|
| 30 |
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
## Model Architecture
|
| 34 |
|
| 35 |
+
### Overview
|
| 36 |
+
|
| 37 |
+
**BaseCNN** - A simple convolutional neural network with 4 conv blocks and 2 fully connected layers.
|
| 38 |
|
| 39 |
```
|
| 40 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
+
β INPUT (256Γ256Γ3) β
|
| 42 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
+
β
|
| 44 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
β CONV BLOCK 1 β
|
| 46 |
+
β Conv2d(3β32, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
|
| 47 |
+
β Output: 128Γ128Γ32 β
|
| 48 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
+
β
|
| 50 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
β CONV BLOCK 2 β
|
| 52 |
+
β Conv2d(32β64, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
|
| 53 |
+
β Output: 64Γ64Γ64 β
|
| 54 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
+
β
|
| 56 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
β CONV BLOCK 3 β
|
| 58 |
+
β Conv2d(64β128, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
|
| 59 |
+
β Output: 32Γ32Γ128 β
|
| 60 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
+
β
|
| 62 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
+
β CONV BLOCK 4 β
|
| 64 |
+
β Conv2d(128β256, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2)β
|
| 65 |
+
β Output: 16Γ16Γ256 β
|
| 66 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
β
|
| 68 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 69 |
+
β GLOBAL POOLING β
|
| 70 |
+
β AdaptiveAvgPool2d(1Γ1) β
|
| 71 |
+
β Output: 1Γ1Γ256 β Flatten β 256 β
|
| 72 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
β
|
| 74 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
β FC BLOCK 1 β
|
| 76 |
+
β Linear(256β256) β ReLU β Dropout(0.4) β
|
| 77 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
+
β
|
| 79 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
+
β FC BLOCK 2 β
|
| 81 |
+
β Linear(256β128) β ReLU β Dropout(0.4) β
|
| 82 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 83 |
+
β
|
| 84 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 85 |
+
β CLASSIFIER β
|
| 86 |
+
β Linear(128β3) β
|
| 87 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
+
β
|
| 89 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
+
β OUTPUT (3 logits) β
|
| 91 |
+
β [Type 1, Type 2, Type 3] β
|
| 92 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
```
|
| 94 |
|
| 95 |
+
### Layer Details
|
| 96 |
+
|
| 97 |
+
| Layer | Type | In Channels | Out Channels | Kernel | Output Size |
|
| 98 |
+
|-------|------|-------------|--------------|--------|-------------|
|
| 99 |
+
| conv_layers.0 | Conv2d | 3 | 32 | 3Γ3 | 256Γ256Γ32 |
|
| 100 |
+
| conv_layers.1 | BatchNorm2d | 32 | 32 | - | 256Γ256Γ32 |
|
| 101 |
+
| conv_layers.2 | ReLU | - | - | - | 256Γ256Γ32 |
|
| 102 |
+
| conv_layers.3 | MaxPool2d | - | - | 2Γ2 | 128Γ128Γ32 |
|
| 103 |
+
| conv_layers.4 | Conv2d | 32 | 64 | 3Γ3 | 128Γ128Γ64 |
|
| 104 |
+
| conv_layers.5 | BatchNorm2d | 64 | 64 | - | 128Γ128Γ64 |
|
| 105 |
+
| conv_layers.6 | ReLU | - | - | - | 128Γ128Γ64 |
|
| 106 |
+
| conv_layers.7 | MaxPool2d | - | - | 2Γ2 | 64Γ64Γ64 |
|
| 107 |
+
| conv_layers.8 | Conv2d | 64 | 128 | 3Γ3 | 64Γ64Γ128 |
|
| 108 |
+
| conv_layers.9 | BatchNorm2d | 128 | 128 | - | 64Γ64Γ128 |
|
| 109 |
+
| conv_layers.10 | ReLU | - | - | - | 64Γ64Γ128 |
|
| 110 |
+
| conv_layers.11 | MaxPool2d | - | - | 2Γ2 | 32Γ32Γ128 |
|
| 111 |
+
| conv_layers.12 | Conv2d | 128 | 256 | 3Γ3 | 32Γ32Γ256 |
|
| 112 |
+
| conv_layers.13 | BatchNorm2d | 256 | 256 | - | 32Γ32Γ256 |
|
| 113 |
+
| conv_layers.14 | ReLU | - | - | - | 32Γ32Γ256 |
|
| 114 |
+
| conv_layers.15 | MaxPool2d | - | - | 2Γ2 | 16Γ16Γ256 |
|
| 115 |
+
| adaptive_pool | AdaptiveAvgPool2d | - | - | - | 1Γ1Γ256 |
|
| 116 |
+
| fc_layers.0 | Linear | 256 | 256 | - | 256 |
|
| 117 |
+
| fc_layers.1 | ReLU | - | - | - | 256 |
|
| 118 |
+
| fc_layers.2 | Dropout | - | - | p=0.4 | 256 |
|
| 119 |
+
| fc_layers.3 | Linear | 256 | 128 | - | 128 |
|
| 120 |
+
| fc_layers.4 | ReLU | - | - | - | 128 |
|
| 121 |
+
| fc_layers.5 | Dropout | - | - | p=0.4 | 128 |
|
| 122 |
+
| classifier | Linear | 128 | 3 | - | 3 |
|
| 123 |
+
|
| 124 |
+
### Model Summary
|
| 125 |
+
|
| 126 |
+
| Property | Value |
|
| 127 |
+
|----------|-------|
|
| 128 |
+
| **Total Parameters** | 488,451 |
|
| 129 |
+
| **Trainable Parameters** | 488,451 |
|
| 130 |
+
| **Input Size** | (B, 3, 256, 256) |
|
| 131 |
+
| **Output Size** | (B, 3) |
|
| 132 |
+
| **Model Size** | ~1.9 MB |
|
| 133 |
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
## Training Configuration
|
| 137 |
|
| 138 |
| Parameter | Value |
|
| 139 |
|-----------|-------|
|
|
|
|
| 141 |
| Batch Size | 32 |
|
| 142 |
| Dropout | 0.4 |
|
| 143 |
| Optimizer | Adam |
|
| 144 |
+
| Loss Function | CrossEntropyLoss |
|
| 145 |
| Epochs | 50 |
|
| 146 |
+
| Best Epoch | 41 |
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
|
| 150 |
## Performance
|
| 151 |
|
|
|
|
| 154 |
| **Validation Accuracy** | 61.69% |
|
| 155 |
| **Macro F1 Score** | 61.81% |
|
| 156 |
|
| 157 |
+
### Per-Class Performance
|
| 158 |
|
| 159 |
+
| Type | Precision | Recall | F1 Score |
|
| 160 |
+
|------|-----------|--------|----------|
|
| 161 |
+
| Type 1 | - | - | 68.32% |
|
| 162 |
+
| Type 2 | - | - | 56.41% |
|
| 163 |
+
| Type 3 | - | - | 60.69% |
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
|
| 167 |
## Usage
|
| 168 |
|
| 169 |
### Installation
|
| 170 |
|
| 171 |
```bash
|
| 172 |
+
pip install torch torchvision safetensors huggingface_hub
|
| 173 |
```
|
| 174 |
|
| 175 |
+
### Quick Start (Local)
|
| 176 |
|
| 177 |
```python
|
| 178 |
import torch
|
|
|
|
| 204 |
print(f"Confidence: {probabilities[0][prediction]:.2%}")
|
| 205 |
```
|
| 206 |
|
| 207 |
+
### Load from Hugging Face Hub
|
| 208 |
|
| 209 |
```python
|
| 210 |
from huggingface_hub import hf_hub_download
|
| 211 |
+
from safetensors.torch import load_file
|
| 212 |
import torch
|
| 213 |
+
import json
|
| 214 |
+
import importlib.util
|
| 215 |
|
| 216 |
+
# Download files
|
| 217 |
+
repo_id = "toderian/cerviguard_transfer_zones"
|
| 218 |
+
model_weights = hf_hub_download(repo_id, "model.safetensors")
|
| 219 |
+
config_file = hf_hub_download(repo_id, "config.json")
|
| 220 |
+
model_file = hf_hub_download(repo_id, "model.py")
|
| 221 |
|
| 222 |
+
# Load model class dynamically
|
| 223 |
+
spec = importlib.util.spec_from_file_location("model", model_file)
|
| 224 |
+
model_module = importlib.util.module_from_spec(spec)
|
| 225 |
+
spec.loader.exec_module(model_module)
|
| 226 |
|
| 227 |
+
# Load config and create model
|
| 228 |
+
with open(config_file) as f:
|
|
|
|
|
|
|
|
|
|
| 229 |
config = json.load(f)
|
| 230 |
|
| 231 |
+
model = model_module.BaseCNN(**config['model_config'])
|
| 232 |
+
model.load_state_dict(load_file(model_weights))
|
| 233 |
model.eval()
|
| 234 |
+
|
| 235 |
+
# Now use model for inference
|
| 236 |
```
|
| 237 |
|
| 238 |
+
---
|
| 239 |
+
|
| 240 |
+
## Files in This Repository
|
| 241 |
+
|
| 242 |
+
| File | Description |
|
| 243 |
+
|------|-------------|
|
| 244 |
+
| `model.safetensors` | Model weights (SafeTensors format, recommended) |
|
| 245 |
+
| `pytorch_model.bin` | Model weights (PyTorch format, backup) |
|
| 246 |
+
| `config.json` | Model architecture configuration |
|
| 247 |
+
| `model.py` | Model class definition (BaseCNN) |
|
| 248 |
+
| `preprocessor_config.json` | Image preprocessing configuration |
|
| 249 |
+
| `README.md` | This model card |
|
| 250 |
+
|
| 251 |
+
---
|
| 252 |
+
|
| 253 |
## Limitations
|
| 254 |
|
| 255 |
- Model was trained on a specific dataset and may not generalize to all cervical imaging equipment
|
| 256 |
- Type 2 classification has lower accuracy (56.41% F1) as it represents an intermediate state
|
| 257 |
+
- Input images should be 256Γ256 RGB
|
| 258 |
+
- This is a custom PyTorch model, not compatible with `transformers.AutoModel`
|
| 259 |
|
| 260 |
+
---
|
| 261 |
|
| 262 |
+
## Citation
|
| 263 |
|
| 264 |
```bibtex
|
| 265 |
+
@misc{cerviguard-transfer-zones,
|
| 266 |
+
title={CerviGuard Cervical Transformation Zone Classifier},
|
| 267 |
+
author={toderian},
|
| 268 |
year={2026},
|
| 269 |
+
howpublished={\url{https://huggingface.co/toderian/cerviguard_transfer_zones}}
|
| 270 |
}
|
| 271 |
```
|
| 272 |
|
| 273 |
+
---
|
| 274 |
+
|
| 275 |
## License
|
| 276 |
|
| 277 |
MIT License
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"do_normalize": true,
|
| 3 |
+
"do_rescale": true,
|
| 4 |
+
"do_resize": true,
|
| 5 |
+
"image_mean": [0.5, 0.5, 0.5],
|
| 6 |
+
"image_std": [0.5, 0.5, 0.5],
|
| 7 |
+
"image_processor_type": "ImageProcessor",
|
| 8 |
+
"resample": 3,
|
| 9 |
+
"rescale_factor": 0.00392156862745098,
|
| 10 |
+
"size": {
|
| 11 |
+
"height": 256,
|
| 12 |
+
"width": 256
|
| 13 |
+
}
|
| 14 |
+
}
|