i’m a newbie to object detection, we wrote a script to identify gender and age of a person using the deepface library and then adds trackers using the Sort library.
The code works well on the CPU and Google Collabs but when i run it on the GPU (RTX 3050) it raises this error:
Error processing frame: {{function_node _wrapped__AddV2_device/job:localhost/replica:0/task:0/device:GPU:0}} failed to allocate memory [Op:AddV2]
I tried setting the experimental.set_memory_growth to True, but it still raised the same error. Task manager showed that the GPU wasn’t being used a 100%, and oddly enough whenever the error occurs the GPU usage goes down to 0%.
Can my GPU not run object detection of this magnitude ? Are there any optimizations that i must employ ?
Source Code:
results = DeepFace.analyze(resized_frame, actions=['gender', 'age'], enforce_detection=True,
detector_backend='retinaface')
# Process each detected face
detections = []
for result in results:
# Extract bounding box and convert to integers
x = int(result['region']['x'])
y = int(result['region']['y'])
w = int(result['region']['w'])
h = int(result['region']['h'])
# confidence = float(result.get('face_confidence', 0.5)) # Default to 0.5 if missing
# Add detection for SORT tracker
if is_within_roi(x, y, w, h):
confidence = float(result.get('face_confidence', 0.5))
detections.append([x, y, x + w, y + h, confidence])
# Ensure detections array is not empty
if detections:
# Update tracker with valid detections
tracked_objects = tracker.update(np.array(detections))
else:
tracked_objects = []
# Iterate through tracked objects
for track in tracked_objects:
track_id = int(track[4]) # Unique ID assigned by tracker
x1, y1, x2, y2 = map(int, track[:4])
# Match tracker object with DeepFace result (basic IOU/position check)
for result in results:
rx, ry, rw, rh = map(int, [result['region']['x'], result['region']['y'], result['region']['w'],
result['region']['h']])
if abs(x1 - rx) < 20 and abs(y1 - ry) < 20: # Adjust threshold if necessary
# Retrieve gender confidence scores
male_confidence = result['gender']['Man']
female_confidence = result['gender']['Woman']
# Retrieve age from DeepFace result
age = int(result.get('age', -1)) # Default to -1 if age is missing
age_range = get_age_range(age)
gender = "Male" if male_confidence > 99.8 else "Female" if female_confidence > 60 else "Unknown"
if track_id not in tracking_data:
tracking_data[track_id] = {
"Ages": [],
"Genders": set(),
"Entry Time": formatted_time,
"Exit Time": None
}
tracking_data[track_id]["Ages"].append(age)
tracking_data[track_id]["Genders"].add(gender)
if is_within_roi(x1, y1, x2 - x1, y2 - y1):
tracking_data[track_id]["Exit Time"] = formatted_time
if gender != "Unknown":
label = f"{gender}, Age: {age_range}"
color = (0, 255, 0)
draw_label(resized_frame, x1, y1, x2 - x1, y2 - y1, label, color)
if gender == "Male":
global_male_count.add(track_id)
male_ages.append(age)
elif gender == "Female":
global_female_count.add(track_id)
female_ages.append(age)
# Draw the label on the frame
draw_label(resized_frame, x1, y1, x2 - x1, y2 - y1, label, color)
Thank you, and any insights would be super helpful
Leroy Jeslyn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.