File size: 7,922 Bytes
c7ada91
 
 
 
 
 
 
 
 
 
 
 
 
89fd7d4
2fe10b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3003a73
2fe10b5
3003a73
2fe10b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbeaaf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fe10b5
 
 
 
 
4b3a45b
 
 
2fe10b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ada91
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
---
license: apache-2.0
library_name: transformers
tags:
- image-processing
- medical-imaging
- fundus
- retinal-imaging
- diabetic-retinopathy
- ophthalmology
- clahe
- preprocessing
---

# EyeCLAHEImageProcessor

A GPU-native Hugging Face ImageProcessor for **Color Fundus Photography (CFP)** images, designed for diabetic retinopathy detection and other retinal imaging tasks.

## Features

- **Eye Region Localization**: Automatically detects and centers on the fundus using gradient-based radial symmetry
- **Smart Cropping**: Border-minimized square crop centered on the detected eye
- **CLAHE Enhancement**: Contrast Limited Adaptive Histogram Equalization for improved visibility
- **Pure PyTorch**: No OpenCV/PIL dependencies at runtime - fully GPU-accelerated
- **Batch Processing**: Efficient batched operations for training pipelines
- **Flexible Input**: Accepts PyTorch tensors, PIL Images, and NumPy arrays

## Installation

```bash
pip install transformers torch
```

## Quick Start

```python
from transformers import AutoImageProcessor
from PIL import Image

# Load the processor
processor = AutoImageProcessor.from_pretrained("iszt/eye-clahe-processor", trust_remote_code=True)

# Process a single image
image = Image.open("fundus_image.jpg")
outputs = processor(image, return_tensors="pt")
pixel_values = outputs["pixel_values"]  # Shape: (1, 3, 512, 512)

# Process on GPU
outputs = processor(image, return_tensors="pt", device="cuda")
```

## Batch Processing

```python
import torch
from PIL import Image

# Load multiple images
images = [Image.open(f"image_{i}.jpg") for i in range(8)]

# Process batch
outputs = processor(images, return_tensors="pt", device="cuda")
pixel_values = outputs["pixel_values"]  # Shape: (8, 3, 512, 512)
```

## With PyTorch Tensors

```python
import torch

# Tensor input: (B, C, H, W) or (C, H, W)
images = torch.rand(4, 3, 512, 512)  # Batch of 4 images

outputs = processor(images, return_tensors="pt")
```

## Configuration Options

| Parameter | Default | Description |
|-----------|---------|-------------|
| `size` | 512 | Output image size (square) |
| `do_crop` | true | Enable eye-centered cropping |
| `do_clahe` | true | Enable CLAHE contrast enhancement |
| `crop_scale_factor` | 1.1 | Padding around detected eye region |
| `clahe_grid_size` | 8 | CLAHE tile grid size |
| `clahe_clip_limit` | 2.0 | CLAHE histogram clip limit |
| `normalization_mode` | "imagenet" | Normalization: "imagenet", "none", or "custom" |
| `min_radius_frac` | 0.1 | Minimum eye radius as fraction of image |
| `max_radius_frac` | 0.9 | Maximum eye radius as fraction of image |
| `allow_overflow` | true | Allow crop box beyond image bounds (fills with black) |
| `softmax_temperature` | 0.3 | Temperature for eye center detection (higher = smoother) |

## Custom Configuration

```python
from transformers import AutoImageProcessor

processor = AutoImageProcessor.from_pretrained(
    "iszt/eye-clahe-processor",
    trust_remote_code=True,
    size=384,
    normalization_mode="imagenet",
    clahe_clip_limit=3.0,
    softmax_temperature=0.3,
)
```

## Processing Pipeline

The processor applies the following steps:

1. **Input Standardization**: Convert PIL/NumPy/Tensor to (B, C, H, W) float32 tensor in [0, 1]
2. **Eye Localization**: Detect fundus center using radial symmetry analysis
3. **Radius Estimation**: Determine fundus boundary from radial intensity profiles
4. **Crop & Resize**: Extract square region centered on eye, resize to target size
5. **CLAHE**: Apply contrast enhancement in LAB color space (L channel only)
6. **Normalization**: Apply ImageNet normalization (optional)

## Use with Vision Models

```python
from transformers import AutoImageProcessor, AutoModel
from PIL import Image

# Load processor and model
processor = AutoImageProcessor.from_pretrained("iszt/eye-clahe-processor", trust_remote_code=True)
model = AutoModel.from_pretrained("google/vit-base-patch16-224")

# Process and run inference
image = Image.open("fundus.jpg")
inputs = processor(image, return_tensors="pt", device="cuda")

# Update normalization for pretrained models
inputs["pixel_values"] = (inputs["pixel_values"] - torch.tensor([0.485, 0.456, 0.406]).view(1,3,1,1).cuda()) / torch.tensor([0.229, 0.224, 0.225]).view(1,3,1,1).cuda()

with torch.no_grad():
    outputs = model(**inputs)
```

## Coordinate Mapping

The processor returns coordinate mapping information that allows you to map coordinates from the processed image back to the original image space. This is useful for applications like lesion detection, where you need to annotate or visualize detected features on the original image.

### Output Format

The processor returns these additional keys:
- `scale_x`, `scale_y`: Scale factors for coordinate mapping (shape: `(B,)`)
- `offset_x`, `offset_y`: Offset values for coordinate mapping (shape: `(B,)`)

### Mapping Formula

To map coordinates from the processed image back to original coordinates:

```python
orig_x = offset_x + cropped_x * scale_x
orig_y = offset_y + cropped_y * scale_y
```

Where `cropped_x` and `cropped_y` are coordinates in the processed image (range: [0, size-1]).

### Example: Single Point Mapping

```python
from PIL import Image

# Process image
processor = AutoImageProcessor.from_pretrained("iszt/eye-clahe-processor", trust_remote_code=True)
image = Image.open("fundus.jpg")
outputs = processor(image, return_tensors="pt")

# Detected point in processed image (e.g., from a model prediction)
detected_x, detected_y = 100.0, 150.0

# Map back to original image coordinates
orig_x = outputs['offset_x'] + detected_x * outputs['scale_x']
orig_y = outputs['offset_y'] + detected_y * outputs['scale_y']

print(f"Original coordinates: ({orig_x.item():.2f}, {orig_y.item():.2f})")
```

### Example: Multiple Points in Batch

```python
import torch

# Process batch of images
images = [Image.open(f"image_{i}.jpg") for i in range(4)]
outputs = processor(images, return_tensors="pt")

# Detected points for each image (B, N, 2) where N is number of points
detected_points = torch.tensor([
    [[50.0, 60.0], [100.0, 120.0]],  # Image 0: 2 points
    [[75.0, 80.0], [150.0, 160.0]],  # Image 1: 2 points
    [[90.0, 95.0], [180.0, 190.0]],  # Image 2: 2 points
    [[65.0, 70.0], [130.0, 140.0]],  # Image 3: 2 points
])

# Map all points back to original coordinates
B, N, _ = detected_points.shape
scale_x = outputs['scale_x'].view(B, 1, 1)
scale_y = outputs['scale_y'].view(B, 1, 1)
offset_x = outputs['offset_x'].view(B, 1, 1)
offset_y = outputs['offset_y'].view(B, 1, 1)

orig_x = offset_x + detected_points[..., 0:1] * scale_x
orig_y = offset_y + detected_points[..., 1:2] * scale_y

original_points = torch.cat([orig_x, orig_y], dim=-1)  # (B, N, 2)
```

### Use Cases

- **Lesion Detection**: Map detected lesion coordinates back for visualization
- **Optic Disc Localization**: Track anatomical landmarks through preprocessing
- **Vessel Segmentation**: Align segmentation masks with original images
- **Quality Control**: Verify feature alignment across processing pipeline

## Technical Details

### Eye Center Detection

Uses a gradient-based radial symmetry approach:
- Computes Sobel gradients to detect edges
- Finds circular boundaries where gradients point inward radially
- Weights by edge strength and proximity to dark regions (background)
- Uses soft argmax for sub-pixel accuracy

### CLAHE Implementation

Pure PyTorch CLAHE with:
- Proper sRGB to CIE LAB conversion
- Vectorized histogram computation using scatter_add
- Bilinear interpolation between tile CDFs
- Only modifies L channel, preserving color information

## License

Apache 2.0

## Citation

If you use this processor in your research, please cite:

```bibtex
@software{eye_clahe_processor,
  title={EyeCLAHEImageProcessor: GPU-Native Fundus Image Preprocessing},
  year={2026},
  url={https://huggingface.co/iszt/eye-clahe-processor}
}
```