Spaces:
Runtime error
Runtime error
Commit ·
e0f6444
0
Parent(s):
Duplicate from thiagohersan/maskformer-satellite-trees-gradio
Browse filesCo-authored-by: Thiago Hersan <thiagohersan@users.noreply.huggingface.co>
- .gitattributes +34 -0
- .gitignore +2 -0
- README.md +14 -0
- app.ipynb +118 -0
- app.py +100 -0
- examples/map-000.jpg +0 -0
- examples/map-010.jpg +0 -0
- examples/map-018.jpg +0 -0
- examples/map-114.jpg +0 -0
- requirements.txt +5 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt 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 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.DS_Store
|
| 2 |
+
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Maskformer Satellite+Trees
|
| 3 |
+
emoji: 🛰
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.16.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
models:
|
| 10 |
+
- thiagohersan/maskformer-satellite-trees
|
| 11 |
+
pinned: false
|
| 12 |
+
license: cc-by-nc-sa-4.0
|
| 13 |
+
duplicated_from: thiagohersan/maskformer-satellite-trees-gradio
|
| 14 |
+
---
|
app.ipynb
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import gradio as gr\n",
|
| 10 |
+
"import numpy as np\n",
|
| 11 |
+
"from os import environ\n",
|
| 12 |
+
"from PIL import Image as PImage\n",
|
| 13 |
+
"from torchvision import transforms as T\n",
|
| 14 |
+
"from transformers import MaskFormerForInstanceSegmentation, MaskFormerImageProcessor"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"ade_mean=[0.485, 0.456, 0.406]\n",
|
| 24 |
+
"ade_std=[0.229, 0.224, 0.225]\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"model_id = f\"thiagohersan/maskformer-satellite-trees\""
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"execution_count": null,
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"outputs": [],
|
| 34 |
+
"source": [
|
| 35 |
+
"# preprocessor = MaskFormerImageProcessor.from_pretrained(model_id)\n",
|
| 36 |
+
"preprocessor = MaskFormerImageProcessor(\n",
|
| 37 |
+
" do_resize=False,\n",
|
| 38 |
+
" do_normalize=False,\n",
|
| 39 |
+
" do_rescale=False,\n",
|
| 40 |
+
" ignore_index=255,\n",
|
| 41 |
+
" reduce_labels=False\n",
|
| 42 |
+
")\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"hf_token = environ.get('HFTOKEN') or True\n",
|
| 45 |
+
"model = MaskFormerForInstanceSegmentation.from_pretrained(model_id, use_auth_token=hf_token)\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"test_transform = T.Compose([\n",
|
| 48 |
+
" T.ToTensor(),\n",
|
| 49 |
+
" T.Normalize(mean=ade_mean, std=ade_std)\n",
|
| 50 |
+
"])\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"with PImage.open(\"../color-filter-calculator/assets/Artshack_screen.jpg\") as img:\n",
|
| 53 |
+
" img_size = (img.height, img.width)\n",
|
| 54 |
+
" norm_image = test_transform(np.array(img))\n",
|
| 55 |
+
" inputs = preprocessor(images=norm_image, return_tensors=\"pt\")\n",
|
| 56 |
+
" "
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"cell_type": "code",
|
| 61 |
+
"execution_count": null,
|
| 62 |
+
"metadata": {},
|
| 63 |
+
"outputs": [],
|
| 64 |
+
"source": [
|
| 65 |
+
"outputs = model(**inputs)"
|
| 66 |
+
]
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"cell_type": "code",
|
| 70 |
+
"execution_count": null,
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"outputs": [],
|
| 73 |
+
"source": [
|
| 74 |
+
"results = preprocessor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]\n",
|
| 75 |
+
"results = results.numpy()\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"labels = np.unique(results)"
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"cell_type": "code",
|
| 82 |
+
"execution_count": null,
|
| 83 |
+
"metadata": {},
|
| 84 |
+
"outputs": [],
|
| 85 |
+
"source": [
|
| 86 |
+
"for label_id in labels:\n",
|
| 87 |
+
" print(model.config.id2label[label_id])"
|
| 88 |
+
]
|
| 89 |
+
}
|
| 90 |
+
],
|
| 91 |
+
"metadata": {
|
| 92 |
+
"kernelspec": {
|
| 93 |
+
"display_name": "Python 3.8.15 ('hf-gradio')",
|
| 94 |
+
"language": "python",
|
| 95 |
+
"name": "python3"
|
| 96 |
+
},
|
| 97 |
+
"language_info": {
|
| 98 |
+
"codemirror_mode": {
|
| 99 |
+
"name": "ipython",
|
| 100 |
+
"version": 3
|
| 101 |
+
},
|
| 102 |
+
"file_extension": ".py",
|
| 103 |
+
"mimetype": "text/x-python",
|
| 104 |
+
"name": "python",
|
| 105 |
+
"nbconvert_exporter": "python",
|
| 106 |
+
"pygments_lexer": "ipython3",
|
| 107 |
+
"version": "3.8.15"
|
| 108 |
+
},
|
| 109 |
+
"orig_nbformat": 4,
|
| 110 |
+
"vscode": {
|
| 111 |
+
"interpreter": {
|
| 112 |
+
"hash": "4888b226c77b860705e4be316b14a092026f41c3585ee0ddb38f3008c0cb495e"
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
},
|
| 116 |
+
"nbformat": 4,
|
| 117 |
+
"nbformat_minor": 2
|
| 118 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from os import environ
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import transforms as T
|
| 7 |
+
from transformers import MaskFormerForInstanceSegmentation, MaskFormerImageProcessor
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
example_images = sorted(glob.glob('examples/map*.jpg'))
|
| 11 |
+
|
| 12 |
+
ade_mean=[0.485, 0.456, 0.406]
|
| 13 |
+
ade_std=[0.229, 0.224, 0.225]
|
| 14 |
+
|
| 15 |
+
test_transform = T.Compose([
|
| 16 |
+
T.ToTensor(),
|
| 17 |
+
T.Normalize(mean=ade_mean, std=ade_std)
|
| 18 |
+
])
|
| 19 |
+
|
| 20 |
+
palette = [
|
| 21 |
+
[120, 120, 120], [4, 200, 4], [4, 4, 250], [6, 230, 230],
|
| 22 |
+
[80, 50, 50], [120, 120, 80], [140, 140, 140], [204, 5, 255]
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
model_id = f"thiagohersan/maskformer-satellite-trees"
|
| 26 |
+
vegetation_labels = ["vegetation"]
|
| 27 |
+
|
| 28 |
+
# preprocessor = MaskFormerImageProcessor.from_pretrained(model_id)
|
| 29 |
+
preprocessor = MaskFormerImageProcessor(
|
| 30 |
+
do_resize=False,
|
| 31 |
+
do_normalize=False,
|
| 32 |
+
do_rescale=False,
|
| 33 |
+
ignore_index=255,
|
| 34 |
+
reduce_labels=False
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
hf_token = environ.get('HFTOKEN')
|
| 38 |
+
model = MaskFormerForInstanceSegmentation.from_pretrained(model_id, use_auth_token=hf_token)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def visualize_instance_seg_mask(img_in, mask, id2label, included_labels):
|
| 42 |
+
img_out = np.zeros((mask.shape[0], mask.shape[1], 3))
|
| 43 |
+
image_total_pixels = mask.shape[0] * mask.shape[1]
|
| 44 |
+
label_ids = np.unique(mask)
|
| 45 |
+
|
| 46 |
+
id2color = {id: palette[id] for id in label_ids}
|
| 47 |
+
id2count = {id: 0 for id in label_ids}
|
| 48 |
+
|
| 49 |
+
for i in range(img_out.shape[0]):
|
| 50 |
+
for j in range(img_out.shape[1]):
|
| 51 |
+
img_out[i, j, :] = id2color[mask[i, j]]
|
| 52 |
+
id2count[mask[i, j]] = id2count[mask[i, j]] + 1
|
| 53 |
+
|
| 54 |
+
image_res = (0.5 * img_in + 0.5 * img_out).astype(np.uint8)
|
| 55 |
+
|
| 56 |
+
dataframe = [[
|
| 57 |
+
f"{id2label[id]}",
|
| 58 |
+
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
|
| 59 |
+
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
|
| 60 |
+
] for id in label_ids if id2label[id] in included_labels]
|
| 61 |
+
|
| 62 |
+
if len(dataframe) < 1:
|
| 63 |
+
dataframe = [[
|
| 64 |
+
f"",
|
| 65 |
+
f"{(0):.2f} %",
|
| 66 |
+
f"{(0):.2f} m"
|
| 67 |
+
]]
|
| 68 |
+
|
| 69 |
+
return image_res, dataframe
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def query_image(image_path):
|
| 73 |
+
img = np.array(Image.open(image_path))
|
| 74 |
+
img_size = (img.shape[0], img.shape[1])
|
| 75 |
+
inputs = preprocessor(images=test_transform(img), return_tensors="pt")
|
| 76 |
+
outputs = model(**inputs)
|
| 77 |
+
results = preprocessor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]
|
| 78 |
+
mask_img, dataframe = visualize_instance_seg_mask(img, results.numpy(), model.config.id2label, vegetation_labels)
|
| 79 |
+
return mask_img, dataframe
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
demo = gr.Interface(
|
| 83 |
+
title="Maskformer Satellite+Trees",
|
| 84 |
+
description="Using a finetuned version of the [facebook/maskformer-swin-base-ade](https://huggingface.co/facebook/maskformer-swin-base-ade) model (created specifically to work with satellite images) to calculate percentage of pixels in an image that belong to vegetation.",
|
| 85 |
+
|
| 86 |
+
fn=query_image,
|
| 87 |
+
inputs=[gr.Image(type="filepath", label="Input Image")],
|
| 88 |
+
outputs=[
|
| 89 |
+
gr.Image(label="Vegetation"),
|
| 90 |
+
gr.DataFrame(label="Info", headers=["Object Label", "Pixel Percent", "Square Length"])
|
| 91 |
+
],
|
| 92 |
+
|
| 93 |
+
examples=example_images,
|
| 94 |
+
cache_examples=True,
|
| 95 |
+
|
| 96 |
+
allow_flagging="never",
|
| 97 |
+
analytics_enabled=None
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
demo.launch(show_api=False)
|
examples/map-000.jpg
ADDED
|
examples/map-010.jpg
ADDED
|
examples/map-018.jpg
ADDED
|
examples/map-114.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow
|
| 2 |
+
scipy
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
transformers
|