Spaces:
Running on Zero
Running on Zero
Delete files ec_models.py ecdetseg/__init__.py ecpose/__init__.py ecpose/engine/edgecrafter/ecpose.py examples/bakery_donuts.jpg examples/tennis_group.jpg with huggingface_hub
Browse files- ec_models.py +0 -112
- ecdetseg/__init__.py +0 -0
- ecpose/__init__.py +0 -0
- ecpose/engine/edgecrafter/ecpose.py +0 -52
- examples/bakery_donuts.jpg +0 -3
- examples/tennis_group.jpg +0 -3
ec_models.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Hugging Face Hub wrappers around the official EdgeCrafter modules.
|
| 3 |
-
|
| 4 |
-
The class definitions below mirror the reference implementation shipped by the
|
| 5 |
-
authors (see `hf_models.ipynb` in https://github.com/Intellindust-AI-Lab/EdgeCrafter)
|
| 6 |
-
and the composition used in `engine/edgecrafter/modeling.py` / `engine/edgecrafter/ecpose.py`.
|
| 7 |
-
The `deploy()` re-parameterisation follows `tools/inference/torch_inf.py`.
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import torch
|
| 11 |
-
import torch.nn as nn
|
| 12 |
-
from huggingface_hub import PyTorchModelHubMixin
|
| 13 |
-
|
| 14 |
-
from ecdetseg.engine.edgecrafter.decoder import ECTransformer
|
| 15 |
-
from ecdetseg.engine.edgecrafter.ecvit import ViTAdapter as DetViTAdapter
|
| 16 |
-
from ecdetseg.engine.edgecrafter.hybrid_encoder import HybridEncoder as DetHybridEncoder
|
| 17 |
-
from ecdetseg.engine.edgecrafter.postprocessor import PostProcessor
|
| 18 |
-
from ecpose.engine.edgecrafter.detrpose_postprocesses import DETRPosePostProcessor
|
| 19 |
-
from ecpose.engine.edgecrafter.detrpose_transformer import DETRTransformer
|
| 20 |
-
from ecpose.engine.edgecrafter.ecvit import ViTAdapter as PoseViTAdapter
|
| 21 |
-
from ecpose.engine.edgecrafter.hybrid_encoder import HybridEncoder as PoseHybridEncoder
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def _deploy(module: nn.Module) -> nn.Module:
|
| 25 |
-
"""`_ECBase.deploy()` from the reference repo: eval + re-parameterisation."""
|
| 26 |
-
module.eval()
|
| 27 |
-
for m in module.modules():
|
| 28 |
-
if hasattr(m, "convert_to_deploy"):
|
| 29 |
-
m.convert_to_deploy()
|
| 30 |
-
return module
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class _ECHubBase(nn.Module, PyTorchModelHubMixin):
|
| 34 |
-
def deploy(self):
|
| 35 |
-
_deploy(self.backbone)
|
| 36 |
-
_deploy(self.encoder)
|
| 37 |
-
_deploy(self.decoder)
|
| 38 |
-
self.postprocessor.deploy()
|
| 39 |
-
self.eval()
|
| 40 |
-
return self
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
class ECDet(_ECHubBase):
|
| 44 |
-
"""Object detection. Returns (labels, boxes, scores)."""
|
| 45 |
-
|
| 46 |
-
def __init__(self, config):
|
| 47 |
-
super().__init__()
|
| 48 |
-
self.config = config
|
| 49 |
-
cfg = dict(config["backbone"])
|
| 50 |
-
cfg["skip_load_backbone"] = True # weights come from the checkpoint below
|
| 51 |
-
self.backbone = DetViTAdapter(**cfg)
|
| 52 |
-
self.encoder = DetHybridEncoder(**config["encoder"])
|
| 53 |
-
self.decoder = ECTransformer(**config["decoder"])
|
| 54 |
-
self.postprocessor = PostProcessor(**config["postprocessor"])
|
| 55 |
-
|
| 56 |
-
def forward(self, x, orig_target_sizes):
|
| 57 |
-
x = self.backbone(x)
|
| 58 |
-
x = self.encoder(x)
|
| 59 |
-
x = self.decoder(x)
|
| 60 |
-
return self.postprocessor(x, orig_target_sizes)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
class ECSeg(_ECHubBase):
|
| 64 |
-
"""Instance segmentation. Returns (labels, boxes, scores, masks)."""
|
| 65 |
-
|
| 66 |
-
def __init__(self, config):
|
| 67 |
-
super().__init__()
|
| 68 |
-
self.config = config
|
| 69 |
-
cfg = dict(config["backbone"])
|
| 70 |
-
cfg["skip_load_backbone"] = True
|
| 71 |
-
self.backbone = DetViTAdapter(**cfg)
|
| 72 |
-
self.encoder = DetHybridEncoder(**config["encoder"])
|
| 73 |
-
self.decoder = ECTransformer(**config["decoder"])
|
| 74 |
-
self.postprocessor = PostProcessor(**config["postprocessor"])
|
| 75 |
-
|
| 76 |
-
def forward(self, x, orig_target_sizes):
|
| 77 |
-
x = self.backbone(x)
|
| 78 |
-
x = self.encoder(x)
|
| 79 |
-
# `modeling.ECSeg` feeds the highest-resolution encoder feature to the
|
| 80 |
-
# mask branch as `spatial_feat`.
|
| 81 |
-
x = self.decoder(x, None, x[0])
|
| 82 |
-
return self.postprocessor(x, orig_target_sizes)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class ECPose(_ECHubBase):
|
| 86 |
-
"""Multi-person 2D pose. Returns (scores, labels, keypoints)."""
|
| 87 |
-
|
| 88 |
-
def __init__(self, config):
|
| 89 |
-
super().__init__()
|
| 90 |
-
self.config = config
|
| 91 |
-
cfg = dict(config["backbone"])
|
| 92 |
-
cfg["skip_load_backbone"] = True
|
| 93 |
-
self.backbone = PoseViTAdapter(**cfg)
|
| 94 |
-
self.encoder = PoseHybridEncoder(**config["encoder"])
|
| 95 |
-
self.decoder = DETRTransformer(**config["decoder"])
|
| 96 |
-
self.postprocessor = DETRPosePostProcessor(**config["postprocessor"])
|
| 97 |
-
|
| 98 |
-
def forward(self, x, orig_target_sizes):
|
| 99 |
-
x = self.backbone(x)
|
| 100 |
-
x = self.encoder(x)
|
| 101 |
-
x = self.decoder(x, None)
|
| 102 |
-
return self.postprocessor(x, orig_target_sizes)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
TASK_CLASSES = {"detection": ECDet, "segmentation": ECSeg, "pose": ECPose}
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
@torch.no_grad()
|
| 109 |
-
def load_model(task: str, repo_id: str, device: str = "cpu"):
|
| 110 |
-
model = TASK_CLASSES[task].from_pretrained(repo_id)
|
| 111 |
-
model.deploy()
|
| 112 |
-
return model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ecdetseg/__init__.py
DELETED
|
File without changes
|
ecpose/__init__.py
DELETED
|
File without changes
|
ecpose/engine/edgecrafter/ecpose.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
# EdgeCrafter: Compact ViTs for Edge Dense Prediction via Task-Specialized Distillation
|
| 3 |
-
# Copyright (c) 2026 The EdgeCrafter Authors. All Rights Reserved.
|
| 4 |
-
# ------------------------------------------------------------------------
|
| 5 |
-
# Modified from DETRPose: Real-time end-to-end transformer model for multi-person pose estimation
|
| 6 |
-
# (https://github.com/SebastianJanampa/DETRPose)
|
| 7 |
-
# ------------------------------------------------------------------------
|
| 8 |
-
# Modified from Conditional DETR model and criterion classes.
|
| 9 |
-
# Copyright (c) 2021 Microsoft. All Rights Reserved.
|
| 10 |
-
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
|
| 11 |
-
# ------------------------------------------------------------------------
|
| 12 |
-
# Modified from DETR (https://github.com/facebookresearch/detr)
|
| 13 |
-
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
| 14 |
-
# ------------------------------------------------------------------------
|
| 15 |
-
# Modified from Deformable DETR (https://github.com/fundamentalvision/Deformable-DETR)
|
| 16 |
-
# Copyright (c) 2020 SenseTime. All Rights Reserved.
|
| 17 |
-
# ------------------------------------------------------------------------
|
| 18 |
-
|
| 19 |
-
from torch import nn
|
| 20 |
-
|
| 21 |
-
from ..core import register
|
| 22 |
-
|
| 23 |
-
__all__ = ['ECPose', ]
|
| 24 |
-
|
| 25 |
-
@register()
|
| 26 |
-
class ECPose(nn.Module):
|
| 27 |
-
__inject__ = ['backbone', 'encoder', 'decoder',]
|
| 28 |
-
|
| 29 |
-
def __init__(
|
| 30 |
-
self,
|
| 31 |
-
backbone,
|
| 32 |
-
encoder,
|
| 33 |
-
decoder
|
| 34 |
-
):
|
| 35 |
-
super().__init__()
|
| 36 |
-
self.backbone = backbone
|
| 37 |
-
self.encoder = encoder
|
| 38 |
-
self.decoder = decoder
|
| 39 |
-
|
| 40 |
-
def deploy(self):
|
| 41 |
-
self.eval()
|
| 42 |
-
for m in self.modules():
|
| 43 |
-
if hasattr(m, "convert_to_deploy"):
|
| 44 |
-
m.convert_to_deploy()
|
| 45 |
-
return self
|
| 46 |
-
|
| 47 |
-
def forward(self, samples, targets=None):
|
| 48 |
-
feats = self.backbone(samples)
|
| 49 |
-
feats = self.encoder(feats)
|
| 50 |
-
out = self.decoder(feats, targets, samples if self.training else None)
|
| 51 |
-
return out
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/bakery_donuts.jpg
DELETED
Git LFS Details
|
examples/tennis_group.jpg
DELETED
Git LFS Details
|