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
| 11 |
+
- microscopy
|
| 12 |
+
- sem
|
| 13 |
+
- computer-vision
|
| 14 |
+
- segmentation-models-pytorch
|
| 15 |
+
datasets:
|
| 16 |
+
- safdar/sem-ni-wc-metal-matrix-composites
|
| 17 |
+
metrics:
|
| 18 |
+
- mean_iou
|
| 19 |
+
- dice
|
| 20 |
+
library_name: segmentation_models_pytorch
|
| 21 |
+
pipeline_tag: image-segmentation
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# SEM Microstructure Semantic Segmentation
|
| 25 |
+
|
| 26 |
+
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.
|
| 27 |
+
|
| 28 |
+
## Model Details
|
| 29 |
+
|
| 30 |
+
| Property | Detail |
|
| 31 |
+
|------------------|---------------------------------------------|
|
| 32 |
+
| Architecture | UNet + ResNet-34 encoder |
|
| 33 |
+
| Encoder weights | ImageNet pretrained |
|
| 34 |
+
| Input channels | 1 (grayscale SEM image) |
|
| 35 |
+
| Output classes | 5 (pixel-wise segmentation) |
|
| 36 |
+
| Framework | PyTorch + segmentation_models_pytorch |
|
| 37 |
+
| Training device | Apple MPS (M-series) |
|
| 38 |
+
|
| 39 |
+
## Classes
|
| 40 |
+
|
| 41 |
+
| ID | Phase |
|
| 42 |
+
|----|----------------|
|
| 43 |
+
| 0 | Matrix |
|
| 44 |
+
| 1 | Carbide |
|
| 45 |
+
| 2 | Void |
|
| 46 |
+
| 3 | Reprecipitate |
|
| 47 |
+
| 4 | Dilution Zone |
|
| 48 |
+
|
| 49 |
+
## Test Performance
|
| 50 |
+
|
| 51 |
+
Evaluated on 54 held-out test images, scored at image level to avoid batch-size bias.
|
| 52 |
+
|
| 53 |
+
| Metric | Mean | Std | Min | Max |
|
| 54 |
+
|--------|-------|-------|-------|-------|
|
| 55 |
+
| mIoU | 0.872 | 0.088 | 0.723 | 0.958 |
|
| 56 |
+
| mDice | 0.912 | 0.079 | 0.759 | 0.978 |
|
| 57 |
+
| mBF1 | 0.728 | 0.027 | 0.678 | 0.773 |
|
| 58 |
+
|
| 59 |
+
### Per-Class Performance
|
| 60 |
+
|
| 61 |
+
| Class | IoU | Dice |
|
| 62 |
+
|---------------|-------|-------|
|
| 63 |
+
| Matrix | 0.939 | 0.969 |
|
| 64 |
+
| Carbide | 0.753 | 0.859 |
|
| 65 |
+
| Void | 0.976 | 0.988 |
|
| 66 |
+
| Reprecipitate | 0.891 | 0.942 |
|
| 67 |
+
| Dilution Zone | 0.881 | 0.937 |
|
| 68 |
+
|
| 69 |
+
## Files
|
| 70 |
+
|
| 71 |
+
| File | Description |
|
| 72 |
+
|----------------------------|------------------------------------|
|
| 73 |
+
| `best_model.pth` | Best pretrained encoder checkpoint |
|
| 74 |
+
| `ScratchUNet_best_unet.pth`| Best scratch-built UNet checkpoint |
|
| 75 |
+
|
| 76 |
+
## How to Use
|
| 77 |
+
|
| 78 |
+
### Install dependencies
|
| 79 |
+
```bash
|
| 80 |
+
pip install torch segmentation-models-pytorch huggingface_hub
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Load and run inference
|
| 84 |
+
```python
|
| 85 |
+
import torch
|
| 86 |
+
from huggingface_hub import hf_hub_download
|
| 87 |
+
import segmentation_models_pytorch as smp
|
| 88 |
+
|
| 89 |
+
# Download checkpoint
|
| 90 |
+
ckpt_path = hf_hub_download(
|
| 91 |
+
repo_id="imranlabs/sem-microstructure-segmentation",
|
| 92 |
+
filename="best_model.pth"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Rebuild architecture
|
| 96 |
+
model = smp.Unet(
|
| 97 |
+
encoder_name = "resnet34",
|
| 98 |
+
encoder_weights = None, # weights loaded from checkpoint
|
| 99 |
+
in_channels = 1,
|
| 100 |
+
classes = 5,
|
| 101 |
+
activation = None,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Load weights
|
| 105 |
+
ckpt = torch.load(ckpt_path, map_location="cpu")
|
| 106 |
+
model.load_state_dict(ckpt["model_state"])
|
| 107 |
+
model.eval()
|
| 108 |
+
|
| 109 |
+
# Inference — input: (1, 1, H, W) float32 tensor normalised to [0, 1]
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
logits = model(image_tensor) # (1, 5, H, W)
|
| 112 |
+
preds = torch.argmax(logits, dim=1) # (1, H, W) class labels
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
## Training Details
|
| 116 |
+
|
| 117 |
+
- **Loss:** 0.6 × Weighted CrossEntropy + 0.4 × DiceLoss
|
| 118 |
+
- **Optimizer:** AdamW (weight decay 1e-5)
|
| 119 |
+
- **Scheduler:** ReduceLROnPlateau
|
| 120 |
+
- **Strategy:** Encoder frozen for first 4 epochs, then full fine-tuning
|
| 121 |
+
- **Early stopping:** Monitored validation mIoU
|
| 122 |
+
- **Best checkpoint:** Epoch 12, validation mIoU = 0.876
|
| 123 |
+
|
| 124 |
+
## Dataset
|
| 125 |
+
|
| 126 |
+
Safdar, M. (2025). *Scanning Electron Microscopy (SEM) Dataset of Additively Manufactured Ni-WC Metal Matrix Composites for Semantic Segmentation* (Version 1). Zenodo.
|
| 127 |
+
https://doi.org/10.5281/zenodo.17315241
|
| 128 |
+
|
| 129 |
+
## Links
|
| 130 |
+
|
| 131 |
+
- GitHub: [sem-microstructure-segmentation](https://github.com/imranlabs/sem-microstructure-segmentation)
|
| 132 |
+
|
| 133 |
+
## Author
|
| 134 |
+
|
| 135 |
+
**Imran Khan** — Physics PhD · SEM Metrology Engineer · Computer Vision / AI
|