toderian's picture
Upload folder using huggingface_hub
8a8bdc2 verified
---
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