rotsl commited on
Commit
073d55f
Β·
verified Β·
1 Parent(s): 642af15

Update README: remove gated access, add model details and demo links

Browse files
Files changed (1) hide show
  1. README.md +152 -62
README.md CHANGED
@@ -6,108 +6,198 @@ tags:
6
  - image-segmentation
7
  - pytorch
8
  - unet
 
 
 
 
 
9
  language:
10
  - en
11
  ---
12
 
13
- # Gray Leaf Spot Segmentation Model
14
 
15
- PyTorch U-Net segmentation model trained for gray leaf spot segmentation.
16
 
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- - `grayleafspot.pt`: main U-Net model artifact
20
- - `best_area_w_0.1.pt`: area consistency U-Net (weight=0.1)
21
- - `best_area_w_0.3.pt`: area consistency U-Net (weight=0.3)
22
- - `best_area_w_0.5.pt`: area consistency U-Net (weight=0.5)
23
- - `best_area_w_0.7.pt`: area consistency U-Net (weight=0.7)
24
- - `requirements.txt`: minimal runtime dependencies
25
 
26
- ## Usage
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  ```python
29
  import torch
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- model = torch.load("grayleafspot.pt", map_location="cpu")
 
32
  model.eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  ```
34
 
35
- ## Training & Inference Pipeline
36
 
37
- This repository follows the TrainPetri pipeline for image segmentation, adapted for gray leaf spot. Key steps:
 
 
 
 
 
 
 
 
 
 
38
 
39
  ### Environment Setup (Apple Silicon recommended)
40
 
41
- 1. Create and activate a Python 3.10+ virtual environment:
42
- ```bash
43
- python3.10 -m venv trainenv
44
- source trainenv/bin/activate
45
- pip install --upgrade pip
46
- pip install -r requirements.txt
47
- ```
48
 
49
  ### Dataset Preparation
50
 
51
- - Place your raw images in `raw/` and corresponding masks in `masks/` (matching filenames).
52
- - To expand the dataset with augmentations:
53
- ```bash
54
- python src/build_augmented_dataset.py --copies-per-image 4 --clean
55
- ```
56
 
57
  ### Training
58
 
59
- - Standard U-Net training:
60
- ```bash
61
- python src/train.py --image-dir augmented_dataset/raw --mask-dir augmented_dataset/masks --epochs 40 --batch-size 4 --lr 1e-4 --image-size 256 --freeze-encoder-epochs 5
62
- ```
63
- - Area consistency U-Net (LabelMe JSON polygons):
64
- ```bash
65
- ./trainenv/bin/python src/area_consistency/train_area.py
66
- ```
67
-
68
- ### Inference
69
-
70
- - On a single image:
71
- ```bash
72
- python src/predict.py --input raw/your_image.jpg --weights models/best_finetuned.pt --output-dir predictions
73
- ```
74
- - On a folder:
75
- ```bash
76
- python src/predict.py --input raw --weights models/best_finetuned.pt --output-dir predictions
77
- ```
 
 
 
 
 
 
 
 
 
 
78
 
79
  ### Best Practices
80
 
81
- - Always keep your trusted human-label masks unchanged.
82
- - Use augmentations and area consistency loss for improved generalization.
83
  - Inspect overlay outputs to verify mask quality.
84
- - For Apple Silicon, MPS acceleration is used automatically if available.
85
 
 
86
 
 
87
 
 
88
 
 
89
 
90
- ## Citation
91
 
92
- If you use this model or code, please cite:
93
 
94
- ```
 
 
 
 
95
  @misc{rohan_r_2026,
96
- author = { rohan r },
97
- title = { grayleafspot-segmentation (Revision 0e85f71) },
98
- year = 2026,
99
- url = { https://huggingface.co/rotsl/grayleafspot-segmentation },
100
- doi = { 10.57967/hf/8416 },
101
- publisher = { Hugging Face }
102
  }
103
  ```
104
 
105
-
106
  ## License
107
 
108
- This project is licensed under the Apache License 2.0. See the LICENSE file for details.
109
-
110
- ## Access
111
-
112
- This repository is gated with manual approval enabled on the Hugging Face Hub.
113
- Users must request access before they can download the model.
 
6
  - image-segmentation
7
  - pytorch
8
  - unet
9
+ - fungal-colony
10
+ - petri-dish
11
+ - morphometry
12
+ - magnaporthe
13
+ - area-consistency
14
  language:
15
  - en
16
  ---
17
 
18
+ # πŸ”¬ Gray Leaf Spot Segmentation Model
19
 
20
+ PyTorch U-Net models for **gray leaf spot** (*Magnaporthe* and related fungal) colony segmentation on 90 mm petri-dish images.
21
 
22
+ **[β–Ά Try the live demo](https://huggingface.co/spaces/rotsl/grayleafspot-segmentation-demo)** β€” upload images, run inference, see overlays & growth charts in your browser.
23
 
24
+ ---
25
+
26
+ ## Model Weights
27
+
28
+ | File | Architecture | Params | Area-Consistency Weight | Description |
29
+ |---|---|---|---|---|
30
+ | `grayleafspot.pt` | smp.Unet (ResNet-34) | ~24.4 M | β€” | Main encoder–decoder model |
31
+ | `best_area_w_0.1.pt` | SmallUNet | ~250 K | 0.1 | Light area regularisation |
32
+ | `best_area_w_0.3.pt` | SmallUNet | ~250 K | 0.3 | Moderate area regularisation |
33
+ | `best_area_w_0.5.pt` | SmallUNet | ~250 K | 0.5 | Balanced BCE + area |
34
+ | **`best_area_w_0.7.pt`** | **SmallUNet** | **~250 K** | **0.7** | **Strong area consistency (used by demo)** |
35
+
36
+ All SmallUNet variants share the same architecture:
37
+
38
+ ```
39
+ Input (3 Γ— 256 Γ— 256)
40
+ β”‚
41
+ β”œβ”€ enc1: ConvBlock(3 β†’ 16) ─── skip s1
42
+ β”œβ”€ enc2: MaxPool2d β†’ ConvBlock(16 β†’ 32) ─── skip s2
43
+ β”œβ”€ enc3: MaxPool2d β†’ ConvBlock(32 β†’ 64) ─── skip s3
44
+ β”œβ”€ enc4: MaxPool2d β†’ ConvBlock(64 β†’ 128) ─── skip s4
45
+ β”‚
46
+ β”œβ”€ bottleneck: MaxPool2d β†’ ConvBlock(128 β†’ 256)
47
+ β”‚
48
+ β”œβ”€ up4: Upsample + cat(s4) β†’ ConvBlock(384 β†’ 128)
49
+ β”œβ”€ up3: Upsample + cat(s3) β†’ ConvBlock(192 β†’ 64)
50
+ β”œβ”€ up2: Upsample + cat(s2) β†’ ConvBlock(96 β†’ 32)
51
+ β”œβ”€ up1: Upsample + cat(s1) β†’ ConvBlock(48 β†’ 16)
52
+ β”‚
53
+ └─ head: Conv2d(16 β†’ 1) β†’ Sigmoid
54
+ ```
55
 
56
+ Each `ConvBlock` = Conv3Γ—3 (no bias) β†’ ReLU β†’ Conv3Γ—3 (no bias) β†’ ReLU.
 
 
 
 
 
57
 
58
+ | Property | Value |
59
+ |---|---|
60
+ | **Input** | 256 Γ— 256 RGB |
61
+ | **Output** | 1-channel sigmoid probability mask |
62
+ | **Training loss** | BCE + area-consistency loss |
63
+ | **CPU compatible** | βœ… Pure PyTorch β€” no custom CUDA kernels |
64
+
65
+ ---
66
+
67
+ ## Quick Start
68
+
69
+ ### Download & Inference (SmallUNet)
70
 
71
  ```python
72
  import torch
73
+ from huggingface_hub import hf_hub_download
74
+
75
+ # Download weights
76
+ path = hf_hub_download("rotsl/grayleafspot-segmentation", "best_area_w_0.7.pt")
77
+
78
+ # Load checkpoint
79
+ ckpt = torch.load(path, map_location="cpu", weights_only=False)
80
+
81
+ # Build model (SmallUNet architecture β€” see demo repo for full class definition)
82
+ # https://huggingface.co/rotsl/grayleafspot-segmentation-demo/blob/main/app.py
83
+ from model import SmallUNet # or copy the class from the demo app.py
84
 
85
+ model = SmallUNet(in_channels=3, out_channels=1, base_channels=16)
86
+ model.load_state_dict(ckpt["model_state_dict"])
87
  model.eval()
88
+
89
+ # Run inference on a 256Γ—256 RGB tensor
90
+ import numpy as np
91
+ from PIL import Image
92
+
93
+ img = Image.open("petri_dish.jpg").convert("RGB").resize((256, 256))
94
+ x = torch.from_numpy(np.array(img).transpose(2, 0, 1)).float() / 255.0
95
+ x = x.unsqueeze(0)
96
+
97
+ with torch.no_grad():
98
+ prob = model(x)[0, 0].numpy()
99
+
100
+ mask = (prob > 0.5).astype(np.uint8) * 255
101
+ Image.fromarray(mask).save("colony_mask.png")
102
  ```
103
 
104
+ ### Download & Inference (Main U-Net)
105
 
106
+ ```python
107
+ import torch
108
+
109
+ path = hf_hub_download("rotsl/grayleafspot-segmentation", "grayleafspot.pt")
110
+ model = torch.load(path, map_location="cpu", weights_only=False)
111
+ model.eval()
112
+ ```
113
+
114
+ ---
115
+
116
+ ## Training & Inference Pipeline
117
 
118
  ### Environment Setup (Apple Silicon recommended)
119
 
120
+ ```bash
121
+ python3.10 -m venv trainenv
122
+ source trainenv/bin/activate
123
+ pip install --upgrade pip
124
+ pip install -r requirements.txt
125
+ ```
 
126
 
127
  ### Dataset Preparation
128
 
129
+ Place raw images in `raw/` and corresponding masks in `masks/` (matching filenames). Expand the dataset with augmentations:
130
+
131
+ ```bash
132
+ python src/build_augmented_dataset.py --copies-per-image 4 --clean
133
+ ```
134
 
135
  ### Training
136
 
137
+ Standard U-Net training:
138
+
139
+ ```bash
140
+ python src/train.py \
141
+ --image-dir augmented_dataset/raw \
142
+ --mask-dir augmented_dataset/masks \
143
+ --epochs 40 --batch-size 4 --lr 1e-4 \
144
+ --image-size 256 --freeze-encoder-epochs 5
145
+ ```
146
+
147
+ Area-consistency U-Net (LabelMe JSON polygons):
148
+
149
+ ```bash
150
+ ./trainenv/bin/python src/area_consistency/train_area.py
151
+ ```
152
+
153
+ ### Inference (CLI)
154
+
155
+ Single image:
156
+
157
+ ```bash
158
+ python src/predict.py --input raw/your_image.jpg --weights models/best_finetuned.pt --output-dir predictions
159
+ ```
160
+
161
+ Folder:
162
+
163
+ ```bash
164
+ python src/predict.py --input raw --weights models/best_finetuned.pt --output-dir predictions
165
+ ```
166
 
167
  ### Best Practices
168
 
169
+ - Keep trusted human-labelled masks unchanged.
170
+ - Use augmentations and area-consistency loss for improved generalisation.
171
  - Inspect overlay outputs to verify mask quality.
172
+ - On Apple Silicon, MPS acceleration is used automatically if available.
173
 
174
+ ---
175
 
176
+ ## Demo Space
177
 
178
+ The full analysis pipeline is available as a Gradio Space:
179
 
180
+ **[`rotsl/grayleafspot-segmentation-demo`](https://huggingface.co/spaces/rotsl/grayleafspot-segmentation-demo)**
181
 
182
+ Features: dish detection β†’ colony segmentation β†’ crack & hyphae analysis β†’ 16 morphometric measurements β†’ time-series growth charts β†’ CSV/JSON export.
183
 
184
+ Source code: [`rotsl/grayleafspot-segmentation-demo`](https://huggingface.co/rotsl/grayleafspot-segmentation-demo) (model repo)
185
 
186
+ ---
187
+
188
+ ## Citation
189
+
190
+ ```bibtex
191
  @misc{rohan_r_2026,
192
+ author = {rohan r},
193
+ title = {grayleafspot-segmentation (Revision 0e85f71)},
194
+ year = 2026,
195
+ url = {https://huggingface.co/rotsl/grayleafspot-segmentation},
196
+ doi = {10.57967/hf/8416},
197
+ publisher = {Hugging Face}
198
  }
199
  ```
200
 
 
201
  ## License
202
 
203
+ Apache License 2.0 β€” see [LICENSE](LICENSE) for details.