第五周。
本周采用了googlenet ,修改的alexnet,darknet19对人脸表情数据集fer2013进行分类。
googlenet:63%
修改的alexnet:63%
darknet19:30%
所以选用修改的alexnet,因为修改的alexnet层数较少
当
base_lr=0.5 59.9%
base_lr=0.05 61%
base_lr=0.005 64%
所以选用base_lr=0.005
对alexnet进行修改:去掉最后两层
下载fer2013
注册一个kaggle的账号,登陆下载
将csv转换成jpg
# -*- coding: utf-8 -*-
import csv
import os
from PIL import Image
import numpy as np
datasets_path = r'.\datasets'
train_csv = os.path.join(datasets_path, 'train.csv')
val_csv = os.path.join(datasets_path, 'val.csv')
test_csv = os.path.join(datasets_path, 'test.csv')
train_set = os.path.join(datasets_path, 'train')
val_set = os.path.join(datasets_path, 'val')
test_set = os.path.join(datasets_path, 'test')
for save_path, csv_file in [(train_set, train_csv), (val_set, val_csv), (test_set, test_csv)]:
if not os.path.exists(save_path):
os.makedirs(save_path)
num = 1
with open(csv_file) as f:
csvr = csv.reader(f)
header = next(csvr)
for i, (label, pixel) in enumerate(csvr):
pixel = np.asarray([float(p) for p in pixel.split()]).reshape(48, 48)
subfolder = os.path.join(save_path, label)
if not os.path.exists(subfolder):
os.makedirs(subfolder)
im = Image.fromarray(pixel).convert('L')
image_name = os.path.join(subfolder, '{:05d}.jpg'.format(i))
print(image_name)
im.save(image_name)
转换成功后将文件夹重命名,并将文件重新命名:
0--angry
1--disgust
2--fear
3--happy
4--sad
5--surprise
6--neutral
生成train.txt
DATA=/home/users/zibojia/facial_expression/
rm train.txt
rm test.txt
find $DATA/train/angry -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 0/">>./train.txt
find $DATA/train/digust -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 1/">>./train.txt
find $DATA/train/fear -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 2/">>./train.txt
find $DATA/train/happy -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 3/">>./train.txt
find $DATA/train/sad -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 4/">>./train.txt
find $DATA/train/surprise -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 5/">>./train.txt
find $DATA/train/neutral -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 6/">>./train.txt
find $DATA/test/angry -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 0/">>./test.txt
find $DATA/test/digust -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 1/">>./test.txt
find $DATA/test/fear -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 2/">>./test.txt
find $DATA/test/happy -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 3/">>./test.txt
find $DATA/test/sad -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 4/">>./test.txt
find $DATA/test/surprise -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 5/">>./test.txt
find $DATA/test/neutral -name *.jpg | cut -d '/' -f6-9 | sed "s/$/ 6/">>./test.txt
利用caffe生成lmdb文件
./build/tools/convert_imageset --shuffle /home/users/zibojia/facial_expression ~/facial_expression/train.txt ./train_lmdb
test_lmdb相同
生成mean.binaryprototxt
./build/tools/compute_image_mean.bin ./train_lmdb/ ./mean.binaryproto
修改train_val.protxt
mean_file:"mean.binaryproto"
train:source: "train_lmdb"
更改最后一个num_output:7
修改solver.prototxt
net: "path/to/train_val.prototxt"
test_iter: 287
test_interval: 100
test_initialization: false
display: 100
average_loss: 40
base_lr: 0.00045
lr_policy: "step"
stepsize: 6400
gamma: 0.96
max_iter: 12000
momentum: 0.9
weight_decay: 0.0002
snapshot: 600
snapshot_prefix: "/path/to/alexnet_"
solver_mode: GPU
训练:
./build/tools/caffe train --solver=/path/to/solver.prototxt
测试:
./build/examples/cpp_classification/classification.bin deploy.prototxt network.caffemodel mean.binaryproto labels.txt img.jpg
将姚明的照片转变成灰度图和正确的大小
convert yaoming.jpg -colorspace Gray yaoming.jpg
convert yaoming.jpg -resize 48x48 yaoming.jpg
---------- Prediction for yaoming.jpg ----------
0.8499 - "happy 开心"
0.0561 - "neutral 平静"
0.0481 - "sad 伤心"
0.0218 - "surprise 惊奇"
0.0182 - "angry 生气"