Upload 5 files
Browse files- .gitattributes +4 -0
- README.md +185 -3
- results/traffic-test.png +3 -0
- results/traffic-test2.png +3 -0
- results/traffic-test3.png +3 -0
- results/traffic-test4.png +3 -0
.gitattributes
CHANGED
|
@@ -38,3 +38,7 @@ Test/Screenshot[[:space:]]2026-07-15[[:space:]]212818.png filter=lfs diff=lfs me
|
|
| 38 |
Test/Screenshot[[:space:]]2026-07-15[[:space:]]212908.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
Test/Screenshot[[:space:]]2026-07-17[[:space:]]132013.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
Test/Screenshot[[:space:]]2026-07-17[[:space:]]132515.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
Test/Screenshot[[:space:]]2026-07-15[[:space:]]212908.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
Test/Screenshot[[:space:]]2026-07-17[[:space:]]132013.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
Test/Screenshot[[:space:]]2026-07-17[[:space:]]132515.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
results/traffic-test.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
results/traffic-test2.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
results/traffic-test3.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
results/traffic-test4.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,3 +1,185 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 11 |
+
|
| 12 |
+
# tra-base-v1
|
| 13 |
+
|
| 14 |
+
`tra-base-v1` is a lightweight, TinyML-focused model designed for vehicle counting and localization through density map estimation. This is a base version (v1) suitable for direct deployment or further fine-tuning on custom datasets.
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## Model Specifications & Resource Usage
|
| 19 |
+
|
| 20 |
+
- **Target Applications**: Traffic monitoring, vehicle counting on edge devices.
|
| 21 |
+
- **Memory Footprint**:
|
| 22 |
+
- Maximum RAM consumption during 32-bit inference is approximately **5.4 MB**.
|
| 23 |
+
- Suitable for microcontrollers and embedded boards with at least **8 MB of RAM**.
|
| 24 |
+
- **Accuracy**: Around **75% to 80%** under recommended deployment conditions.
|
| 25 |
+
- **Optimal Deployment Conditions**: For best results, the camera should be positioned at an angle of **45 to 60 degrees** relative to the road.
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## Visualizations & Test Results
|
| 30 |
+
|
| 31 |
+
The following test samples demonstrate the model's performance, resource usage, and predicted density maps. The images are loaded from the `results/` folder:
|
| 32 |
+
|
| 33 |
+
<div align="center">
|
| 34 |
+
<img src="results/traffic-test.png" style="border-radius: 15px; margin: 10px; max-width: 70%;" />
|
| 35 |
+
</div>
|
| 36 |
+
<div align="center">
|
| 37 |
+
<img src="results/traffic-test2.png" style="border-radius: 15px; margin: 10px; max-width: 70%;" />
|
| 38 |
+
</div>
|
| 39 |
+
<div align="center">
|
| 40 |
+
<img src="results/traffic-test3.png" style="border-radius: 15px; margin: 10px; max-width: 70%;" />
|
| 41 |
+
</div>
|
| 42 |
+
<div align="center">
|
| 43 |
+
<img src="results/traffic-test4.png" style="border-radius: 15px; margin: 10px; max-width: 70%;" />
|
| 44 |
+
</div>
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
## Limitations
|
| 49 |
+
|
| 50 |
+
- **High Density Traffic**: As shown in the test images, the model may struggle to provide highly precise counts in congested areas with high vehicle density or significant overlapping.
|
| 51 |
+
- **Base Version**: This is a base model; while it performs reasonably well given its extremely low memory usage, additional fine-tuning may be required for complex environments.
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## Installation & Requirements
|
| 56 |
+
|
| 57 |
+
To install the required libraries and prepare the environment, run the following commands:
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
pip install onnxruntime opencv-python-headless scipy matplotlib psutil --quiet
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
from huggingface_hub import snapshot_download
|
| 65 |
+
import os
|
| 66 |
+
|
| 67 |
+
repo_id = "realAABeigi/tra-base-1"
|
| 68 |
+
|
| 69 |
+
print(f"[INFO] Downloading all files from {repo_id} to root...")
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# This will download all files from the repo and place them in the current directory
|
| 73 |
+
snapshot_download(
|
| 74 |
+
repo_id=repo_id,
|
| 75 |
+
local_dir="./",
|
| 76 |
+
local_dir_use_symlinks=False
|
| 77 |
+
)
|
| 78 |
+
print("[SUCCESS] All files downloaded to root directory.")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"[ERROR] Failed to download: {e}")
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
```python
|
| 84 |
+
!pip install onnxruntime opencv-python-headless scipy matplotlib psutil --quiet
|
| 85 |
+
|
| 86 |
+
import os
|
| 87 |
+
import cv2
|
| 88 |
+
import numpy as np
|
| 89 |
+
import matplotlib.pyplot as plt
|
| 90 |
+
from scipy.ndimage import maximum_filter
|
| 91 |
+
import gc
|
| 92 |
+
import tracemalloc
|
| 93 |
+
import time
|
| 94 |
+
import psutil
|
| 95 |
+
import onnxruntime as ort
|
| 96 |
+
|
| 97 |
+
THRESHOLD = 0.4
|
| 98 |
+
IMG_SIZE = 192
|
| 99 |
+
GRID_SIZE = 48
|
| 100 |
+
ONNX_PATH = "tiny_heatmap_car.onnx"
|
| 101 |
+
DATA_PATH = "tiny_heatmap_car.onnx.data"
|
| 102 |
+
TEST_DIR = "/content/Test"
|
| 103 |
+
|
| 104 |
+
def get_process_memory():
|
| 105 |
+
process = psutil.Process(os.getpid())
|
| 106 |
+
return process.memory_info().rss
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
model_size = os.path.getsize(ONNX_PATH)
|
| 110 |
+
if os.path.exists(DATA_PATH):
|
| 111 |
+
model_size += os.path.getsize(DATA_PATH)
|
| 112 |
+
|
| 113 |
+
session = ort.InferenceSession(ONNX_PATH, providers=['CPUExecutionProvider'])
|
| 114 |
+
input_name = session.get_inputs()[0].name
|
| 115 |
+
|
| 116 |
+
def preprocess(img_path):
|
| 117 |
+
orig_img = cv2.imread(img_path)
|
| 118 |
+
if orig_img is None: return None, None
|
| 119 |
+
img_rgb = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)
|
| 120 |
+
img_resized = cv2.resize(img_rgb, (IMG_SIZE, IMG_SIZE))
|
| 121 |
+
img_data = img_resized.astype(np.float32) / 255.0
|
| 122 |
+
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
| 123 |
+
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
| 124 |
+
img_data = (img_data - mean) / std
|
| 125 |
+
img_data = np.transpose(img_data, (2, 0, 1))
|
| 126 |
+
img_data = np.expand_dims(img_data, axis=0)
|
| 127 |
+
return img_data, img_rgb
|
| 128 |
+
|
| 129 |
+
def run_onnx_cpu_inference(img_path):
|
| 130 |
+
gc.collect()
|
| 131 |
+
tracemalloc.start()
|
| 132 |
+
|
| 133 |
+
mem_before = get_process_memory()
|
| 134 |
+
start_time = time.time()
|
| 135 |
+
|
| 136 |
+
img_data, img_rgb = preprocess(img_path)
|
| 137 |
+
if img_data is None: return
|
| 138 |
+
|
| 139 |
+
outputs = session.run(None, {input_name: img_data})
|
| 140 |
+
|
| 141 |
+
mem_after = get_process_memory()
|
| 142 |
+
inference_time = (time.time() - start_time) * 1000
|
| 143 |
+
|
| 144 |
+
current, peak = tracemalloc.get_traced_memory()
|
| 145 |
+
tracemalloc.stop()
|
| 146 |
+
|
| 147 |
+
heatmap = outputs[0].squeeze()
|
| 148 |
+
data_max = maximum_filter(heatmap, size=3)
|
| 149 |
+
maxima = (heatmap == data_max) & (heatmap > THRESHOLD)
|
| 150 |
+
y_coords, x_coords = np.where(maxima)
|
| 151 |
+
|
| 152 |
+
system_delta = mem_after - mem_before
|
| 153 |
+
total_footprint_kb = (model_size + peak + abs(system_delta)) / 1024
|
| 154 |
+
|
| 155 |
+
print(f"\n--- Image: {os.path.basename(img_path)} ---")
|
| 156 |
+
print(f"Latency: {inference_time:.2f}ms")
|
| 157 |
+
print(f"System Memory Change: {system_delta/1024:.2f} KB")
|
| 158 |
+
print(f"Total RAM Footprint (Est): {total_footprint_kb:.2f} KB")
|
| 159 |
+
|
| 160 |
+
plt.figure(figsize=(10, 4))
|
| 161 |
+
plt.subplot(1, 2, 1)
|
| 162 |
+
display_img = cv2.resize(img_rgb, (384, 384))
|
| 163 |
+
for y, x in zip(y_coords, x_coords):
|
| 164 |
+
cx, cy = int(x * (384/GRID_SIZE)), int(y * (384/GRID_SIZE))
|
| 165 |
+
cv2.circle(display_img, (cx, cy), 6, (255, 0, 0), -1)
|
| 166 |
+
plt.imshow(display_img)
|
| 167 |
+
plt.title(f"Cars: {len(y_coords)} | Time: {inference_time:.1f}ms")
|
| 168 |
+
plt.axis('off')
|
| 169 |
+
|
| 170 |
+
plt.subplot(1, 2, 2)
|
| 171 |
+
plt.imshow(heatmap, cmap='jet')
|
| 172 |
+
plt.title(f"Total RAM: {total_footprint_kb:.1f} KB")
|
| 173 |
+
plt.axis('off')
|
| 174 |
+
plt.show()
|
| 175 |
+
|
| 176 |
+
if os.path.exists(TEST_DIR):
|
| 177 |
+
image_files = [f for f in os.listdir(TEST_DIR) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 178 |
+
for img_file in image_files:
|
| 179 |
+
run_onnx_cpu_inference(os.path.join(TEST_DIR, img_file))
|
| 180 |
+
else:
|
| 181 |
+
print("Folder Test not found.")
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print(f"[ERROR]: {e}")
|
| 185 |
+
```
|
results/traffic-test.png
ADDED
|
Git LFS Details
|
results/traffic-test2.png
ADDED
|
Git LFS Details
|
results/traffic-test3.png
ADDED
|
Git LFS Details
|
results/traffic-test4.png
ADDED
|
Git LFS Details
|