【XMU学科实践二】颜值打分代码实例讲解(paddle框架)

该文介绍了一个基于华南理工大学实验室数据集的人脸颜值打分模型的训练过程。首先,对数据进行解压、分类,然后进行数据增广,包括旋转、缩放和扭曲等操作。接着,使用PaddlePaddle框架定义CNN网络并进行训练,最终在验证集上评估模型性能,并对特定图片进行预测打分。

数据集介绍
训练数据集为华南理工大学实验室公布的数据集

数据中包含500张女生图片,分别由70人进行打分,最终取平均值即为该图片的打分情况。

我们在实践中将图片分值设定为1-5。

500张图片中,450张用于训练,50张用于验证。

任务: 分析、利用给定的数据集,训练一个人脸颜值打分模型,给出模型在验证集上的准确率,并利用模型给 work/1.jpg 图片打分


笔者的话

这个代码是我很早期的版本了,后面查出来了不少问题,但是好在准确率还不错。如果用来应付学科实践作业的话,请君自取(文章最后有github链接)。但是要打比赛的话,不要用我的这个哦,会玩球的

导包

#导入需要的包
import os
import zipfile
import random
import json
import cv2
import numpy as np
from PIL import Image
import paddle
import matplotlib.pyplot as plt
from paddle.io import Dataset
import shutil

参数初始配置

'''
参数配置
'''
train_parameters = {
   
   
    "input_size": [3,224,224],                           #输入图片的shape
    "class_dim": -1,                                     #分类数
    "src_path":"data/data18736/face_data_5.zip",       #原始数据集路径
    "target_path":"/home/aistudio/data/dataset",        #要解压的路径 
    "train_list_path": "./train.txt",              #train_data.txt路径
    "eval_list_path": "./eval.txt",                  #eval_data.txt路径
    "label_dict":{
   
   },                                    #标签字典
    "readme_path": "/home/aistudio/data/readme.json",   #readme.json路径
    "num_epochs": 50,                                    #训练轮数
    "train_batch_size": 16,                             #批次的大小
    "learning_strategy": {
   
                                 #优化函数相关的配置
        "lr": 0.01                                     #超参数学习率
    } ,
    "checkpoints": "/home/aistudio/work/checkpoints",          #保存的路径
    "skip_steps": 10,                                     #训练时输出日志的间隔
    "save_steps": 100,                                        #训练时保存模型参数的间隔
    "class_path":  "/home/aistudio/data_new",   #按分数分类的新的图片地址
    "augment_path":"/home/aistudio/augment"   #数据增强图片目录
}

解压分类数据集

定义unzip_data函数:解压数据集

def unzip_data(src_path,target_path):

    '''
    解压原始数据集,将src_path路径下的zip包解压至data/dataset目录下
    '''
    if(not os.path.isdir(target_path)):    
        z = zipfile.ZipFile(src_path, 'r')
        z.extractall(path=target_path)
        z.close()
    else:
        print("文件已解压")

定义get_data_new函数:将dataset下的数据转移并分类至data_new


def get_data_new(target_path,train_list_path,eval_list_path,class_path):
    '''
    将图片按分数分类
    '''
    #存放所有类别的信息
    class_detail = []
    #获取所有类别保存的文件夹名称
    data_list_path=target_path
    class_dirs = os.listdir(data_list_path)
    if '__MACOSX' in class_dirs:
        class_dirs.remove('__MACOSX')
    # #总的图像数量
    all_class_images = 0
    # #存放类别标签
    class_label=0
    # #存放类别数目
    class_dim = 0
    # #存储要写进eval.txt和train.txt中的内容
    trainer_list=[]
    eval_list=[]

    #读取每个类别
    for class_dir in class_dirs:
        if class_dir != ".DS_Store":
            class_dim += 1
            #每个类别的信息
            class_detail_list = {
   
   }
            eval_sum = 0
            trainer_sum = 0
            #统计每个类别有多少张图片
            class_sum = 0
            #获取类别路径 
            path = os.path.join(data_list_path,class_dir)
            # 获取所有图片
            img_paths = os.listdir(path)
            for img_path in img_paths:                                  # 遍历文件夹下的每个图片
                if img_path =='.DS_Store':
                    continue
                name_path = os.path.join(path,img_path) 
                for img_path2 in os.listdir(name_path):
                    if img_path2 =='.DS_Store':
                        continue
                    name_path2=os.path.join(name_path,img_path2)                     # 每张图片的原始路径

                    #将图片按分数分类
                    shutil.move(name_path2,class_path+"/"+img_path2[0]+"/"+img_path2)
    print("data_new已生成!")
    

执行unzip_data 、get_data_new并更新参数


'''
参数初始化
'''
src_path=train_parameters['src_path']
target_path=train_parameters['target_path']
train_list_path=train_parameters['train_list_path']
eval_list_path=train_parameters['eval_list_path']
batch_size=train_parameters['train_batch_size']
class_path=train_parameters['class_path']
augment_path=train_parameters['augment_path']
'''
解压原始数据到指定路径
'''
unzip_data(src_path,target_path)

#新建分类目录
#注意:只可运行第一次
os.mkdir(r'/home/aistudio/data_new')
os.mkdir(r'/home/aistudio/data_new/1')
os.mkdir(r'/home/aistudio/data_new/2')
os.mkdir(r'/home/aistudio/data_new/3')
os.mkdir(r'/home/aistudio/data_new/4')
os.mkdir(r'/home/aistudio/data_new/5')

#每次生成数据列表前,首先清空train.txt和eval.txt
with open(train_list_path, 'w') as f: 
 f.seek(0)
 f.truncate() 
with open(eval_list_path, 'w') as f: 
 f.seek(0)
 f.truncate() 
 
#将图片按分数分类   
get_data_new(target_path,train_list_path,eval_list_path,class_path)


'''
参数更新
'''
train_parameters = {
   
   
 "input_size": [3,224,224],                           #输入图片的shape
 "class_dim": -1,                                     #分类数
 "src_path":"data/data18736/face_data_5.zip",       #原始数据集路径
 "target_path":"/home/aistudio/data_new",        #更新
 "train_list_path": "./train.txt",              #train_data.txt路径
 "eval_list_path": "./eval.txt",                  #eval_data.txt路径
 "label_dict":{
   
   },                                    #标签字典
 "readme_path": "/home/aistudio/data/readme.json",   #readme.json路径
 "num_epochs": 40,                                    #训练轮数
 "train_batch_size": 16,                             #批次的大小
 "learning_strategy": {
   
                                 #优化函数相关的配置
     "lr": 0.01                                     #超参数学习率
 }
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快苏排序OAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值