MTerryJack commited on
Commit
c538497
·
verified ·
1 Parent(s): ccba54b

Upload 4 files

Browse files
chute_config.yml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Image:
2
+ from_base: parachutes/python:3.12
3
+ run_command:
4
+ - pip install --upgrade setuptools wheel
5
+ - pip install huggingface_hub==0.19.4 ultralytics==8.2.40 'torch<2.6' opencv-python-headless
6
+ set_workdir: /app
7
+
8
+ NodeSelector:
9
+ gpu_count: 1
10
+ min_vram_gb_per_gpu: 16
11
+ exclude:
12
+ - "5090"
13
+ - b200
14
+ - h200
15
+ - h20
16
+ - mi300x
17
+
18
+ Chute:
19
+ timeout_seconds: 300
20
+ concurrency: 4
21
+ max_instances: 5
22
+ scaling_threshold: 0.5
football-pitch-detection.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:45e38c9dcf6c2497dc90f8a6714b0eb9b6c28d80f8ef90daee38e4b5bc535561
3
+ size 134
football-player-detection.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:96624f74b0529e855d2b1b47c0d9570942c4c7d787947fb63a671a7ef5f4af3a
3
+ size 134
miner.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ from ultralytics import YOLO
4
+ from numpy import ndarray
5
+ from pydantic import BaseModel
6
+
7
+
8
+ class BoundingBox(BaseModel):
9
+ x1: int
10
+ y1: int
11
+ x2: int
12
+ y2: int
13
+ cls_id: int
14
+ conf: float
15
+
16
+
17
+ class TVFrameResult(BaseModel):
18
+ frame_id: int
19
+ boxes: list[BoundingBox]
20
+ keypoints: list[tuple[int, int]]
21
+
22
+
23
+ class Miner:
24
+ """
25
+ This class is responsible for:
26
+ - Loading ML models.
27
+ - Running batched predictions on images.
28
+ - Parsing ML model outputs into structured results (TVFrameResult).
29
+
30
+ This class can be modified, but it must have the following to be compatible with the chute:
31
+ - be named `Miner`
32
+ - have a `predict_batch` function with the inputs and outputs specified
33
+ - be stored in a file called `miner.py` which lives in the root of the HFHub repo
34
+ """
35
+
36
+ def __init__(self, path_hf_repo: Path) -> None:
37
+ """
38
+ Loads all ML models from the repository.
39
+ -----(Adjust as needed)----
40
+
41
+ Args:
42
+ path_hf_repo (Path):
43
+ Path to the downloaded HuggingFace Hub repository
44
+
45
+ Returns:
46
+ None
47
+ """
48
+ self.bbox_model = YOLO(path_hf_repo / "football-player-detection.pt")
49
+ print(f"✅ BBox Model Loaded")
50
+ self.keypoints_model = YOLO(path_hf_repo / "football-pitch-detection.pt")
51
+ print(f"✅ Keypoints Model Loaded")
52
+
53
+ def __repr__(self) -> str:
54
+ """
55
+ Information about miner returned in the health endpoint
56
+ to inspect the loaded ML models (and their types)
57
+ -----(Adjust as needed)----
58
+ """
59
+ return f"BBox Model: {type(self.bbox_model).__name__}\nKeypoints Model: {type(self.keypoints_model).__name__}"
60
+
61
+ def predict_batch(
62
+ self,
63
+ batch_images: list[ndarray],
64
+ offset: int,
65
+ n_keypoints: int,
66
+ ) -> list[TVFrameResult]:
67
+ """
68
+ Miner prediction for a batch of images.
69
+ Handles the orchestration of ML models and any preprocessing and postprocessing
70
+ -----(Adjust as needed)----
71
+
72
+ Args:
73
+ batch_images (list[np.ndarray]):
74
+ A list of images (as NumPy arrays) to process in this batch.
75
+ offset (int):
76
+ The frame number corresponding to the first image in the batch.
77
+ Used to correctly index frames in the output results.
78
+ n_keypoints (int):
79
+ The number of keypoints expected for each frame in this challenge type.
80
+
81
+ Returns:
82
+ list[TVFrameResult]:
83
+ A list of predictions for each image in the batch
84
+ """
85
+
86
+ bboxes: dict[int, list[BoundingBox]] = {}
87
+ bbox_model_results = self.bbox_model.predict(batch_images)
88
+ if bbox_model_results is not None:
89
+ for frame_number_in_batch, detection in enumerate(bbox_model_results):
90
+ if not hasattr(detection, "boxes") or detection.boxes is None:
91
+ continue
92
+ boxes = []
93
+ for box in detection.boxes.data:
94
+ x1, y1, x2, y2, conf, cls_id = box.tolist()
95
+ boxes.append(
96
+ BoundingBox(
97
+ x1=int(x1),
98
+ y1=int(y1),
99
+ x2=int(x2),
100
+ y2=int(y2),
101
+ cls_id=int(cls_id),
102
+ conf=float(conf),
103
+ )
104
+ )
105
+ bboxes[offset + frame_number_in_batch] = boxes
106
+ print("✅ BBoxes predicted")
107
+
108
+ keypoints: dict[int, tuple[int, int]] = {}
109
+ keypoints_model_results = self.keypoints_model.predict(batch_images)
110
+ if keypoints_model_results is not None:
111
+ for frame_number_in_batch, detection in enumerate(keypoints_model_results):
112
+ if not hasattr(detection, "keypoints") or detection.keypoints is None:
113
+ continue
114
+ frame_keypoints: list[tuple[int, int]] = []
115
+ for part_points in detection.keypoints.data:
116
+ for x, y, _ in part_points:
117
+ frame_keypoints.append((int(x), int(y)))
118
+ if len(frame_keypoints) < n_keypoints:
119
+ frame_keypoints.extend(
120
+ [(0, 0)] * (n_keypoints - len(frame_keypoints))
121
+ )
122
+ else:
123
+ frame_keypoints = frame_keypoints[:n_keypoints]
124
+ keypoints[offset + frame_number_in_batch] = frame_keypoints
125
+ print("✅ Keypoints predicted")
126
+
127
+ results: list[TVFrameResult] = []
128
+ for frame_number in range(offset, offset + len(batch_images)):
129
+ results.append(
130
+ TVFrameResult(
131
+ frame_id=frame_number,
132
+ boxes=bboxes.get(frame_number, []),
133
+ keypoints=keypoints.get(
134
+ frame_number, [(0, 0) for _ in range(n_keypoints)]
135
+ ),
136
+ )
137
+ )
138
+ print("✅ Combined results as TVFrameResult")
139
+ return results