Spaces:
Sleeping
Sleeping
Commit
·
32e703d
1
Parent(s):
073572d
Add application code and requirements
Browse files- README.md +0 -1
- app.py +74 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -10,4 +10,3 @@ pinned: false
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
|
|
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
import torchvision as tv
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
CATEGORIES = [
|
| 8 |
+
"agricultural",
|
| 9 |
+
"airplane",
|
| 10 |
+
"baseballdiamond",
|
| 11 |
+
"beach",
|
| 12 |
+
"buildings",
|
| 13 |
+
"chaparral",
|
| 14 |
+
"denseresidential",
|
| 15 |
+
"forest",
|
| 16 |
+
"freeway",
|
| 17 |
+
"golfcourse",
|
| 18 |
+
"harbor",
|
| 19 |
+
"intersection",
|
| 20 |
+
"mediumresidential",
|
| 21 |
+
"mobilehomepark",
|
| 22 |
+
"overpass",
|
| 23 |
+
"parkinglot",
|
| 24 |
+
"river",
|
| 25 |
+
"runway",
|
| 26 |
+
"sparseresidential",
|
| 27 |
+
"storagetanks",
|
| 28 |
+
"tenniscourt",
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class Classifier:
|
| 33 |
+
def __init__(self, model_path):
|
| 34 |
+
self.model_path = model_path
|
| 35 |
+
self.session = ort.InferenceSession(self.model_path)
|
| 36 |
+
|
| 37 |
+
self.img_transforms = tv.transforms.Compose(
|
| 38 |
+
[
|
| 39 |
+
tv.transforms.Resize((256, 256)),
|
| 40 |
+
tv.transforms.ToTensor(),
|
| 41 |
+
tv.transforms.Normalize(
|
| 42 |
+
(0.48422758, 0.49005175, 0.45050276),
|
| 43 |
+
(0.17348297, 0.16352356, 0.15547496),
|
| 44 |
+
),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def predict(self, image):
|
| 49 |
+
inp = self.img_transforms(image).unsqueeze(0).numpy()
|
| 50 |
+
logits = self.session.run(
|
| 51 |
+
None,
|
| 52 |
+
{self.session.get_inputs()[0].name: inp},
|
| 53 |
+
)[0]
|
| 54 |
+
probs = np.exp(logits) / np.sum(np.exp(logits))
|
| 55 |
+
return {
|
| 56 |
+
category: float(prob)
|
| 57 |
+
for category, prob in zip(
|
| 58 |
+
CATEGORIES,
|
| 59 |
+
probs[0],
|
| 60 |
+
)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
model_path = hf_hub_download(
|
| 65 |
+
repo_id="SatwikKambham/land_use_classifier",
|
| 66 |
+
filename="model.onnx",
|
| 67 |
+
)
|
| 68 |
+
classifier = Classifier(model_path)
|
| 69 |
+
interface = gr.Interface(
|
| 70 |
+
fn=classifier.predict,
|
| 71 |
+
inputs=gr.components.Image(label="Input image", type="pil"),
|
| 72 |
+
outputs=gr.components.Label(label="Predicted class", num_top_classes=3),
|
| 73 |
+
)
|
| 74 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
onnxruntime
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
huggingface_hub
|