File size: 3,980 Bytes
b465fe3 | 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 | ---
license: mit
language:
- en
tags:
- semantic-segmentation
- pytorch
- unet
- resnet34
- materials-science
- microscopy
- sem
- computer-vision
- segmentation-models-pytorch
datasets:
- safdar/sem-ni-wc-metal-matrix-composites
metrics:
- mean_iou
- dice
library_name: segmentation_models_pytorch
pipeline_tag: image-segmentation
---
# SEM Microstructure Semantic Segmentation
UNet with a pretrained ResNet-34 encoder for pixel-wise segmentation of Scanning Electron Microscopy (SEM) images of additively manufactured Ni-WC metal matrix composites. Trained to identify five microstructural phases at pixel level.
## Model Details
| Property | Detail |
|------------------|---------------------------------------------|
| Architecture | UNet + ResNet-34 encoder |
| Encoder weights | ImageNet pretrained |
| Input channels | 1 (grayscale SEM image) |
| Output classes | 5 (pixel-wise segmentation) |
| Framework | PyTorch + segmentation_models_pytorch |
| Training device | Apple MPS (M-series) |
## Classes
| ID | Phase |
|----|----------------|
| 0 | Matrix |
| 1 | Carbide |
| 2 | Void |
| 3 | Reprecipitate |
| 4 | Dilution Zone |
## Test Performance
Evaluated on 54 held-out test images, scored at image level to avoid batch-size bias.
| Metric | Mean | Std | Min | Max |
|--------|-------|-------|-------|-------|
| mIoU | 0.872 | 0.088 | 0.723 | 0.958 |
| mDice | 0.912 | 0.079 | 0.759 | 0.978 |
| mBF1 | 0.728 | 0.027 | 0.678 | 0.773 |
### Per-Class Performance
| Class | IoU | Dice |
|---------------|-------|-------|
| Matrix | 0.939 | 0.969 |
| Carbide | 0.753 | 0.859 |
| Void | 0.976 | 0.988 |
| Reprecipitate | 0.891 | 0.942 |
| Dilution Zone | 0.881 | 0.937 |
## Files
| File | Description |
|----------------------------|------------------------------------|
| `best_model.pth` | Best pretrained encoder checkpoint |
| `ScratchUNet_best_unet.pth`| Best scratch-built UNet checkpoint |
## How to Use
### Install dependencies
```bash
pip install torch segmentation-models-pytorch huggingface_hub
```
### Load and run inference
```python
import torch
from huggingface_hub import hf_hub_download
import segmentation_models_pytorch as smp
# Download checkpoint
ckpt_path = hf_hub_download(
repo_id="imranlabs/sem-microstructure-segmentation",
filename="best_model.pth"
)
# Rebuild architecture
model = smp.Unet(
encoder_name = "resnet34",
encoder_weights = None, # weights loaded from checkpoint
in_channels = 1,
classes = 5,
activation = None,
)
# Load weights
ckpt = torch.load(ckpt_path, map_location="cpu")
model.load_state_dict(ckpt["model_state"])
model.eval()
# Inference — input: (1, 1, H, W) float32 tensor normalised to [0, 1]
with torch.no_grad():
logits = model(image_tensor) # (1, 5, H, W)
preds = torch.argmax(logits, dim=1) # (1, H, W) class labels
```
## Training Details
- **Loss:** 0.6 × Weighted CrossEntropy + 0.4 × DiceLoss
- **Optimizer:** AdamW (weight decay 1e-5)
- **Scheduler:** ReduceLROnPlateau
- **Strategy:** Encoder frozen for first 4 epochs, then full fine-tuning
- **Early stopping:** Monitored validation mIoU
- **Best checkpoint:** Epoch 12, validation mIoU = 0.876
## Dataset
Safdar, M. (2025). *Scanning Electron Microscopy (SEM) Dataset of Additively Manufactured Ni-WC Metal Matrix Composites for Semantic Segmentation* (Version 1). Zenodo.
https://doi.org/10.5281/zenodo.17315241
## Links
- GitHub: [sem-microstructure-segmentation](https://github.com/imranlabs/sem-microstructure-segmentation)
## Author
**Imran Khan** — Physics PhD · SEM Metrology Engineer · Computer Vision / AI
|