Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +90 -0
- model.bin +3 -0
- requirements.txt +12 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import torch.nn as nn
|
5 |
+
from torchvision import transforms
|
6 |
+
from sklearn.model_selection import GroupKFold
|
7 |
+
import cv2
|
8 |
+
from skimage import io
|
9 |
+
import torch
|
10 |
+
from torch import nn
|
11 |
+
import os
|
12 |
+
from datetime import datetime
|
13 |
+
import time
|
14 |
+
import random
|
15 |
+
import cv2
|
16 |
+
import pandas as pd
|
17 |
+
import numpy as np
|
18 |
+
import albumentations as A
|
19 |
+
from albumentations.pytorch.transforms import ToTensorV2
|
20 |
+
import sklearn
|
21 |
+
from efficientnet_pytorch import EfficientNet
|
22 |
+
|
23 |
+
def get_net():
|
24 |
+
net = EfficientNet.from_pretrained('efficientnet-b4')
|
25 |
+
net._fc = nn.Linear(in_features=1792, out_features=4, bias=True)
|
26 |
+
return net
|
27 |
+
|
28 |
+
def get_test_transforms(mode):
|
29 |
+
if mode == 0:
|
30 |
+
return A.Compose([
|
31 |
+
A.Resize(height=512, width=512, p=1.0),
|
32 |
+
ToTensorV2(p=1.0),
|
33 |
+
], p=1.0)
|
34 |
+
elif mode == 1:
|
35 |
+
return A.Compose([
|
36 |
+
A.HorizontalFlip(p=1),
|
37 |
+
A.Resize(height=512, width=512, p=1.0),
|
38 |
+
ToTensorV2(p=1.0),
|
39 |
+
], p=1.0)
|
40 |
+
elif mode == 2:
|
41 |
+
return A.Compose([
|
42 |
+
A.VerticalFlip(p=1),
|
43 |
+
A.Resize(height=512, width=512, p=1.0),
|
44 |
+
ToTensorV2(p=1.0),
|
45 |
+
], p=1.0)
|
46 |
+
else:
|
47 |
+
return A.Compose([
|
48 |
+
A.HorizontalFlip(p=1),
|
49 |
+
A.VerticalFlip(p=1),
|
50 |
+
A.Resize(height=512, width=512, p=1.0),
|
51 |
+
ToTensorV2(p=1.0),
|
52 |
+
], p=1.0)
|
53 |
+
|
54 |
+
def preprocess_image(image_path, transforms=None):
|
55 |
+
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
|
56 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)
|
57 |
+
image /= 255.0 # Normalize to [0, 1]
|
58 |
+
if transforms:
|
59 |
+
sample = {'image': image}
|
60 |
+
sample = transforms(**sample)
|
61 |
+
image = sample['image']
|
62 |
+
image = torch.tensor(image, dtype=torch.float32).unsqueeze(0)
|
63 |
+
return image
|
64 |
+
|
65 |
+
net = get_net()
|
66 |
+
checkpoint = torch.load('model.bin', map_location="cpu")
|
67 |
+
net.load_state_dict(checkpoint['model_state_dict']);
|
68 |
+
net.eval()
|
69 |
+
|
70 |
+
path = "uploads" # Define path for uploads
|
71 |
+
os.makedirs(path, exist_ok=True) # Ensure directory exists
|
72 |
+
|
73 |
+
def predict_image(img):
|
74 |
+
img_path = os.path.join(path, img.name)
|
75 |
+
img.save(img_path)
|
76 |
+
|
77 |
+
image_tensor = preprocess_image(img_path, get_test_transforms(224))
|
78 |
+
pred = net(image_tensor)
|
79 |
+
y_pred = 1 - nn.functional.softmax(pred, dim=1).data.cpu().numpy()[:, 0]
|
80 |
+
return float(y_pred.item())
|
81 |
+
|
82 |
+
iface = gr.Interface(
|
83 |
+
fn=predict_image,
|
84 |
+
inputs=gr.Image(type="file"),
|
85 |
+
outputs="number",
|
86 |
+
title="Image Prediction App",
|
87 |
+
description="Upload an image and get a model prediction."
|
88 |
+
)
|
89 |
+
|
90 |
+
iface.launch()
|
model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:30a5fe4104ba332c3398854d8d0aec0bf81f8ed5b06b3ea75b6dd60ee0372546
|
3 |
+
size 211430648
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
pandas
|
4 |
+
matplotlib
|
5 |
+
scikit-learn
|
6 |
+
opencv-python
|
7 |
+
scikit-image
|
8 |
+
torch
|
9 |
+
torchvision
|
10 |
+
timm
|
11 |
+
efficientnet-pytorch
|
12 |
+
albumentations
|