论文:《Age and Gender Classification using Convolutional Neural Networks》

本文介绍了一种基于卷积神经网络(CNN)进行年龄和性别识别的方法,并提供了详细的实现步骤与模型结构。该模型是在Adience数据集上训练得到的。
部署运行你感兴趣的模型镜像

深度学习(十四)基于CNN的性别、年龄识别

作者主页

Age and Gender Classification Using Convolutional Neural Networks - Demo

数据地址:

http://www.cslab.openu.ac.il/personal/Hassner/adiencedb/AdienceBenchmarkOfUnfilteredFacesForGenderAndAgeClassification/

主要任务点:

按照例程实现实际模型的运用,这是动手实现的第一个caffe应用,理解了大体轮廓与应用过程。

import os
import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe

caffe_root = '/home/kangyemeng/caffe/'
sys.path.insert(0,caffe_root + 'python')

plt.rcParams['figure.figsize'] = (10,10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

#loading the mean image
mean_filename = '/home/kangyemeng/AgeGenderDeepLearning/models/mean.binaryproto'
proto_data = open(mean_filename,'rb').read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]

#loading the age network
age_net_pretrained = '/home/kangyemeng/AgeGenderDeepLearning/models/age_net.caffemodel'
age_net_model_file = '/home/kangyemeng/AgeGenderDeepLearning/models/model_and_data/deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file,age_net_pretrained,
                           mean = mean,
                           channel_swap = (2,1,0),
                           raw_scale = 255,
                           image_dims=(256,256))
                           
#loading the gender network
gender_net_pretrained='/home/kangyemeng/AgeGenderDeepLearning/models/age_net.caffemodel'
gender_net_model_file='/home/kangyemeng/AgeGenderDeepLearning/models/model_and_data/deploy_age.prototxt'
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained,
                       mean=mean,
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(256,256))

#labels
age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
gender_list=['Male','Female']

#reading and plotting the input image
example_image = '/home/kangyemeng/AgeGenderDeepLearning/models/model_and_data/timg10.jpg'
input_image = caffe.io.load_image(example_image)
_ = plt.imshow(input_image)    #服务器端绘图无法显示,原因还不知道。。。。?!

#age prediction
prediction = age_net.predict([input_image])
print 'predicted age:',age_list[prediction[0].argmax()]

#gender prediction
prediction = gender_net.predict([input_image])
print 'predicted gender:',gender_list[prediction[0].argmax()]

本文使用的网络结构为根据AlexNet删减得来,具体结构如下

name: "CaffeNet"
layers {
  name: "data"
  type: DATA
  top: "data"
  top: "label"
  data_param {
    source: "/home/ubuntu/AdienceFaces/lmdb/Test_fold_is_4/gender_train_lmdb"
    backend: LMDB
    batch_size: 50
  }
  transform_param {
    crop_size: 227
    mean_file: "/home/ubuntu/AdienceFaces/mean_image/Test_folder_is_4/mean.binaryproto"
    mirror: true
  }
  include: { phase: TRAIN }
}
layers {
  name: "data"
  type: DATA
  top: "data"
  top: "label"
  data_param {
    source:  "/home/ubuntu/AdienceFaces/lmdb/Test_fold_is_4/gender_val_lmdb"
    backend: LMDB
    batch_size: 50
  }
  transform_param {
    crop_size: 227
    mean_file: "/home/ubuntu/AdienceFaces/mean_image/Test_folder_is_4/mean.binaryproto"
    mirror: false
  }
  include: { phase: TEST }
}
layers {
  name: "conv1"
  type: CONVOLUTION
  bottom: "data"
  top: "conv1"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 96
    kernel_size: 7
    stride: 4
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers {
  name: "relu1"
  type: RELU
  bottom: "conv1"
  top: "conv1"
}
layers {
  name: "pool1"
  type: POOLING
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "norm1"
  type: LRN
  bottom: "pool1"
  top: "norm1"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layers {
  name: "conv2"
  type: CONVOLUTION
  bottom: "norm1"
  top: "conv2"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 256
    pad: 2
    kernel_size: 5
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layers {
  name: "relu2"
  type: RELU
  bottom: "conv2"
  top: "conv2"
}
layers {
  name: "pool2"
  type: POOLING
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "norm2"
  type: LRN
  bottom: "pool2"
  top: "norm2"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layers {
  name: "conv3"
  type: CONVOLUTION
  bottom: "norm2"
  top: "conv3"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  convolution_param {
    num_output: 384
    pad: 1
    kernel_size: 3
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers{
  name: "relu3" 
  type: RELU
  bottom: "conv3"
  top: "conv3"
}
layers {
  name: "pool5"
  type: POOLING
  bottom: "conv3"
  top: "pool5"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layers {
  name: "fc6"
  type: INNER_PRODUCT
  bottom: "pool5"
  top: "fc6"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layers {
  name: "relu6"
  type: RELU
  bottom: "fc6"
  top: "fc6"
}
layers {
  name: "drop6"
  type: DROPOUT
  bottom: "fc6"
  top: "fc6"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layers {
  name: "fc7"
  type: INNER_PRODUCT
  bottom: "fc6"
  top: "fc7"
  blobs_lr: 1
  blobs_lr: 2
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 512
    weight_filler {
      type: "gaussian"
      std: 0.005
    }
    bias_filler {
      type: "constant"
      value: 1
    }
  }
}
layers {
  name: "relu7"
  type: RELU
  bottom: "fc7"
  top: "fc7"
}
layers {
  name: "drop7"
  type: DROPOUT
  bottom: "fc7"
  top: "fc7"
  dropout_param {
    dropout_ratio: 0.5
  }
}
layers {
  name: "fc8"
  type: INNER_PRODUCT
  bottom: "fc7"
  top: "fc8"
  blobs_lr: 10
  blobs_lr: 20
  weight_decay: 1
  weight_decay: 0
  inner_product_param {
    num_output: 2
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
layers {
  name: "accuracy"
  type: ACCURACY
  bottom: "fc8"
  bottom: "label"
  top: "accuracy"
  include: { phase: TEST }
}
layers {
  name: "loss"
  type: SOFTMAX_LOSS
  bottom: "fc8"
  bottom: "label"
  top: "loss"
}

 

原始AlexNet如下所示

 

您可能感兴趣的与本文相关的镜像

ComfyUI

ComfyUI

AI应用
ComfyUI

ComfyUI是一款易于上手的工作流设计工具,具有以下特点:基于工作流节点设计,可视化工作流搭建,快速切换工作流,对显存占用小,速度快,支持多种插件,如ADetailer、Controlnet和AnimateDIFF等

### ImageNet Classification Using Deep Convolutional Neural Networks Paper Implementation and Explanation #### Overview of the Approach The approach described involves utilizing a deep convolutional neural network (ConvNet) for classifying images from the ImageNet dataset. When an unseen image enters this system, it undergoes forward propagation within the ConvNet structure. The outcome is a set of probabilities corresponding to different classes that the input could belong to[^1]. These probabilities result from computations involving optimized weights derived during training. #### Training Process Insights Training plays a crucial role in ensuring accurate classifications by optimizing these weights so they can effectively categorize previously seen data points accurately. A sufficiently large training set enhances generalization capabilities; thus, when presented with entirely novel inputs post-training phase completion, the model should still perform reliably well at assigning appropriate labels based on learned features rather than memorized instances. #### Historical Context and Impact In 2012, a groundbreaking paper titled "ImageNet Classification with Deep Convolutional Neural Networks" was published, marking significant advancements in computer vision technology. This work introduced innovations such as deeper architectures compared to earlier models along with improved techniques like ReLU activation functions which accelerated learning processes significantly over traditional methods used before then[^2]. #### Detailed Architecture Review For those interested in delving deeper into recent developments surrounding CNNs up until around 2019, surveys provide comprehensive reviews covering various aspects including architectural improvements made since AlexNet's introduction back in 2012[^3]. Such resources offer valuable insights not only regarding specific design choices but also broader trends shaping modern approaches towards building efficient yet powerful visual recognition systems capable of handling complex tasks efficiently while maintaining high accuracy levels across diverse datasets similar or even larger scale versions of what existed originally within ImageNet itself. ```python import torch from torchvision import models # Load pretrained ResNet-50 model trained on ImageNet model = models.resnet50(pretrained=True) # Set evaluation mode model.eval() def predict_image(image_tensor): """Predicts the class label given an image tensor.""" with torch.no_grad(): outputs = model(image_tensor.unsqueeze(0)) _, predicted_class = torch.max(outputs.data, 1) return predicted_class.item() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值