Spaces:
Sleeping
Sleeping
UCLV\beel
commited on
Commit
·
072aac4
1
Parent(s):
9a2df16
add model and gradio app
Browse files- app.py +66 -0
- textfile3-2.pk1 +3 -0
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ModelTester.ipynb.
|
2 |
+
|
3 |
+
# %% auto 0
|
4 |
+
__all__ = ['learn', 'categories', 'examples', 'intf', 'OrdinalRegressionMetric', 'classify_image']
|
5 |
+
|
6 |
+
# %% ModelTester.ipynb 1
|
7 |
+
from fastai.vision.all import *
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# %% ModelTester.ipynb 3
|
11 |
+
from fastai.metrics import Metric
|
12 |
+
|
13 |
+
class OrdinalRegressionMetric(Metric):
|
14 |
+
def __init__(self):
|
15 |
+
super().__init__()
|
16 |
+
self.total = 0
|
17 |
+
self.count = 0
|
18 |
+
|
19 |
+
def accumulate(self, learn):
|
20 |
+
# Get predictions and targets
|
21 |
+
preds, targs = learn.pred, learn.y
|
22 |
+
|
23 |
+
# Your custom logic to convert predictions and targets to numeric values
|
24 |
+
preds_numeric = torch.argmax(preds, dim=1)
|
25 |
+
targs_numeric = targs
|
26 |
+
|
27 |
+
#print("preds_numeric: ",preds_numeric)
|
28 |
+
#print("targs_numeric: ",targs_numeric)
|
29 |
+
|
30 |
+
# Calculate the metric (modify this based on your specific needs)
|
31 |
+
squared_diff = torch.sum(torch.sqrt((preds_numeric - targs_numeric)**2))
|
32 |
+
|
33 |
+
# Normalize by the maximum possible difference
|
34 |
+
max_diff = torch.sqrt((torch.max(targs_numeric) - torch.min(targs_numeric))**2)
|
35 |
+
|
36 |
+
#print("squared_diff: ",squared_diff)
|
37 |
+
#print("max_diff: ",max_diff)
|
38 |
+
|
39 |
+
# Update the metric value
|
40 |
+
self.total += squared_diff
|
41 |
+
#print("self.total: ",self.total)
|
42 |
+
self.count += max_diff
|
43 |
+
#print("self.count: ",self.count)
|
44 |
+
@property
|
45 |
+
def value(self):
|
46 |
+
if self.count == 0:
|
47 |
+
return 0.0 # or handle this case appropriately
|
48 |
+
#print("self.total / self.count: ", (self.total / self.count))
|
49 |
+
# Calculate the normalized metric value
|
50 |
+
metric_value = 1/(self.total / self.count)
|
51 |
+
return metric_value
|
52 |
+
|
53 |
+
# %% ModelTester.ipynb 4
|
54 |
+
learn = load_learner("textfile3-2.pk1")
|
55 |
+
|
56 |
+
# %% ModelTester.ipynb 6
|
57 |
+
categories = ("1","1-2","2","2-3","3","3-4","4","4-5","5")
|
58 |
+
|
59 |
+
def classify_image(img):
|
60 |
+
pred, idx, probs = learn.predict(img)
|
61 |
+
return dict(zip(categories, map(float, probs)))
|
62 |
+
|
63 |
+
# %% ModelTester.ipynb 8
|
64 |
+
|
65 |
+
intf = gr.Interface(fn=classify_image, inputs='image', outputs='label')
|
66 |
+
intf.launch(inline=False)
|
textfile3-2.pk1
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c86bf454f4bcf7dd0fba8c4628ac89872ce3431fb4d2997ccbdbab5ba70c3dbb
|
3 |
+
size 46998504
|