File size: 2,014 Bytes
a2f233b
3e94d6c
a2f233b
3e94d6c
 
 
 
 
 
a2f233b
 
3e94d6c
 
a2f233b
3e94d6c
a2f233b
3e94d6c
 
 
 
 
a2f233b
3e94d6c
a2f233b
3e94d6c
a2f233b
 
 
3e94d6c
 
 
 
 
a2f233b
3e94d6c
 
 
 
 
 
 
 
 
 
a2f233b
3e94d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2f233b
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
---
license: mit
tags:
- medical
- vision
- pytorch
- optometry
pipeline_tag: image-regression
library_name: timm
---

# Visionary-Net 
**AI-Powered Refractive Error Estimation**

Visionary-Net is a deep learning model that acts as a "Neural Auto-Refractor." It analyzes blur patterns in an image to estimate the optical prescription needed to correct them.

## ⚡ Model Specs
- **Backbone:** EfficientNet-B0
- **Input:** 224x224 RGB Image
- **Output:** Sphere (SPH), Cylinder (CYL), Axis (Sin/Cos)
- **Best Checkpoint:** `model_v1_ep9.pth` (Included in repo)

## 💻 How to Use

You need `timm`, `torch`, and `opencv-python`.

```python
import torch
import torch.nn as nn
import timm
import cv2
import numpy as np
from huggingface_hub import hf_hub_download

# 1. Define Architecture
class VisionaryNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.backbone = timm.create_model('efficientnet_b0', pretrained=False, num_classes=0)
        self.head = nn.Sequential(
            nn.Linear(1280, 512), nn.ReLU(), nn.Dropout(0.2), nn.Linear(512, 4)
        )
    def forward(self, x): 
        return self.head(self.backbone(x))

# 2. Load the Best Checkpoint (Epoch 9)
model_path = hf_hub_download(repo_id="sanskxr02/Visionary-Net", filename="model_v1_ep9.pth")

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = VisionaryNet().to(device)
model.load_state_dict(torch.load(model_path, map_location=device))
model.eval()

# 3. Predict on an Image
img = cv2.imread("test_blur.jpg") # Load image
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
img = cv2.resize(img, (224, 224)) / 255.0  # Resize & Normalize

img_t = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0).float().to(device)

with torch.no_grad():
    preds = model(img_t)[0].cpu().numpy()

sph, cyl, sin_a, cos_a = preds
axis = np.degrees(np.arctan2(sin_a, cos_a)) / 2.0
if axis < 0: axis += 180

print(f"👁️ Prescription: SPH {sph:.2f} D | CYL {cyl:.2f} D | AXIS {axis:.0f}°")