Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import DPTFeatureExtractor, DPTForSemanticSegmentation
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
import requests
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
#st.text(str(logits))
|
| 20 |
-
st.success("Success")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
from transformers import DPTFeatureExtractor, DPTForSemanticSegmentation
|
| 6 |
from PIL import Image
|
| 7 |
+
from torch import nn
|
| 8 |
import requests
|
| 9 |
import streamlit as st
|
| 10 |
|
| 11 |
+
img_path = None
|
| 12 |
+
st.title('Semantic Segmentation using Beit')
|
| 13 |
+
file_upload = st.file_uploader('Raw Input Image')
|
| 14 |
+
image_path = st.selectbox(
|
| 15 |
+
'Choose any one image for inference',
|
| 16 |
+
('Select image', 'image1.jpg', 'image2.jpg', 'image3.jpg'))
|
| 17 |
|
| 18 |
+
if file_upload is None:
|
| 19 |
+
raw_image = image_path
|
| 20 |
+
else:
|
| 21 |
+
raw_image = file_upload
|
| 22 |
|
| 23 |
+
if raw_image != 'Select image':
|
| 24 |
+
df = pd.read_csv('class_dict_seg.csv')
|
| 25 |
+
classes = df['name']
|
| 26 |
+
palette = df[[' r', ' g', ' b']].values
|
| 27 |
+
id2label = classes.to_dict()
|
| 28 |
+
label2id = {v: k for k, v in id2label.items()}
|
| 29 |
|
| 30 |
+
image = Image.open(raw_image)
|
| 31 |
+
image = np.asarray(image)
|
| 32 |
+
|
| 33 |
+
with st.spinner('Loading Model...'):
|
| 34 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large-ade")
|
| 35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
+
model = DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade",ignore_mismatched_sizes=True,num_labels=len(id2label), id2label=id2label, label2id=label2id,reshape_last_stage=True)
|
| 37 |
+
model = model.to(device)
|
| 38 |
+
model.eval()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
st.success("Success")
|
| 42 |
+
|
| 43 |
+
#url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 44 |
+
#image = Image.open(requests.get(url, stream=True).raw)
|
| 45 |
+
#st.success("Image open: Success")
|
| 46 |
+
|
| 47 |
+
#feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large-ade")
|
| 48 |
+
#model = DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade")
|
| 49 |
+
#st.success("Load model: Success")
|
| 50 |
+
|
| 51 |
+
#inputs = feature_extractor(images=image, return_tensors="pt")
|
| 52 |
+
#st.success("Feature extraction: Success")
|
| 53 |
+
|
| 54 |
+
#outputs = model(**inputs)
|
| 55 |
+
#logits = outputs.logits
|
| 56 |
#st.text(str(logits))
|
| 57 |
+
#st.success("Success")
|