Spaces:
Running
Running
Upload 11 files
Browse files- .gitattributes +1 -0
- README.md +1 -1
- app.py +197 -0
- food-1.jpg +0 -0
- food-2.jpg +0 -0
- food-3.jpg +0 -0
- food-4.jpg +0 -0
- food-5.jpg +3 -0
- food-6.jpg +0 -0
- labels.txt +104 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
food-5.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🏃
|
|
4 |
colorFrom: purple
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: purple
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.44.4
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
ADDED
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from matplotlib import gridspec
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import tensorflow as tf
|
8 |
+
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
9 |
+
|
10 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
11 |
+
"prem-timsina/segformer-b0-finetuned-food"
|
12 |
+
)
|
13 |
+
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
14 |
+
"prem-timsina/segformer-b0-finetuned-food"
|
15 |
+
)
|
16 |
+
|
17 |
+
def ade_palette():
|
18 |
+
"""ADE20K palette that maps each class to RGB values."""
|
19 |
+
return [
|
20 |
+
[93, 93, 93],
|
21 |
+
[43, 240, 132],
|
22 |
+
[139, 136, 240],
|
23 |
+
[158, 83, 109],
|
24 |
+
[6, 76, 151],
|
25 |
+
[95, 170, 87],
|
26 |
+
[273, 236, 139],
|
27 |
+
[21, 155, 160],
|
28 |
+
[188, 220, 166],
|
29 |
+
[238, 96, 247],
|
30 |
+
[223, 180, 221],
|
31 |
+
[29, 97, 24],
|
32 |
+
[3, 233, 248],
|
33 |
+
[105, 118, 44],
|
34 |
+
[203, 237, 63],
|
35 |
+
[234, 100, 240],
|
36 |
+
[19, 179, 164],
|
37 |
+
[65, 22, 115],
|
38 |
+
[111, 128, 194],
|
39 |
+
[232, 41, 17],
|
40 |
+
[11, 250, 159],
|
41 |
+
[137, 163, 129],
|
42 |
+
[212, 223, 210],
|
43 |
+
[51, 37, 4],
|
44 |
+
[37, 63, 239],
|
45 |
+
[257, 180, 163],
|
46 |
+
[172, 53, 105],
|
47 |
+
[104, 150, 99],
|
48 |
+
[80, 157, 133],
|
49 |
+
[195, 104, 202],
|
50 |
+
[42, 187, 110],
|
51 |
+
[133, 225, 66],
|
52 |
+
[132, 99, 213],
|
53 |
+
[178, 248, 209],
|
54 |
+
[93, 147, 60],
|
55 |
+
[105, 109, 115],
|
56 |
+
[26, 65, 115],
|
57 |
+
[239, 52, 182],
|
58 |
+
[242, 19, 204],
|
59 |
+
[157, 101, 214],
|
60 |
+
[248, 85, 198],
|
61 |
+
[103, 198, 171],
|
62 |
+
[44, 129, 75],
|
63 |
+
[159, 32, 120],
|
64 |
+
[155, 77, 71],
|
65 |
+
[233, 231, 155],
|
66 |
+
[135, 196, 206],
|
67 |
+
[81, 53, 51],
|
68 |
+
[134, 221, 213],
|
69 |
+
[192, 27, 152],
|
70 |
+
[127, 127, 194],
|
71 |
+
[82, 161, 1],
|
72 |
+
[71, 80, 161],
|
73 |
+
[148, 9, 159],
|
74 |
+
[91, 110, 124],
|
75 |
+
[127, 157, 223],
|
76 |
+
[25, 210, 232],
|
77 |
+
[129, 0, 114],
|
78 |
+
[231, 187, 138],
|
79 |
+
[23, 17, 224],
|
80 |
+
[25, 255, 29],
|
81 |
+
[158, 19, 53],
|
82 |
+
[157, 190, 176],
|
83 |
+
[114, 140, 221],
|
84 |
+
[46, 104, 87],
|
85 |
+
[17, 114, 122],
|
86 |
+
[221, 12, 229],
|
87 |
+
[54, 20, 92],
|
88 |
+
[215, 191, 252],
|
89 |
+
[144, 127, 146],
|
90 |
+
[141, 116, 77],
|
91 |
+
[100, 89, 89],
|
92 |
+
[104, 115, 249],
|
93 |
+
[179, 212, 38],
|
94 |
+
[140, 248, 179],
|
95 |
+
[177, 230, 240],
|
96 |
+
[219, 98, 8],
|
97 |
+
[74, 219, 53],
|
98 |
+
[161, 28, 243],
|
99 |
+
[64, 57, 184],
|
100 |
+
[147, 193, 113],
|
101 |
+
[182, 15, 30],
|
102 |
+
[151, 204, 109],
|
103 |
+
[187, 76, 21],
|
104 |
+
[118, 163, 155],
|
105 |
+
[158, 30, 220],
|
106 |
+
[227, 170, 63],
|
107 |
+
[199, 186, 72],
|
108 |
+
[0, 241, 168],
|
109 |
+
[80, 150, 225],
|
110 |
+
[237, 250, 4],
|
111 |
+
[29, 210, 181],
|
112 |
+
[176, 120, 81],
|
113 |
+
[134, 47, 123],
|
114 |
+
[240, 141, 130],
|
115 |
+
[250, 41, 115],
|
116 |
+
[29, 88, 143],
|
117 |
+
[66, 151, 87],
|
118 |
+
[241, 231, 144],
|
119 |
+
[238, 107, 153],
|
120 |
+
[181, 96, 220],
|
121 |
+
[239, 122, 133],
|
122 |
+
[205, 120, 21],
|
123 |
+
[168, 12, 77],
|
124 |
+
|
125 |
+
]
|
126 |
+
|
127 |
+
labels_list = []
|
128 |
+
|
129 |
+
with open(r'labels.txt', 'r') as fp:
|
130 |
+
for line in fp:
|
131 |
+
labels_list.append(line[:-1])
|
132 |
+
|
133 |
+
colormap = np.asarray(ade_palette())
|
134 |
+
|
135 |
+
def label_to_color_image(label):
|
136 |
+
if label.ndim != 2:
|
137 |
+
raise ValueError("Expect 2-D input label")
|
138 |
+
|
139 |
+
if np.max(label) >= len(colormap):
|
140 |
+
raise ValueError("label value too large.")
|
141 |
+
return colormap[label]
|
142 |
+
|
143 |
+
def draw_plot(pred_img, seg):
|
144 |
+
fig = plt.figure(figsize=(20, 15))
|
145 |
+
|
146 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
147 |
+
|
148 |
+
plt.subplot(grid_spec[0])
|
149 |
+
plt.imshow(pred_img)
|
150 |
+
plt.axis('off')
|
151 |
+
LABEL_NAMES = np.asarray(labels_list)
|
152 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
153 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
154 |
+
|
155 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
156 |
+
ax = plt.subplot(grid_spec[1])
|
157 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
158 |
+
ax.yaxis.tick_right()
|
159 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
160 |
+
plt.xticks([], [])
|
161 |
+
ax.tick_params(width=0.0, labelsize=25)
|
162 |
+
return fig
|
163 |
+
|
164 |
+
def sepia(input_img):
|
165 |
+
input_img = Image.fromarray(input_img)
|
166 |
+
|
167 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
168 |
+
outputs = model(**inputs)
|
169 |
+
logits = outputs.logits
|
170 |
+
|
171 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
172 |
+
logits = tf.image.resize(
|
173 |
+
logits, input_img.size[::-1]
|
174 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
175 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
176 |
+
|
177 |
+
color_seg = np.zeros(
|
178 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
179 |
+
) # height, width, 3
|
180 |
+
for label, color in enumerate(colormap):
|
181 |
+
color_seg[seg.numpy() == label, :] = color
|
182 |
+
|
183 |
+
# Show image + mask
|
184 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
185 |
+
pred_img = pred_img.astype(np.uint8)
|
186 |
+
|
187 |
+
fig = draw_plot(pred_img, seg)
|
188 |
+
return fig
|
189 |
+
|
190 |
+
demo = gr.Interface(fn=sepia,
|
191 |
+
inputs=gr.Image(shape=(400, 600)),
|
192 |
+
outputs=['plot'],
|
193 |
+
examples=["food-1.jpg","food-2.jpg", "food-3.jpg", "food-4.jpg", "food-5.jpg", "food-6.jpg"],
|
194 |
+
allow_flagging='never')
|
195 |
+
|
196 |
+
|
197 |
+
demo.launch()
|
food-1.jpg
ADDED
![]() |
food-2.jpg
ADDED
![]() |
food-3.jpg
ADDED
![]() |
food-4.jpg
ADDED
![]() |
food-5.jpg
ADDED
![]() |
Git LFS Details
|
food-6.jpg
ADDED
![]() |
labels.txt
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
background
|
2 |
+
candy
|
3 |
+
egg tart
|
4 |
+
french fries
|
5 |
+
chocolate
|
6 |
+
biscuit
|
7 |
+
popcorn
|
8 |
+
pudding
|
9 |
+
ice cream
|
10 |
+
cheese butter
|
11 |
+
cake
|
12 |
+
wine
|
13 |
+
milkshake
|
14 |
+
coffee
|
15 |
+
juice
|
16 |
+
milk
|
17 |
+
tea
|
18 |
+
almond
|
19 |
+
red beans
|
20 |
+
cashew
|
21 |
+
dried cranberries
|
22 |
+
soy
|
23 |
+
walnut
|
24 |
+
peanut
|
25 |
+
egg
|
26 |
+
apple
|
27 |
+
date
|
28 |
+
apricot
|
29 |
+
avocado
|
30 |
+
banana
|
31 |
+
strawberry
|
32 |
+
cherry
|
33 |
+
blueberry
|
34 |
+
raspberry
|
35 |
+
mango
|
36 |
+
olives
|
37 |
+
peach
|
38 |
+
lemon
|
39 |
+
pear
|
40 |
+
fig
|
41 |
+
pineapple
|
42 |
+
grape
|
43 |
+
kiwi
|
44 |
+
melon
|
45 |
+
orange
|
46 |
+
watermelon
|
47 |
+
steak
|
48 |
+
pork
|
49 |
+
chicken duck
|
50 |
+
sausage
|
51 |
+
fried meat
|
52 |
+
lamb
|
53 |
+
sauce
|
54 |
+
crab
|
55 |
+
fish
|
56 |
+
shellfish
|
57 |
+
shrimp
|
58 |
+
soup
|
59 |
+
bread
|
60 |
+
corn
|
61 |
+
hamburg
|
62 |
+
pizza
|
63 |
+
hanamaki baozi
|
64 |
+
wonton dumplings
|
65 |
+
pasta
|
66 |
+
noodles
|
67 |
+
rice
|
68 |
+
pie
|
69 |
+
tofu
|
70 |
+
eggplant
|
71 |
+
potato
|
72 |
+
garlic
|
73 |
+
cauliflower
|
74 |
+
tomato
|
75 |
+
kelp
|
76 |
+
seaweed
|
77 |
+
spring onion
|
78 |
+
rape
|
79 |
+
ginger
|
80 |
+
okra
|
81 |
+
lettuce
|
82 |
+
pumpkin
|
83 |
+
cucumber
|
84 |
+
white radish
|
85 |
+
carrot
|
86 |
+
asparagus
|
87 |
+
bamboo shoots
|
88 |
+
broccoli
|
89 |
+
celery stick
|
90 |
+
cilantro mint
|
91 |
+
snow peas
|
92 |
+
cabbage
|
93 |
+
bean sprouts
|
94 |
+
onion
|
95 |
+
pepper
|
96 |
+
green beans
|
97 |
+
French beans
|
98 |
+
king oyster mushroom
|
99 |
+
shiitake
|
100 |
+
enoki mushroom
|
101 |
+
oyster mushroom
|
102 |
+
white button mushroom
|
103 |
+
salad
|
104 |
+
other ingredients
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
tensorflow
|
4 |
+
numpy
|
5 |
+
Image
|
6 |
+
matplotlib
|