Initial commit
Browse files- .gitattributes +1 -0
- app.py +178 -122
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ 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 |
+
ckpts/best_val.pt filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import tempfile
|
|
| 11 |
import threading
|
| 12 |
import time
|
| 13 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 14 |
|
| 15 |
from evals.video_classification_frozen.models import init_module
|
| 16 |
from src.models.attentive_pooler import AttentiveClassifier
|
|
@@ -23,7 +24,135 @@ ACTION_LABELS = {
|
|
| 23 |
4: "Inspect Shelf",
|
| 24 |
}
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
cap = cv2.VideoCapture(video_path)
|
| 28 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 29 |
if fps == 0 or np.isnan(fps):
|
|
@@ -34,102 +163,36 @@ def process_video(video_path, encoder, classifier, device, frames_per_clip=16, r
|
|
| 34 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 35 |
out_path = tmpfile.name
|
| 36 |
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 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 |
-
rect_w, rect_h = text_w + 40, text_h + 30
|
| 68 |
-
center_x = width // 2
|
| 69 |
-
rect_x1 = center_x - rect_w // 2
|
| 70 |
-
rect_y1 = height - rect_h - 30
|
| 71 |
-
rect_x2 = rect_x1 + rect_w
|
| 72 |
-
rect_y2 = rect_y1 + rect_h
|
| 73 |
-
radius = 15
|
| 74 |
-
cv2.rectangle(overlay, (rect_x1 + radius, rect_y1), (rect_x2 - radius, rect_y2), (0,0,0), -1)
|
| 75 |
-
cv2.rectangle(overlay, (rect_x1, rect_y1 + radius), (rect_x2, rect_y2 - radius), (0,0,0), -1)
|
| 76 |
-
cv2.circle(overlay, (rect_x1 + radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 77 |
-
cv2.circle(overlay, (rect_x2 - radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 78 |
-
cv2.circle(overlay, (rect_x1 + radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 79 |
-
cv2.circle(overlay, (rect_x2 - radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 80 |
-
cv2.addWeighted(overlay, alpha, f_disp, 1 - alpha, 0, f_disp)
|
| 81 |
-
text_x = center_x - text_w // 2
|
| 82 |
-
text_y = rect_y1 + rect_h // 2 + text_h // 2 - 5
|
| 83 |
-
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, outline_color, thickness + 2, cv2.LINE_AA)
|
| 84 |
-
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, text_color, thickness, cv2.LINE_AA)
|
| 85 |
-
out.write(f_disp)
|
| 86 |
-
frames = []
|
| 87 |
-
for f in frames:
|
| 88 |
-
f_disp = (f.permute(1,2,0).cpu().numpy() * 255).astype(np.uint8)
|
| 89 |
-
f_disp = cv2.cvtColor(f_disp, cv2.COLOR_RGB2BGR)
|
| 90 |
-
f_disp = cv2.resize(f_disp, (width, height))
|
| 91 |
-
overlay = f_disp.copy()
|
| 92 |
-
text = "-"
|
| 93 |
-
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 94 |
-
font_scale = 1.2
|
| 95 |
-
thickness = 2
|
| 96 |
-
text_color = (255, 255, 255)
|
| 97 |
-
outline_color = (0, 0, 0)
|
| 98 |
-
alpha = 0.6
|
| 99 |
-
(text_w, text_h), baseline = cv2.getTextSize(text, font, font_scale, thickness)
|
| 100 |
-
rect_w, rect_h = text_w + 40, text_h + 30
|
| 101 |
-
center_x = width // 2
|
| 102 |
-
rect_x1 = center_x - rect_w // 2
|
| 103 |
-
rect_y1 = height - rect_h - 30
|
| 104 |
-
rect_x2 = rect_x1 + rect_w
|
| 105 |
-
rect_y2 = rect_y1 + rect_h
|
| 106 |
-
radius = 15
|
| 107 |
-
cv2.rectangle(overlay, (rect_x1 + radius, rect_y1), (rect_x2 - radius, rect_y2), (0,0,0), -1)
|
| 108 |
-
cv2.rectangle(overlay, (rect_x1, rect_y1 + radius), (rect_x2, rect_y2 - radius), (0,0,0), -1)
|
| 109 |
-
cv2.circle(overlay, (rect_x1 + radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 110 |
-
cv2.circle(overlay, (rect_x2 - radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 111 |
-
cv2.circle(overlay, (rect_x1 + radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 112 |
-
cv2.circle(overlay, (rect_x2 - radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 113 |
-
cv2.addWeighted(overlay, alpha, f_disp, 1 - alpha, 0, f_disp)
|
| 114 |
-
text_x = center_x - text_w // 2
|
| 115 |
-
text_y = rect_y1 + rect_h // 2 + text_h // 2 - 5
|
| 116 |
-
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, outline_color, thickness + 2, cv2.LINE_AA)
|
| 117 |
-
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, text_color, thickness, cv2.LINE_AA)
|
| 118 |
-
out.write(f_disp)
|
| 119 |
-
cap.release()
|
| 120 |
-
out.release()
|
| 121 |
-
return out_path
|
| 122 |
-
|
| 123 |
-
def gradio_infer(video, encoder, classifier, device):
|
| 124 |
-
processed_path = process_video(video, encoder, classifier, device)
|
| 125 |
-
def cleanup(path):
|
| 126 |
-
time.sleep(60)
|
| 127 |
-
try:
|
| 128 |
-
Path(path).unlink()
|
| 129 |
-
except Exception:
|
| 130 |
-
pass
|
| 131 |
-
threading.Thread(target=cleanup, args=(processed_path,), daemon=True).start()
|
| 132 |
-
return processed_path
|
| 133 |
|
| 134 |
def load_config(config_path):
|
| 135 |
with open(config_path, 'r') as f:
|
|
@@ -138,39 +201,32 @@ def load_config(config_path):
|
|
| 138 |
def load_models():
|
| 139 |
# Paths are relative to HuggingFace directory
|
| 140 |
config_path = "configs/merl.yaml"
|
| 141 |
-
|
| 142 |
-
repo_id="facebook/vjepa2-vitl-fpc64-256",
|
| 143 |
-
filename="model.safetensors",
|
| 144 |
-
repo_type="model"
|
| 145 |
-
)
|
| 146 |
classifier_ckpt = "ckpts/best_val.pt"
|
| 147 |
-
|
|
|
|
| 148 |
config = load_config(config_path)
|
| 149 |
-
model_kwargs = config['model_kwargs']['pretrain_kwargs']
|
| 150 |
-
wrapper_kwargs = config['model_kwargs'].get('wrapper_kwargs', {})
|
| 151 |
-
module_name = config['model_kwargs']['module_name']
|
| 152 |
frames_per_clip = config['experiment']['data']['frames_per_clip']
|
| 153 |
resolution = config['experiment']['data']['resolution']
|
| 154 |
num_heads = config['experiment']['classifier']['num_heads']
|
| 155 |
depth = config['experiment']['classifier']['num_probe_blocks']
|
| 156 |
num_classes = config['experiment']['data']['num_classes']
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
device=device,
|
| 165 |
-
)
|
| 166 |
encoder.eval()
|
| 167 |
encoder.to(device)
|
|
|
|
| 168 |
classifier_ckpt_data = torch.load(classifier_ckpt, map_location='cpu')
|
| 169 |
state_dict = classifier_ckpt_data['classifier']
|
| 170 |
if any(k.startswith('module.') for k in state_dict.keys()):
|
| 171 |
state_dict = {k.replace('module.', '', 1): v for k, v in state_dict.items()}
|
| 172 |
classifier = AttentiveClassifier(
|
| 173 |
-
embed_dim=
|
| 174 |
num_heads=num_heads,
|
| 175 |
depth=depth,
|
| 176 |
num_classes=num_classes
|
|
@@ -178,19 +234,19 @@ def load_models():
|
|
| 178 |
classifier.load_state_dict(state_dict, strict=True)
|
| 179 |
classifier.eval()
|
| 180 |
classifier.to(device)
|
| 181 |
-
return encoder, classifier,
|
| 182 |
-
|
| 183 |
-
encoder, classifier, device, frames_per_clip, resolution = load_models()
|
| 184 |
|
| 185 |
-
def gradio_wrapper(
|
| 186 |
-
|
|
|
|
| 187 |
|
| 188 |
demo = gr.Interface(
|
| 189 |
fn=gradio_wrapper,
|
| 190 |
inputs=gr.Video(label="Upload Video"),
|
| 191 |
outputs=gr.Video(label="Processed Video with Action Labels"),
|
| 192 |
title="V-JEPA2 Video Action Recognition Demo",
|
| 193 |
-
description="Upload a video or use your webcam. The model will recognize and localize actions in real-time.
|
|
|
|
| 194 |
allow_flagging="never",
|
| 195 |
live=False,
|
| 196 |
)
|
|
|
|
| 11 |
import threading
|
| 12 |
import time
|
| 13 |
from huggingface_hub import hf_hub_download
|
| 14 |
+
from transformers import AutoModel, AutoVideoProcessor
|
| 15 |
|
| 16 |
from evals.video_classification_frozen.models import init_module
|
| 17 |
from src.models.attentive_pooler import AttentiveClassifier
|
|
|
|
| 24 |
4: "Inspect Shelf",
|
| 25 |
}
|
| 26 |
|
| 27 |
+
VIT_EMBED_DIMS = {
|
| 28 |
+
"vit_synthetic": 1,
|
| 29 |
+
"vit_tiny": 192,
|
| 30 |
+
"vit_small": 384,
|
| 31 |
+
"vit_base": 768,
|
| 32 |
+
"vit_large": 1024,
|
| 33 |
+
"vit_huge": 1280,
|
| 34 |
+
"vit_giant": 1408,
|
| 35 |
+
"vit_gigantic": 1664,
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Support MPS (Apple Silicon) in addition to CUDA/CPU
|
| 39 |
+
if torch.cuda.is_available():
|
| 40 |
+
device = torch.device('cuda')
|
| 41 |
+
elif getattr(torch.backends, "mps", None) is not None and torch.backends.mps.is_available():
|
| 42 |
+
device = torch.device('mps')
|
| 43 |
+
else:
|
| 44 |
+
device = torch.device('cpu')
|
| 45 |
+
print(f'Using device {device}')
|
| 46 |
+
|
| 47 |
+
def gradio_infer(video_path, encoder, classifier, hf_transform, frames_per_clip, resolution):
|
| 48 |
+
with torch.inference_mode():
|
| 49 |
+
cap = cv2.VideoCapture(video_path)
|
| 50 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 51 |
+
if fps == 0 or np.isnan(fps):
|
| 52 |
+
fps = 25
|
| 53 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 54 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 55 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 56 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 57 |
+
out_path = tmpfile.name
|
| 58 |
+
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 59 |
+
frames = []
|
| 60 |
+
while True:
|
| 61 |
+
ret, frame = cap.read()
|
| 62 |
+
if not ret:
|
| 63 |
+
break
|
| 64 |
+
frame_resized = cv2.resize(frame, (resolution, resolution))
|
| 65 |
+
frame_rgb = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
|
| 66 |
+
frame_tensor = torch.from_numpy(frame_rgb).permute(2, 0, 1).float() # C H W
|
| 67 |
+
frames.append(frame_tensor)
|
| 68 |
+
if len(frames) == frames_per_clip:
|
| 69 |
+
clip = torch.stack(frames) # T C H W
|
| 70 |
+
clip = hf_transform(clip, return_tensors="pt")["pixel_values_videos"].to(device)
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
features = encoder.get_vision_features(clip)
|
| 73 |
+
logits = classifier(features)
|
| 74 |
+
pred = logits.argmax(dim=1).item()
|
| 75 |
+
label = ACTION_LABELS.get(pred, str(pred))
|
| 76 |
+
for f in frames:
|
| 77 |
+
f_disp = (f.permute(1,2,0).cpu().numpy()).astype(np.uint8)
|
| 78 |
+
f_disp = cv2.cvtColor(f_disp, cv2.COLOR_RGB2BGR)
|
| 79 |
+
f_disp = cv2.resize(f_disp, (width, height))
|
| 80 |
+
overlay = f_disp.copy()
|
| 81 |
+
text = label
|
| 82 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 83 |
+
font_scale = 1.2
|
| 84 |
+
thickness = 2
|
| 85 |
+
text_color = (255, 255, 255) # white
|
| 86 |
+
outline_color = (0, 0, 0) # black
|
| 87 |
+
alpha = 0.6 # transparency for rectangle
|
| 88 |
+
(text_w, text_h), baseline = cv2.getTextSize(text, font, font_scale, thickness)
|
| 89 |
+
rect_w, rect_h = text_w + 40, text_h + 30
|
| 90 |
+
center_x = width // 2
|
| 91 |
+
rect_x1 = center_x - rect_w // 2
|
| 92 |
+
rect_y1 = height - rect_h - 30
|
| 93 |
+
rect_x2 = rect_x1 + rect_w
|
| 94 |
+
rect_y2 = rect_y1 + rect_h
|
| 95 |
+
radius = 15
|
| 96 |
+
cv2.rectangle(overlay, (rect_x1 + radius, rect_y1), (rect_x2 - radius, rect_y2), (0,0,0), -1)
|
| 97 |
+
cv2.rectangle(overlay, (rect_x1, rect_y1 + radius), (rect_x2, rect_y2 - radius), (0,0,0), -1)
|
| 98 |
+
cv2.circle(overlay, (rect_x1 + radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 99 |
+
cv2.circle(overlay, (rect_x2 - radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 100 |
+
cv2.circle(overlay, (rect_x1 + radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 101 |
+
cv2.circle(overlay, (rect_x2 - radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 102 |
+
cv2.addWeighted(overlay, alpha, f_disp, 1 - alpha, 0, f_disp)
|
| 103 |
+
text_x = center_x - text_w // 2
|
| 104 |
+
text_y = rect_y1 + rect_h // 2 + text_h // 2 - 5
|
| 105 |
+
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, outline_color, thickness + 2, cv2.LINE_AA)
|
| 106 |
+
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, text_color, thickness, cv2.LINE_AA)
|
| 107 |
+
out.write(f_disp)
|
| 108 |
+
frames = []
|
| 109 |
+
for f in frames:
|
| 110 |
+
f_disp = (f.permute(1,2,0).cpu().numpy() * 255).astype(np.uint8)
|
| 111 |
+
f_disp = cv2.cvtColor(f_disp, cv2.COLOR_RGB2BGR)
|
| 112 |
+
f_disp = cv2.resize(f_disp, (width, height))
|
| 113 |
+
overlay = f_disp.copy()
|
| 114 |
+
text = "-"
|
| 115 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 116 |
+
font_scale = 1.2
|
| 117 |
+
thickness = 2
|
| 118 |
+
text_color = (255, 255, 255)
|
| 119 |
+
outline_color = (0, 0, 0)
|
| 120 |
+
alpha = 0.6
|
| 121 |
+
(text_w, text_h), baseline = cv2.getTextSize(text, font, font_scale, thickness)
|
| 122 |
+
rect_w, rect_h = text_w + 40, text_h + 30
|
| 123 |
+
center_x = width // 2
|
| 124 |
+
rect_x1 = center_x - rect_w // 2
|
| 125 |
+
rect_y1 = height - rect_h - 30
|
| 126 |
+
rect_x2 = rect_x1 + rect_w
|
| 127 |
+
rect_y2 = rect_y1 + rect_h
|
| 128 |
+
radius = 15
|
| 129 |
+
cv2.rectangle(overlay, (rect_x1 + radius, rect_y1), (rect_x2 - radius, rect_y2), (0,0,0), -1)
|
| 130 |
+
cv2.rectangle(overlay, (rect_x1, rect_y1 + radius), (rect_x2, rect_y2 - radius), (0,0,0), -1)
|
| 131 |
+
cv2.circle(overlay, (rect_x1 + radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 132 |
+
cv2.circle(overlay, (rect_x2 - radius, rect_y1 + radius), radius, (0,0,0), -1)
|
| 133 |
+
cv2.circle(overlay, (rect_x1 + radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 134 |
+
cv2.circle(overlay, (rect_x2 - radius, rect_y2 - radius), radius, (0,0,0), -1)
|
| 135 |
+
cv2.addWeighted(overlay, alpha, f_disp, 1 - alpha, 0, f_disp)
|
| 136 |
+
text_x = center_x - text_w // 2
|
| 137 |
+
text_y = rect_y1 + rect_h // 2 + text_h // 2 - 5
|
| 138 |
+
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, outline_color, thickness + 2, cv2.LINE_AA)
|
| 139 |
+
cv2.putText(f_disp, text, (text_x, text_y), font, font_scale, text_color, thickness, cv2.LINE_AA)
|
| 140 |
+
out.write(f_disp)
|
| 141 |
+
cap.release()
|
| 142 |
+
out.release()
|
| 143 |
+
|
| 144 |
+
def cleanup(path):
|
| 145 |
+
time.sleep(60)
|
| 146 |
+
try:
|
| 147 |
+
Path(path).unlink()
|
| 148 |
+
except Exception:
|
| 149 |
+
pass
|
| 150 |
+
threading.Thread(target=cleanup, args=(out_path,), daemon=True).start()
|
| 151 |
+
return out_path
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def process_video(video_path):
|
| 156 |
cap = cv2.VideoCapture(video_path)
|
| 157 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 158 |
if fps == 0 or np.isnan(fps):
|
|
|
|
| 163 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 164 |
out_path = tmpfile.name
|
| 165 |
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 166 |
+
return
|
| 167 |
+
|
| 168 |
+
# def forward_vjepa_video(encoder, classifier, hf_transform, video_path):
|
| 169 |
+
# with torch.inference_mode():
|
| 170 |
+
# cap = cv2.VideoCapture(video_path)
|
| 171 |
+
# fps = cap.get(cv2.CAP_PROP_FPS)
|
| 172 |
+
# if fps == 0 or np.isnan(fps):
|
| 173 |
+
# fps = 25
|
| 174 |
+
# width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 175 |
+
# height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 176 |
+
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 177 |
+
# with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 178 |
+
# out_path = tmpfile.name
|
| 179 |
+
# out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 180 |
+
# frames = []
|
| 181 |
+
# while True:
|
| 182 |
+
# ret, frame = cap.read()
|
| 183 |
+
# if not ret:
|
| 184 |
+
# break
|
| 185 |
+
# frame = torch.from_numpy(frame).
|
| 186 |
+
|
| 187 |
+
# def forward_vjepa_video(model, hf_transform):
|
| 188 |
+
# # Run a sample inference with VJEPA
|
| 189 |
+
# with torch.inference_mode():
|
| 190 |
+
# # Read and pre-process the image
|
| 191 |
+
# video = get_video() # T x H x W x C
|
| 192 |
+
# video = torch.from_numpy(video).permute(0, 3, 1, 2) # T x C x H x W
|
| 193 |
+
# x_hf = hf_transform(video, return_tensors="pt")["pixel_values_videos"].to(device)
|
| 194 |
+
# out_patch_features_hf = model.get_vision_features(x_hf)
|
| 195 |
+
# return out_patch_features_hf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
def load_config(config_path):
|
| 198 |
with open(config_path, 'r') as f:
|
|
|
|
| 201 |
def load_models():
|
| 202 |
# Paths are relative to HuggingFace directory
|
| 203 |
config_path = "configs/merl.yaml"
|
| 204 |
+
hf_model_name = "facebook/vjepa2-vitl-fpc64-256"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
classifier_ckpt = "ckpts/best_val.pt"
|
| 206 |
+
|
| 207 |
+
|
| 208 |
config = load_config(config_path)
|
|
|
|
|
|
|
|
|
|
| 209 |
frames_per_clip = config['experiment']['data']['frames_per_clip']
|
| 210 |
resolution = config['experiment']['data']['resolution']
|
| 211 |
num_heads = config['experiment']['classifier']['num_heads']
|
| 212 |
depth = config['experiment']['classifier']['num_probe_blocks']
|
| 213 |
num_classes = config['experiment']['data']['num_classes']
|
| 214 |
+
|
| 215 |
+
# Build HuggingFace preprocessing transform
|
| 216 |
+
hf_transform = AutoVideoProcessor.from_pretrained(hf_model_name)
|
| 217 |
+
img_size = hf_transform.crop_size["height"]
|
| 218 |
+
|
| 219 |
+
# Initialize the HuggingFace model, load pretrained weights
|
| 220 |
+
encoder = AutoModel.from_pretrained(hf_model_name)
|
|
|
|
|
|
|
| 221 |
encoder.eval()
|
| 222 |
encoder.to(device)
|
| 223 |
+
|
| 224 |
classifier_ckpt_data = torch.load(classifier_ckpt, map_location='cpu')
|
| 225 |
state_dict = classifier_ckpt_data['classifier']
|
| 226 |
if any(k.startswith('module.') for k in state_dict.keys()):
|
| 227 |
state_dict = {k.replace('module.', '', 1): v for k, v in state_dict.items()}
|
| 228 |
classifier = AttentiveClassifier(
|
| 229 |
+
embed_dim=VIT_EMBED_DIMS['vit_large'],
|
| 230 |
num_heads=num_heads,
|
| 231 |
depth=depth,
|
| 232 |
num_classes=num_classes
|
|
|
|
| 234 |
classifier.load_state_dict(state_dict, strict=True)
|
| 235 |
classifier.eval()
|
| 236 |
classifier.to(device)
|
| 237 |
+
return encoder, classifier, hf_transform, frames_per_clip, resolution
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
def gradio_wrapper(video_path):
|
| 240 |
+
encoder, classifier, hf_transform, frames_per_clip, resolution = load_models()
|
| 241 |
+
return gradio_infer(video_path, encoder, classifier, hf_transform, frames_per_clip, resolution)
|
| 242 |
|
| 243 |
demo = gr.Interface(
|
| 244 |
fn=gradio_wrapper,
|
| 245 |
inputs=gr.Video(label="Upload Video"),
|
| 246 |
outputs=gr.Video(label="Processed Video with Action Labels"),
|
| 247 |
title="V-JEPA2 Video Action Recognition Demo",
|
| 248 |
+
description="Upload a video or use your webcam. The model will recognize and localize actions in real-time. \
|
| 249 |
+
Recognizable actions: Reach To Shelf, Retract From Shelf, Hand In Shelf, Inspect Product, Inspect Shelf",
|
| 250 |
allow_flagging="never",
|
| 251 |
live=False,
|
| 252 |
)
|