Coder Social home page Coder Social logo

yu4u / age-gender-estimation Goto Github PK

View Code? Open in Web Editor NEW
1.4K 41.0 502.0 3.29 MB

Keras implementation of a CNN network for age and gender estimation

License: MIT License

Jupyter Notebook 92.83% Python 7.11% Shell 0.07%
deep-learning convolutional-neural-networks imdb gender-estimation age-estimation

age-gender-estimation's Introduction

Age and Gender Estimation

This is a Keras implementation of a CNN for estimating age and gender from a face image [1, 2]. In training, the IMDB-WIKI dataset is used.

  • [Aug. 21, 2020] Refactored; use tensorflow.keras
  • [Jun. 30, 2019] Another PyTorch-based project was released
  • [Nov. 12, 2018] Enable Adam optimizer; seems to be better than momentum SGD
  • [Sep. 23, 2018] Demo from directory
  • [Aug. 11, 2018] Add age estimation sub-project here
  • [Jul. 5, 2018] The UTKFace dataset became available for training.
  • [Apr. 10, 2018] Evaluation result on the APPA-REAL dataset was added.

Dependencies

  • Python3.6+

Tested on:

  • Ubuntu 16.04, Python 3.6.9, Tensorflow 2.3.0, CUDA 10.01, cuDNN 7.6

Usage

Use trained model for demo

Run the demo script (requires web cam). You can use --image_dir [IMAGE_DIR] option to use images in the [IMAGE_DIR] directory instead.

python demo.py

The trained model will be automatically downloaded to the pretrained_models directory.

Create training data from the IMDB-WIKI dataset

First, download the dataset. The dataset is downloaded and extracted to the data directory by:

./download.sh

Secondly, filter out noise data and serialize labels into .csv file. Please check check_dataset.ipynb for the details of the dataset. The training data is created by:

python create_db.py --db imdb
usage: create_db.py [-h] [--db DB] [--min_score MIN_SCORE]

This script cleans-up noisy labels and creates database for training.

optional arguments:
  -h, --help            show this help message and exit
  --db DB               dataset; wiki or imdb (default: imdb)
  --min_score MIN_SCORE minimum face_score (default: 1.0)

The resulting files with default parameters are included in this repo (meta/imdb.csv and meta/wiki.csv), thus there is no need to run this by yourself.

Create training data from the UTKFace dataset [currently not supported]

Firstly, download images from the website of the UTKFace dataset. UTKFace.tar.gz can be downloaded from Aligned&Cropped Faces in Datasets section. Then, extract the archive.

tar zxf UTKFace.tar.gz UTKFace

Finally, run the following script to create the training data:

python create_db_utkface.py -i UTKFace -o UTKFace.mat

[NOTE]: Because the face images in the UTKFace dataset is tightly cropped (there is no margin around the face region), faces should also be cropped in demo.py if weights trained by the UTKFace dataset is used. Please set the margin argument to 0 for tight cropping:

python demo.py --weight_file WEIGHT_FILE --margin 0

The pre-trained weights can be found here.

Train model

Train the model architecture using the training data created above:

python train.py

Trained weight files are stored as checkpoints/*.hdf5 for each epoch if the validation loss becomes minimum over previous epochs.

Changing model or the other training parameters

You can change default setting(s) from command line as:

python train.py model.model_name=EfficientNetB3 model.batch_size=64

Available models can be found here.

Check training curve

The training logs can be easily visualized via wandb by:

  1. create account from here
  2. create new project in wandb (e.g. "age-gender-estimation")
  3. run wandb login on terminal and authorize
  4. run training script with wandb.project=age-gender-estimation argument
  5. check dashboard!

Use the trained model

python demo.py
usage: demo.py [-h] [--weight_file WEIGHT_FILE] [--margin MARGIN]
               [--image_dir IMAGE_DIR]

This script detects faces from web cam input, and estimates age and gender for
the detected faces.

optional arguments:
  -h, --help            show this help message and exit
  --weight_file WEIGHT_FILE
                        path to weight file (e.g. weights.28-3.73.hdf5)
                        (default: None)
  --margin MARGIN       margin around detected face for age-gender estimation
                        (default: 0.4)
  --image_dir IMAGE_DIR
                        target image directory; if set, images in image_dir
                        are used instead of webcam (default: None)

Please use the best model among checkpoints/*.hdf5 for WEIGHT_FILE if you use your own trained models.

Estimated results

Trained on imdb, tested on wiki.

Evaluation

Evaluation on the APPA-REAL dataset

You can evaluate a trained model on the APPA-REAL (validation) dataset by:

python evaluate_appa_real.py --weight_file WEIGHT_FILE

Please refer to here for the details of the APPA-REAL dataset.

The results of trained model is:

MAE Apparent: 5.33
MAE Real: 6.22

The best result reported in [5] is:

MAE Apparent: 4.08
MAE Real: 5.30

Please note that the above result was achieved by finetuning the model using the training set of the APPA-REAL dataset.

License

This project is released under the MIT license. However, the IMDB-WIKI dataset used in this project is originally provided under the following conditions.

Please notice that this dataset is made available for academic research purpose only. All the images are collected from the Internet, and the copyright belongs to the original owners. If any of the images belongs to you and you would like it removed, please kindly inform us, we will remove it from our dataset immediately.

Therefore, the pretrained model(s) included in this repository is restricted by these conditions (available for academic research purpose only).

References

[1] R. Rothe, R. Timofte, and L. V. Gool, "DEX: Deep EXpectation of apparent age from a single image," in Proc. of ICCV, 2015.

[2] R. Rothe, R. Timofte, and L. V. Gool, "Deep expectation of real and apparent age from a single image without facial landmarks," in IJCV, 2016.

[3] H. Zhang, M. Cisse, Y. N. Dauphin, and D. Lopez-Paz, "mixup: Beyond Empirical Risk Minimization," in arXiv:1710.09412, 2017.

[4] Z. Zhong, L. Zheng, G. Kang, S. Li, and Y. Yang, "Random Erasing Data Augmentation," in arXiv:1708.04896, 2017.

[5] E. Agustsson, R. Timofte, S. Escalera, X. Baro, I. Guyon, and R. Rothe, "Apparent and real age estimation in still images with deep residual regressors on APPA-REAL database," in Proc. of FG, 2017.

age-gender-estimation's People

Contributors

ageitgey avatar lkdmttg7 avatar moralfish avatar otacon94 avatar xjdeng avatar yu4u avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

age-gender-estimation's Issues

Can't train a model using the IMDB-WIKI dataset-possible due to corrupted .mat file

When I tried to run "create_db.py", after the progress bar reached 100%, my computer was frozen. After a while, the command line output a word "killed". Since a file called imdb_db.mat was found in the directory "data",I tried to continue the training process, but the error "IOError: could not read bytes" appeared.

I checked the "imdb.mat" in the directory "imdb_crop" with Octave(I don't have MATLAB), I found that I can't be loaded into Octave. The error "warning: load: can not read non-ASCII portions of UTF characters; replacing unreadable characters with'?'" appeared when I tried to load the "imdb.mat". I did the same to "imdb_db.mat",the error " error: load: reading matrix data for 'image'
error: load: trouble reading binary file '/home/mt/Downloads/age-gender-estimation-master/data/imdb_db.mat'" appeared. Only gender,img_size,min_score was loaded.

Is the file "imdb.mat" corrupted? Or I used the file wrongly? I am looking forward to your reply. Your help will be greatly appreciated.

test on age MAE and gender accuracy

Thank you @yu4u for this project.
I have some questions. In your project, you have trained on imdb dataset and tested on wiki dataset, but can you tell me the specific test age MAE and gender accuracy?

Image resizing lost aspect ratio

Afaik this code
https://github.com/yu4u/age-gender-estimation/blob/master/demo.py#L99
https://github.com/yu4u/age-gender-estimation/blob/master/create_db.py#L56

breaks aspect ratio of an input image.

But from what I see here https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/ cropped images are perfect squares padded using cv2.BORDER_REPLICATE strategy.

Could you please confirm which resizing strategy or input datasets you used for training?

Either cropped square images only, or full dataset with additional face detection of your own.

demo used for detecting ages and gender of images

Hello,

I have modified the demo.py code for detecting ages and genders for images as an input instead of the webcam feed.

I was wondering, the pretrained_model; is it trained with the IMDB dataset?
I am using images of 6 different emotion recognition datasets but for females, it often classifies them as male.
Also, the ages aren't well-estimated. For an 70 year old male it classifies him as a 27 year old male.
I realize that this is due the fact that the images I use are very different from how the network is trained.
(eg. 576 x 720 size images)

Should I re-train the algorithm with the IMDB dataset or is this un-needed work?
I only have around 1500 images so I was assuming that it being trained on 500.000 images should be enough.

I am using images from MMI dataset, Cohn-Kanade dataset, etc.

Or do you suggest another age- gender recognition network that would be better to use for my case?

How can I change this keras model to cocurrent ...

This code is written and run perfectly, but when I use this code in Flask+Gunicorn which is multithread, it will throw:

ValueError: Tensor Tensor("predictions_1/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.

I search in stackoverflow, found this is caused by Keras. Link is https://github.com/fchollet/keras/issues/2397 and solution is what ‘avital ’ and ‘jglwiz’ said:

keras with tensorflow backend.

Details:
global thread:

self.model = load_model(model_path)
self.model._make_predict_function()
self.graph = tf.get_default_graph()

another thread:

with self.graph.as_default():
    labels = self.model.predict(data)

but how can I modify our code ?( I am totally a newbie in Keras ... Sorry ...)
I tried following code but made no sense, maybe "load_weights" is different with "load_model" method:

Global:

self.agegender_classifier = WideResNet()()
self.agegender_classifier.load_weights(agegender_emotion_model_path)
self.agegender_classifier._make_predict_function()
self.agegender_graph = tf.get_default_graph()

Predict:

with self.agegender_graph.as_default():
     results = self.agegender_classifier.predict(faces_agegender)

Trying to speed up the detection

I tried to speed up the serving program and found three solutions :
-1 Replace dlib.get_frontal_face_detector with Face Detection from Yeephycho's SSD Mobilenet (https://github.com/yeephycho/tensorflow-face-detection)
-2 Using tensorflow.keras rather than Keras for age and gender classification.
-3 Using imutils.video.WebcamVideoStream in order to have a Thread dedicated to VideoCapture (https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/).

I didn't benchmark my updates but (1) allows me to jump from 5fps to 25fps (with a 1080Ti), (2) jump from 260ms by age/gender classification to 220ms (PC with no GPU) and (3) ... I don't know but it looks better !

If someone is interested, I can share my updates.

MAE(7.436) and e-error(0.543) compared with References[2]'s best score MAE(3.252) and e-error(0.282)

Hi Yusuke. Thanks your repo firstly. It helps me a lot. My work following your repo is as below:
1, I cleaned the imdb dataset manually and converted them to imdb_zz.mat.
2, Training on imdb_zz.mat. --aug Ture, depth 16 width 8 as default. num_epochs 30.
3, the minimum val_loss is 3.49 and the weights stored as weights.25-3.49.hdf5.
4, Using weights.25-3.49.hdf5 to predict images. I test on LAP dataset as References[2] mentioned. However, my score is much worse than References[2] as discribled in title and it predicts young people(age<15) badly.

i am looking forward to your reply and any advice is welcomed.
Thank you.

Errors when running python3 demo.py

I have run python3 demo.py code
and I got this
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds) Using TensorFlow backend. /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds) /usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds) Downloading data from https://github.com/yu4u/age-gender-estimation/releases/download/v0.5/weights.18-4.06.hdf5 195813376/195810464 [==============================] - 56s 0us/step 2018-07-24 16:57:16.697715: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA VIDEOIO ERROR: V4L: can't open camera by index 0 Traceback (most recent call last): File "demo.py", line 121, in <module> main() File "demo.py", line 82, in main for img in yield_images(): File "demo.py", line 60, in yield_images yield img File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__ self.gen.throw(type, value, traceback) File "demo.py", line 42, in video_capture yield cap File "demo.py", line 58, in yield_images raise RuntimeError("Failed to capture image") RuntimeError: Failed to capture image

NOTE: I'm on Ubuntu 16.04 installed in VMware and I have a webcam in my laptop, I think I've installed all the dependencies correctly

Age accuracy issues - For both IMDB and UTK

Hi,

I've been doing some simple analysis over both pre-trained models, validating using appa-real dataset.
Here's what I'm doing:

For each cropped and rotated image i.e. (_face.jpg) prefix:

  • Resize it to 256x256
  • Create 2 copies of each image, one is gray scaled and the other RGB
  • For each of those, I pass them through dlib frontal face detector,
  • If needed, for UTK I crop the face (margin=0) and with IMDB I'm giving it some margin of .4 (default in your code), for the margin part I did grid search and found that those values work (also noted in your documentations and I'm assuming after seeing UTKFaces that that's the way the models were trained)
  • At prediction step the image is begin resized to 64x64, this part is a copy-paste from demo.py code

Note that I'm using real_age as supplied by appa dataset and not the expected value of apperent_age.
In total I've sampled 1978 images, I see that:

  • Both models are struggling with younger and older ages, imdb handles it a bit better for some reasons (which I still yet to check why, I have assumptions tho.) and the range I'm getting from both is around 12-68 where the domain of my ages are 0 to 100.
  • Using gray scaled images, even tho at least with UTK I'm assuming was trained with color images, is giving better results.

I'm attaching some plots I've done, where my goal is to find if either I'm doing something incorrectly or maybe the accuracy is just off a bit? Maybe best to re-train on another dataset?

screen shot 2018-07-29 at 14 35 37

screen shot 2018-07-29 at 14 35 41

screen shot 2018-07-29 at 14 35 46

screen shot 2018-07-29 at 14 35 49

Again, thank you @yu4u for this project, I'm assuming I'm at fault here. Would love to get your feedback.

Pre-Trained Model

Hi,

I would appreciate if you share pre-trained models and checkpoints, so that I could download it and evaluate the model. Pre-trained model might be small, just for demo purposes.

Thank you a lot,
I really like your project.

Using other face detection algorithm

Hello, I have a small issue with the code.
First of all, thanks so much for providing us with this code!

In the demo.py the code looks as follows:


    #Get BGR in order for the detector to work.
    input_img = cv2.cvtColor(img.astype('uint8'), cv2.COLOR_GRAY2BGR)
    detected = detector(input_img, 1)
    img_h, img_w, _ = np.shape(input_img)
    faces = np.empty((len(detected), img_size, img_size, 3))
    for i, d in enumerate(detected):
        x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
        xw1 = max(int(x1 - 0.4 * w), 0)
        yw1 = max(int(y1 - 0.4 * h), 0)
        xw2 = min(int(x2 + 0.4 * w), img_w - 1)
        yw2 = min(int(y2 + 0.4 * h), img_h - 1)
        cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
        faces[i, :, :, :] = cv2.resize(input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
    
    # predict ages and genders of the detected faces
    results = model.predict(faces)

However, is there any way to use the model.predict(faces) on the input image?
This is because in an earlier stage, I already detected and extracted the faces of the subject that I am working with. This is the input image that I am trying to feed in. This means it is a time loss to do this again, and sometimes the faces can't be detected due to it already being cropped.

When I use the model.predict on the original image, I of course get an error; being:

Error when checking : expected input_11 to have 4 dimensions, but got array with shape (224, 224, 3)

Which is expected. However, is there a simple way to get past this?

The algorithm I am workng with has as output:

cut_image = img[y_m-he:y_m+he,x_m-wi:x_m+wi]
cut_image = cv2.resize(cut_image,(width,height), interpolation = cv2.INTER_AREA)

Thanks so much

Pre-trained model on Android application

Hi

First of all, thanks for this work. My question is that can I use the pre-trained weights you provide in an android application? if so, how can I create the .pb file for this purpose?

Thanks in advance,,

The time of training is too long

Hi,yu4u,
I am a student, I need to learn age and gender estimation for my graduation design. I'm using my computer to use IMDB-WIKI dataset to train the model,but this process takes a long time, It takes about 4 hours one epoch. Can you give me your checkpoint folder? I promise I won't use it for any commercial purpose. Thank you very much.

Age on dataset imdb and wiki

Hello, I found strange things on dataset maximum age is 2014 and minimum age is -31.
I tried using your code in
https://github.com/yu4u/age-gender-estimation/blob/master/create_db.py in line 34 and
I add several code

mat_path = "../imdb.mat"
db = "imdb"
full_path, dob, gender, photo_taken, face_score, second_face_score, age\
    = get_meta(mat_path, db)

path_list = [str(item[0]) for item in full_path.tolist()]
max_age = np.max(age) #result 2014
max_age_idx = np.argmax(age) #result 181492
path_photo = path_list[max_age_idx] #result '64/nm1002664_rm1109196544_0-7-31_2015.jpg'

and link dataset I download is https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_crop.tar

wide_resnet error

from wide_resnet import WideResNet
ModuleNotFoundError: No module named 'wide_resnet'

Age and Gender prediction takes too long

When I was running demo.py, I found that the template of weights.18-4.06. Hdf5 was time-consuming to predict, so I felt whether the template could be improved

Training time

I am new in python. I training the data from UTFFace, but it too very long time. Any method that can train the data in short time? Thank you.

Using theano as backend

I have used tensorflow but the output is too laggy even after reducing the depth.
So how do we use theano to optimize the performance?

Training on age & custom factor - generated model dimension issue

Hey Yusuke

This is a continuation from issue 17.

After i ran train.py, I generated checkpoints, along with these files:

In you predict.ipynb, I changed the

model.load_weights(os.path.join("pretrained_models", "weights.18-4.06.hdf5"))

to

model.load_weights("WRN_16_8.h5")

And I'm getting this odd error:

ValueError: Dimension 0 in both shapes must be equal, but are 131072 and 32768 for 'Assign_137' (op: 'Assign') with input shapes: [131072,2], [32768,2].

Any ideas where I might've gone wrong? I made sure I am using same widenet_res.py in both training and in prediction

BTW, I changed the nb_epoch from 30 to 100 during training but i doubt this is the cause...?

Mean substraction

In the demo file, I can't see any mean subtraction from the input images on the webcam. Is that correct?

How can I get a more stable model?

Thank you for sharing, I have a question. When using cam to test, the face is almost unchanged, but the probability of the model output is very unstable. Do you have any good solutions?

Training on age & custom factor

Im testing your code on different attributes: gender & ethnicity, where ethnicity is one of 7:

ethnicity_map = {
	"Asian" : 0,
	"Black" : 1,
	"Caucasian" : 2, 
	"Hispanic" : 3,
	"Indian" :  4,
	"Middle Eastern" : 5, 
	"Native American" : 6
}

def ethnicity_category(eth):
	return ethnicity_map[eth] if eth in ethnicity_map else None

Then I follow your procedure for generating /reading the matlab file


    out_genders = []
    out_ethnicities = []
    out_imgs = []

    df = pd.read_csv("../STEP3/data.csv")

    for index,row in df.iterrows():
        
        gender 		= 1 if (row["gender"].strip() == "Male") else 0
        ethnicity 	= ethnicity_category(row["ethnicity"].strip())
        image		= row["image"].strip()
        
        if(ethnicity is not None):
            out_genders.append(gender)
            out_ethnicities.append(ethnicity)
            img = cv2.imread("../STEP3/data/{}".format(image))
            out_imgs.append( cv2.resize(img, (img_size, img_size)) )


    output = {
    	"image"    : np.array(out_imgs), 
    	"ethnicity": np.array(out_ethnicities), 
    	"gender"   : np.array(out_genders),
        "img_size" : img_size,
    }

    scipy.io.savemat(output_path, output)

Every other parameter is the same (including img_size).

For reading the weights:

    X_data = image
    y_data_g = np_utils.to_categorical(gender, num_classes=2)
    y_data_e = np_utils.to_categorical(ethnicity, num_classes=8)

I changed wide_resnet.py from

predictions_g = Dense(units=2, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)
predictions_a = Dense(units=101, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)

model = Model(inputs=inputs, outputs=[predictions_g, predictions_a])

to

predictions_g = Dense(units=2, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)
predictions_e = Dense(units=7, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)

model = Model(inputs=inputs, outputs=[predictions_g, predictions_e])

And I am getting this error when i start training:

ValueError: Error when checking target: expected dense_2 to have shape (None, 7) but got array with shape (75797, 8)

Any ideas?

Accuracy issue

When I run this model on video, i've noticed the prediction changes pretty wildly from frame to frame, depending on the face dimensions, lighting, position, etc. But as soon as the face becomes closed to the camera, the predictions are more consistent.

Two questions

  1. the default 32x32... why was that chosen?
  2. could prediction be helped by training / running on larger dimensions? (64x64 or even much larger)

training problem

Hello ,
for the error on train.py: the following arguments are required: --input/-i
I've already download the image data set. I'd like to know how to change the data to .mat and how to use train.py to training my model.Thanks

Strange behavior when training on smaller dataset

Hi
I'm trying to train the model on my own smaller dataset. When I predict gender, I get an array full of only [1,0], [1,0], [1,0],...... instead of getting different probabilities.

Why does this happen? What is the minimum size of dataset I can use?

Thanks

weights of the model

Hi @yu4u ,
Where can the weights found here age only weights be used? Can I use it normally while running demo?

I tried running it in the demo.py script and got this error, so I assume my depth and width of the model should be changed accordingly. Any leads on that?
ValueError: You are trying to load a weight file containing 107 layers into a model with 31 layers.

Training with LAP dataset

Hello,

I am trying to train the model using the LAP dataset, but I am not sure how to do it.

I downloaded the training dataset with the images and th CVS file. Can you tell me how to change the code and traing the model with this dataset.

Join hdf5 files

After training, I have got series of hdf5 file. How do I combine files hdf5 files after training?

Age classification calculation in demo.py

Hi there,

I'm confused about this calculation in demo.py:

  results = model.predict(faces)
  predicted_genders = results[0]
  ages = np.arange(0, 101).reshape(101, 1)
  predicted_ages = results[1].dot(ages).flatten()

Why is there a dot product calculated instead of index with max value?

Imagine the following scenario with one face with this output:

results[1]:
[[6.88381488e-06 4.99174821e-05 5.31654223e-05 8.81420328e-06
  3.14831013e-05 4.00605095e-05 4.33792193e-05 5.87120885e-05
  4.47424136e-05 7.89339829e-05 9.78237658e-05 2.98563100e-04
  5.30292047e-04 1.20672619e-03 4.25174553e-03 6.22432539e-03
  1.31485937e-02 1.25159947e-02 2.79996358e-02 2.95896754e-02
  4.22185808e-02 4.98768501e-02 8.72164890e-02 8.13968405e-02
  4.06013280e-02 7.37321898e-02 6.11355454e-02 5.95830940e-02
  5.18204570e-02 6.64811879e-02 4.87477817e-02 2.88283490e-02
  5.48146665e-02 2.56483611e-02 1.41099067e-02 1.93021428e-02
  1.27679640e-02 2.21271571e-02 8.21464416e-03 9.39735305e-03
  7.45108770e-03 7.62853073e-03 4.46106028e-03 3.77128413e-03
  3.19997105e-03 3.59987095e-03 1.93364581e-03 2.93601328e-03
  2.00799946e-03 1.00616459e-03 1.59712264e-03 6.33452029e-04
  5.70641481e-04 4.81930183e-04 4.77437454e-04 3.11825279e-04
  3.33868637e-04 3.99059849e-04 3.31061863e-04 3.73236981e-04
  3.08186543e-04 1.56975817e-04 1.60575451e-04 2.16696353e-04
  2.38688110e-04 1.83488766e-04 9.20830789e-05 1.01336227e-04
  9.11841635e-05 6.63516548e-05 6.84541301e-05 6.43297026e-05
  6.82058817e-05 5.28406053e-05 4.04599705e-05 3.28831848e-05
  2.44612365e-05 2.94256006e-05 2.54540537e-05 3.53251671e-05
  1.79423114e-05 9.23900279e-06 1.36023336e-05 1.01442247e-05
  1.07303895e-05 6.55067834e-06 5.72359386e-06 6.08522032e-06
  5.98073939e-06 6.34011349e-06 4.15177055e-06 3.75096920e-06
  4.02725482e-06 4.76177183e-06 4.53801522e-06 3.85877502e-06
  4.49254321e-06 3.67203666e-06 4.07408197e-06 3.67203666e-06
  3.67203666e-06]]

The biggest probability is 8.72164890e-02 with index 22, however, using the calculation currently implemented, result is 26.89 and predicted age is therefore 26. Why? Is pretrained model really trained this way?

"Illegal instruction (core dumped)" when running the program with tensorflow with gpu

I succeeded running the program with tensorflow without gpu. However, I can't run the program with tensorflow with gpu.
The following error appears when I run the program:

Using TensorFlow backend.
2017-07-05 10:18:44.115782: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:893] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-07-05 10:18:44.116126: I tensorflow/core/common_runtime/gpu/gpu_device.cc:938] Found device 0 with properties:
name: GeForce GTX 1050
major: 6 minor: 1 memoryClockRate (GHz) 1.468
pciBusID 0000:01:00.0
Total memory: 1.95GiB
Free memory: 1.72GiB
2017-07-05 10:18:44.116175: I tensorflow/core/common_runtime/gpu/gpu_device.cc:959] DMA: 0
2017-07-05 10:18:44.116189: I tensorflow/core/common_runtime/gpu/gpu_device.cc:969] 0: Y
2017-07-05 10:18:44.116214: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1028] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0)
Illegal instruction (core dumped)

Does this program compatible with tensorflow with gpu?
The system I am using is list as following:
Ubuntu 16.04,Python 2.7.12 ,Keras 2.0.5,Tensorflow 1.2.0,CUDA 8.0, V8.0.61
,cuDNN 6.0

where to get WRN_16_8.json?

hello, I want to run predict.ipny. But it seems to need WRN_16_8.json So where to get WRN_16_8.json?
looking forward to your reply.

I want to train the model with UTKFace

I ran the demo.py on my computer already.
It is perfect but somehow it can not distinguish gender correctly.
I am wondering it probably was trained by IMDB-wiki and does not perform well on Asian's face.
So I decide to train the model with UTKFace which will be linked right here: https://susanqq.github.io/UTKFace/

Is my assumption right?
Could you give me some advice about how to train the model instead of IMDB-wiki?

Plot Accuracy

I think plot_accuracy.py should be changed in order to reflect the changes made to train.py
from dense_2_acc to pred_age_acc and val_dense_2_acc to val_pred_age_acc, etc

Having issue with data shape

Versions:
Python 3.6.2 :: Anaconda custom (64-bit)
$ python -c "import cv2; print(cv2.version);" => 3.3.0
Keras==2.0.6
dlib==19.7.0
scikit-image==0.13.0
scikit-learn==0.19.0
scipy==0.19.1
numpy==1.13.1
pandas==0.20.3
h5py==2.7.0
tables==3.4.2

When I run demo.py, I get this error:

Traceback (most recent call last):
File "demo.py", line 99, in
main()
File "demo.py", line 80, in main
results = model.predict(faces)
File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1499, in predict
check_batch_axis=False)
File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 140, in _standardize_input_data
str(array.shape))
ValueError: Error when checking : expected input_1 to have shape (None, 3, 64, 64) but got array with shape (1, 64, 64, 3)

Any ideas?

Illegal instruction: 4

Hi!
I have some trouble with launching.
Mac OS 10.13 High Sierra, Macbook white 2010.
I tried to launch demo.py script by

python3 demo.py

but the issue Illegal instruction: 4 has occured. I know it could happen as my notebook is too old and processor is not understand some instructions of dlib. I tried to change SSE4 to SSE2 instructions before dlib recompiling but dlib was updated to 10.13 and I can't find the necessary strings in CMakeLists.txt currently. What should I do to launch demoscript?

InvalidArgumentError while serving with tensorflow serving api

Im using the following code to convert the keras model to tensorflow.

from wide_resnet import WideResNet
from keras.layers.core import K
import tensorflow as tf
import os.path as osp
K._LEARNING_PHASE = tf.constant(0)
input_fld = 'input'
weight_file = 'weights.18-4.06.hdf5'
num_output = 1
write_graph_def_ascii_flag = True
prefix_output_node_names_of_final_network = 'softmax'
output_graph_name = 'age_weight_two.pb'


output_fld = input_fld + 'tensorflow_model/'

img_size = 64
model = WideResNet(img_size, depth=16, k=8)()
model.load_weights("input/weights.18-4.06.hdf5")


pred = [None]*num_output
pred_node_names = [None]*num_output
for i in range(num_output):
    pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)
    print (pred_node_names[i] )
    pred[i] = tf.identity(model.output[i], name=pred_node_names[i])
print('output nodes names are: ', pred_node_names)

sess = K.get_session()

if write_graph_def_ascii_flag:
    f = 'only_the_graph_def.pb.ascii'
    tf.train.write_graph(sess.graph.as_graph_def(), output_fld, f, as_text=True)
    print('saved the graph definition in ascii format at: ', osp.join(output_fld, f))

from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)
print('saved the constant graph (ready for inference) at: ', osp.join(output_fld, output_graph_name))

In my prediction, I run the prediction as follows,

sess.run(softmax_tensor, feed_dict={'input_1:0': faces})

However it throws the following error,

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'batch_normalization_1/keras_learning_phase' with dtype bool
[[Node: batch_normalization_1/keras_learning_phase = Placeholderdtype=DT_BOOL, shape=, _device="/job:localhost/replica:0/task:0/cpu:0"]]

How can I be able to sort this out?

Can not Train the model

Hi,

I unzipped the imdb_0.tar in the data folder I created.
After I run python create_db.py --output data/imdb_db.mat --db imdb --img_size 64
and get the following error:

Traceback (most recent call last):
File "create_db.py", line 66, in
main()
File "create_db.py", line 36, in main
full_path, dob, gender, photo_taken, face_score, second_face_score, age = get_meta(mat_path, db)
File "C:\Users\xxx\Desktop\Eye_Tracking\age-gender-estimation-master\utils.py", line 17, in get_meta
meta = loadmat(mat_path)
File "C:\Users\xxxl\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\io\matlab\mio.py", line 141, in loadmat
MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
File "C:\Users\xxx\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\io\matlab\mio.py", line 64, in mat_reader_factory
byte_stream, file_opened = _open_file(file_name, appendmat)
TypeError: 'NoneType' object is not iterable

Can you help me? I think is a dataset problem

Predicting always the same class

Your pretrained model works great!

However, when trying to reproduce your results by training myself with the same training data, I face the problem that the neural net only learns to predict the same class for each image. Overfitting on a small training data set (only 1-2 images per class) works, but beyond that I could not generalize. I tried various parameter ranges including the default values for learning rate, dropout, depth and width and so on, but it didn't help.

Any ideas?

Error in `python3': double free or corruption (!prev):

Hello yu4u,when I run 'python3 train.py --input data/imdb_db.mat' ,
(ubuntu14.04, python3.5,cuda8.0,cudnn5.0,tensorflow1.0.1)
the print log is as follows:
Using TensorFlow backend.
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
DEBUG:root:Loading data...
DEBUG:root:image_dim_ordering = 'tf'
DEBUG:root:Creating model...
DEBUG:root:Model summary...


DEBUG:root:Saving model...
DEBUG:root:Running training...
Train on 154666 samples, validate on 17186 samples
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

  • Error in `python3': double free or corruption (!prev): 0x000000000188cf30 *
    Aborted (core dumped)

Can u give me some advice about this ?

Check for an age range?

First all, thank you for this program.
If I wanted to check for the accuracy for a particular age range, how would I do it? For example, if I wanted to check if the age range was between 20-30 and everyone that lies in that age range is a hit and outside of it is a miss, what do you think would be the best way to approach this? I am mostly concerned about the accuracy which I believe is dense_2_acc
Thank you

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 9716

hello, interested in your code, but when I run demo.py, it shows error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 9716
Traceback (most recent call last):
  File "demo.py", line 70, in <module>
    main()
  File "demo.py", line 33, in main
    input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor

How to fix it? My cv2 in python is 3.2.0. Python is 2.7
thank you very much~~

Training with Image Size 128px

Hi yu4u,

I want to train the network with 128 image size. (40k images)
However, the lost reduce stop at 24 (at 20 epochs) and then it doesn't decrease anymore. I've tried with adam, sgd, rmsprop. But the results are all the same.
Do you have any tips for training the network from scratch? Or is there any pretrained weights for 128 image size?

Thanks for the age-gender-estimation code!

Resize image

The model is too heavy for the cpu to process. So i want to resize the image to 128x128 before training. How can I optimize it by resizing the input?

Slow frame

Why is that the frame is too slow. It is lagging. How do I optimize it

Isolating the imdb train images to folders

I would like to train the model with imagenet and see if it can improve the performance. Can you please give me a script that can seperate the imdb dataset to various folders with respect to the age?

Prediction accuracy & over-fitting

Hi, I really appreciate your neat code and it works perfectly. However, it seems the age estimation accuracy on validation set is quite low and seems only attend 0.04. I guess it is due to the number of classes. But would the model predict better if I reduce the class number?
In addition, I re-checked your accuracy curve and observed that it might overfit when epoch over 25.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.