Spaces:
Running
Running
File size: 7,706 Bytes
93b2c27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import torch
import torch.nn as nn
import torchvision.transforms as T
import gradio as gr
from PIL import Image
from copy import deepcopy
import os, sys
sys.path.append('./DETRPose')
sys.path.append('./DETRPose/tools/inference')
from DETRPose.src.core import LazyConfig, instantiate
from DETRPose.tools.inference.annotator import Annotator
from DETRPose.tools.inference.annotator_crowdpose import AnnotatorCrowdpose
DETRPOSE_MODELS = {
# For COCO2017
"DETRPose-N": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_n.py', 'n'],
"DETRPose-S": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_s.py', 's'],
"DETRPose-M": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_m.py', 'm'],
"DETRPose-L": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_l.py', 'l'],
"DETRPose-X": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_x.py', 'x'],
# For CrowdPose
"DETRPose-N-CrowdPose": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_n_crowdpose.py', 'n_crowdpose'],
"DETRPose-S-CrowdPose": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_s_crowdpose.py', 's_crowdpose'],
"DETRPose-M-CrowdPose": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_m_crowdpose.py', 'm_crowdpose'],
"DETRPose-L-CrowdPose": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_l_crowdpose.py', 'l_crowdpose'],
"DETRPose-X-CrowdPose": ['./DETRPose/configs/detrpose/detrpose_hgnetv2_x_crowdpose.py', 'x_crowdpose'],
}
transforms = T.Compose(
[
T.Resize((640, 640)),
T.ToTensor(),
]
)
example_images = [
["assets/example1.jpg"],
["assets/example2.jpg"],
]
description = """
<h1 align="center">
<ins>DETRPose</ins>
<br>
Real-time end-to-end transformer model for multi-person pose estimation
</h1>
<h2 align="center">
<a href="https://www.linkedin.com/in/sebastianjr/">Sebastian Janampa</a>
and
<a href="https://www.linkedin.com/in/marios-pattichis-207b0119/">Marios Pattichis</a>
</h2>
<h2 align="center">
<a href="https://github.com/SebastianJanampa/DETRPose.git">GitHub</a> |
<a href="https://colab.research.google.com/github/SebastianJanampa/DETRPose/blob/main/DETRPose_tutorial.ipynb">Colab</a>
</h2>
## Getting Started
DETRPose is the first real-time end-to-end transformer model for multi-person pose estimation,
achieving outstanding results on the COCO and CrowdPose datasets. In this work, we propose a
new denoising technique suitable for pose estimation that uses the Object Keypoint Similarity (OKS) metric
to generate positive and negative queries. Additionally, we develop a new classification head
and a new classification loss that are variations of the LQE head and the varifocal loss used in D-FINE.
To get started, upload an image or select one of the examples below.
You can choose between different model size, change the confidence threshold and visualize the results.
### Acknowledgement
This work has been supported by [LambdaLab](https://lambda.ai)
"""
def create_model(model_name):
config_path = DETRPOSE_MODELS[model_name][0]
model_name = DETRPOSE_MODELS[model_name][1]
cfg = LazyConfig.load(config_path)
if hasattr(cfg.model.backbone, 'pretrained'):
cfg.model.backbone.pretrained = False
download_url = f"https://github.com/SebastianJanampa/DETRPose/releases/download/model_weights/detrpose_hgnetv2_{model_name}.pth"
state_dict = torch.hub.load_state_dict_from_url(
download_url, map_location="cpu", file_name=f"detrpose_hgnetv2_{model_name}.pth"
)
model = instantiate(cfg.model)
postprocessor = instantiate(cfg.postprocessor)
model.load_state_dict(state_dict['model'], strict=True)
class Model(nn.Module):
def __init__(self):
super().__init__()
self.model = model.deploy()
self.postprocessor = postprocessor.deploy()
def forward(self, images, orig_target_sizes):
outputs = self.model(images)
outputs = self.postprocessor(outputs, orig_target_sizes)
return outputs
model = Model()
model.eval()
global Drawer
if 'crowdpose' in model_name:
Drawer = AnnotatorCrowdpose
else:
Drawer = Annotator
return model#, Drawer
def draw(image, scores, labels, keypoints, h, w, thrh):
annotator = Drawer(deepcopy(image))
for kpt, score in zip(keypoints, scores):
if score > thrh:
annotator.kpts(
kpt,
[h, w]
)
annotated_image = annotator.result()
return annotated_image[..., ::-1]
def filter(lines, scores, threshold):
filtered_lines, filter_scores = [], []
for line, scr in zip(lines, scores):
idx = scr > threshold
filtered_lines.append(line[idx])
filter_scores.append(scr[idx])
return filtered_lines, filter_scores
def process_results(
image_path,
model_size,
threshold
):
""" Process the image an returns the detected lines """
if image_path is None:
raise gr.Error("Please upload an image first.")
model = create_model(model_size)
im_pil = Image.open(image_path).convert("RGB")
w, h = im_pil.size
orig_size = torch.tensor([[w, h]])
im_data = transforms(im_pil).unsqueeze(0)
output = model(im_data, orig_size)
scores, labels, keypoints = output
scores, labels, keypoints = scores[0], labels[0], keypoints[0]
annotated_image = draw(im_pil, scores, labels, keypoints, h, w, thrh=threshold)
return annotated_image, (scores, labels, keypoints, h, w)
def update_threshold(
image_path,
raw_results,
threshold
):
scores, labels, keypoints, h, w = raw_results
im_pil = Image.open(image_path).convert("RGB")
annotated_image = draw(im_pil, scores, labels, keypoints, h, w, thrh=threshold)
return annotated_image
def update_model(
image_path,
model_size,
threshold
):
if image_path is None:
raise gr.Error("Please upload an image first.")
return None, None, None
return process_results(image_path, model_size, threshold)
def main():
global Drawer
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(description)
with gr.Row():
with gr.Column():
gr.Markdown("""## Input Image""")
image_path = gr.Image(label="Upload image", type="filepath")
model_size = gr.Dropdown(
choices=list(DETRPOSE_MODELS.keys()), label="Choose a DETRPose model.", value="DETRPose-M"
)
threshold = gr.Slider(
label="Confidence Threshold",
minimum=0.0,
maximum=1.0,
step=0.05,
interactive=True,
value=0.50,
)
submit_btn = gr.Button("Detect Human Keypoints")
gr.Examples(examples=example_images, inputs=[image_path, model_size])
with gr.Column():
gr.Markdown("""## Results""")
image_output = gr.Image(label="Detected Human Keypoints")
# Define the action when the button is clicked
raw_results = gr.State()
plot_inputs = [
raw_results,
threshold,
]
submit_btn.click(
fn=process_results,
inputs=[image_path, model_size] + plot_inputs[1:],
outputs=[image_output, raw_results],
)
# Define the action when the plot checkboxes are clicked
threshold.change(fn=update_threshold, inputs=[image_path] + plot_inputs, outputs=[image_output])
demo.launch()
if __name__ == "__main__":
main() |