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
+
- pytorch
|
| 13 |
+
- crime-detection
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Smart Surveillance System
|
| 18 |
+
|
| 19 |
+
we leveraged a pre-trained I3D model and fine-tuned it using two strategies:
|
| 20 |
+
|
| 21 |
+
Block-level tuning Adjusting and retraining groups of layers (blocks) to adapt the model to the new dataset.
|
| 22 |
+
|
| 23 |
+
Layer-level tuning Fine-tuning specific layers for more granular control over feature learning.
|
| 24 |
+
|
| 25 |
+
The final classification layer of the I3D model was removed and replaced with a custom output layer tailored to our binary classification task: predicting whether an activity represents a crime (1) or non-crime (0).
|
| 26 |
+
|
| 27 |
+
## How Run
|
| 28 |
+
|
| 29 |
+
```python
|
| 30 |
+
import torch
|
| 31 |
+
import torch.nn as nn
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class UCFModel(nn.Module):
|
| 35 |
+
def __init__(self, model_name="i3d_r50"):
|
| 36 |
+
super().__init__()
|
| 37 |
+
self.model_name = model_name
|
| 38 |
+
|
| 39 |
+
self.model = torch.hub.load("facebookresearch/pytorchvideo", model_name, pretrained=True)
|
| 40 |
+
|
| 41 |
+
in_features = self.model.blocks[-1].proj.in_features
|
| 42 |
+
self.model.blocks[-1].proj = nn.Linear(in_features, 2)
|
| 43 |
+
|
| 44 |
+
def forward(self, frames):
|
| 45 |
+
return self.model(frames)
|
| 46 |
+
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
import torch
|
| 51 |
+
from PIL import Image
|
| 52 |
+
from huggingface_hub import hf_hub_download
|
| 53 |
+
from torchvision import transforms
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
inference_transform = transforms.Compose(
|
| 57 |
+
[
|
| 58 |
+
transforms.Resize((224, 224)),
|
| 59 |
+
transforms.ToTensor(),
|
| 60 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class UCFInferenceByFrames:
|
| 66 |
+
def __init__(self, repo_id):
|
| 67 |
+
self.repo_id = repo_id
|
| 68 |
+
|
| 69 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 70 |
+
self.model = self.load_model()
|
| 71 |
+
|
| 72 |
+
def load_model(self):
|
| 73 |
+
model_path = hf_hub_download(repo_id=self.repo_id, filename="ucf_model.pth")
|
| 74 |
+
state_dict = torch.load(model_path)
|
| 75 |
+
|
| 76 |
+
model = UCFModel().to(device=self.device)
|
| 77 |
+
model.load_state_dict(state_dict)
|
| 78 |
+
model.eval()
|
| 79 |
+
|
| 80 |
+
return model
|
| 81 |
+
|
| 82 |
+
def inference(self, frames):
|
| 83 |
+
video_tensor_list = []
|
| 84 |
+
for frame in frames:
|
| 85 |
+
frame_pil = Image.fromarray(frame)
|
| 86 |
+
frame_tensor = inference_transform(frame_pil)
|
| 87 |
+
video_tensor_list.append(frame_tensor)
|
| 88 |
+
|
| 89 |
+
video_tensor = torch.stack(video_tensor_list)
|
| 90 |
+
video_tensor = video_tensor.permute(1, 0, 2, 3).unsqueeze(0).float()
|
| 91 |
+
|
| 92 |
+
video_tensor = video_tensor.to(self.device)
|
| 93 |
+
|
| 94 |
+
with torch.no_grad():
|
| 95 |
+
output = self.model(video_tensor)
|
| 96 |
+
|
| 97 |
+
return output.argmax(1)
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
import cv2 as cv
|
| 102 |
+
import numpy as np
|
| 103 |
+
|
| 104 |
+
ucf = UCFInferenceByFrames("amjad-awad/ucf-i3d-model-by-3-block-lr-0.001")
|
| 105 |
+
|
| 106 |
+
def inference(ucf_model, video_path, max_frames=16):
|
| 107 |
+
cap = cv.VideoCapture(video_path)
|
| 108 |
+
|
| 109 |
+
if not cap.isOpened():
|
| 110 |
+
print("No video")
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
frames = []
|
| 114 |
+
|
| 115 |
+
while True:
|
| 116 |
+
ret, frame = cap.read()
|
| 117 |
+
|
| 118 |
+
if not ret:
|
| 119 |
+
break
|
| 120 |
+
|
| 121 |
+
frames.append(frame)
|
| 122 |
+
|
| 123 |
+
length = len(frames)
|
| 124 |
+
indices = np.linspace(0, length - 1, max_frames, dtype=int)
|
| 125 |
+
frames = [frames[i] for i in indices]
|
| 126 |
+
predict = ucf_model.inference(frames)
|
| 127 |
+
|
| 128 |
+
return "Crime" if int(predict) == 1 else "No-Crime"
|
| 129 |
+
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
```python
|
| 133 |
+
predict = inference(ucf_model=ucf, video_path="YOUR_VIDEO_PATH.mp4")
|
| 134 |
+
print(predict)
|
| 135 |
+
```
|