File size: 4,982 Bytes
e5bb95a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""демо.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1NK3gtM_1xpqJt79c_lDgu45FY4aMd3kr
"""

from huggingface_hub import hf_hub_download

# Загрузка файла конфигурации и модели
config_path = hf_hub_download(repo_id="alexakup05/eye_disease_classifier", filename="config.json")
model_path = hf_hub_download(repo_id="alexakup05/eye_disease_classifier", filename="model1.pth")

print(f"Модель и конфигурация загружены: {config_path}, {model_path}")

import json

# Загружаем конфигурацию
with open(config_path, 'r') as f:
    config = json.load(f)

print(config)  # Проверим содержимое конфигурации

import torch
import torch.nn as nn
from torchvision import models

class EyeDiseaseEfficientNet(nn.Module):
    def __init__(self, config):
        super(EyeDiseaseEfficientNet, self).__init__()
        self.efficientnet = models.efficientnet_b4(pretrained=False)
        self.efficientnet.classifier = nn.Identity()
        self.fc_age_sex = nn.Sequential(
            nn.Linear(2, 64),
            nn.ReLU(),
            nn.Dropout(0.5)
        )
        self.fc_combined = nn.Sequential(
            nn.Linear(1792 + 64, 512),
            nn.ReLU(),
            nn.Dropout(0.6),
            nn.Linear(512, 8)
        )

    def forward(self, x_img, x_age_sex):
        x_img = self.efficientnet(x_img)
        x_age_sex = self.fc_age_sex(x_age_sex)
        x = torch.cat((x_img, x_age_sex), dim=1)
        x = self.fc_combined(x)
        return x

model = EyeDiseaseEfficientNet(config)
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))

device = torch.device("cpu")
model = model.to(device)
model.eval()

input_image = torch.randn(1, 3, 224, 224).to(device)
input_age_sex = torch.tensor([[45, 1]], dtype=torch.float32).to(device)

with torch.no_grad():
    output = model(input_image, input_age_sex)
    print(output)

import torch.nn.functional as F

logits = torch.tensor([[-2.6384, -1.8599,  0.0206,  2.0523,  0.2476,  1.9363,  1.5297, -1.0108]], device='cpu')
probabilities = F.softmax(logits, dim=1)
predicted_class = torch.argmax(probabilities, dim=1)
print(f"Предсказанный класс: {predicted_class.item()}")

import gradio as gr
import cv2
import numpy as np
from PIL import Image

def detect_eye(img):
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    eyes = eye_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    if len(eyes) > 0:
        (x, y, w, h) = eyes[0]
        img = img[y:y+h, x:x+w]
    return img

def preprocess_image(img):
    img = cv2.medianBlur(img, 3)
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    l = clahe.apply(l)
    lab = cv2.merge((l, a, b))
    img = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
    return img

def resize_with_padding(img, target_size=(224, 224)):
    h, w = img.shape[:2]
    scale = min(target_size[0] / h, target_size[1] / w)
    new_w, new_h = int(w * scale), int(h * scale)
    resized_img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
    pad_w = (target_size[1] - new_w) // 2
    pad_h = (target_size[0] - new_h) // 2
    padded_img = cv2.copyMakeBorder(
        resized_img, pad_h, target_size[0] - new_h - pad_h, pad_w, target_size[1] - new_w - pad_w,
        cv2.BORDER_CONSTANT, value=[0, 0, 0]
    )
    return padded_img

def predict(age, sex, img):
    img = detect_eye(img)
    img = preprocess_image(img)
    img = resize_with_padding(img)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = Image.fromarray(img)
    img_tensor = torch.tensor(np.array(img)).permute(2, 0, 1).unsqueeze(0).float()
    age_sex_tensor = torch.tensor([[age, 0 if sex == "Male" else 1]]).float()
    with torch.no_grad():
        outputs = model(img_tensor, age_sex_tensor)
    probabilities = torch.softmax(outputs, dim=1).cpu().numpy()[0]
    disease_labels = [
        "Normal",
        "Diabetic Retinopathy",
        "Glaucoma",
        "Cataract",
        "Age-related Macular Degeneration",
        "Hypertension",
        "Pathological Myopia",
        "Other Diseases/Abnormalities"
    ]

    result = {disease_labels[i]: f"{probabilities[i]*100:.2f}%" for i in range(len(disease_labels))}
    return result, img

examples = [
    [30, "Male", "myopia.png"]
]

iface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Slider(minimum=0, maximum=100, step=1, label="Age"),
        gr.Radio(["Male", "Female"], label="Gener"),
        gr.Image(type="numpy", label="Upload Eye Image/ your Selfies / photo")
    ],
    outputs=[gr.JSON(label="Predictions"), gr.Image(label="Processed Image")],
    examples=examples
)

iface.launch()