I'm trying to use a Caffe model (link to it below).
In the second last line net.forward() gives the following error.
OpenCV Error: Assertion failed (srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == CV_32F && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols))) in run, file /home/pi/opencv-3.3.0/modules/dnn/src/layers/fully_connected_layer.cpp, line 132
Traceback (most recent call last):
File "neural_net.py", line 25, in <module>
preds = net.forward()
cv2.error: /home/pi/opencv-3.3.0/modules/dnn/src/layers/fully_connected_layer.cpp:132: error: (-215) srcMat.dims == 2 && srcMat.cols == weights.cols && dstMat.rows == srcMat.rows && dstMat.cols == weights.rows && srcMat.type() == weights.type() && weights.type() == dstMat.type() && srcMat.type() == CV_32F && (biasMat.empty() || (biasMat.type() == srcMat.type() && biasMat.isContinuous() && (int)biasMat.total() == dstMat.cols)) in function run
Link to Model
http://www.openu.ac.il/home/hassner/projects/cnn_agegender/cnn_age_gender_models_and_data.0.0.2.zip
My Code
import numpy as np
import argparse
import time
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True)
ap.add_argument("-p", "--prototxt", required=True)
ap.add_argument("-m", "--model", required=True)
ap.add_argument("-l", "--labels", required=True)
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
rows = open(args["labels"]).read().strip().strip("\n")
classes = [r[r.find(" ")+1:].split(",")[0] for r in rows]
blob = cv2.dnn.blobFromImage(image, 1, (224,224), (104, 117, 123))
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
net.setInput(blob)
start = time.time()
preds = net.forward()
end= time.time()
I'm running OpenCV3.3.0
I have been able to run the above code with the googlenet Caffe model.
Am I doing something wrong? Is there something wrong with the model? How can I fix it?
@michaelkhan3, models you faced the problem process 227x227 input images, GoogLeNet works with 224x224.
@dkurt That worked. Thanks very much. Just to help me out in the future. Where did you see that the model took a 227x227 image?
@michaelkhan3, see at .prototxt configuration file. Input image dimensionality is a key thing we have to know about the models.
"""
Created on Sat May 30 18:55:07 2020
@author: #PSP
"""
import cv2
import face_recognition
webcam_video_stream = cv2.VideoCapture(0)
all_face_location = []
while True:
ret, current_frame = webcam_video_stream.read()
current_frame_small = cv2.resize(current_frame, (0, 0), fx = 0.25, fy = 0.25)
all_face_location = face_recognition.face_locations(current_frame_small, number_of_times_to_upsample = 2, model = "hog")
for index, current_face_location in enumerate(all_face_location):
#Splitting tuple to get the four positions values
top_pos, right_pos, bottom_pos, left_pos = current_face_location
top_pos = top_pos * 4
right_pos = right_pos * 4
bottom_pos = bottom_pos * 4
left_pos = left_pos * 4
#Printing the faces coordinates
#print('Found face {} at top : {}, right: {}, bottom: {}, left: {}'.format(index + 1, top_pos, right_pos, bottom_pos, left_pos))
#Blurring face
current_face_image = current_frame[top_pos:bottom_pos, left_pos:right_pos]
#current_face_image = cv2.GaussianBlur(current_face_image, (99, 99), 30)
#current_frame[top_pos:bottom_pos, left_pos:right_pos] = current_face_image
AGE_GENDER_MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 144.895847746)
#Create the blob for current face
current_face_image_blob = cv2.dnn.blobFromImage(current_face_image, 1, (277, 277), AGE_GENDER_MODEL_MEAN_VALUES, swapRB = False)
gender_label_list = ["Male", "Female"]
gender_protext = "dataset/deploy_gender.prototxt"
gender_caffemodel = "dataset/gender_net.caffemodel"
#Creating model
gender_convolution_n_network = cv2.dnn.readNet(gender_caffemodel, gender_protext)
#Giving input to the model
gender_convolution_n_network.setInput(current_face_image_blob)
#Getting the predictions from the model
gender_predictions = gender_convolution_n_network.forward()
#Finding the maximum value from the predictions
gender = gender_label_list[gender_predictions[0].argmax()]
age_label_list = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
age_protext = "dataset/deploy_age.prototxt"
age_caffemodel = "dataset/age_net.caffemodel"
#Creating model
age_convolution_n_network = cv2.dnn.readNet(age_caffemodel, age_protext)
#Giving input to the model
age_convolution_n_network.setInput(current_face_image_blob)
#Getting the predictions from the model
age_predictions = age_convolution_n_network.forward()
#Finding the maximum value from the predictions
age = age_label_list[age_predictions[0].argmax()]
cv2.rectangle(current_frame, (left_pos, top_pos), (right_pos, bottom_pos + 20), (0, 0, 255), 2) # Here (0, 0, 0) means the border have trancparent colour i.e. excluding the border
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(current_frame, gender + " " + age + "yrs", (left_pos, bottom_pos), font, 0.5, (0, 255, 0), 1)
cv2.imshow("Webcam Video", current_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
webcam_video_stream.release()
cv2.destroyAllWindows()
Still not working
Most helpful comment
@michaelkhan3, models you faced the problem process
227x227input images, GoogLeNet works with224x224.