upload app
Browse files- .gitattributes +3 -0
- .gitignore +1 -0
- README.md +1 -0
- app.py +24 -0
- assets/examples/girl_praying.jpg +3 -0
- assets/examples/man_with_arms_open.jpg +3 -0
- assets/examples/man_with_camera_in_hand.jpg +3 -0
- model.py +65 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ 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 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.gradio
|
README.md
CHANGED
|
@@ -8,6 +8,7 @@ sdk_version: 5.9.1
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
branch: test
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from model import predict
|
| 4 |
+
|
| 5 |
+
description = """
|
| 6 |
+
- This work is a part of the [DepthPro: Beyond Depth Estimation](https://github.com/geetu040/depthpro-beyond-depth) repository, which further explores this model's capabilities on:
|
| 7 |
+
- Image Segmentation - Human Segmentation
|
| 8 |
+
- Image Super Resolution - 384px to 1536px (4x Upscaling)
|
| 9 |
+
- Image Super Resolution - 256px to 1024px (4x Upscaling)
|
| 10 |
+
"""
|
| 11 |
+
examples_dir = "assets/examples/"
|
| 12 |
+
examples = [[os.path.join(examples_dir, filename)] for filename in os.listdir(examples_dir)]
|
| 13 |
+
|
| 14 |
+
interface = gr.Interface(
|
| 15 |
+
fn=predict,
|
| 16 |
+
inputs=gr.Image(type="pil"),
|
| 17 |
+
outputs=gr.Image(type="pil"),
|
| 18 |
+
title="DepthPro: Segmentation Human",
|
| 19 |
+
description=description,
|
| 20 |
+
examples=examples,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
interface.launch()
|
assets/examples/girl_praying.jpg
ADDED
|
Git LFS Details
|
assets/examples/man_with_arms_open.jpg
ADDED
|
Git LFS Details
|
assets/examples/man_with_camera_in_hand.jpg
ADDED
|
Git LFS Details
|
model.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# custom installation from this PR: https://github.com/huggingface/transformers/pull/34583
|
| 8 |
+
# !pip install git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers
|
| 9 |
+
from transformers import DepthProConfig, DepthProImageProcessorFast, DepthProForDepthEstimation
|
| 10 |
+
|
| 11 |
+
# initialize model
|
| 12 |
+
config = DepthProConfig(use_fov_model=False)
|
| 13 |
+
model = DepthProForDepthEstimation(config)
|
| 14 |
+
features = config.fusion_hidden_size
|
| 15 |
+
semantic_classifier_dropout = 0.1
|
| 16 |
+
num_labels = 1
|
| 17 |
+
model.head.head = nn.Sequential(
|
| 18 |
+
nn.Conv2d(features, features, kernel_size=3, padding=1, bias=False),
|
| 19 |
+
nn.BatchNorm2d(features),
|
| 20 |
+
nn.ReLU(),
|
| 21 |
+
nn.Dropout(semantic_classifier_dropout),
|
| 22 |
+
nn.Conv2d(features, features, kernel_size=1),
|
| 23 |
+
nn.ConvTranspose2d(features, num_labels, kernel_size=2, stride=2, padding=0, bias=True),
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# load weights
|
| 27 |
+
weights_path = hf_hub_download(repo_id="geetu040/DepthPro_Segmentation_Human", filename="model_weights.pth")
|
| 28 |
+
model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu'), weights_only=True))
|
| 29 |
+
|
| 30 |
+
# load to device
|
| 31 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 32 |
+
model = model.to(device)
|
| 33 |
+
|
| 34 |
+
# load image processor
|
| 35 |
+
image_processor = DepthProImageProcessorFast()
|
| 36 |
+
|
| 37 |
+
def predict(image):
|
| 38 |
+
# inference
|
| 39 |
+
|
| 40 |
+
image = image.convert("RGB")
|
| 41 |
+
|
| 42 |
+
# prepare image for the model
|
| 43 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
| 44 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 45 |
+
|
| 46 |
+
# inference
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
output = model(**inputs)
|
| 49 |
+
|
| 50 |
+
# convert tensors to PIL.Image
|
| 51 |
+
output = output[0] # get output logits
|
| 52 |
+
output = F.interpolate(
|
| 53 |
+
output.unsqueeze(0),
|
| 54 |
+
size=(image.height, image.width)
|
| 55 |
+
) # interpolate to match size
|
| 56 |
+
output = output.squeeze() # get first and only batch and channel
|
| 57 |
+
output = output.sigmoid() # apply sigmoid for binary segmentation
|
| 58 |
+
output = (output > 0.5).float() # threshold to create binary mask
|
| 59 |
+
output = output.cpu() # unload from cuda if used
|
| 60 |
+
output = output * 255 # convert [0, 1] to [0, 255]
|
| 61 |
+
output = output.numpy() # convert to numpy
|
| 62 |
+
output = output.astype('uint8') # convert to PIL.Image compatible format
|
| 63 |
+
output = Image.fromarray(output) # create PIL.Image object
|
| 64 |
+
|
| 65 |
+
return output
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
pillow
|
| 4 |
+
torch
|
| 5 |
+
torchvision
|
| 6 |
+
git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers
|