Thiago Hersan
commited on
Commit
•
a71ecc9
1
Parent(s):
19e4dee
fix fastapi version
Browse files- app.ipynb +51 -10
- requirements.txt +6 -5
app.ipynb
CHANGED
@@ -23,7 +23,51 @@
|
|
23 |
"ade_mean=[0.485, 0.456, 0.406]\n",
|
24 |
"ade_std=[0.229, 0.224, 0.225]\n",
|
25 |
"\n",
|
26 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
]
|
28 |
},
|
29 |
{
|
@@ -38,7 +82,7 @@
|
|
38 |
" do_normalize=False,\n",
|
39 |
" do_rescale=False,\n",
|
40 |
" ignore_index=255,\n",
|
41 |
-
"
|
42 |
")\n",
|
43 |
"\n",
|
44 |
"hf_token = environ.get('HFTOKEN') or True\n",
|
@@ -72,9 +116,7 @@
|
|
72 |
"outputs": [],
|
73 |
"source": [
|
74 |
"results = preprocessor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]\n",
|
75 |
-
"
|
76 |
-
"\n",
|
77 |
-
"labels = np.unique(results)"
|
78 |
]
|
79 |
},
|
80 |
{
|
@@ -83,14 +125,13 @@
|
|
83 |
"metadata": {},
|
84 |
"outputs": [],
|
85 |
"source": [
|
86 |
-
"
|
87 |
-
" print(model.config.id2label[label_id])"
|
88 |
]
|
89 |
}
|
90 |
],
|
91 |
"metadata": {
|
92 |
"kernelspec": {
|
93 |
-
"display_name": "Python 3.8.15 ('
|
94 |
"language": "python",
|
95 |
"name": "python3"
|
96 |
},
|
@@ -104,12 +145,12 @@
|
|
104 |
"name": "python",
|
105 |
"nbconvert_exporter": "python",
|
106 |
"pygments_lexer": "ipython3",
|
107 |
-
"version": "3.
|
108 |
},
|
109 |
"orig_nbformat": 4,
|
110 |
"vscode": {
|
111 |
"interpreter": {
|
112 |
-
"hash": "
|
113 |
}
|
114 |
}
|
115 |
},
|
|
|
23 |
"ade_mean=[0.485, 0.456, 0.406]\n",
|
24 |
"ade_std=[0.229, 0.224, 0.225]\n",
|
25 |
"\n",
|
26 |
+
"palette = [\n",
|
27 |
+
" [120, 120, 120], [4, 200, 4], [4, 4, 250], [6, 230, 230],\n",
|
28 |
+
" [80, 50, 50], [120, 120, 80], [140, 140, 140], [204, 5, 255]\n",
|
29 |
+
"]\n",
|
30 |
+
"\n",
|
31 |
+
"model_id = f\"thiagohersan/maskformer-satellite-trees\"\n",
|
32 |
+
"\n",
|
33 |
+
"vegetation_labels = [\"vegetation\"]"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": null,
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"def visualize_instance_seg_mask(img_in, mask, id2label, included_labels):\n",
|
43 |
+
" img_out = np.zeros((mask.shape[0], mask.shape[1], 3))\n",
|
44 |
+
" image_total_pixels = mask.shape[0] * mask.shape[1]\n",
|
45 |
+
" label_ids = np.unique(mask)\n",
|
46 |
+
"\n",
|
47 |
+
" id2color = {id: palette[id] for id in label_ids}\n",
|
48 |
+
" id2count = {id: 0 for id in label_ids}\n",
|
49 |
+
"\n",
|
50 |
+
" for i in range(img_out.shape[0]):\n",
|
51 |
+
" for j in range(img_out.shape[1]):\n",
|
52 |
+
" img_out[i, j, :] = id2color[mask[i, j]]\n",
|
53 |
+
" id2count[mask[i, j]] = id2count[mask[i, j]] + 1\n",
|
54 |
+
"\n",
|
55 |
+
" image_res = (0.5 * img_in + 0.5 * img_out).astype(np.uint8)\n",
|
56 |
+
"\n",
|
57 |
+
" dataframe = [[\n",
|
58 |
+
" f\"{id2label[id]}\",\n",
|
59 |
+
" f\"{(100 * id2count[id] / image_total_pixels):.2f} %\",\n",
|
60 |
+
" f\"{np.sqrt(id2count[id] / image_total_pixels):.2f} m\"\n",
|
61 |
+
" ] for id in label_ids if id2label[id] in included_labels]\n",
|
62 |
+
"\n",
|
63 |
+
" if len(dataframe) < 1:\n",
|
64 |
+
" dataframe = [[\n",
|
65 |
+
" f\"\",\n",
|
66 |
+
" f\"{(0):.2f} %\",\n",
|
67 |
+
" f\"{(0):.2f} m\"\n",
|
68 |
+
" ]]\n",
|
69 |
+
"\n",
|
70 |
+
" return image_res, dataframe\n"
|
71 |
]
|
72 |
},
|
73 |
{
|
|
|
82 |
" do_normalize=False,\n",
|
83 |
" do_rescale=False,\n",
|
84 |
" ignore_index=255,\n",
|
85 |
+
" do_reduce_labels=False\n",
|
86 |
")\n",
|
87 |
"\n",
|
88 |
"hf_token = environ.get('HFTOKEN') or True\n",
|
|
|
116 |
"outputs": [],
|
117 |
"source": [
|
118 |
"results = preprocessor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]\n",
|
119 |
+
"mask_img, dataframe = visualize_instance_seg_mask(np.array(img), results.numpy(), model.config.id2label, vegetation_labels)"
|
|
|
|
|
120 |
]
|
121 |
},
|
122 |
{
|
|
|
125 |
"metadata": {},
|
126 |
"outputs": [],
|
127 |
"source": [
|
128 |
+
"dataframe"
|
|
|
129 |
]
|
130 |
}
|
131 |
],
|
132 |
"metadata": {
|
133 |
"kernelspec": {
|
134 |
+
"display_name": "Python 3.8.15 ('gradio2023')",
|
135 |
"language": "python",
|
136 |
"name": "python3"
|
137 |
},
|
|
|
145 |
"name": "python",
|
146 |
"nbconvert_exporter": "python",
|
147 |
"pygments_lexer": "ipython3",
|
148 |
+
"version": "3.9.17"
|
149 |
},
|
150 |
"orig_nbformat": 4,
|
151 |
"vscode": {
|
152 |
"interpreter": {
|
153 |
+
"hash": "311e94dbd43374307e33a15d3b7324b15a4f7b1d7ecfe8226f18075b87b9fae7"
|
154 |
}
|
155 |
}
|
156 |
},
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
1 |
+
fastapi==0.89.0
|
2 |
+
Pillow
|
3 |
+
scipy
|
4 |
+
torch
|
5 |
+
torchvision
|
6 |
+
transformers
|