update README
Browse files
README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
# DepthPro: Human Segmentation
|
| 6 |
+
|
| 7 |
+
- 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:
|
| 8 |
+
- Image Segmentation - Human Segmentation
|
| 9 |
+
- Image Super Resolution - 384px to 1536px (4x Upscaling)
|
| 10 |
+
- Image Super Resolution - 256px to 1024px (4x Upscaling)
|
| 11 |
+
|
| 12 |
+
# Usage
|
| 13 |
+
|
| 14 |
+
Install the required libraries:
|
| 15 |
+
```bash
|
| 16 |
+
pip install -q numpy pillow torch torchvision
|
| 17 |
+
pip install -q git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
Import the required libraries:
|
| 21 |
+
```py
|
| 22 |
+
import requests
|
| 23 |
+
from PIL import Image
|
| 24 |
+
import torch
|
| 25 |
+
import torch.nn as nn
|
| 26 |
+
import torch.nn.functional as F
|
| 27 |
+
from huggingface_hub import hf_hub_download
|
| 28 |
+
import matplotlib.pyplot as plt
|
| 29 |
+
|
| 30 |
+
# custom installation from this PR: https://github.com/huggingface/transformers/pull/34583
|
| 31 |
+
# !pip install git+https://github.com/geetu040/transformers.git@depth-pro-projects#egg=transformers
|
| 32 |
+
from transformers import DepthProConfig, DepthProImageProcessorFast, DepthProForDepthEstimation
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
Load the model and image processor:
|
| 36 |
+
```py
|
| 37 |
+
# initialize model
|
| 38 |
+
config = DepthProConfig(use_fov_model=False)
|
| 39 |
+
model = DepthProForDepthEstimation(config)
|
| 40 |
+
features = config.fusion_hidden_size
|
| 41 |
+
semantic_classifier_dropout = 0.1
|
| 42 |
+
num_labels = 1
|
| 43 |
+
model.head.head = nn.Sequential(
|
| 44 |
+
nn.Conv2d(features, features, kernel_size=3, padding=1, bias=False),
|
| 45 |
+
nn.BatchNorm2d(features),
|
| 46 |
+
nn.ReLU(),
|
| 47 |
+
nn.Dropout(semantic_classifier_dropout),
|
| 48 |
+
nn.Conv2d(features, features, kernel_size=1),
|
| 49 |
+
nn.ConvTranspose2d(features, num_labels, kernel_size=2, stride=2, padding=0, bias=True),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# load weights
|
| 53 |
+
weights_path = hf_hub_download(repo_id="geetu040/DepthPro_Segmentation_Human", filename="model_weights.pth")
|
| 54 |
+
model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu'), weights_only=True))
|
| 55 |
+
|
| 56 |
+
# load to device
|
| 57 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 58 |
+
model = model.to(device)
|
| 59 |
+
|
| 60 |
+
# load image processor
|
| 61 |
+
image_processor = DepthProImageProcessorFast()
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
Inference:
|
| 65 |
+
```py
|
| 66 |
+
# inference
|
| 67 |
+
|
| 68 |
+
url = "https://huggingface.co/spaces/geetu040/DepthPro_Segmentation_Human/resolve/main/assets/examples/man_with_arms_open.jpg"
|
| 69 |
+
|
| 70 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 71 |
+
image = image.convert("RGB")
|
| 72 |
+
|
| 73 |
+
# prepare image for the model
|
| 74 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
| 75 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 76 |
+
|
| 77 |
+
# inference
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
output = model(**inputs)
|
| 80 |
+
|
| 81 |
+
# convert tensors to PIL.Image
|
| 82 |
+
output = output[0] # get output logits
|
| 83 |
+
output = F.interpolate(
|
| 84 |
+
output.unsqueeze(0),
|
| 85 |
+
size=(image.height, image.width)
|
| 86 |
+
) # interpolate to match size
|
| 87 |
+
output = output.squeeze() # get first and only batch and channel
|
| 88 |
+
output = output.sigmoid() # apply sigmoid for binary segmentation
|
| 89 |
+
output = (output > 0.5).float() # threshold to create binary mask
|
| 90 |
+
output = output.cpu() # unload from cuda if used
|
| 91 |
+
output = output * 255 # convert [0, 1] to [0, 255]
|
| 92 |
+
output = output.numpy() # convert to numpy
|
| 93 |
+
output = output.astype('uint8') # convert to PIL.Image compatible format
|
| 94 |
+
output = Image.fromarray(output) # create PIL.Image object
|
| 95 |
+
|
| 96 |
+
# visualize the prediction
|
| 97 |
+
fig, axes = plt.subplots(1, 2, figsize=(20, 20))
|
| 98 |
+
axes[0].imshow(image)
|
| 99 |
+
axes[0].set_title(f'Low-Resolution (LR) {image.size}')
|
| 100 |
+
axes[0].axis('off')
|
| 101 |
+
axes[1].imshow(output)
|
| 102 |
+
axes[1].set_title(f'Super-Resolution (SR) {output.size}')
|
| 103 |
+
axes[1].axis('off')
|
| 104 |
+
plt.subplots_adjust(wspace=0, hspace=0)
|
| 105 |
+
plt.show()
|
| 106 |
+
```
|