Commit
·
432e4a1
1
Parent(s):
a04ec84
reading image with PIL
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
5 |
from transformers import SegformerForSemanticSegmentation, SegformerFeatureExtractor
|
6 |
from torch import nn
|
7 |
import streamlit as st
|
@@ -9,14 +10,14 @@ import streamlit as st
|
|
9 |
|
10 |
raw_image = st.file_uploader('Raw Input Image')
|
11 |
if raw_image is not None:
|
12 |
-
|
13 |
df = pd.read_csv('class_dict_seg.csv')
|
14 |
classes = df['name']
|
15 |
palette = df[[' r', ' g', ' b']].values
|
16 |
id2label = classes.to_dict()
|
17 |
label2id = {v: k for k, v in id2label.items()}
|
18 |
|
19 |
-
image =
|
|
|
20 |
|
21 |
feature_extractor = SegformerFeatureExtractor(align=False, reduce_zero_label=False)
|
22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -25,21 +26,17 @@ if raw_image is not None:
|
|
25 |
num_labels=len(id2label), id2label=id2label, label2id=label2id,
|
26 |
reshape_last_stage=True)
|
27 |
model = model.to(device)
|
28 |
-
|
29 |
# prepare the image for the model (aligned resize)
|
30 |
feature_extractor_inference = SegformerFeatureExtractor(do_random_crop=False, do_pad=False)
|
31 |
-
|
32 |
pixel_values = feature_extractor_inference(image, return_tensors="pt").pixel_values.to(device)
|
33 |
model.eval()
|
34 |
outputs = model(pixel_values=pixel_values)# logits are of shape (batch_size, num_labels, height/4, width/4)
|
35 |
logits = outputs.logits.cpu()
|
36 |
-
|
37 |
# First, rescale logits to original image size
|
38 |
upsampled_logits = nn.functional.interpolate(logits,
|
39 |
size=image.shape[:-1], # (height, width)
|
40 |
mode='bilinear',
|
41 |
align_corners=False)
|
42 |
-
|
43 |
# Second, apply argmax on the class dimension
|
44 |
seg = upsampled_logits.argmax(dim=1)[0]
|
45 |
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3\
|
@@ -47,9 +44,7 @@ if raw_image is not None:
|
|
47 |
color_seg[seg == label, :] = color
|
48 |
# Convert to BGR
|
49 |
color_seg = color_seg[..., ::-1]
|
50 |
-
|
51 |
# Show image + mask
|
52 |
img = np.array(image) * 0.5 + color_seg * 0.5
|
53 |
img = img.astype(np.uint8)
|
54 |
-
|
55 |
st.image(img, caption="Segmented Image")
|
|
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import torch
|
5 |
+
from PIL import Image
|
6 |
from transformers import SegformerForSemanticSegmentation, SegformerFeatureExtractor
|
7 |
from torch import nn
|
8 |
import streamlit as st
|
|
|
10 |
|
11 |
raw_image = st.file_uploader('Raw Input Image')
|
12 |
if raw_image is not None:
|
|
|
13 |
df = pd.read_csv('class_dict_seg.csv')
|
14 |
classes = df['name']
|
15 |
palette = df[[' r', ' g', ' b']].values
|
16 |
id2label = classes.to_dict()
|
17 |
label2id = {v: k for k, v in id2label.items()}
|
18 |
|
19 |
+
image = Image.open(raw_image)
|
20 |
+
image = np.asarray(image)
|
21 |
|
22 |
feature_extractor = SegformerFeatureExtractor(align=False, reduce_zero_label=False)
|
23 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
26 |
num_labels=len(id2label), id2label=id2label, label2id=label2id,
|
27 |
reshape_last_stage=True)
|
28 |
model = model.to(device)
|
|
|
29 |
# prepare the image for the model (aligned resize)
|
30 |
feature_extractor_inference = SegformerFeatureExtractor(do_random_crop=False, do_pad=False)
|
|
|
31 |
pixel_values = feature_extractor_inference(image, return_tensors="pt").pixel_values.to(device)
|
32 |
model.eval()
|
33 |
outputs = model(pixel_values=pixel_values)# logits are of shape (batch_size, num_labels, height/4, width/4)
|
34 |
logits = outputs.logits.cpu()
|
|
|
35 |
# First, rescale logits to original image size
|
36 |
upsampled_logits = nn.functional.interpolate(logits,
|
37 |
size=image.shape[:-1], # (height, width)
|
38 |
mode='bilinear',
|
39 |
align_corners=False)
|
|
|
40 |
# Second, apply argmax on the class dimension
|
41 |
seg = upsampled_logits.argmax(dim=1)[0]
|
42 |
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3\
|
|
|
44 |
color_seg[seg == label, :] = color
|
45 |
# Convert to BGR
|
46 |
color_seg = color_seg[..., ::-1]
|
|
|
47 |
# Show image + mask
|
48 |
img = np.array(image) * 0.5 + color_seg * 0.5
|
49 |
img = img.astype(np.uint8)
|
|
|
50 |
st.image(img, caption="Segmented Image")
|