File size: 11,633 Bytes
b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae 8a8bdc2 b915bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
---
license: mit
tags:
- image-classification
- medical-imaging
- cervical-cancer
- pytorch
- safetensors
- cnn
datasets:
- custom
metrics:
- accuracy
- f1
pipeline_tag: image-classification
library_name: pytorch
---
# CerviGuard - Cervical Transformation Zone Classifier
## Model Description
This model classifies cervical images into 3 transformation zone types, which is important for colposcopy evaluation and cervical cancer screening.
| Label | Type | Description |
|-------|------|-------------|
| 0 | Type 1 | Transformation zone fully visible on ectocervix |
| 1 | Type 2 | Transformation zone partially visible (extends into endocervical canal) |
| 2 | Type 3 | Transformation zone not visible (entirely within endocervical canal) |
---
## Model Architecture
### Overview
**BaseCNN** - A simple convolutional neural network with 4 conv blocks and 2 fully connected layers.
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β INPUT (256Γ256Γ3) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONV BLOCK 1 β
β Conv2d(3β32, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
β Output: 128Γ128Γ32 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONV BLOCK 2 β
β Conv2d(32β64, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
β Output: 64Γ64Γ64 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONV BLOCK 3 β
β Conv2d(64β128, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2) β
β Output: 32Γ32Γ128 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONV BLOCK 4 β
β Conv2d(128β256, 3Γ3) β BatchNorm2d β ReLU β MaxPool2d(2Γ2)β
β Output: 16Γ16Γ256 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GLOBAL POOLING β
β AdaptiveAvgPool2d(1Γ1) β
β Output: 1Γ1Γ256 β Flatten β 256 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FC BLOCK 1 β
β Linear(256β256) β ReLU β Dropout(0.4) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FC BLOCK 2 β
β Linear(256β128) β ReLU β Dropout(0.4) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLASSIFIER β
β Linear(128β3) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OUTPUT (3 logits) β
β [Type 1, Type 2, Type 3] β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
### Layer Details
| Layer | Type | In Channels | Out Channels | Kernel | Output Size |
|-------|------|-------------|--------------|--------|-------------|
| conv_layers.0 | Conv2d | 3 | 32 | 3Γ3 | 256Γ256Γ32 |
| conv_layers.1 | BatchNorm2d | 32 | 32 | - | 256Γ256Γ32 |
| conv_layers.2 | ReLU | - | - | - | 256Γ256Γ32 |
| conv_layers.3 | MaxPool2d | - | - | 2Γ2 | 128Γ128Γ32 |
| conv_layers.4 | Conv2d | 32 | 64 | 3Γ3 | 128Γ128Γ64 |
| conv_layers.5 | BatchNorm2d | 64 | 64 | - | 128Γ128Γ64 |
| conv_layers.6 | ReLU | - | - | - | 128Γ128Γ64 |
| conv_layers.7 | MaxPool2d | - | - | 2Γ2 | 64Γ64Γ64 |
| conv_layers.8 | Conv2d | 64 | 128 | 3Γ3 | 64Γ64Γ128 |
| conv_layers.9 | BatchNorm2d | 128 | 128 | - | 64Γ64Γ128 |
| conv_layers.10 | ReLU | - | - | - | 64Γ64Γ128 |
| conv_layers.11 | MaxPool2d | - | - | 2Γ2 | 32Γ32Γ128 |
| conv_layers.12 | Conv2d | 128 | 256 | 3Γ3 | 32Γ32Γ256 |
| conv_layers.13 | BatchNorm2d | 256 | 256 | - | 32Γ32Γ256 |
| conv_layers.14 | ReLU | - | - | - | 32Γ32Γ256 |
| conv_layers.15 | MaxPool2d | - | - | 2Γ2 | 16Γ16Γ256 |
| adaptive_pool | AdaptiveAvgPool2d | - | - | - | 1Γ1Γ256 |
| fc_layers.0 | Linear | 256 | 256 | - | 256 |
| fc_layers.1 | ReLU | - | - | - | 256 |
| fc_layers.2 | Dropout | - | - | p=0.4 | 256 |
| fc_layers.3 | Linear | 256 | 128 | - | 128 |
| fc_layers.4 | ReLU | - | - | - | 128 |
| fc_layers.5 | Dropout | - | - | p=0.4 | 128 |
| classifier | Linear | 128 | 3 | - | 3 |
### Model Summary
| Property | Value |
|----------|-------|
| **Total Parameters** | 488,451 |
| **Trainable Parameters** | 488,451 |
| **Input Size** | (B, 3, 256, 256) |
| **Output Size** | (B, 3) |
| **Model Size** | ~1.9 MB |
---
## Training Configuration
| Parameter | Value |
|-----------|-------|
| Learning Rate | 1e-4 |
| Batch Size | 32 |
| Dropout | 0.4 |
| Optimizer | Adam |
| Loss Function | CrossEntropyLoss |
| Epochs | 50 |
| Best Epoch | 41 |
---
## Performance
| Metric | Value |
|--------|-------|
| **Validation Accuracy** | 61.69% |
| **Macro F1 Score** | 61.81% |
### Per-Class Performance
| Type | Precision | Recall | F1 Score |
|------|-----------|--------|----------|
| Type 1 | - | - | 68.32% |
| Type 2 | - | - | 56.41% |
| Type 3 | - | - | 60.69% |
---
## Usage
### Installation
```bash
pip install torch torchvision safetensors huggingface_hub
```
### Quick Start (Local)
```python
import torch
from PIL import Image
from torchvision import transforms
# Load model
from model import BaseCNN
model = BaseCNN.from_pretrained("./")
model.eval()
# Preprocess image
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
])
image = Image.open("cervical_image.jpg").convert("RGB")
input_tensor = transform(image).unsqueeze(0)
# Inference
with torch.no_grad():
output = model(input_tensor)
probabilities = torch.softmax(output, dim=1)
prediction = output.argmax(dim=1).item()
labels = ["Type 1", "Type 2", "Type 3"]
print(f"Prediction: {labels[prediction]}")
print(f"Confidence: {probabilities[0][prediction]:.2%}")
```
### Load from Hugging Face Hub
```python
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
import torch
import json
import importlib.util
# Download files
repo_id = "toderian/cerviguard_transfer_zones"
model_weights = hf_hub_download(repo_id, "model.safetensors")
config_file = hf_hub_download(repo_id, "config.json")
model_file = hf_hub_download(repo_id, "model.py")
# Load model class dynamically
spec = importlib.util.spec_from_file_location("model", model_file)
model_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(model_module)
# Load config and create model
with open(config_file) as f:
config = json.load(f)
model = model_module.BaseCNN(**config['model_config'])
model.load_state_dict(load_file(model_weights))
model.eval()
# Now use model for inference
```
---
## Files in This Repository
| File | Description |
|------|-------------|
| `model.safetensors` | Model weights (SafeTensors format, recommended) |
| `pytorch_model.bin` | Model weights (PyTorch format, backup) |
| `config.json` | Model architecture configuration |
| `model.py` | Model class definition (BaseCNN) |
| `preprocessor_config.json` | Image preprocessing configuration |
| `README.md` | This model card |
---
## Limitations
- Model was trained on a specific dataset and may not generalize to all cervical imaging equipment
- Type 2 classification has lower accuracy (56.41% F1) as it represents an intermediate state
- Input images should be 256Γ256 RGB
- This is a custom PyTorch model, not compatible with `transformers.AutoModel`
---
## Citation
```bibtex
@misc{cerviguard-transfer-zones,
title={CerviGuard Cervical Transformation Zone Classifier},
author={toderian},
year={2026},
howpublished={\url{https://huggingface.co/toderian/cerviguard_transfer_zones}}
}
```
---
## License
MIT License
|