Comments (6)
Face detection on dlib and age/gender estimation on CNN are the most time-consuming.
You can resize the input frame into smaller size to accelerate face detection, and use more lightweight CNN to accelerate age/gender estimation.
Please use --depth
and --width
options in train.py
to control the size of CNN.
The result of line_profiler:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
16 @profile
17 def main():
18 # for face detection
19 1 296641 296641.0 1.5 detector = dlib.get_frontal_face_detector()
20
21 # load model and weights
22 1 5 5.0 0.0 img_size = 64
23 1 3599541 3599541.0 18.7 model = WideResNet(img_size, depth=16, k=8)()
24 1 2809484 2809484.0 14.6 model.load_weights(os.path.join("pretrained_models", "weights.18-4.06.hdf5"))
25
26 # capture video
27 1 754699 754699.0 3.9 cap = cv2.VideoCapture(0)
28 1 322 322.0 0.0 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
29 1 35069 35069.0 0.2 cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
30
31 1 4 4.0 0.0 while True:
32 # get video frame
33 21 370552 17645.3 1.9 ret, img = cap.read()
34
35 21 112 5.3 0.0 if not ret:
36 print("error: failed to capture image")
37 return -1
38
39 21 4649 221.4 0.0 input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
40 21 294 14.0 0.0 img_h, img_w, _ = np.shape(input_img)
41
42 # detect faces using dlib detector
43 21 4994374 237827.3 26.0 detected = detector(input_img, 1)
44 21 447 21.3 0.0 faces = np.empty((len(detected), img_size, img_size, 3))
45
46 42 1482 35.3 0.0 for i, d in enumerate(detected):
47 21 138 6.6 0.0 x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
48 21 143 6.8 0.0 xw1 = max(int(x1 - 0.4 * w), 0)
49 21 44 2.1 0.0 yw1 = max(int(y1 - 0.4 * h), 0)
50 21 59 2.8 0.0 xw2 = min(int(x2 + 0.4 * w), img_w - 1)
51 21 49 2.3 0.0 yw2 = min(int(y2 + 0.4 * h), img_h - 1)
52 21 3669 174.7 0.0 cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
53 # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
54 21 3102 147.7 0.0 faces[i,:,:,:] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
55
56 21 64 3.0 0.0 if len(detected) > 0:
57 # predict ages and genders of the detected faces
58 21 5350109 254767.1 27.9 results = model.predict(faces)
59 21 75 3.6 0.0 predicted_genders = results[0]
60 21 263 12.5 0.0 ages = np.arange(0, 101).reshape(101, 1)
61 21 715 34.0 0.0 predicted_ages = results[1].dot(ages).flatten()
62
63 # draw results
64 42 940 22.4 0.0 for i, d in enumerate(detected):
65 21 132 6.3 0.0 label = "{}, {}".format(int(predicted_ages[i]),
66 21 438 20.9 0.0 "F" if predicted_genders[i][0] > 0.5 else "M")
67 21 1897 90.3 0.0 draw_label(img, (d.left(), d.top()), label)
68
69 21 138714 6605.4 0.7 cv2.imshow("result", img)
70 21 837285 39870.7 4.4 key = cv2.waitKey(30)
71
72 21 191 9.1 0.0 if key == 27:
73 1 2 2.0 0.0 break
from age-gender-estimation.
Thank you. I thought it is because of 'model.predict'
while trying to predict the gender.
If we run it on worker thread, will it make frame smooth?
Since it is running on top of 4gb cpu, may i know how much width and depth will you suggest while training?
from age-gender-estimation.
I tried python train.py --input data/imdb_db.mat --depth 8 --width 4
but i got the following error
File "train.py", line 94, in
main()
File "train.py", line 61, in main
model = WideResNet(image_size, depth=depth, k=k)()
File "/Users/Downloads/age-gender-estimation-master/wide_resnet.py", line 110, in call
assert ((self._depth - 4) % 6 == 0)
AssertionError
from age-gender-estimation.
As mentioned in the error messages, (depth - 4) % 6 == 0
should be satisfied.
Thus possible minimum depth
is 10
.
I think that depth = 10
and width = 4
is reasonable choice, expecting half computational cost.
If this does not satisfy your requirement, the other way is to use other lightweight CNNs as base network like SqueezeNet or MobileNet.
If we run it on worker thread, will it make frame smooth?
predict
is already multi-threaded, so I'm afraid that the above solution has a little impact.
from age-gender-estimation.
Thank you son much. May I know where I can get SqueezeNet / MobileNet? and Training with SqueezeNet /MobileNet is same as that of the current one?
from age-gender-estimation.
I use cpu. can you please train the model with depth 10 and width 4 and share with me the model.It will be a great help for me to learn by experimenting.
from age-gender-estimation.
Related Issues (20)
- onnx HOT 1
- Citation for this repository? HOT 2
- Low gender accurate HOT 1
- train doesn't use detection's result HOT 1
- Requirement versions
- How to draw ROC curve?
- cannot import name 'EfficientNetB0' HOT 3
- Hi! The face image in folder should be cropped? HOT 1
- accuracy in "age_estimation" folder
- run demo.py in "age_estimation" folder HOT 2
- Turn off logging outputs
- Variable shape and weights shape are not matching HOT 5
- Please help me to custom model only age-estimation
- How to re-traning when stop HOT 1
- cv2.imshow returns error
- labels.txt for age and gender
- why dot ages arange(0, 101) rather than max in predict HOT 1
- You can consider using the new B3FD dataset or IMDB-WIKI filtration lists for even better results HOT 1
- OmegaConf error
- mae performance results are different in Debug mode and Release mode.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from age-gender-estimation.