人类年龄预测(2)

加载模型

首先我们将模型结构和权重加载,预训练模型位置存储在weight_file中。

In [16]:
from SSRNET_model import SSR_net

weight_file = "./ssrnet_3_3_3_64_1.0_1.0.h5"

img_size = 64
stage_num = [3,3,3]
lambda_local = 1
lambda_d = 1
model = SSR_net(img_size,stage_num, lambda_local, lambda_d)()
model.load_weights(weight_file)

Using TensorFlow backend.

WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

WARNING:tensorflow:From /home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

模型层级结构

In [17]:
model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 64, 64, 3)    0                                            
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 62, 62, 16)   448         input_1[0][0]                    
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 62, 62, 32)   896         input_1[0][0]                    
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 62, 62, 16)   64          conv2d_5[0][0]                   
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 62, 62, 32)   128         conv2d_1[0][0]                   
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 62, 62, 16)   0           batch_normalization_5[0][0]      
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 62, 62, 32)   0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 31, 31, 16)   0           activation_5[0][0]               
__________________________________________________________________________________________________
average_pooling2d_1 (AveragePoo (None, 31, 31, 32)   0           activation_1[0][0]               
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 29, 29, 16)   2320        max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 29, 29, 32)   9248        average_pooling2d_1[0][0]        
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 29, 29, 16)   64          conv2d_6[0][0]                   
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 29, 29, 32)   128         conv2d_2[0][0]                   
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 29, 29, 16)   0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 29, 29, 32)   0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 14, 14, 16)   0           activation_6[0][0]               
__________________________________________________________________________________________________
average_pooling2d_2 (AveragePoo (None, 14, 14, 32)   0           activation_2[0][0]               
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, 12, 12, 16)   2320        max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 12, 12, 32)   9248        average_pooling2d_2[0][0]        
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 12, 12, 16)   64          conv2d_7[0][0]                   
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 12, 12, 32)   128         conv2d_3[0][0]                   
__________________________________________________________________________________________________
activation_7 (Activation)       (None, 12, 12, 16)   0           batch_normalization_7[0][0]      
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 12, 12, 32)   0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D)  (None, 6, 6, 16)     0           activation_7[0][0]               
__________________________________________________________________________________________________
average_pooling2d_3 (AveragePoo (None, 6, 6, 32)     0           activation_3[0][0]               
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, 4, 4, 16)     2320        max_pooling2d_3[0][0]            
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 4, 4, 32)     9248        average_pooling2d_3[0][0]        
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 4, 4, 16)     64          conv2d_8[0][0]                   
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 4, 4, 32)     128         conv2d_4[0][0]                   
__________________________________________________________________________________________________
activation_8 (Activation)       (None, 4, 4, 16)     0           batch_normalization_8[0][0]      
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 4, 4, 32)     0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 14, 14, 10)   170         max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 14, 14, 10)   330         average_pooling2d_2[0][0]        
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 31, 31, 10)   170         max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
conv2d_14 (Conv2D)              (None, 31, 31, 10)   330         average_pooling2d_1[0][0]        
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, 4, 4, 10)     170         activation_8[0][0]               
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 4, 4, 10)     330         activation_4[0][0]               
__________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D)  (None, 3, 3, 10)     0           conv2d_11[0][0]                  
__________________________________________________________________________________________________
average_pooling2d_4 (AveragePoo (None, 3, 3, 10)     0           conv2d_12[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_5 (MaxPooling2D)  (None, 3, 3, 10)     0           conv2d_13[0][0]                  
__________________________________________________________________________________________________
average_pooling2d_5 (AveragePoo (None, 3, 3, 10)     0           conv2d_14[0][0]                  
__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 160)          0           conv2d_9[0][0]                   
__________________________________________________________________________________________________
flatten_2 (Flatten)             (None, 160)          0           conv2d_10[0][0]                  
__________________________________________________________________________________________________
flatten_3 (Flatten)             (None, 90)           0           max_pooling2d_4[0][0]            
__________________________________________________________________________________________________
flatten_4 (Flatten)             (None, 90)           0           average_pooling2d_4[0][0]        
__________________________________________________________________________________________________
flatten_5 (Flatten)             (None, 90)           0           max_pooling2d_5[0][0]            
__________________________________________________________________________________________________
flatten_6 (Flatten)             (None, 90)           0           average_pooling2d_5[0][0]        
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 160)          0           flatten_1[0][0]                  
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 160)          0           flatten_2[0][0]                  
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 90)           0           flatten_3[0][0]                  
__________________________________________________________________________________________________
dropout_4 (Dropout)             (None, 90)           0           flatten_4[0][0]                  
__________________________________________________________________________________________________
dropout_5 (Dropout)             (None, 90)           0           flatten_5[0][0]                  
__________________________________________________________________________________________________
dropout_6 (Dropout)             (None, 90)           0           flatten_6[0][0]                  
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 3)            483         dropout_1[0][0]                  
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 3)            483         dropout_2[0][0]                  
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 3)            273         dropout_3[0][0]                  
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 3)            273         dropout_4[0][0]                  
__________________________________________________________________________________________________
dense_7 (Dense)                 (None, 3)            273         dropout_5[0][0]                  
__________________________________________________________________________________________________
dense_8 (Dense)                 (None, 3)            273         dropout_6[0][0]                  
__________________________________________________________________________________________________
multiply_2 (Multiply)           (None, 3)            0           dense_1[0][0]                    
                                                                 dense_2[0][0]                    
__________________________________________________________________________________________________
multiply_4 (Multiply)           (None, 3)            0           dense_4[0][0]                    
                                                                 dense_5[0][0]                    
__________________________________________________________________________________________________
multiply_6 (Multiply)           (None, 3)            0           dense_7[0][0]                    
                                                                 dense_8[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 6)            24          multiply_2[0][0]                 
__________________________________________________________________________________________________
dense_6 (Dense)                 (None, 6)            24          multiply_4[0][0]                 
__________________________________________________________________________________________________
dense_9 (Dense)                 (None, 6)            24          multiply_6[0][0]                 
__________________________________________________________________________________________________
multiply_1 (Multiply)           (None, 160)          0           flatten_1[0][0]                  
                                                                 flatten_2[0][0]                  
__________________________________________________________________________________________________
multiply_3 (Multiply)           (None, 90)           0           flatten_3[0][0]                  
                                                                 flatten_4[0][0]                  
__________________________________________________________________________________________________
multiply_5 (Multiply)           (None, 90)           0           flatten_5[0][0]                  
                                                                 flatten_6[0][0]                  
__________________________________________________________________________________________________
pred_age_stage1 (Dense)         (None, 3)            21          dense_3[0][0]                    
__________________________________________________________________________________________________
pred_age_stage2 (Dense)         (None, 3)            21          dense_6[0][0]                    
__________________________________________________________________________________________________
pred_age_stage3 (Dense)         (None, 3)            21          dense_9[0][0]                    
__________________________________________________________________________________________________
delta_s1 (Dense)                (None, 1)            161         multiply_1[0][0]                 
__________________________________________________________________________________________________
delta_s2 (Dense)                (None, 1)            91          multiply_3[0][0]                 
__________________________________________________________________________________________________
delta_s3 (Dense)                (None, 1)            91          multiply_5[0][0]                 
__________________________________________________________________________________________________
local_delta_stage1 (Dense)      (None, 3)            21          dense_3[0][0]                    
__________________________________________________________________________________________________
local_delta_stage2 (Dense)      (None, 3)            21          dense_6[0][0]                    
__________________________________________________________________________________________________
local_delta_stage3 (Dense)      (None, 3)            21          dense_9[0][0]                    
__________________________________________________________________________________________________
pred_a (Lambda)                 (None, 1)            0           pred_age_stage1[0][0]            
                                                                 pred_age_stage2[0][0]            
                                                                 pred_age_stage3[0][0]            
                                                                 delta_s1[0][0]                   
                                                                 delta_s2[0][0]                   
                                                                 delta_s3[0][0]                   
                                                                 local_delta_stage1[0][0]         
                                                                 local_delta_stage2[0][0]         
                                                                 local_delta_stage3[0][0]         
==================================================================================================
Total params: 40,915
Trainable params: 40,531
Non-trainable params: 384
__________________________________________________________________________________________________

准备输入数据

In [18]:
faces = np.empty((len(detected), img_size, img_size, 3))
faces.shape
Out[18]:
(1, 64, 64, 3)

获取人脸区域图片,并缩放

将人脸检测结果进行裁剪和缩放

In [19]:
ad = 0.4

img_h, img_w, _ = np.shape(img)

for i,d in enumerate(detected):
    if d['confidence'] >=0.95 :
        x1,y1,w,h = d['box']
        x2 = x1 + w
        y2 = y1 + h
        xw1 = max(int(x1 - ad * w), 0)
        yw1 = max(int(y1 - ad * h), 0)
        xw2 = min(int(x2 + ad * w), img_w - 1)
        yw2 = min(int(y2 + ad * h), img_h - 1)
        img = cv2.resize(img[yw1:yw2+1, xw1:xw2+1, :], (img_size, img_size))
        faces[i,:,:,:] = img
        
res_img = Image.fromarray(img)
res_img
Out[19]:

预测年龄

将人脸区域图片输入模型,获得预测结果

In [20]:
res = model.predict(faces)
In [21]:
print("预测年龄为:"+str(int(res[0])))

预测年龄为:27
(完)