Spaces:
Runtime error
Runtime error
Commit Β·
582a46c
0
Parent(s):
Duplicate from akhaliq/DPT-Large
Browse filesCo-authored-by: Ahsen Khaliq <akhaliq@users.noreply.huggingface.co>
- .gitattributes +27 -0
- README.md +38 -0
- app.py +58 -0
- requirements.txt +6 -0
.gitattributes
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: DPT Large
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
duplicated_from: akhaliq/DPT-Large
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Configuration
|
| 13 |
+
|
| 14 |
+
`title`: _string_
|
| 15 |
+
Display title for the Space
|
| 16 |
+
|
| 17 |
+
`emoji`: _string_
|
| 18 |
+
Space emoji (emoji-only character allowed)
|
| 19 |
+
|
| 20 |
+
`colorFrom`: _string_
|
| 21 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
| 22 |
+
|
| 23 |
+
`colorTo`: _string_
|
| 24 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
| 25 |
+
|
| 26 |
+
`sdk`: _string_
|
| 27 |
+
Can be either `gradio` or `streamlit`
|
| 28 |
+
|
| 29 |
+
`sdk_version` : _string_
|
| 30 |
+
Only applicable for `streamlit` SDK.
|
| 31 |
+
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
| 32 |
+
|
| 33 |
+
`app_file`: _string_
|
| 34 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
| 35 |
+
Path is relative to the root of the repository.
|
| 36 |
+
|
| 37 |
+
`pinned`: _boolean_
|
| 38 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import torch
|
| 3 |
+
import urllib.request
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
| 10 |
+
urllib.request.urlretrieve(url, filename)
|
| 11 |
+
|
| 12 |
+
model_type = "DPT_Large" # MiDaS v3 - Large (highest accuracy, slowest inference speed)
|
| 13 |
+
#model_type = "DPT_Hybrid" # MiDaS v3 - Hybrid (medium accuracy, medium inference speed)
|
| 14 |
+
#model_type = "MiDaS_small" # MiDaS v2.1 - Small (lowest accuracy, highest inference speed)
|
| 15 |
+
|
| 16 |
+
midas = torch.hub.load("intel-isl/MiDaS", model_type)
|
| 17 |
+
|
| 18 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 19 |
+
midas.to(device)
|
| 20 |
+
midas.eval()
|
| 21 |
+
|
| 22 |
+
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
|
| 23 |
+
|
| 24 |
+
if model_type == "DPT_Large" or model_type == "DPT_Hybrid":
|
| 25 |
+
transform = midas_transforms.dpt_transform
|
| 26 |
+
else:
|
| 27 |
+
transform = midas_transforms.small_transform
|
| 28 |
+
|
| 29 |
+
def inference(img):
|
| 30 |
+
img = cv2.imread(img.name)
|
| 31 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 32 |
+
|
| 33 |
+
input_batch = transform(img).to(device)
|
| 34 |
+
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
prediction = midas(input_batch)
|
| 37 |
+
|
| 38 |
+
prediction = torch.nn.functional.interpolate(
|
| 39 |
+
prediction.unsqueeze(1),
|
| 40 |
+
size=img.shape[:2],
|
| 41 |
+
mode="bicubic",
|
| 42 |
+
align_corners=False,
|
| 43 |
+
).squeeze()
|
| 44 |
+
|
| 45 |
+
output = prediction.cpu().numpy()
|
| 46 |
+
formatted = (output * 255 / np.max(output)).astype('uint8')
|
| 47 |
+
img = Image.fromarray(formatted)
|
| 48 |
+
return img
|
| 49 |
+
|
| 50 |
+
inputs = gr.inputs.Image(type='file', label="Original Image")
|
| 51 |
+
outputs = gr.outputs.Image(type="pil",label="Output Image")
|
| 52 |
+
|
| 53 |
+
title = "DPT-Large"
|
| 54 |
+
description = "Gradio demo for DPT-Large:Vision Transformers for Dense Prediction.To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
| 55 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2103.13413' target='_blank'>Vision Transformers for Dense Prediction</a> | <a href='https://github.com/intel-isl/MiDaS' target='_blank'>Github Repo</a></p>"
|
| 56 |
+
|
| 57 |
+
examples=[['dog.jpg']]
|
| 58 |
+
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, analytics_enabled=False,examples=examples, enable_queue=True).launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python-headless
|
| 2 |
+
torch
|
| 3 |
+
matplotlib
|
| 4 |
+
numpy
|
| 5 |
+
Pillow
|
| 6 |
+
timm
|