当前位置: 首页 > news >正文

做文案策划有些网站可看网络推广app

做文案策划有些网站可看,网络推广app,python不用框架做网站,广西网站建设与规划目录 一、任务概述 二、算法研发 2.1 下载数据集 2.2 数据集预处理 2.3 安装PaddleClas套件 2.4 算法训练 2.5 静态图导出 2.6 静态图推理 三、小结 一、任务概述 最近遇到个需求,需要将图像中的人物区分为成人和小孩,这是一个典型的二分类问题…

目录

一、任务概述

二、算法研发

2.1 下载数据集

2.2 数据集预处理

2.3 安装PaddleClas套件

2.4 算法训练

2.5 静态图导出

2.6 静态图推理

三、小结


一、任务概述

    最近遇到个需求,需要将图像中的人物区分为成人和小孩,这是一个典型的二分类问题,打算采用飞桨的图像分类套件PaddleClas来完成算法研发。本文记录相关流程。

二、算法研发

2.1 下载数据集

    本文采用MaGaAge_Asian数据集,该数据集主要由亚洲人图片组成,训练集包含40000张图像,验证集包含3495张图像,每张图像都有对应的年龄真值,所有图像均处理成了统一的大小,宽178像素,高218像素。

数据集地址下载链接。数据集部分示例如下图所示:

    该数据集本意是用来做年龄预测的,属于一个数值回归任务,本文将其变成二分类任务,以13岁年龄为界限,小于该年龄的属于小孩,大于该年龄的属于成人。这里之所以选择13岁,因为这个任务是需要筛选出长得很“像”小孩的小孩,13岁以上的青少年很多本身已经长的像成人了,因此,选择13岁作为分界线。

    下面首先对该数据集进行处理。

2.2 数据集预处理

    MaGaAge_Asian数据集每张图片对应的人物年龄存放在list文件夹的两个文件中,其中train_age.txt存放训练集对应的年龄真值,test_age.txt存放验证集对应的年龄真值。下面要写一个脚本,将所有小于13岁的图片移动到一个文件夹内,所有大于等于13岁的图片移动到另一个文件夹内。

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件        :split_asian.py
@说明        :拆分megaage_asian数据集,将小于13岁的移动到一个文件夹,大于等于13岁的移动到另一个文件夹
@时间        :2024/07/16 09:11:16
@作者        :Bin Qian
@版本        :1.0
'''import os
import cv2thr = 13 # 年龄阈值# 读取年龄列表
agefile = 'megaage_asian/list/test_age.txt'
f=open(agefile) 
ageLst = f.read().splitlines()
f.close() # 读取图像
imgFolder = 'megaage_asian/val'
imgnames = os.listdir(imgFolder)
index = 50000
for imgname in imgnames:imgPath = os.path.join(imgFolder,imgname)img = cv2.imread(imgPath)if img is None:continueprint(imgPath)imgindex = int(imgname.split('.')[0])age = int(ageLst[imgindex-1])if age < thr:dstFolder = 'ageclas/child'else:dstFolder = 'ageclas/adult'savePath = os.path.join(dstFolder,str(index)+'_asian.jpg')cv2.imwrite(savePath,img)index += 1
print('完成')

值得注意的是MaGaAge_Asian数据集中有很多质量较差的图像,这些“脏”图像会影响学习效果,最好手工检查这些数据并将其剔除。

另外,为了能够取得更好的效果,本文从互联网和FFHQ数据集里面再挑选出一些小孩和成人的照片进行补充。部分代码如下:

import os
import cv2# 读取图像
imgFolder = 'adult'
imgnames = os.listdir(imgFolder)
index = 1
for imgname in imgnames:imgPath = os.path.join(imgFolder,imgname)img = cv2.imread(imgPath)if img is None:continueprint(imgPath)dstFolder = 'ageclas/adult'savePath = os.path.join(dstFolder,str(index)+'_data.jpg')cv2.imwrite(savePath,img)index += 1
print('完成')

补充完整后,最后对整理好的数据集进行拆分,并且获得对应的文件列表:

# 导入系统库
import os
import random
import cv2# 定义参数
img_folder = 'ageclas'
trainlst = 'train_list.txt'
vallst = 'val_list.txt'
ratio = 0.95 # 训练集占比
labellst='label.txt'def writeLst(lstpath,namelst):'''保存文件列表'''print('正在写入 '+lstpath)random.shuffle (namelst)# 写入训练样本文件f=open(lstpath, 'a', encoding='utf-8')for i in range(len(namelst)):text = namelst[i]+'\n'f.write(text)f.close()print(lstpath+ '已完成写入')def main():'''主函数'''# 查找文件夹folderlst = os.listdir(img_folder)print('共找到 %d 个文件夹' % len(folderlst))# 循环处理trainnamelst = list()valnamelst = list()labelnamelst = list()for i in range(len(folderlst)):class_name = folderlst[i]class_label = iprint('开始处理 '+class_name+' 文件夹')# 获取子文件夹文件列表filenamelst = os.listdir(os.path.join(img_folder,class_name))totalNum = len(filenamelst)print('当前文件夹图片数量为: ' + str(totalNum)) trainNum = int(ratio*totalNum)text =  str(class_label)+ ' ' + class_namelabelnamelst.append(text)# 检查并校验图像for j in range(totalNum):imgpath = os.path.join(img_folder,class_name,filenamelst[j])img = cv2.imread(imgpath, cv2.IMREAD_COLOR)if img is None:continuetext = imgpath + ' ' + str(class_label)if j <= trainNum: trainnamelst.append(text)else:valnamelst.append(text)writeLst(trainlst,trainnamelst)writeLst(vallst,valnamelst)   writeLst(labellst,labelnamelst)     print('全部完成')if __name__ == '__main__':'''程序入口'''main()

运行后会生成train_lst.txt、val_lst.txt以及label.txt三个文件,有了这三个文件就可以使用PaddleClas套件进行算法研发了。

2.3 安装PaddleClas套件

git clone https://gitee.com/paddlepaddle/PaddleClas.git
cd PaddleClas
sudo python setup.py install

2.4 算法训练

在PaddleClas目录下新建一个配置文件config_lcnet.yaml,采用PPLCNet_x0_5模型来训练,配置文件代码如下:

# global configs
Global:checkpoints: nullpretrained_model: nulloutput_dir: ./output/device: gpusave_interval: 5eval_during_train: Trueeval_interval: 5epochs: 200print_batch_step: 10use_visualdl: True# used for static mode and model exportimage_shape: [3, 224, 224]save_inference_dir: ./output/inference
# model architecture
Arch:name: PPLCNet_x0_5class_num: 2# loss function config for traing/eval process
Loss:Train:- CELoss:weight: 1.0epsilon: 0.1Eval:- CELoss:weight: 1.0Optimizer:name: Momentummomentum: 0.9lr:name: Cosinelearning_rate: 0.8warmup_epoch: 5regularizer:name: 'L2'coeff: 0.00003# data loader for train and eval
DataLoader:Train:dataset:name: ImageNetDatasetimage_root: ../process_data/cls_label_path: ../process_data/train_list.txttransform_ops:- DecodeImage:to_rgb: Truechannel_first: False- ResizeImage:size: [224,224]- RandFlipImage:flip_code: 1- NormalizeImage:scale: 1.0/255.0mean: [0.485, 0.456, 0.406]std: [0.229, 0.224, 0.225]order: ''sampler:name: DistributedBatchSamplerbatch_size: 64drop_last: Falseshuffle: Trueloader:num_workers: 4use_shared_memory: TrueEval:dataset: name: ImageNetDatasetimage_root: ../process_data/cls_label_path: ../process_data/val_list.txttransform_ops:- DecodeImage:to_rgb: Truechannel_first: False- ResizeImage:size: [224,224]- NormalizeImage:scale: 1.0/255.0mean: [0.485, 0.456, 0.406]std: [0.229, 0.224, 0.225]order: ''sampler:name: DistributedBatchSamplerbatch_size: 64drop_last: Falseshuffle: Falseloader:num_workers: 4use_shared_memory: TrueInfer:infer_imgs: "../testimgs/10.jpg"batch_size: 1transforms:- DecodeImage:to_rgb: Truechannel_first: False- ResizeImage:size: [224,224]- NormalizeImage:scale: 1.0/255.0mean: [0.485, 0.456, 0.406]std: [0.229, 0.224, 0.225]order: ''- ToCHWImage:PostProcess:name: Topktopk: 1class_id_map_file: "../process_data/label.txt"Metric:Train:- TopkAcc:topk: [1]Eval:- TopkAcc:topk: [1]

然后使用下面的命令进行训练:

export CUDA_VISIBLE_DEVICES=0,1
python3 -m paddle.distributed.launch \--gpus="0,1" \tools/train.py \-c config_lcnet.yaml 

训练完成后可以使用下面的命令可视化查看训练结果:

visualdl --logdir results/vdl

运行效果如下:

可以看到,基本在epoch=100以后就收敛了,最高top1准确率达到97.5%,准确率还是比较高的。

下面可以使用动态图对单张图像进行测试,命令如下:

python3 tools/infer.py -c config_lcnet.yaml -o Global.pretrained_model=output/PPLCNet_x0_5/best_model

输出如下:

[{'class_ids': [1], 'scores': [0.93522], 'file_name': '../testimgs/10.jpg', 'label_names': ['adult']}]

2.5 静态图导出

为了方便后面进行模型部署,将训练好的最佳模型进行静态图导出。具体命令如下:

python3 tools/export_model.py \-c config_lcnet.yaml \-o Global.pretrained_model=output/PPLCNet_x0_5/best_model \-o Global.save_inference_dir=output/inference

导出的静态图模型存放在output/inference文件夹下面,整个模型参数加起来不超过3M,因此可以看出这个训练好的PPLCNet_x0_5模型是一个非常轻量级的模型。

2.6 静态图推理

下面使用静态图来进行推理。在推理前先使用visualdl工具查看下静态图模型的输入和输出,这将为编写推理脚本奠定基础。

可以看到,输入是[batch,3,224,224]的float型图像数据,输出是[batch,2]的float型数据。尤其是输出的两个值,代表的是两个类别的概率。

有了上面的分析,下面可以用PaddleInference写一个推理脚本infer.py:

import cv2
import numpy as np
from paddle.inference import create_predictor
from paddle.inference import Config as PredictConfig# 加载静态图模型
model_path = "./output/inference/inference.pdmodel"
params_path = "./output/inference/inference.pdiparams"
pred_cfg = PredictConfig(model_path, params_path)
pred_cfg.enable_memory_optim()  # 启用内存优化
pred_cfg.switch_ir_optim(True)
pred_cfg.enable_use_gpu(500, 0)  # 启用GPU推理
predictor = create_predictor(pred_cfg)  # 创建PaddleInference推理器# 解析模型输入输出
input_names = predictor.get_input_names()
input_handle = {}
for i in range(len(input_names)):input_handle[input_names[i]] = predictor.get_input_handle(input_names[i])
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])# 图像预处理
img = cv2.imread("../testimgs/10.jpg", flags=cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_AREA)
img = img.astype(np.float32)
PIXEL_MEANS =(0.485, 0.456, 0.406)    # RGB格式的均值和方差
PIXEL_STDS = (0.229, 0.224, 0.225)
img/=255.0
img-=np.array(PIXEL_MEANS)
img/=np.array(PIXEL_STDS)
img = np.transpose(img[np.newaxis, :, :, :], (0, 3, 1, 2))# 预测
input_handle["x"].copy_from_cpu(img)
predictor.run()
results = output_handle.copy_to_cpu()# 后处理
results = results.squeeze(0)
if results[0]>results[1]:print('小孩'+"  "+str(results[0]))
else:print('大人'+"  "+str(results[1]))

从网上随便找两张照片,运行效果如下:

输出结果:

小孩  0.7256172

输出结果:

大人  0.9533998

可以看到,推理效果还是比较满意的。

三、小结

本文以项目为主线,使用了PaddleClas算法套件解决了年龄分类问题。后续读者如果想要深入学习PaddlePaddle(飞桨)及相关算法套件,可以关注我的书籍(链接)。


文章转载自:
http://homochromatic.c7624.cn
http://biannulate.c7624.cn
http://congealer.c7624.cn
http://atavism.c7624.cn
http://wrangler.c7624.cn
http://capcom.c7624.cn
http://cosurveillance.c7624.cn
http://carrick.c7624.cn
http://outpouring.c7624.cn
http://chromeplate.c7624.cn
http://ibm.c7624.cn
http://collet.c7624.cn
http://destrier.c7624.cn
http://equanimous.c7624.cn
http://butylate.c7624.cn
http://hydrograph.c7624.cn
http://imperforation.c7624.cn
http://axite.c7624.cn
http://cephalization.c7624.cn
http://gana.c7624.cn
http://prolong.c7624.cn
http://lempert.c7624.cn
http://ares.c7624.cn
http://decerebrate.c7624.cn
http://adherence.c7624.cn
http://evapotranspire.c7624.cn
http://commonality.c7624.cn
http://maladaptive.c7624.cn
http://abdication.c7624.cn
http://contrasuggestible.c7624.cn
http://result.c7624.cn
http://hadith.c7624.cn
http://kraken.c7624.cn
http://ruggery.c7624.cn
http://polygene.c7624.cn
http://five.c7624.cn
http://nankeen.c7624.cn
http://redirect.c7624.cn
http://mousse.c7624.cn
http://dejection.c7624.cn
http://spiggoty.c7624.cn
http://sulfaguanidine.c7624.cn
http://pholas.c7624.cn
http://anecdotal.c7624.cn
http://outfitter.c7624.cn
http://courseware.c7624.cn
http://prudish.c7624.cn
http://inconstancy.c7624.cn
http://affirm.c7624.cn
http://zenographic.c7624.cn
http://plantimal.c7624.cn
http://additory.c7624.cn
http://kainite.c7624.cn
http://rondino.c7624.cn
http://recontaminate.c7624.cn
http://rifty.c7624.cn
http://nagging.c7624.cn
http://epsomite.c7624.cn
http://foliiferous.c7624.cn
http://gedankenexperiment.c7624.cn
http://telesoftware.c7624.cn
http://holomorphism.c7624.cn
http://refringent.c7624.cn
http://allottee.c7624.cn
http://lur.c7624.cn
http://acting.c7624.cn
http://prairial.c7624.cn
http://caesious.c7624.cn
http://nephropexia.c7624.cn
http://sphaerosome.c7624.cn
http://familist.c7624.cn
http://dey.c7624.cn
http://wolfeite.c7624.cn
http://reticulocyte.c7624.cn
http://undissembled.c7624.cn
http://clubwoman.c7624.cn
http://calcaneal.c7624.cn
http://disyllable.c7624.cn
http://sanctimony.c7624.cn
http://footgear.c7624.cn
http://medal.c7624.cn
http://polytonalism.c7624.cn
http://lancashire.c7624.cn
http://osteosis.c7624.cn
http://duorail.c7624.cn
http://methionine.c7624.cn
http://diffidently.c7624.cn
http://bisk.c7624.cn
http://microlite.c7624.cn
http://velum.c7624.cn
http://discusser.c7624.cn
http://hydrous.c7624.cn
http://assertively.c7624.cn
http://viviparously.c7624.cn
http://spookish.c7624.cn
http://tarawa.c7624.cn
http://incondensability.c7624.cn
http://megabit.c7624.cn
http://centric.c7624.cn
http://ort.c7624.cn
http://www.zhongyajixie.com/news/76497.html

相关文章:

  • 辽宁建设工程信息网官网查不良行为唐山seo推广公司
  • 北海网站建设关键词怎么做快速的有排名
  • 成都网站建设的公司百度seo整站优化
  • 西宁网站设计seo优化网站词
  • 网站建设越来越难做百度下载免费安装
  • 茶山网站仿做成都网站seo设计
  • 厦门建公司网站seo和sem的关系
  • php二次网站开发步骤天津百度快速排名优化
  • 网站建设推广招代理加盟太原好的网站制作排名
  • 去菲律宾做网站如何提高网站排名seo
  • 个人网站 前置审批创建自己的网站
  • 做学校网站的内容广告联盟平台
  • 南宁网站制作建设seo必备软件
  • 建立独立域名的网站怎样才算是自己的调研报告万能模板
  • 手机与pc的网站开发优化大师怎么强力卸载
  • 做门户网站怎么赚钱b2b免费发布网站大全
  • asp.net网站开发案例免费获客平台
  • 建网站可以卖钱seo的方式包括
  • 怎么做网站的主页面朋友圈推广怎么收费
  • 赵朴初网站建设品牌营销推广要怎么做
  • 中山市做网站专业的网络宣传渠道有哪些
  • 江门网站推广策划天津百度百科
  • 众搜科技做百度网站百度seo营销推广
  • 网站 工商备案网站优化分析
  • 政务网站建设云计算中心百度有刷排名软件
  • 网站的运营与管理免费的推广网站
  • 网站建设考试商业公司的域名
  • 河北建网站2023年8月疫情严重吗
  • 成都网站建设哪家信息流优化师职业规划
  • 斗鱼网站开发是用什么语言世界杯最新排名